Skip to content

Commit

Permalink
🐱 The cat blinks now!
Browse files Browse the repository at this point in the history
  • Loading branch information
Paito Anderson committed Dec 7, 2016
1 parent b1eede9 commit 1f8702d
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 44 deletions.
95 changes: 52 additions & 43 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
Expand All @@ -9,7 +8,7 @@
namespace Wallcat
{
// TODO: Update Wallpaper Daily
// TODO: Make cat blink when busy
// TODO: Display info about the wallpaper

internal static class Program
{
Expand All @@ -23,7 +22,7 @@ private static void Main()
Console.WriteLine($@"Current Channel: {Properties.Settings.Default.CurrentChannel}");
Console.WriteLine($@"Current Wallpaper: {Properties.Settings.Default.CurrentWallpaper}");
Console.WriteLine($@"Last Checked: {Properties.Settings.Default.LastChecked}");
Console.WriteLine($@"Storage: {ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath}");
Console.WriteLine($@"Storage: {System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath}");
#endif

Application.EnableVisualStyles();
Expand All @@ -35,39 +34,48 @@ private static void Main()
public class MyCustomApplicationContext : ApplicationContext
{
private readonly NotifyIcon _trayIcon;
private readonly ContextMenu _contextMenu;
private readonly IconAnimation _iconAnimation;
private readonly WallcatService _wallcatService;

public MyCustomApplicationContext()
{
Application.ApplicationExit += OnApplicationExit;

_wallcatService = new WallcatService();
_contextMenu = new ContextMenu();
_trayIcon = new NotifyIcon
{
Icon = Resources.AppIcon,
ContextMenu = _contextMenu,
Visible = true
};
_iconAnimation = new IconAnimation(ref _trayIcon);

var contextMenu = new ContextMenu();
contextMenu.MenuItems.Add(new MenuItem("Channels") { Enabled = false });
// Add Menu Items
_iconAnimation.Start();
var channels = _wallcatService.GetChannels();
foreach (var channel in channels)
_contextMenu.MenuItems.Add("Channels", channels.Select(x => new MenuItem(x.title, SelectChannel) { Tag = x.id }).ToArray());
_contextMenu.MenuItems.AddRange(new[]
{
contextMenu.MenuItems.Add(new MenuItem(channel.title, SelectChannel) { Tag = channel.id });
}
new MenuItem("Credit", (sender, args) => Process.Start("https://wall.cat/")),
new MenuItem("Exit", (sender, args) => Application.Exit())
});

contextMenu.MenuItems.Add(new MenuItem("-") { Enabled = false });
contextMenu.MenuItems.Add(new MenuItem("Credit", Website));
contextMenu.MenuItems.Add(new MenuItem("Exit", Exit));

// Set Current Channel
// Set Default Channel
if (string.IsNullOrWhiteSpace(Properties.Settings.Default.CurrentChannel))
{
Properties.Settings.Default.CurrentChannel = channels.FirstOrDefault(x => x.isDefault)?.id;
Properties.Settings.Default.Save();
var channel = channels.FirstOrDefault(x => x.isDefault);
if (channel != null)
{
Properties.Settings.Default.CurrentChannel = channel.id;
Properties.Settings.Default.Save();
UpdateWallpaper();

_trayIcon.ShowBalloonTip(10 * 1000, "Welcome to Wallcat", $"Enjoy the {channel.title} channel!", ToolTipIcon.Info);
}
}
UpdateWallpaper();

// Initialize Tray Icon
_trayIcon = new NotifyIcon
{
Icon = Resources.AppIcon,
ContextMenu = contextMenu,
Visible = true
};
_iconAnimation.Stop();
}

private void UpdateWallpaper()
Expand All @@ -80,33 +88,34 @@ private void UpdateWallpaper()
}
}


private void SelectChannel(object sender, EventArgs e)
{
var channel = (string)((MenuItem)sender).Tag;
var wallpaper = _wallcatService.GetWallpaper(channel);
if (wallpaper.id == Properties.Settings.Default.CurrentWallpaper) return;
var filePath = DownloadFile.Get(wallpaper.url.o);
SetWallpaper.Apply(filePath, SetWallpaper.Style.Span);
_iconAnimation.Start();

// Update Settings
Properties.Settings.Default.CurrentChannel = channel;
Properties.Settings.Default.CurrentWallpaper = wallpaper.id;
Properties.Settings.Default.LastChecked = DateTime.Now.Date;
Properties.Settings.Default.Save();
}

private static void Website(object sender, EventArgs e)
{
Process.Start("https://wall.cat/");
try
{
var channel = (string) ((MenuItem) sender).Tag;
var wallpaper = _wallcatService.GetWallpaper(channel);
if (wallpaper.id == Properties.Settings.Default.CurrentWallpaper) return;
var filePath = DownloadFile.Get(wallpaper.url.o);
SetWallpaper.Apply(filePath, SetWallpaper.Style.Span);

// Update Settings
Properties.Settings.Default.CurrentChannel = channel;
Properties.Settings.Default.CurrentWallpaper = wallpaper.id;
Properties.Settings.Default.LastChecked = DateTime.Now.Date;
Properties.Settings.Default.Save();
}
finally
{
_iconAnimation.Stop();
}
}

private void Exit(object sender, EventArgs e)
private void OnApplicationExit(object sender, EventArgs e)
{
// Hide tray icon, otherwise it will remain shown until user mouses over it
_trayIcon.Visible = false;

Application.Exit();
}
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Unofficial Wallcat for Windows
==============================

![Demo](https://github.com/PaitoAnderson/WallcatWindows/raw/master/demo.gif)
![Demo](https://github.com/PaitoAnderson/WallcatWindows/raw/master/demo.gif)

![Download Latest](https://github.com/PaitoAnderson/WallcatWindows/releases/latest)
10 changes: 10 additions & 0 deletions Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,7 @@
<data name="AppIcon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\AppIcon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="AppIconAlt" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\AppIconAlt.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Binary file added Resources/AppIconAlt.ico
Binary file not shown.
44 changes: 44 additions & 0 deletions Util/IconAnimation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Windows.Forms;

namespace Wallcat.Util
{
public class IconAnimation
{
private readonly Timer _timer = new Timer {Interval = 500};
private readonly NotifyIcon _notifyIcon;
private int _currentIteration;
private const int Frames = 1;

public IconAnimation(ref NotifyIcon notifyIcon)
{
_notifyIcon = notifyIcon;

// Timer Event
_timer.Tick += (sender, args) =>
{
_currentIteration++;
if (_currentIteration > Frames) _currentIteration = 0;
_notifyIcon.Icon = (_currentIteration == 0) ? Resources.AppIcon : Resources.AppIconAlt;
};
}

public void Start()
{
if (_timer.Enabled == false)
{
_notifyIcon.Icon = Resources.AppIconAlt;
_currentIteration = 0;
_timer.Start();
}
}

public void Stop()
{
if (_timer.Enabled)
{
_timer.Stop();
_notifyIcon.Icon = Resources.AppIcon;
}
}
}
}
1 change: 1 addition & 0 deletions Wallcat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
</Compile>
<Compile Include="Services\WallcatService.cs" />
<Compile Include="Util\DownloadFile.cs" />
<Compile Include="Util\IconAnimation.cs" />
<Compile Include="Util\SetWallpaper.cs" />
<EmbeddedResource Include="Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand Down

0 comments on commit 1f8702d

Please sign in to comment.