tootle-linux-client/src/MainWindow.vala

181 lines
6.2 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
public class Tootle.MainWindow: Gtk.Window {
2018-05-18 23:14:12 +02:00
2018-06-13 15:13:41 +02:00
private Overlay overlay;
2018-05-18 23:14:12 +02:00
private Granite.Widgets.Toast toast;
2018-06-13 15:13:41 +02:00
private Grid grid;
2018-05-29 14:25:56 +02:00
private Stack primary_stack;
private Stack secondary_stack;
2018-06-13 15:13:41 +02:00
public HeaderBar header;
2018-05-29 14:25:56 +02:00
private Granite.Widgets.ModeButton button_mode;
private AccountsButton button_accounts;
private Spinner spinner;
private Button button_toot;
private Button button_back;
2018-06-13 15:13:41 +02:00
public HomeView home = new HomeView ();
public NotificationsView notifications = new NotificationsView ();
public LocalView local = new LocalView ();
public FederatedView federated = new FederatedView ();
2018-04-14 14:09:06 +02:00
construct {
var provider = new Gtk.CssProvider ();
2018-05-20 23:37:33 +02:00
provider.load_from_resource ("/com/github/bleakgrey/tootle/app.css");
2018-04-14 14:09:06 +02:00
StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
2018-05-20 23:37:33 +02:00
settings.changed.connect (update_theme);
update_theme ();
2018-04-14 14:09:06 +02:00
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:25:42 +02:00
primary_stack.hexpand = true;
primary_stack.vexpand = true;
2018-05-29 14:25:56 +02:00
spinner = new Spinner ();
spinner.active = true;
button_accounts = new AccountsButton ();
button_back = new Button ();
button_back.label = _("Back");
button_back.get_style_context ().add_class (Granite.STYLE_CLASS_BACK_BUTTON);
button_back.clicked.connect (() => back ());
button_toot = new Button ();
button_toot.tooltip_text = _("Toot");
button_toot.image = new Gtk.Image.from_icon_name ("document-edit-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
button_toot.clicked.connect (() => PostDialog.open ());
button_mode = new Granite.Widgets.ModeButton ();
button_mode.get_style_context ().add_class ("mode");
button_mode.mode_changed.connect (widget => {
secondary_stack.set_visible_child_name (widget.tooltip_text);
});
button_mode.show ();
header = new Gtk.HeaderBar ();
header.show_close_button = true;
header.custom_title = button_mode;
header.pack_start (button_back);
header.pack_start (button_toot);
header.pack_end (button_accounts);
header.pack_end (spinner);
2018-05-31 14:52:06 +02:00
header.show_all ();
2018-04-29 18:04:26 +02:00
2018-05-18 23:14:12 +02:00
grid = new Gtk.Grid ();
2018-04-29 18:25:42 +02:00
grid.attach (primary_stack, 0, 0, 1, 1);
2018-06-13 15:13:41 +02:00
add_header_view (home);
add_header_view (notifications);
add_header_view (local);
add_header_view (federated);
2018-05-29 14:25:56 +02:00
button_mode.set_active (0);
2018-05-31 14:52:06 +02:00
toast = new Granite.Widgets.Toast ("");
overlay = new Gtk.Overlay ();
overlay.add_overlay (grid);
overlay.add_overlay (toast);
overlay.set_size_request (450, 600);
add (overlay);
2018-04-14 14:09:06 +02:00
show_all ();
2018-05-29 14:25:56 +02:00
button_mode.valign = Gtk.Align.FILL;
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
);
window_position = WindowPosition.CENTER;
update_header ();
2018-05-31 14:52:06 +02:00
set_titlebar (header);
2018-04-14 14:09:06 +02:00
2018-05-29 14:25:56 +02:00
app.toast.connect (on_toast);
network.started.connect (() => spinner.show ());
network.finished.connect (() => spinner.hide ());
2018-05-31 14:13:21 +02:00
accounts.updated (accounts.saved_accounts);
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 ();
2018-05-29 14:25:56 +02:00
button_mode.append (img);
2018-05-05 17:38:01 +02:00
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-29 12:05:04 +02:00
public int get_visible_id () {
return int.parse (primary_stack.get_visible_child_name ());
}
public void open_view (AbstractView widget) {
var i = get_visible_id ();
2018-04-25 15:16:57 +02:00
i++;
2018-05-29 12:05:04 +02:00
widget.stack_pos = i;
widget.show ();
2018-04-25 15:16:57 +02:00
primary_stack.add_named (widget, i.to_string ());
primary_stack.set_visible_child_name (i.to_string ());
2018-05-29 14:25:56 +02:00
update_header ();
2018-04-19 21:03:28 +02:00
}
2018-04-29 18:04:26 +02:00
2018-05-29 12:05:04 +02:00
public void back () {
var i = get_visible_id ();
var child = primary_stack.get_child_by_name (i.to_string ());
2018-05-29 14:25:56 +02:00
primary_stack.set_visible_child_name ((i-1).to_string ());
2018-05-29 12:05:04 +02:00
child.destroy ();
2018-05-29 14:25:56 +02:00
update_header ();
2018-05-29 12:05:04 +02:00
}
public void reopen_view (int view_id) {
var i = get_visible_id ();
while (i != view_id && view_id != 0) {
back ();
i = get_visible_id ();
}
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 ();
}
2018-05-03 10:56:04 +02:00
public override bool delete_event (Gdk.EventAny event) {
2018-05-08 18:09:38 +02:00
this.destroy.connect (() => {
2018-05-27 18:25:16 +02:00
if (!Tootle.settings.always_online || Tootle.accounts.is_empty ())
2018-05-08 18:09:38 +02:00
Tootle.app.remove_window (Tootle.window_dummy);
Tootle.window = null;
});
return false;
2018-05-03 10:56:04 +02:00
}
2018-05-20 23:37:33 +02:00
private void update_theme () {
var provider = new Gtk.CssProvider ();
var is_dark = settings.dark_theme;
var theme = is_dark ? "dark" : "light";
provider.load_from_resource ("/com/github/bleakgrey/tootle/%s.css".printf (theme));
StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
Gtk.Settings.get_default ().gtk_application_prefer_dark_theme = is_dark;
}
2018-05-29 14:25:56 +02:00
private void update_header () {
bool primary_mode = get_visible_id () == 0;
button_mode.set_visible (primary_mode);
button_toot.set_visible (primary_mode);
button_back.set_visible (!primary_mode);
button_accounts.set_visible (true);
}
2018-04-14 14:09:06 +02:00
}