GUI code LOL

This commit is contained in:
Melon 2020-09-23 20:22:51 +01:00
parent cc507148fd
commit 3f57dc43c5

View File

@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Threading;
using Gtk;
using MelonVPNCore;
@ -10,18 +12,23 @@ public partial class MainWindow : Window
private Button startBtn;
private Button stopBtn;
private Button refreshBtn;
private Button viewClientsBtn;
private ScrolledWindow clientsListScroller;
private TextView clientsListText;
private Thread startupCheckThread;
private ThreadWrapper wrapper;
private StatusIcon statusIcon;
public static readonly string MelonIconImg = "MiniMelonVPNIcon.png";
public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png";
public MainWindow() : base(WindowType.Toplevel)
{
Build();
Title = "Melon VPN";
UpdateIcon(MelonIconImg);
SetSizeRequest(300, 300);
SetDefaultSize(300, 300);
SetPosition(WindowPosition.Center);
SetPosition(WindowPosition.CenterAlways);
Resizable = false;
TypeHint = Gdk.WindowTypeHint.Normal;
@ -61,14 +68,19 @@ public partial class MainWindow : Window
refreshBtn.Clicked += OnRefreshClicked;
fixed1.Put(refreshBtn, 190, 40);
viewClientsBtn = new Button
clientsListText = new TextView
{
Visible = true,
Label = "View Clients"
Editable = false
};
viewClientsBtn.SetSizeRequest(100, 30);
viewClientsBtn.Clicked += OnViewClientsClicked;
fixed1.Put(viewClientsBtn, 10, 80);
clientsListScroller = new ScrolledWindow
{
Visible = true
};
clientsListScroller.SetSizeRequest(280, 200);
clientsListScroller.Add(clientsListText);
fixed1.Put(clientsListScroller, 10, 80);
GUISocketServer.Receive += delegate (object sender, ClientResponseState s)
{
@ -81,10 +93,9 @@ public partial class MainWindow : Window
GUISocketServer.ClientListUpdate += delegate (object sender, ConnectedClient[] clients)
{
Console.WriteLine(clients.Length);
foreach (ConnectedClient c in clients)
{
Console.WriteLine(c.name + "--" + c.ip);
}
string[][] a = new string[clients.Length][];
for (int i = 0; i < clients.Length; i++) a[i] = new string[] { clients[i].name, clients[i].ip };
clientsListText.Buffer.Text = GenerateTable(a);
};
wrapper = new ThreadWrapper(GUISocketServer.StartServer);
@ -93,6 +104,36 @@ public partial class MainWindow : Window
IsBackground = true
};
startupCheckThread.Start();
statusIcon = new StatusIcon
{
Visible = true
};
}
void UpdateIcon(string file)
{
DirectoryInfo ExeLocation = Directory.GetParent(Assembly.GetEntryAssembly().Location);
string IconPath = System.IO.Path.Combine(ExeLocation.FullName, file);
Console.WriteLine("Trying to update icon to: " + IconPath);
if (File.Exists(IconPath))
{
Icon = new Gdk.Pixbuf(IconPath);
if (statusIcon != null) statusIcon.Icon = new Gdk.Pixbuf(IconPath);
}
}
string GenerateTable(string[][] a)
{
if (a.Length == 0) return "";
int maxlength = a[0][0].Length;
foreach (string b in a[0])
{
if (b.Length > maxlength) maxlength = b.Length;
}
string[] c = new string[a.Length];
for (int i = 0; i < a.Length; i++) c[i] = a[i][0].PadRight(maxlength, ' ') + a[i][1];
return string.Join("\n", c);
}
string GetVpnInternalIp()
@ -152,21 +193,23 @@ public partial class MainWindow : Window
{
case ClientResponseState.Error:
statusLabel.Text = "Client Error";
UpdateIcon(MelonIconImg);
break;
case ClientResponseState.ServerError:
statusLabel.Text = "Server Error";
UpdateIcon(MelonIconImg);
break;
case ClientResponseState.Blank:
statusLabel.Text = "No reply";
UpdateIcon(MelonIconImg);
break;
case ClientResponseState.Online:
statusLabel.Text = "Online - " + GetVpnInternalIp();
UpdateIcon(MelonOnlineImg);
break;
case ClientResponseState.Offline:
statusLabel.Text = "Offline";
break;
default:
statusLabel.Text = "Unknown";
UpdateIcon(MelonIconImg);
break;
}
}
@ -178,6 +221,7 @@ public partial class MainWindow : Window
wrapper.Kill();
wrapper.Join();
}
if (statusIcon != null) statusIcon.Dispose();
Application.Quit();
a.RetVal = true;
}