Some minor improvements

This commit is contained in:
Melon 2020-10-22 22:59:42 +01:00
parent d6116f45b8
commit 080b1e8b2c
2 changed files with 69 additions and 26 deletions

View File

@ -22,6 +22,7 @@ public partial class MainWindow : Window
private Indicator trayIcon; private Indicator trayIcon;
private PopupMenu trayMenu; private PopupMenu trayMenu;
private bool ConnectedToVPN; private bool ConnectedToVPN;
private bool IsHidden;
public static readonly string MelonIconImg = "MiniMelonVPNIcon.png"; public static readonly string MelonIconImg = "MiniMelonVPNIcon.png";
public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png"; public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png";
@ -122,8 +123,8 @@ public partial class MainWindow : Window
private void OnActivateTrayIcon() private void OnActivateTrayIcon()
{ {
Visible = !Visible; if (IsHidden) JumpFromTray();
UpdateTrayMenu(); else SendToTray();
} }
void OnToggleMenuItem() void OnToggleMenuItem()
@ -134,7 +135,7 @@ public partial class MainWindow : Window
void UpdateTrayMenu() void UpdateTrayMenu()
{ {
if (trayMenu != null) trayMenu.Update(ConnectedToVPN, Visible); if (trayMenu != null) trayMenu.Update(ConnectedToVPN, !IsHidden);
} }
void UpdateIcon(string file) void UpdateIcon(string file)
@ -249,23 +250,18 @@ public partial class MainWindow : Window
{ {
if (old != current) if (old != current)
{ {
if (current == true) 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", "Connected to the VPN"); Notification pop = new Notification("Melon VPN", msg, icon);
pop.Show(); pop.Show();
CloseNotificationAfterWait(pop, 1000); CloseNotificationAfterWait(pop, 1000);
} }
else
{
Notification pop = new Notification("Melon VPN", "Disconnected from the VPN");
pop.Show();
CloseNotificationAfterWait(pop, 1000);
}
}
} }
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 wait = new Thread(() =>
{ {
Thread.Sleep(timeout); Thread.Sleep(timeout);
@ -277,24 +273,75 @@ public partial class MainWindow : Window
wait.Start(); wait.Start();
} }
protected void OnDeleteEvent(object sender, DeleteEventArgs a) void SendToTray()
{ {
if (ConnectedToVPN) 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; Visible = false;
})
{
IsBackground = true
};
thread.Start();
UpdateTrayMenu(); 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)
{
// Try to prevent destroying the tray icon
// while connected to VPN
if (ConnectedToVPN) SendToTray();
else QuitApp(); else QuitApp();
a.RetVal = true; a.RetVal = true;
} }
protected void QuitApp() protected void QuitApp()
{ {
// Kill this thread if it exists
if (wrapper != null) if (wrapper != null)
{ {
wrapper.Kill(); wrapper.Kill();
wrapper.Join(); wrapper.Join();
} }
// Destroy the tray icon first
if (trayIcon != null) trayIcon.Dispose(); if (trayIcon != null) trayIcon.Dispose();
Application.Quit(); Application.Quit();
} }
@ -339,9 +386,4 @@ public partial class MainWindow : Window
{ {
Refresh(); Refresh();
} }
void OnViewClientsClicked(object sender, EventArgs e)
{
Refresh();
}
} }

View File

@ -5,6 +5,7 @@
</configuration> </configuration>
<import> <import>
<widget-library name="glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" /> <widget-library name="glade-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
<widget-library name="../net-libs/appindicator3-sharp.dll" />
<widget-library name="../bin/Release/MelonVPNClient.exe" internal="true" /> <widget-library name="../bin/Release/MelonVPNClient.exe" internal="true" />
</import> </import>
<widget class="Gtk.Window" id="MainWindow" design-size="204 86"> <widget class="Gtk.Window" id="MainWindow" design-size="204 86">