From ee26a51b346aff60d8e47d81cfb8c3d1d712e7f9 Mon Sep 17 00:00:00 2001 From: MrMelon Date: Fri, 23 Oct 2020 11:45:59 +0100 Subject: [PATCH] Minor improvements to the hide and show effect --- MelonVPNClient/MainWindow.cs | 93 ++++++++++++++-------------- MelonVPNClient/gtk-gui/MainWindow.cs | 1 + MelonVPNClient/gtk-gui/gui.stetic | 1 + 3 files changed, 48 insertions(+), 47 deletions(-) diff --git a/MelonVPNClient/MainWindow.cs b/MelonVPNClient/MainWindow.cs index 26470fa..998eb41 100644 --- a/MelonVPNClient/MainWindow.cs +++ b/MelonVPNClient/MainWindow.cs @@ -5,7 +5,7 @@ using System.Reflection; using System.Threading; using AppIndicator3; using Gtk; -using MelonVPNClient; +using MelonVPNClient; using MelonVPNCore; using Notify; @@ -114,10 +114,10 @@ public partial class MainWindow : Window startupCheckThread.Start(); trayMenu = new PopupMenu(OnActivateTrayIcon, OnToggleMenuItem, QuitApp); - trayIcon = new Indicator("melonvpn", "MiniMelonVPNIcon", (int)IndicatorCategory.ApplicationStatus) - { - Menu = trayMenu.GetMenu(), - Status = (int)IndicatorStatus.Active + trayIcon = new Indicator("melonvpn", "MiniMelonVPNIcon", (int)IndicatorCategory.ApplicationStatus) + { + Menu = trayMenu.GetMenu(), + Status = (int)IndicatorStatus.Active }; } @@ -125,17 +125,17 @@ public partial class MainWindow : Window { if (IsHidden) JumpFromTray(); else SendToTray(); - } - + } + void OnToggleMenuItem() { if (ConnectedToVPN) OnStopClicked(this, new EventArgs()); else OnStartClicked(this, new EventArgs()); } - void UpdateTrayMenu() - { - if (trayMenu != null) trayMenu.Update(ConnectedToVPN, !IsHidden); + void UpdateTrayMenu() + { + if (trayMenu != null) trayMenu.Update(ConnectedToVPN, !IsHidden); } void UpdateIcon(string file) @@ -250,7 +250,7 @@ public partial class MainWindow : Window { if (old != current) { - string msg = (current ? "Connected to" : "Disconnected from")+" the VPN"; + 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(); @@ -258,22 +258,22 @@ public partial class MainWindow : Window } } - void CloseNotificationAfterWait(Notification pop, int timeout) + 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); - pop.Close(); - }) - { - IsBackground = true - }; - wait.Start(); + // so it doesn't hang around + Thread wait = new Thread(() => + { + Thread.Sleep(timeout); + pop.Close(); + }) + { + IsBackground = true + }; + wait.Start(); } - void SendToTray() + void SendToTray() { IsHidden = true; @@ -286,41 +286,29 @@ public partial class MainWindow : Window // delayed for the window to start minimizing Thread thread = new Thread(() => { - Thread.Sleep(100); - Visible = false; + Thread.Sleep(100); + Visible = false; }) { IsBackground = true }; thread.Start(); - UpdateTrayMenu(); + UpdateTrayMenu(); } - void JumpFromTray() + void JumpFromTray() { IsHidden = false; - Visible = true; + // Present triggers `Visible = true;` + // it is then called again once the window is visible + // so it triggers `Deiconify();` + Present(); 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(); + UpdateTrayMenu(); } protected void OnDeleteEvent(object sender, DeleteEventArgs a) @@ -382,8 +370,19 @@ public partial class MainWindow : Window } } - void OnRefreshClicked(object sender, EventArgs e) + void OnRefreshClicked(object sender, EventArgs e) => Refresh(); + + protected void OnVisibilityNotifyEvent(object o, VisibilityNotifyEventArgs args) { - Refresh(); - } + // Calling present again forces the window to `Deiconify();` + Thread thread = new Thread(() => + { + Thread.Sleep(100); + Present(); + }) + { + IsBackground = true + }; + thread.Start(); + } } diff --git a/MelonVPNClient/gtk-gui/MainWindow.cs b/MelonVPNClient/gtk-gui/MainWindow.cs index d819174..2cd50c7 100644 --- a/MelonVPNClient/gtk-gui/MainWindow.cs +++ b/MelonVPNClient/gtk-gui/MainWindow.cs @@ -25,5 +25,6 @@ public partial class MainWindow this.DefaultHeight = 86; this.Show(); this.DeleteEvent += new global::Gtk.DeleteEventHandler(this.OnDeleteEvent); + this.VisibilityNotifyEvent += new global::Gtk.VisibilityNotifyEventHandler(this.OnVisibilityNotifyEvent); } } diff --git a/MelonVPNClient/gtk-gui/gui.stetic b/MelonVPNClient/gtk-gui/gui.stetic index f947766..eefdfbd 100644 --- a/MelonVPNClient/gtk-gui/gui.stetic +++ b/MelonVPNClient/gtk-gui/gui.stetic @@ -13,6 +13,7 @@ MainWindow CenterOnParent +