gustav/source/gui.d

422 lines
10 KiB
D
Raw Normal View History

2020-10-17 11:54:07 +00:00
module gui;
import core.thread;
import gtk.MainWindow;
import gtk.ListBox;
import gtk.Label;
import gtk.Notebook;
import gdk.Threads : te = threadsEnter, tl = threadsLeave;
import gtk.MenuBar;
import gtk.Box;
import gtk.Menu;
import gtk.MenuItem;
import std.stdio;
import gtk.Statusbar;
import gtk.Toolbar;
2020-10-19 14:23:00 +00:00
import gtk.ToolButton;
2020-10-19 16:01:00 +00:00
import gtk.ScrolledWindow;
2020-10-20 06:00:20 +00:00
import gtk.SeparatorToolItem;
2020-10-17 11:54:07 +00:00
import Connection;
import Channel;
2020-10-17 11:54:07 +00:00
import std.socket;
import std.conv;
2020-10-17 11:54:07 +00:00
public class GUI : Thread
{
/* Main window (GUI homepage) */
public MainWindow mainWindow;
private MenuBar menuBar;
private Toolbar toolbar;
2020-10-17 11:54:07 +00:00
public Notebook notebook;
private Statusbar statusBar;
2020-10-17 11:54:07 +00:00
private Connection[] connections;
private ListBox list;
this()
{
super(&worker);
}
private void worker()
{
initializeGUI();
te();
tl();
writeln("brg");
while(true)
{
}
}
private void initializeGUI()
{
initializeMainWindow();
/* Test adding a connection */
for(uint i = 0; i < 5; i++)
{
// connections ~= new Connection(this, parseAddress("0.0.0.0", 7777));
2020-10-17 11:54:07 +00:00
}
connections ~= new Connection(this, parseAddress("0.0.0.0", 7777), ["testGustav"~to!(string)(connections.length), "bruh"]);
2020-10-17 11:54:07 +00:00
}
/**
* Initializes the main home screen window
*/
private void initializeMainWindow()
{
/* Get GTK lock */
te();
/* Create a window */
mainWindow = new MainWindow("unamed");
/**
* Create a Box in vertical layout mode
* and adds it to the window
*
* This lays out components like so:
*
* |component 1|
* |component 2|
*/
Box box = new Box(GtkOrientation.VERTICAL, 1);
/**
* Add needed components
*
* Menubar, tabbed pane switcher, statusbar
*/
menuBar = initializeMenuBar();
box.add(menuBar);
toolbar = getToolbar();
box.add(toolbar);
2020-10-17 11:54:07 +00:00
notebook = new Notebook();
2020-10-18 20:48:21 +00:00
notebook.setScrollable(true);
2020-10-17 11:54:07 +00:00
box.add(notebook);
2020-10-19 12:57:16 +00:00
statusBar = new Statusbar();
statusBar.add(new Label("Gustav: Bruh"));
2020-10-19 12:57:16 +00:00
2020-10-17 18:43:20 +00:00
box.setChildPacking(notebook, true, true, 0, GtkPackType.START);
box.packEnd(statusBar, 0, 0, 0);
2020-10-17 11:54:07 +00:00
//notebook.add(createServerTab());
2020-10-17 18:43:20 +00:00
2020-10-17 11:54:07 +00:00
/* Add the Box to main window */
mainWindow.add(box);
mainWindow.showAll();
/* Unlock GTK lock */
tl();
writeln("unlock gui setup");
}
private Toolbar getToolbar()
{
Toolbar toolbar = new Toolbar();
/* Status selector dropdown */
import gtk.ComboBox;
import gtk.ToolButton;
// Menu menu = new Menu();
// menu.add(new MenuItem(""));
ComboBox statusBox = new ComboBox();
statusBox.setTitle("Status");
/* Set available button */
ToolButton setAvail = new ToolButton("");
setAvail.setLabel("available");
setAvail.setIconName("user-available");
toolbar.add(setAvail);
/* Set away button */
ToolButton setAway = new ToolButton("");
setAway.setLabel("away");
setAway.setIconName("user-away");
toolbar.add(setAway);
/* Set busy button */
ToolButton setBusy = new ToolButton("");
setBusy.setLabel("busy");
setBusy.setIconName("user-busy");
toolbar.add(setBusy);
/* Assign actions */
2020-10-18 19:44:50 +00:00
setAvail.addOnClicked(&setStatus);
setAway.addOnClicked(&setStatus);
setBusy.addOnClicked(&setStatus);
2020-10-19 12:57:16 +00:00
2020-10-20 06:00:20 +00:00
/* Add a seperator */
2020-10-19 12:57:16 +00:00
toolbar.add(new SeparatorToolItem());
/* List channels button */
2020-10-19 16:01:00 +00:00
ToolButton channelListButton = new ToolButton("");
channelListButton.setIconName("emblem-documents");
2020-10-19 12:57:16 +00:00
channelListButton.setTooltipText("List channels");
2020-10-19 14:23:00 +00:00
channelListButton.addOnClicked(&listChannels);
2020-10-19 12:57:16 +00:00
toolbar.add(channelListButton);
import gtk.SearchEntry;
import gtk.Entry;
Entry d = new Entry();
d.addOnActivate(&setStatusMessage);
import gtk.ToolItem;
ToolItem k = new ToolItem();
k.add(d);
toolbar.add(k);
return toolbar;
}
import gtk.Entry;
private void setStatusMessage(Entry f)
{
/* Get the current connection */
Connection currentConnection = connections[notebook.getCurrentPage()];
currentConnection.getClient().setProperty("status", f.getBuffer().getText());
//f.setInputHints(GtkInputHints.)
}
2020-10-19 14:14:28 +00:00
private void about(MenuItem)
2020-10-19 14:09:08 +00:00
{
import gtk.AboutDialog;
AboutDialog about = new AboutDialog();
about.setVersion("21893");
/* TODO: License */
/* TODO: Icon */
/* TODO: Buttons or close */
/* TODO: Set version based on compiler flag */
about.setLogoIconName("user-available");
2020-10-19 14:11:24 +00:00
about.setArtists(["i wonder if I could commision an artwork from her"]);
2020-10-19 14:09:08 +00:00
/* Set all the information */
about.setLicense("LICENSE GOES HERE");
about.setComments("A clean GTK+ graphical DNET client");
about.setWebsite("http://deavmi.assigned.network/docs/dnet/site");
about.setDocumenters(["ss","fdsfsd"]);
about.setAuthors(["Tristan B. Kildaire (Deavmi) - deavmi@disroot.org"]);
/* Show the about dialog */
about.showAll();
}
/**
* List channels
*
* Brings up a window listing channels of the current server
*/
2020-10-19 14:23:00 +00:00
private void listChannels(ToolButton)
2020-10-19 14:09:08 +00:00
{
import gtk.Window;
2020-10-19 14:23:00 +00:00
/* Create the window */
Window win = new Window(GtkWindowType.TOPLEVEL);
/* Create the list of channels */
ListBox channelsList = new ListBox();
2020-10-19 16:01:00 +00:00
win.add(new ScrolledWindow(channelsList));
2020-10-19 14:23:00 +00:00
/* Get the current connection */
Connection currentConnection = connections[notebook.getCurrentPage()];
/* Fetch the channels */
string[] channels = currentConnection.getClient().list();
/* Add each channel */
foreach(string channel; channels)
{
channelsList.add(new Label(channel));
channelsList.showAll();
}
/* TODO: Add handler for clicking label that lets you join the channel */
channelsList.addOnSelectedRowsChanged(&selectChannel);
//channelsList.add
win.showAll();
}
2020-10-20 06:00:20 +00:00
/**
* Opens a new window for connecting to a server
*/
private void connect(MenuItem)
{
import gtk.Window;
/* Create the window */
Window win = new Window(GtkWindowType.TOPLEVEL);
//import gtk.Text
win.showAll();
}
private void selectChannel(ListBox s)
{
/* Get the current connection */
Connection currentConnection = connections[notebook.getCurrentPage()];
/* Get the name of the channel selected */
string channelSelected = (cast(Label)(s.getSelectedRow().getChild())).getText();
/* Join the channel on this connection */
currentConnection.joinChannel(channelSelected);
2020-10-19 14:09:08 +00:00
}
private void setStatus(ToolButton x)
{
/* Get the current connection */
Connection currentConnection = connections[notebook.getCurrentPage()];
/* Set the status */
currentConnection.getClient().setStatus(x.getLabel()~",Hey there"); /* TODO: Remove */
currentConnection.getClient().setProperty("pres", x.getLabel());
//currentConnection.getClient().setProperty("status", "is plikking");
}
2020-10-17 11:54:07 +00:00
private MenuBar initializeMenuBar()
{
MenuBar menuBar = new MenuBar();
/* Gustav menu */
MenuItem gustavMenuItem = new MenuItem();
gustavMenuItem.setLabel("Gustav");
Menu gustavMenu = new Menu();
gustavMenuItem.setSubmenu(gustavMenu);
/* Connect option */
MenuItem connectItem = new MenuItem();
connectItem.setLabel("Connect");
2020-10-17 17:32:31 +00:00
connectItem.addOnActivate(&connectButton);
gustavMenu.add(connectItem);
2020-10-20 06:00:20 +00:00
/* Connect v2 option */
MenuItem connectItem2 = new MenuItem();
connectItem2.setLabel("Connect");
connectItem2.addOnActivate(&connect);
gustavMenu.add(connectItem2);
/* Exit option */
2020-10-17 11:54:07 +00:00
MenuItem exitItem = new MenuItem();
exitItem.setLabel("Exit");
exitItem.addOnActivate(&exitButton);
gustavMenu.add(exitItem);
2020-10-19 14:14:28 +00:00
/* Help menu */
MenuItem helpMenuItem = new MenuItem();
helpMenuItem.setLabel("Help");
Menu helpMenu = new Menu();
helpMenuItem.setSubmenu(helpMenu);
/* About option */
MenuItem aboutItem = new MenuItem();
aboutItem.setLabel("About");
aboutItem.addOnActivate(&about);
2020-10-19 14:15:01 +00:00
helpMenu.add(aboutItem);
2020-10-19 14:14:28 +00:00
2020-10-17 11:54:07 +00:00
2020-10-17 11:54:07 +00:00
/* Add all menues */
menuBar.add(gustavMenuItem);
2020-10-19 14:14:28 +00:00
menuBar.add(helpMenuItem);
2020-10-17 11:54:07 +00:00
return menuBar;
}
private void exitButton(MenuItem)
{
writeln("bruh");
/* TODO: Implement exit */
// tl();
//te();
shutdownConnections();
// mainWindow.showAll();
// tl();
}
2020-10-17 17:32:31 +00:00
private void connectButton(MenuItem)
{
connections ~= new Connection(this, parseAddress("0.0.0.0", 7777), ["testGustav"~to!(string)(connections.length), "bruh"]);
2020-10-17 17:32:31 +00:00
}
2020-10-17 11:54:07 +00:00
private void shutdownConnections()
{
foreach(Connection connection; connections)
{
/**
* TODO: This is called by signal handler, we need no mutexes for signal handler
* hence it means that connection
*/
connection.shutdown();
Thread.sleep(dur!("seconds")(2));
}
}
private void newServer()
{
}
private Box createServerTab()
{
Box serverTab = new Box(GtkOrientation.HORIZONTAL, 1);
serverTab.add(new Label("hello"));
// serverTab.add();
return serverTab;
}
}