Skip to content

Commit

Permalink
Fix tray icon menu position and color
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Nov 25, 2018
1 parent c4f067b commit 99e7f13
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 108 deletions.
3 changes: 3 additions & 0 deletions GUI/CKAN-GUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@
<Compile Include="MainTabControl.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="MainTrayIcon.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MainWait.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
3 changes: 2 additions & 1 deletion GUI/Main.Designer.cs

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

108 changes: 1 addition & 107 deletions GUI/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public Main(string[] cmdlineArgs, KSPManager mgr, GUIUser user, bool showConsole
settingsToolStripMenuItem.DropDown.Renderer = new FlatToolStripRenderer();
helpToolStripMenuItem.DropDown.Renderer = new FlatToolStripRenderer();
FilterToolButton.DropDown.Renderer = new FlatToolStripRenderer();
this.minimizedContextMenuStrip.Renderer = new FlatToolStripRenderer();

// We need to initialize the error dialog first to display errors.
errorDialog = controlFactory.CreateControl<ErrorDialog>();
Expand Down Expand Up @@ -1131,113 +1132,6 @@ private void Main_Resize(object sender, EventArgs e)
UpdateTrayState();
}

private void minimizeNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
OpenWindow();
}

private void updatesToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenWindow();
MarkAllUpdatesToolButton_Click(sender, e);
}

private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
UpdateRepo();
}

private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
{
configuration.RefreshPaused = !configuration.RefreshPaused;
if (configuration.RefreshPaused)
{
refreshTimer.Stop();
pauseToolStripMenuItem.Text = "Resume";
}
else
{
refreshTimer.Start();
pauseToolStripMenuItem.Text = "Pause";
}
}

private void openCKANToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenWindow();
}

private void cKANSettingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
OpenWindow();
new SettingsDialog().ShowDialog();
}

#region Tray Behaviour

public void CheckTrayState()
{
enableTrayIcon = configuration.EnableTrayIcon;
minimizeToTray = configuration.MinimizeToTray;
pauseToolStripMenuItem.Enabled = winReg.RefreshRate != 0;
pauseToolStripMenuItem.Text = configuration.RefreshPaused ? "Resume" : "Pause";
UpdateTrayState();
}

private void UpdateTrayState()
{
if (enableTrayIcon)
{
minimizeNotifyIcon.Visible = true;

if (WindowState == FormWindowState.Minimized)
{
if (minimizeToTray)
{
// Remove our taskbar entry
Hide();
}
}
else
{
// Save the window state
configuration.IsWindowMaximised = WindowState == FormWindowState.Maximized;
configuration.Save();
}
}
else
{
minimizeNotifyIcon.Visible = false;
}
}

public void UpdateTrayInfo()
{
var count = mainModList.CountModsByFilter(GUIModFilter.InstalledUpdateAvailable);

if (count == 0)
{
updatesToolStripMenuItem.Enabled = false;
updatesToolStripMenuItem.Text = "No available updates";
}
else
{
updatesToolStripMenuItem.Enabled = true;
updatesToolStripMenuItem.Text = $"{count} available update" + (count == 1 ? "" : "s");
}
}

/// <summary>
/// Open the GUI and set it to the correct state.
/// </summary>
public void OpenWindow()
{
Show();
WindowState = configuration.IsWindowMaximised ? FormWindowState.Maximized : FormWindowState.Normal;
}

#endregion

#region Navigation History

private void NavInit()
Expand Down
152 changes: 152 additions & 0 deletions GUI/MainTrayIcon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace CKAN
{
public partial class Main
{
#region Tray Behaviour

public void CheckTrayState()
{
enableTrayIcon = configuration.EnableTrayIcon;
minimizeToTray = configuration.MinimizeToTray;
pauseToolStripMenuItem.Enabled = winReg.RefreshRate != 0;
pauseToolStripMenuItem.Text = configuration.RefreshPaused ? "Resume" : "Pause";
UpdateTrayState();
}

private void UpdateTrayState()
{
if (enableTrayIcon)
{
minimizeNotifyIcon.Visible = true;

if (WindowState == FormWindowState.Minimized)
{
if (minimizeToTray)
{
// Remove our taskbar entry
Hide();
}
}
else
{
// Save the window state
configuration.IsWindowMaximised = WindowState == FormWindowState.Maximized;
configuration.Save();
}
}
else
{
//minimizeNotifyIcon.Visible = false;
}
}

public void UpdateTrayInfo()
{
var count = mainModList.CountModsByFilter(GUIModFilter.InstalledUpdateAvailable);

if (count == 0)
{
updatesToolStripMenuItem.Enabled = false;
updatesToolStripMenuItem.Text = "No available updates";
}
else
{
updatesToolStripMenuItem.Enabled = true;
updatesToolStripMenuItem.Text = $"{count} available update" + (count == 1 ? "" : "s");
}
}

/// <summary>
/// Open the GUI and set it to the correct state.
/// </summary>
public void OpenWindow()
{
Show();
WindowState = configuration.IsWindowMaximised ? FormWindowState.Maximized : FormWindowState.Normal;
}

private void minimizeNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
OpenWindow();
}

private void updatesToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenWindow();
MarkAllUpdatesToolButton_Click(sender, e);
}

private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
UpdateRepo();
}

private void pauseToolStripMenuItem_Click(object sender, EventArgs e)
{
configuration.RefreshPaused = !configuration.RefreshPaused;
if (configuration.RefreshPaused)
{
refreshTimer.Stop();
pauseToolStripMenuItem.Text = "Resume";
}
else
{
refreshTimer.Start();
pauseToolStripMenuItem.Text = "Pause";
}
}

private void openCKANToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenWindow();
}

private void cKANSettingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
OpenWindow();
new SettingsDialog().ShowDialog();
}

private void minimizedContextMenuStrip_Opening(object sender, CancelEventArgs e)
{
// The menu location can be partly off-screen by default.
// Fix it.
minimizedContextMenuStrip.Location = ClampedLocation(
minimizedContextMenuStrip.Location,
minimizedContextMenuStrip.Size
);
}

private Point ClampedLocation(Point location, Size size)
{
var rect = new Rectangle(location, size);
// Find a screen that the default position overlaps
foreach (Screen screen in Screen.AllScreens)
{
if (screen.WorkingArea.IntersectsWith(rect))
{
// Slide the whole menu fully onto the screen
if (location.X < screen.WorkingArea.Top)
location.X = screen.WorkingArea.Top;
if (location.Y < screen.WorkingArea.Left)
location.Y = screen.WorkingArea.Left;
if (location.X + size.Width > screen.WorkingArea.Right)
location.X = screen.WorkingArea.Right - size.Width;
if (location.Y + size.Height > screen.WorkingArea.Bottom)
location.Y = screen.WorkingArea.Bottom - size.Height;
// Stop checking screens
break;
}
}
return location;
}

#endregion

}
}

0 comments on commit 99e7f13

Please sign in to comment.