From 99e7f13ce52d7e25a5eb9d8e8548139b505adaa4 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Fri, 23 Nov 2018 23:38:41 -0600 Subject: [PATCH] Fix tray icon menu position and color --- GUI/CKAN-GUI.csproj | 3 + GUI/Main.Designer.cs | 3 +- GUI/Main.cs | 108 +----------------------------- GUI/MainTrayIcon.cs | 152 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 108 deletions(-) create mode 100644 GUI/MainTrayIcon.cs diff --git a/GUI/CKAN-GUI.csproj b/GUI/CKAN-GUI.csproj index 41390010ba..5688b09e28 100644 --- a/GUI/CKAN-GUI.csproj +++ b/GUI/CKAN-GUI.csproj @@ -176,6 +176,9 @@ Component + + Form + Form diff --git a/GUI/Main.Designer.cs b/GUI/Main.Designer.cs index 8bd38a3474..8a52d6f830 100644 --- a/GUI/Main.Designer.cs +++ b/GUI/Main.Designer.cs @@ -1146,7 +1146,8 @@ private void InitializeComponent() this.quitToolStripMenuItem}); this.minimizedContextMenuStrip.Name = "minimizedContextMenuStrip"; this.minimizedContextMenuStrip.Size = new System.Drawing.Size(181, 148); - // + this.minimizedContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(this.minimizedContextMenuStrip_Opening); + // // updatesToolStripMenuItem // this.updatesToolStripMenuItem.Name = "updatesToolStripMenuItem"; diff --git a/GUI/Main.cs b/GUI/Main.cs index 15ba9b4400..585fc3fe8d 100644 --- a/GUI/Main.cs +++ b/GUI/Main.cs @@ -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(); @@ -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"); - } - } - - /// - /// Open the GUI and set it to the correct state. - /// - public void OpenWindow() - { - Show(); - WindowState = configuration.IsWindowMaximised ? FormWindowState.Maximized : FormWindowState.Normal; - } - - #endregion - #region Navigation History private void NavInit() diff --git a/GUI/MainTrayIcon.cs b/GUI/MainTrayIcon.cs new file mode 100644 index 0000000000..0383e9d708 --- /dev/null +++ b/GUI/MainTrayIcon.cs @@ -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"); + } + } + + /// + /// Open the GUI and set it to the correct state. + /// + 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 + + } +}