From 080b1e8b2c3f2b5d019f4f7e2ee55826acdc75a3 Mon Sep 17 00:00:00 2001 From: MrMelon Date: Thu, 22 Oct 2020 22:59:42 +0100 Subject: [PATCH] Some minor improvements --- MelonVPNClient/MainWindow.cs | 94 ++++++++++++++++++++++--------- MelonVPNClient/gtk-gui/gui.stetic | 1 + 2 files changed, 69 insertions(+), 26 deletions(-) diff --git a/MelonVPNClient/MainWindow.cs b/MelonVPNClient/MainWindow.cs index f4ec22e..26470fa 100644 --- a/MelonVPNClient/MainWindow.cs +++ b/MelonVPNClient/MainWindow.cs @@ -22,6 +22,7 @@ public partial class MainWindow : Window private Indicator trayIcon; private PopupMenu trayMenu; private bool ConnectedToVPN; + private bool IsHidden; public static readonly string MelonIconImg = "MiniMelonVPNIcon.png"; public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png"; @@ -122,8 +123,8 @@ public partial class MainWindow : Window private void OnActivateTrayIcon() { - Visible = !Visible; - UpdateTrayMenu(); + if (IsHidden) JumpFromTray(); + else SendToTray(); } void OnToggleMenuItem() @@ -134,7 +135,7 @@ public partial class MainWindow : Window void UpdateTrayMenu() { - if (trayMenu != null) trayMenu.Update(ConnectedToVPN, Visible); + if (trayMenu != null) trayMenu.Update(ConnectedToVPN, !IsHidden); } void UpdateIcon(string file) @@ -249,23 +250,18 @@ public partial class MainWindow : Window { if (old != current) { - if (current == true) - { - Notification pop = new Notification("Melon VPN", "Connected to the VPN"); - pop.Show(); - CloseNotificationAfterWait(pop, 1000); - } - else - { - Notification pop = new Notification("Melon VPN", "Disconnected from the VPN"); - pop.Show(); - CloseNotificationAfterWait(pop, 1000); - } + string msg = (current ? "Connected to" : "Disconnected from")+" the VPN"; + string icon = "/usr/lib/melon-vpn/MiniMelonVPN" + (current ? "Online" : "Icon") + ".png"; + Notification pop = new Notification("Melon VPN", msg, icon); + pop.Show(); + CloseNotificationAfterWait(pop, 1000); } } void CloseNotificationAfterWait(Notification pop, int timeout) - { + { + // Close the notification after a wait + // so it doesn't hang around Thread wait = new Thread(() => { Thread.Sleep(timeout); @@ -277,24 +273,75 @@ public partial class MainWindow : Window wait.Start(); } + void SendToTray() + { + IsHidden = true; + + // Try hiding the window + Iconify(); + SkipPagerHint = true; + SkipTaskbarHint = true; + + // Changing the visible property needs to be + // delayed for the window to start minimizing + Thread thread = new Thread(() => + { + Thread.Sleep(100); + Visible = false; + }) + { + IsBackground = true + }; + thread.Start(); + + UpdateTrayMenu(); + } + + void JumpFromTray() + { + IsHidden = false; + + Visible = true; + SkipPagerHint = false; + SkipTaskbarHint = false; + + // Just wait before triggering these to get + // the right effect + Thread thread = new Thread(() => + { + Thread.Sleep(100); + // Deiconify and Present seem to do the same + // thing but sometimes Deiconify doesn't work + Deiconify(); + Present(); + }) + { + IsBackground = true + }; + thread.Start(); + + UpdateTrayMenu(); + } + protected void OnDeleteEvent(object sender, DeleteEventArgs a) { - if (ConnectedToVPN) - { - Visible = false; - UpdateTrayMenu(); - } + // Try to prevent destroying the tray icon + // while connected to VPN + if (ConnectedToVPN) SendToTray(); else QuitApp(); a.RetVal = true; } protected void QuitApp() { + // Kill this thread if it exists if (wrapper != null) { wrapper.Kill(); wrapper.Join(); } + + // Destroy the tray icon first if (trayIcon != null) trayIcon.Dispose(); Application.Quit(); } @@ -339,9 +386,4 @@ public partial class MainWindow : Window { Refresh(); } - - void OnViewClientsClicked(object sender, EventArgs e) - { - Refresh(); - } } diff --git a/MelonVPNClient/gtk-gui/gui.stetic b/MelonVPNClient/gtk-gui/gui.stetic index 3efaffc..f947766 100644 --- a/MelonVPNClient/gtk-gui/gui.stetic +++ b/MelonVPNClient/gtk-gui/gui.stetic @@ -5,6 +5,7 @@ +