Skip to content

Commit

Permalink
Colored icon in tray bar
Browse files Browse the repository at this point in the history
  • Loading branch information
jokedst committed Apr 8, 2018
1 parent 2c22689 commit 8f061b2
Show file tree
Hide file tree
Showing 55 changed files with 7,310 additions and 7,582 deletions.
7 changes: 7 additions & 0 deletions ATray/ATray.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
Expand Down Expand Up @@ -144,6 +145,7 @@
<Reference Include="System.Design" />
<Reference Include="System.Management" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
Expand Down Expand Up @@ -206,6 +208,8 @@
<Compile Include="Dialogs\SettingsForm.Designer.cs">
<DependentUpon>SettingsForm.cs</DependentUpon>
</Compile>
<Compile Include="Tools\BitmapTools.cs" />
<Compile Include="Tools\OverallStatusType.cs" />
<EmbeddedResource Include="Dialogs\ActivityHistoryForm.resx">
<DependentUpon>ActivityHistoryForm.cs</DependentUpon>
</EmbeddedResource>
Expand Down Expand Up @@ -245,6 +249,8 @@
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="icons.png" />
<None Include="Resources\icons.bmp" />
<None Include="Resources\anim.png" />
<None Include="Resources\anim.jpg" />
<None Include="Resources\Animation.bmp" />
Expand Down Expand Up @@ -276,6 +282,7 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- Squirrel - create release package on Release builds -->
<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
Expand Down
1 change: 1 addition & 0 deletions ATray/Dialogs/SettingsForm.Designer.cs

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

51 changes: 40 additions & 11 deletions ATray/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ATray
using ATray.Tools;

namespace ATray
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -27,13 +29,14 @@ public partial class MainWindow : Form
private DateTime lastTimerEvent = DateTime.MinValue;
private ActivityHistoryForm historyForm;
private SettingsForm settingsForm;
private OverallStatusType OverallStatus;

public MainWindow()
{
InitializeComponent();
WindowState = FormWindowState.Minimized;
ShowInTaskbar = false;
Icon = trayIcon.Icon = Program.MainIcon;
Icon = trayIcon.Icon = Program.GreyIcon;
Program.Repositories.RepositoryStatusChanged += OnRepositoryStatusChanged;
SystemEvents.SessionSwitch += SystemEventsOnSessionSwitch;
#if DEBUG
Expand All @@ -43,9 +46,12 @@ public MainWindow()
// new DiskUsageForm().Show();
#endif
CreateRepositoryMenyEntries();

var animTray = new IconAnimator(trayIcon, Properties.Resources.anim1);
animTray.StartAnimation();
trayMenu.Items.Insert(0, new ToolStripMenuItem("DEBUGTEST", null, (sender, args) =>
{
this.trayIcon.Icon = Program.GreyIcon;
}));
//var animTray = new IconAnimator(trayIcon, Properties.Resources.anim1);
//animTray.StartAnimation();
}

public void CreateRepositoryMenyEntries()
Expand Down Expand Up @@ -74,8 +80,7 @@ public void CreateRepositoryMenyEntries()
if (trayMenu.Items.ContainsKey(repo.Location))
continue;
var repoLocation = repo.Location; // for clojures
var submenu = new ToolStripMenuItem();
submenu.Name = repoLocation;
var submenu = new ToolStripMenuItem {Name = repoLocation};

if (repo is GitRepository && Program.TortoiseGitLocation != null)
{
Expand All @@ -95,13 +100,33 @@ public void CreateRepositoryMenyEntries()
UpdateRepoMenu(submenu, repo.Name, repo.LastStatus);
trayMenu.Items.Insert(0, submenu);
}

UpdateIcon();
}

private void UpdateIcon()
{
var worstStatus = Program.Repositories.Select(x => x.LastStatus.ToOverallStatus()).OrderBy(x=>x).LastOrDefault();
if (worstStatus == OverallStatus) return;
OverallStatus = worstStatus;
switch (OverallStatus)
{
case OverallStatusType.Ok:
this.trayIcon.Icon = Program.GreyIcon;
break;
case OverallStatusType.WarnAhead:
case OverallStatusType.WarnBehind:
this.trayIcon.Icon = Program.YellowIcon;
break;
case OverallStatusType.CodeRed:
this.trayIcon.Icon = Program.MainIcon;
break;
}
}

private void UpdateRepoMenu(ToolStripItem menuItem, string repoName, RepoStatus status)
{
menuItem.Text = $"{repoName}: {Environment.NewLine} {status}";
//var r = new Random();
//menuItem.BackColor = Color.FromArgb(r.Next(256), r.Next(256), r.Next(256));

if (status == RepoStatus.Conflict)
menuItem.BackColor = Color.FromArgb(0x80, Color.Red);
Expand All @@ -126,7 +151,11 @@ private void OnRepositoryStatusChanged(object sender, RepositoryEventArgs e)
foreach (var menuRow in trayMenu.Items.Find(e.Location, false))
{
//this.UIThread(() => menuRow.Text = e.Name + ": \n" + e.NewStatus);
this.UIThread(() => UpdateRepoMenu(menuRow, e.Name, e.NewStatus));
this.UIThread(() =>
{
UpdateRepoMenu(menuRow, e.Name, e.NewStatus);
UpdateIcon();
});
}
}

Expand Down Expand Up @@ -265,7 +294,7 @@ private void OnMenuClickHistory(object sender, EventArgs e)
historyForm = new ActivityHistoryForm();
if (historyForm.IsDisposed) return;
historyForm.Show();
settingsForm.Focus();
historyForm.Focus();
}

private void OnMenuClickSettings(object sender, EventArgs e)
Expand Down
24 changes: 22 additions & 2 deletions ATray/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Drawing;
using System.Reflection;
using NuGet;

Expand All @@ -18,11 +19,15 @@ public static class Program
/// <summary> Directory for storing application data </summary>
#if DEBUG
internal static string SettingsDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Application.CompanyName, Application.ProductName, "DEBUG");
internal static System.Drawing.Icon MainIcon = Properties.Resources.debug_icon;
//internal static Icon MainIcon = Properties.Resources.debug_icon;
#else
internal static string SettingsDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Application.CompanyName, Application.ProductName);
internal static System.Drawing.Icon MainIcon = Properties.Resources.main_icon;
//internal static Icon MainIcon = Properties.Resources.main_icon;
#endif
internal static Icon GreyIcon;
internal static Icon YellowIcon;
internal static Icon MainIcon;

internal static string RepoListFilePath = Path.Combine(SettingsDirectory, "repositories.json");
internal static string ConfigurationFilePath = Path.Combine(SettingsDirectory, "atray.ini");

Expand All @@ -47,6 +52,10 @@ public static void Main()
return;
}

var icobmp=Properties.Resources.main_icon.ToBitmap();


LoadIcons();
if (!Directory.Exists(SettingsDirectory))
Directory.CreateDirectory(SettingsDirectory);

Expand All @@ -67,6 +76,17 @@ public static void Main()
Trace.TraceInformation("LEAVING!");
}

private static void LoadIcons()
{
int y = 0;
#if DEBUG
y += 32;
#endif
GreyIcon = Icon.FromHandle(Properties.Resources.icons.Clone(new Rectangle(0, y, 32, 32), Properties.Resources.icons.PixelFormat).GetHicon());
YellowIcon = Icon.FromHandle(Properties.Resources.icons.Clone(new Rectangle(32, y, 32, 32), Properties.Resources.icons.PixelFormat).GetHicon());
MainIcon = Icon.FromHandle(Properties.Resources.icons.Clone(new Rectangle(64, y, 32, 32), Properties.Resources.icons.PixelFormat).GetHicon());
}

private static void DetectInstalledPrograms()
{
var paths = Environment.GetEnvironmentVariable("Path")?.Split(';');
Expand Down
10 changes: 10 additions & 0 deletions ATray/Properties/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 ATray/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
<data name="debug_icon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\debug.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="icons" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\icons.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="main_icon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\favicon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
Expand Down
Binary file added ATray/Resources/icons.bmp
Binary file not shown.
Binary file added ATray/Resources/icons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions ATray/Tools/BitmapTools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ATray.Tools
{
public static class BitmapTools
{
public static void ApplyPerPixel(this Bitmap bmp, Action<IntPtr> modifier)
{
if (bmp.PixelFormat != PixelFormat.Format32bppArgb)
throw new Exception("Ahhh my icon has changed format");
var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
var bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);

var bitsPerPixel = Image.GetPixelFormatSize(bmpData.PixelFormat);
var bytesPerPixel = (bitsPerPixel + 7) / 8;
var unusedBytes = bmpData.Stride - (bmpData.Width * bytesPerPixel);

unsafe
{
var scan0 = (byte*)bmpData.Scan0.ToPointer();
var pixelPointer = scan0;
for (var i = 0; i < bmpData.Height; ++i)
{
for (var j = 0; j < bmpData.Width; ++j)
{
modifier((IntPtr) pixelPointer);
pixelPointer += bytesPerPixel;
}

pixelPointer += unusedBytes;
}
}
bmp.UnlockBits(bmpData);
}
}
}
50 changes: 50 additions & 0 deletions ATray/Tools/OverallStatusType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Drawing;
using RepositoryManager;

namespace ATray.Tools
{
public enum OverallStatusType
{
Ok=0,
WarnAhead=1,
WarnBehind=2,
CodeRed=3
}

public static class Extensions
{
public static OverallStatusType ToOverallStatus(this RepoStatus status)
{
switch (status)
{
case RepoStatus.Conflict:
return OverallStatusType.CodeRed;
case RepoStatus.Behind:
return OverallStatusType.WarnBehind;
case RepoStatus.Dirty:
return OverallStatusType.WarnAhead;
default:
return OverallStatusType.Ok;
}
}

public static OverallStatusType WorstOf(this OverallStatusType overallStatus,RepoStatus status)
{
var converted = status.ToOverallStatus();
return converted > overallStatus ? converted : overallStatus;
}

public static Color ToColor(OverallStatusType overallStatus)
{
switch (overallStatus)
{
case OverallStatusType.Ok: return Color.Transparent;
case OverallStatusType.WarnAhead: return Color.FromArgb(0x80, 0xB1, 0xB1, 0xFF);
case OverallStatusType.CodeRed: return Color.FromArgb(0x80, Color.Red);
case OverallStatusType.WarnBehind: return Color.FromArgb(0x80, Color.Yellow);
default: throw new ArgumentOutOfRangeException(nameof(overallStatus), overallStatus, null);
}
}
}
}
Binary file added ATray/icons.pdn
Binary file not shown.
Binary file added ATray/icons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion RepositoryManager/CredentialsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static Credentials WinCred(string url, string usernameFromUrl, SupportedC
return new UsernamePasswordCredentials { Username = creds.Username, Password = creds.Password };
var uri = new Uri(url);
var userPart = (string.IsNullOrEmpty(usernameFromUrl) ? string.Empty : usernameFromUrl + "@");
creds.Target = string.Format("git:{0}://{1}{2}", uri.Scheme, userPart, uri.Host);
creds.Target = $"git:{uri.Scheme}://{userPart}{uri.Host}";

if (!creds.Load())
{
Expand Down
5 changes: 5 additions & 0 deletions RepositoryManager/DelayedFileSystemWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public DelayedFileSystemWatcher(string path, Action callbackAction)
Trace.TraceInformation($"Watched path {path} modified ({e.ChangeType.ToString()} {e.FullPath})");
_postponedEvent.StartOrUpdate();
};
_watcher.Deleted+= (s, e) =>
{
Trace.TraceInformation($"Watched path {path} deleted ({e.ChangeType.ToString()} {e.FullPath})");
_postponedEvent.StartOrUpdate();
};
}

/// <summary> Releases all resources used by the current instance of <see cref="DelayedFileSystemWatcher" />. </summary>
Expand Down
Loading

0 comments on commit 8f061b2

Please sign in to comment.