Use libappindicator for tray icon

This commit is contained in:
Melon 2020-10-06 10:54:25 +01:00
parent 2573e3211a
commit 7c0b73ea8c
4 changed files with 266 additions and 210 deletions

View File

@ -3,7 +3,9 @@ using System.IO;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Threading;
using AppIndicator3;
using Gtk;
using MelonVPNClient;
using MelonVPNCore;
using Notify;
@ -17,7 +19,8 @@ public partial class MainWindow : Window
private TextView clientsListText;
private Thread startupCheckThread;
private ThreadWrapper wrapper;
private StatusIcon trayIcon;
private Indicator trayIcon;
private PopupMenu trayMenu;
private bool ConnectedToVPN;
public static readonly string MelonIconImg = "MiniMelonVPNIcon.png";
public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png";
@ -109,46 +112,31 @@ public partial class MainWindow : Window
};
startupCheckThread.Start();
trayIcon = new StatusIcon
trayMenu = new PopupMenu(OnActivateTrayIcon, OnToggleMenuItem, QuitApp);
trayIcon = new Indicator("melonvpn", "MiniMelonVPNIcon", (int)IndicatorCategory.ApplicationStatus)
{
Visible = true,
TooltipText = "Melon VPN"
Menu = trayMenu.GetMenu(),
Status = (int)IndicatorStatus.Active
};
trayIcon.Activate += OnActivateTrayIcon;
trayIcon.PopupMenu += OnTrayIconPopup;
}
private void OnActivateTrayIcon(object sender, EventArgs args)
private void OnActivateTrayIcon()
{
Visible = !Visible;
UpdateTrayMenu();
}
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)
void OnToggleMenuItem()
{
if (ConnectedToVPN) OnStopClicked(this, new EventArgs());
else OnStartClicked(this, new EventArgs());
}
void UpdateTrayMenu()
{
if (trayMenu != null) trayMenu.Update(ConnectedToVPN, Visible);
}
void UpdateIcon(string file)
{
DirectoryInfo ExeLocation = Directory.GetParent(Assembly.GetEntryAssembly().Location);
@ -157,7 +145,7 @@ public partial class MainWindow : Window
if (File.Exists(IconPath))
{
Icon = new Gdk.Pixbuf(IconPath);
if (trayIcon != null) trayIcon.Icon = new Gdk.Pixbuf(IconPath);
if (trayIcon != null) trayIcon.Icon = file.Replace(".png", "");
}
}
@ -253,6 +241,7 @@ public partial class MainWindow : Window
}
if (ConnectedToVPN) UpdateIcon(MelonOnlineImg);
else UpdateIcon(MelonIconImg);
UpdateTrayMenu();
NotifyStateUpdate(oldState, ConnectedToVPN);
}
@ -264,18 +253,37 @@ public partial class MainWindow : Window
{
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);
}
}
}
void CloseNotificationAfterWait(Notification pop, int timeout)
{
Thread wait = new Thread(() =>
{
Thread.Sleep(timeout);
pop.Close();
})
{
IsBackground = true
};
wait.Start();
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
if (ConnectedToVPN) Visible = false;
if (ConnectedToVPN)
{
Visible = false;
UpdateTrayMenu();
}
else QuitApp();
a.RetVal = true;
}

View File

@ -63,6 +63,9 @@
<Reference Include="libnotify.net">
<HintPath>..\..\..\net-libs\libnotify.net.dll</HintPath>
</Reference>
<Reference Include="appindicator3-sharp">
<HintPath>..\..\..\..\..\lib\mono\gac\appindicator3-sharp\12.10.0.0__bf81553d61d0bd64\appindicator3-sharp.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="gtk-gui\gui.stetic">
@ -75,6 +78,7 @@
<Compile Include="gtk-gui\MainWindow.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PopupMenu.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MelonVPNCore\MelonVPNCore.csproj">

View File

@ -0,0 +1,39 @@
using Gtk;
namespace MelonVPNClient
{
public class PopupMenu
{
private Menu me;
private MenuItem openItem;
private MenuItem toggleItem;
private MenuItem quitItem;
public PopupMenu(System.Action open, System.Action toggle, System.Action quit)
{
me = new Menu();
openItem = new MenuItem("...");
me.Add(openItem);
openItem.Activated += delegate { open(); };
toggleItem = new MenuItem("...");
me.Add(toggleItem);
toggleItem.Activated += delegate { toggle(); };
quitItem = new MenuItem("Quit");
me.Add(quitItem);
quitItem.Activated += delegate { quit(); };
me.ShowAll();
}
public void Update(bool ConnectedToVPN, bool Visible)
{
openItem.Label = Visible ? "Hide" : "Show";
toggleItem.Label = ConnectedToVPN ? "Disconnect" : "Connect";
}
public Menu GetMenu() => me;
}
}

View File

@ -43,6 +43,11 @@ sudo cp MelonVPNClient/bin/Release/MiniMelonVPNIcon.png /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/MiniMelonVPNOnline.png /usr/lib/melon-vpn/
sudo cp MelonVPNClient/bin/Release/MelonVPNDesktopIcon.png /usr/lib/melon-vpn/
# copy app indicator icons
mkdir -p ~/.local/share/icons/hicolor/128x128/apps/
cp MelonVPNClient/bin/Release/MiniMelonVPNIcon.png ~/.local/share/icons/hicolor/128x128/apps/
cp MelonVPNClient/bin/Release/MiniMelonVPNOnline.png ~/.local/share/icons/hicolor/128x128/apps/
# make all exe files in melon vpn projects executable
sudo chmod +x /usr/lib/melon-vpn/*.exe