Add tray icon and notifications

This commit is contained in:
Melon 2020-10-05 13:06:11 +01:00
parent 52150b29e9
commit 2573e3211a
4 changed files with 224 additions and 155 deletions

View File

@ -5,6 +5,7 @@ using System.Reflection;
using System.Threading; using System.Threading;
using Gtk; using Gtk;
using MelonVPNCore; using MelonVPNCore;
using Notify;
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
@ -16,7 +17,8 @@ public partial class MainWindow : Window
private TextView clientsListText; private TextView clientsListText;
private Thread startupCheckThread; private Thread startupCheckThread;
private ThreadWrapper wrapper; private ThreadWrapper wrapper;
private StatusIcon statusIcon; private StatusIcon trayIcon;
private bool ConnectedToVPN;
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";
@ -31,7 +33,9 @@ public partial class MainWindow : Window
SetPosition(WindowPosition.CenterAlways); SetPosition(WindowPosition.CenterAlways);
Resizable = false; Resizable = false;
TypeHint = Gdk.WindowTypeHint.Normal; TypeHint = Gdk.WindowTypeHint.Dialog;
ShowAll();
Present(); Present();
statusLabel = new Label statusLabel = new Label
@ -105,10 +109,44 @@ public partial class MainWindow : Window
}; };
startupCheckThread.Start(); startupCheckThread.Start();
statusIcon = new StatusIcon trayIcon = new StatusIcon
{ {
Visible = true Visible = true,
TooltipText = "Melon VPN"
}; };
trayIcon.Activate += OnActivateTrayIcon;
trayIcon.PopupMenu += OnTrayIconPopup;
}
private void OnActivateTrayIcon(object sender, EventArgs args)
{
Visible = !Visible;
}
private void OnTrayIconPopup(object o, PopupMenuArgs args)
{
Menu popupMenu = new Menu();
ImageMenuItem menuItemToggle = new ImageMenuItem(ConnectedToVPN ? "Disconnect" : "Connect");
Image toggleimg = new Image(ConnectedToVPN ? Stock.MediaPause : Stock.MediaPlay);
menuItemToggle.Image = toggleimg;
popupMenu.Add(menuItemToggle);
menuItemToggle.Activated += OnToggleMenuItem;
ImageMenuItem menuItemQuit = new ImageMenuItem("Quit");
Image appimg = new Image(Stock.Quit, IconSize.Menu);
menuItemQuit.Image = appimg;
popupMenu.Add(menuItemQuit);
menuItemQuit.Activated += delegate { QuitApp(); };
popupMenu.ShowAll();
popupMenu.Popup();
}
void OnToggleMenuItem(object sender, EventArgs args)
{
if (ConnectedToVPN) OnStopClicked(this, new EventArgs());
else OnStartClicked(this, new EventArgs());
} }
void UpdateIcon(string file) void UpdateIcon(string file)
@ -119,7 +157,7 @@ public partial class MainWindow : Window
if (File.Exists(IconPath)) if (File.Exists(IconPath))
{ {
Icon = new Gdk.Pixbuf(IconPath); Icon = new Gdk.Pixbuf(IconPath);
if (statusIcon != null) statusIcon.Icon = new Gdk.Pixbuf(IconPath); if (trayIcon != null) trayIcon.Icon = new Gdk.Pixbuf(IconPath);
} }
} }
@ -189,41 +227,68 @@ public partial class MainWindow : Window
void UpdateStatus(ClientResponseState s) void UpdateStatus(ClientResponseState s)
{ {
bool oldState = ConnectedToVPN;
switch (s) switch (s)
{ {
case ClientResponseState.Error: case ClientResponseState.Error:
statusLabel.Text = "Client Error"; statusLabel.Text = "Client Error";
UpdateIcon(MelonIconImg); ConnectedToVPN = false;
break; break;
case ClientResponseState.ServerError: case ClientResponseState.ServerError:
statusLabel.Text = "Server Error"; statusLabel.Text = "Server Error";
UpdateIcon(MelonIconImg); ConnectedToVPN = false;
break; break;
case ClientResponseState.Blank: case ClientResponseState.Blank:
statusLabel.Text = "No reply"; statusLabel.Text = "No reply";
UpdateIcon(MelonIconImg); ConnectedToVPN = false;
break; break;
case ClientResponseState.Online: case ClientResponseState.Online:
statusLabel.Text = "Online - " + GetVpnInternalIp(); statusLabel.Text = "Online - " + GetVpnInternalIp();
UpdateIcon(MelonOnlineImg); ConnectedToVPN = true;
break; break;
case ClientResponseState.Offline: case ClientResponseState.Offline:
statusLabel.Text = "Offline"; statusLabel.Text = "Offline";
UpdateIcon(MelonIconImg); ConnectedToVPN = false;
break; break;
} }
if (ConnectedToVPN) UpdateIcon(MelonOnlineImg);
else UpdateIcon(MelonIconImg);
NotifyStateUpdate(oldState,ConnectedToVPN);
}
void NotifyStateUpdate(bool old, bool current)
{
if(old!=current)
{
if(current==true)
{
Notification pop = new Notification("Melon VPN", "Connected to the VPN");
pop.Show();
}
else
{
Notification pop = new Notification("Melon VPN", "Disconnected from the VPN");
pop.Show();
}
}
} }
protected void OnDeleteEvent(object sender, DeleteEventArgs a) protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
if (ConnectedToVPN) Visible = false;
else QuitApp();
a.RetVal = true;
}
protected void QuitApp()
{ {
if (wrapper != null) if (wrapper != null)
{ {
wrapper.Kill(); wrapper.Kill();
wrapper.Join(); wrapper.Join();
} }
if (statusIcon != null) statusIcon.Dispose(); if (trayIcon != null) trayIcon.Dispose();
Application.Quit(); Application.Quit();
a.RetVal = true;
} }
void OnStartClicked(object sender, EventArgs e) void OnStartClicked(object sender, EventArgs e)

View File

@ -60,6 +60,9 @@
<Reference Include="Newtonsoft.Json"> <Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\net-libs\Newtonsoft.Json.dll</HintPath> <HintPath>..\..\..\net-libs\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="libnotify.net">
<HintPath>..\..\..\net-libs\libnotify.net.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="gtk-gui\gui.stetic"> <EmbeddedResource Include="gtk-gui\gui.stetic">

View File

@ -6,7 +6,7 @@ namespace MelonVPNClient
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
Application.Init(); Application.Init("Melon VPN", ref args);
MainWindow win = new MainWindow(); MainWindow win = new MainWindow();
win.Show(); win.Show();
Application.Run(); Application.Run();

View File

@ -36,6 +36,7 @@ fi
sudo chown root:root /etc/melon-vpn/client.cfg sudo chown root:root /etc/melon-vpn/client.cfg
# copy more files # copy more files
sudo cp MelonVPNClient/bin/Release/libnotify.net.dll /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/MelonVPNClient.exe /usr/lib/melon-vpn/ sudo cp MelonVPNClient/bin/Release/MelonVPNClient.exe /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/MelonVPNClient.exe.config /usr/lib/melon-vpn/ sudo cp MelonVPNClient/bin/Release/MelonVPNClient.exe.config /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/MiniMelonVPNIcon.png /usr/lib/melon-vpn/ sudo cp MelonVPNClient/bin/Release/MiniMelonVPNIcon.png /usr/lib/melon-vpn/