tootle-linux-client/src/MainWindow.vala

122 lines
4.0 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
public class Tootle.MainWindow: Gtk.Window {
2018-04-29 18:04:26 +02:00
Gtk.Overlay overlay;
Granite.Widgets.Toast toast;
2018-04-30 19:56:02 +02:00
public Tootle.HeaderBar header;
2018-04-20 13:51:18 +02:00
public Stack primary_stack;
public Stack secondary_stack;
2018-04-14 14:09:06 +02:00
construct {
var provider = new Gtk.CssProvider ();
provider.load_from_resource ("/com/github/bleakgrey/tootle/Application.css");
StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
2018-04-29 18:04:26 +02:00
toast = new Granite.Widgets.Toast ("");
2018-04-29 18:25:42 +02:00
overlay = new Gtk.Overlay ();
overlay.add_overlay (toast);
2018-04-19 20:38:30 +02:00
secondary_stack = new Stack();
secondary_stack.transition_type = Gtk.StackTransitionType.SLIDE_LEFT_RIGHT;
secondary_stack.show ();
primary_stack = new Stack();
primary_stack.transition_type = Gtk.StackTransitionType.SLIDE_LEFT_RIGHT;
primary_stack.show ();
2018-04-25 15:16:57 +02:00
primary_stack.add_named (secondary_stack, "0");
2018-04-29 18:04:26 +02:00
primary_stack.set_size_request (400, 500);
2018-04-29 18:25:42 +02:00
primary_stack.hexpand = true;
primary_stack.vexpand = true;
2018-04-20 13:51:18 +02:00
header = new Tootle.HeaderBar ();
2018-04-29 18:04:26 +02:00
2018-04-29 18:25:42 +02:00
var grid = new Gtk.Grid ();
grid.set_size_request (400, 500);
grid.attach (primary_stack, 0, 0, 1, 1);
grid.attach (overlay, 0, 0, 1, 1);
add (grid);
2018-04-14 14:09:06 +02:00
show_all ();
2018-04-20 13:51:18 +02:00
}
public MainWindow (Gtk.Application application) {
Object (application: application,
icon_name: "com.github.bleakgrey.tootle",
title: "Tootle",
resizable: true
);
set_titlebar (header);
window_position = WindowPosition.CENTER;
2018-04-14 14:09:06 +02:00
2018-04-27 20:50:08 +02:00
Tootle.accounts.switched.connect(on_account_switched);
2018-04-29 18:04:26 +02:00
Tootle.app.error.connect (on_error);
Tootle.app.toast.connect (on_toast);
2018-05-03 11:09:55 +02:00
Tootle.accounts.init ();
2018-04-14 14:09:06 +02:00
}
2018-05-05 17:38:01 +02:00
private void reset () {
header.button_mode.clear_children ();
secondary_stack.forall (widget => widget.destroy ());
}
2018-04-14 14:09:06 +02:00
2018-05-05 17:38:01 +02:00
private void on_account_switched(Account? account = Tootle.accounts.current){
reset ();
2018-04-14 19:53:09 +02:00
if(account == null)
2018-05-05 17:38:01 +02:00
build_setup_view ();
2018-04-14 14:09:06 +02:00
else
2018-05-05 17:38:01 +02:00
build_main_view ();
2018-04-14 14:09:06 +02:00
}
2018-05-05 17:38:01 +02:00
private void build_setup_view (){
2018-04-14 14:09:06 +02:00
var add_account = new AddAccountView ();
2018-04-19 20:38:30 +02:00
secondary_stack.add_named (add_account, add_account.get_name ());
2018-04-20 13:51:18 +02:00
header.update (false, true);
2018-04-14 14:09:06 +02:00
}
2018-05-05 17:38:01 +02:00
private void build_main_view (){
add_header_view (new HomeView ());
add_header_view (new NotificationsView ());
add_header_view (new LocalView ());
add_header_view (new FederatedView ());
2018-04-20 13:51:18 +02:00
header.update (true);
2018-04-14 14:09:06 +02:00
}
2018-05-05 17:38:01 +02:00
private void add_header_view (AbstractView view) {
var img = new Gtk.Image.from_icon_name(view.get_icon (), Gtk.IconSize.LARGE_TOOLBAR);
img.tooltip_text = view.get_name ();
header.button_mode.append (img);
view.image = img;
secondary_stack.add_named(view, view.get_name ());
if (view is NotificationsView)
img.pixel_size = 20; // For some reason Notifications icon is too small without this
2018-04-14 14:09:06 +02:00
}
2018-04-19 21:03:28 +02:00
2018-05-05 17:38:01 +02:00
public void open_view (Widget widget) {
2018-04-19 21:03:28 +02:00
widget.show ();
2018-04-25 15:16:57 +02:00
var i = int.parse (primary_stack.get_visible_child_name ());
i++;
primary_stack.add_named (widget, i.to_string ());
primary_stack.set_visible_child_name (i.to_string ());
2018-04-20 13:51:18 +02:00
header.update (false);
2018-04-19 21:03:28 +02:00
}
2018-04-29 18:04:26 +02:00
private void on_toast (string msg){
toast.title = msg;
toast.send_notification ();
}
private void on_error (string title, string msg){
var message_dialog = new Granite.MessageDialog.with_image_from_icon_name (title, msg, "dialog-warning");
message_dialog.transient_for = this;
message_dialog.run ();
message_dialog.destroy ();
}
2018-05-03 10:56:04 +02:00
public override bool delete_event (Gdk.EventAny event) {
var do_not_exit = Tootle.network.is_active_in_background ();
if (do_not_exit)
hide ();
return do_not_exit;
}
2018-04-14 14:09:06 +02:00
}