Minor improvements to the hide and show effect

This commit is contained in:
Melon 2020-10-23 11:45:59 +01:00
parent 080b1e8b2c
commit ee26a51b34
3 changed files with 48 additions and 47 deletions

View File

@ -5,7 +5,7 @@ using System.Reflection;
using System.Threading; using System.Threading;
using AppIndicator3; using AppIndicator3;
using Gtk; using Gtk;
using MelonVPNClient; using MelonVPNClient;
using MelonVPNCore; using MelonVPNCore;
using Notify; using Notify;
@ -114,10 +114,10 @@ public partial class MainWindow : Window
startupCheckThread.Start(); startupCheckThread.Start();
trayMenu = new PopupMenu(OnActivateTrayIcon, OnToggleMenuItem, QuitApp); trayMenu = new PopupMenu(OnActivateTrayIcon, OnToggleMenuItem, QuitApp);
trayIcon = new Indicator("melonvpn", "MiniMelonVPNIcon", (int)IndicatorCategory.ApplicationStatus) trayIcon = new Indicator("melonvpn", "MiniMelonVPNIcon", (int)IndicatorCategory.ApplicationStatus)
{ {
Menu = trayMenu.GetMenu(), Menu = trayMenu.GetMenu(),
Status = (int)IndicatorStatus.Active Status = (int)IndicatorStatus.Active
}; };
} }
@ -125,17 +125,17 @@ public partial class MainWindow : Window
{ {
if (IsHidden) JumpFromTray(); if (IsHidden) JumpFromTray();
else SendToTray(); else SendToTray();
} }
void OnToggleMenuItem() void OnToggleMenuItem()
{ {
if (ConnectedToVPN) OnStopClicked(this, new EventArgs()); if (ConnectedToVPN) OnStopClicked(this, new EventArgs());
else OnStartClicked(this, new EventArgs()); else OnStartClicked(this, new EventArgs());
} }
void UpdateTrayMenu() void UpdateTrayMenu()
{ {
if (trayMenu != null) trayMenu.Update(ConnectedToVPN, !IsHidden); if (trayMenu != null) trayMenu.Update(ConnectedToVPN, !IsHidden);
} }
void UpdateIcon(string file) void UpdateIcon(string file)
@ -250,7 +250,7 @@ public partial class MainWindow : Window
{ {
if (old != current) 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"; string icon = "/usr/lib/melon-vpn/MiniMelonVPN" + (current ? "Online" : "Icon") + ".png";
Notification pop = new Notification("Melon VPN", msg, icon); Notification pop = new Notification("Melon VPN", msg, icon);
pop.Show(); 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 // Close the notification after a wait
// so it doesn't hang around // so it doesn't hang around
Thread wait = new Thread(() => Thread wait = new Thread(() =>
{ {
Thread.Sleep(timeout); Thread.Sleep(timeout);
pop.Close(); pop.Close();
}) })
{ {
IsBackground = true IsBackground = true
}; };
wait.Start(); wait.Start();
} }
void SendToTray() void SendToTray()
{ {
IsHidden = true; IsHidden = true;
@ -286,41 +286,29 @@ public partial class MainWindow : Window
// delayed for the window to start minimizing // delayed for the window to start minimizing
Thread thread = new Thread(() => Thread thread = new Thread(() =>
{ {
Thread.Sleep(100); Thread.Sleep(100);
Visible = false; Visible = false;
}) })
{ {
IsBackground = true IsBackground = true
}; };
thread.Start(); thread.Start();
UpdateTrayMenu(); UpdateTrayMenu();
} }
void JumpFromTray() void JumpFromTray()
{ {
IsHidden = false; 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; SkipPagerHint = false;
SkipTaskbarHint = 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) 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();
}
} }

View File

@ -25,5 +25,6 @@ public partial class MainWindow
this.DefaultHeight = 86; this.DefaultHeight = 86;
this.Show(); this.Show();
this.DeleteEvent += new global::Gtk.DeleteEventHandler(this.OnDeleteEvent); this.DeleteEvent += new global::Gtk.DeleteEventHandler(this.OnDeleteEvent);
this.VisibilityNotifyEvent += new global::Gtk.VisibilityNotifyEventHandler(this.OnVisibilityNotifyEvent);
} }
} }

View File

@ -13,6 +13,7 @@
<property name="Title" translatable="yes">MainWindow</property> <property name="Title" translatable="yes">MainWindow</property>
<property name="WindowPosition">CenterOnParent</property> <property name="WindowPosition">CenterOnParent</property>
<signal name="DeleteEvent" handler="OnDeleteEvent" /> <signal name="DeleteEvent" handler="OnDeleteEvent" />
<signal name="VisibilityNotifyEvent" handler="OnVisibilityNotifyEvent" />
<child> <child>
<widget class="Gtk.Fixed" id="fixed1"> <widget class="Gtk.Fixed" id="fixed1">
<property name="MemberName" /> <property name="MemberName" />