tootle-linux-client/src/Dialogs/MainWindow.vala

141 lines
4.5 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
2019-03-11 13:28:51 +01:00
using Gdk;
2018-04-14 14:09:06 +02:00
2020-05-29 14:19:35 +02:00
[GtkTemplate (ui = "/com/github/bleakgrey/tootle/ui/dialogs/main.ui")]
2019-03-11 15:14:37 +01:00
public class Tootle.Dialogs.MainWindow: Gtk.Window, ISavedWindow {
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
[GtkChild]
protected Stack view_stack;
[GtkChild]
protected Stack timeline_stack;
[GtkChild]
protected HeaderBar header;
[GtkChild]
protected Button back_button;
[GtkChild]
protected Button compose_button;
[GtkChild]
protected Granite.Widgets.ModeButton timeline_switcher;
[GtkChild]
protected Widgets.AccountsButton accounts_button;
2018-04-14 14:09:06 +02:00
construct {
var provider = new Gtk.CssProvider ();
2020-05-29 14:19:35 +02:00
provider.load_from_resource (@"$(Build.RESOURCES)app.css");
StyleContext.add_provider_for_screen (Screen.get_default (), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
back_button.clicked.connect (() => back ());
Desktop.set_hotkey_tooltip (back_button, _("Back"), app.ACCEL_BACK);
2018-04-14 14:09:06 +02:00
2020-05-29 14:19:35 +02:00
compose_button.clicked.connect (() => new Dialogs.Compose ());
Desktop.set_hotkey_tooltip (compose_button, _("Compose"), app.ACCEL_NEW_POST);
2019-03-11 13:28:51 +01:00
2020-05-29 14:19:35 +02:00
timeline_switcher.mode_changed.connect (on_mode_changed);
2019-03-12 09:12:53 +01:00
2020-05-29 14:19:35 +02:00
add_header_view (new Views.Home (), app.ACCEL_TIMELINE_0, 0);
add_header_view (new Views.Notifications (), app.ACCEL_TIMELINE_1, 1);
add_header_view (new Views.Local (), app.ACCEL_TIMELINE_2, 2);
add_header_view (new Views.Federated (), app.ACCEL_TIMELINE_3, 3);
timeline_switcher.set_active (0);
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
button_press_event.connect (on_button_press);
settings.changed.connect (update_theme);
update_theme ();
update_header ();
2020-05-29 14:19:35 +02:00
restore_state ();
}
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
public MainWindow (Gtk.Application app) {
Object (application: app, icon_name: Build.DOMAIN, resizable: true, window_position: WindowPosition.CENTER);
if (accounts.is_empty ())
open_view (new Views.NewAccount (false));
2019-03-11 13:28:51 +01:00
}
private bool on_button_press (EventButton ev) {
if (ev.button == 8)
return back ();
return false;
2018-04-14 14:09:06 +02:00
}
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
private void add_header_view (Views.Base view, string[] accelerators, int32 num) {
2018-10-28 13:54:09 +01:00
var img = new Image.from_icon_name (view.get_icon (), IconSize.LARGE_TOOLBAR);
2019-03-09 12:42:27 +01:00
Desktop.set_hotkey_tooltip (img, view.get_name (), accelerators);
2020-05-29 14:19:35 +02:00
timeline_switcher.append (img);
2018-05-05 17:38:01 +02:00
view.image = img;
2019-03-11 21:01:14 +01:00
timeline_stack.add_named (view, num.to_string ());
2018-04-14 14:09:06 +02:00
}
2019-03-07 17:16:52 +01:00
2018-05-29 12:05:04 +02:00
public int get_visible_id () {
2019-03-11 21:01:14 +01:00
return int.parse (view_stack.get_visible_child_name ());
2018-05-29 12:05:04 +02:00
}
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
public bool open_view (Views.Base widget) {
2018-05-29 12:05:04 +02:00
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 ();
2019-03-11 21:01:14 +01:00
view_stack.add_named (widget, i.to_string ());
view_stack.set_visible_child_name (i.to_string ());
2018-05-29 14:25:56 +02:00
update_header ();
2019-03-11 13:28:51 +01:00
return true;
2018-04-19 21:03:28 +02:00
}
2019-03-07 17:16:52 +01:00
2019-03-11 13:28:51 +01:00
public bool back () {
2018-05-29 12:05:04 +02:00
var i = get_visible_id ();
2018-10-23 12:22:43 +02:00
if (i == 0)
2019-03-11 13:28:51 +01:00
return false;
2019-03-07 17:16:52 +01:00
2019-03-11 21:01:14 +01:00
var child = view_stack.get_child_by_name (i.to_string ());
view_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 ();
2019-03-11 13:28:51 +01:00
return true;
2018-05-29 12:05:04 +02:00
}
2019-03-07 17:16:52 +01:00
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
}
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
public override bool delete_event (EventAny event) {
2019-03-11 13:28:51 +01:00
destroy.connect (() => {
2018-10-23 12:05:24 +02:00
if (!settings.always_online || accounts.is_empty ())
app.remove_window (window_dummy);
window = null;
2018-05-08 18:09:38 +02:00
});
return false;
2018-05-03 10:56:04 +02:00
}
2019-03-07 17:16:52 +01:00
2018-10-28 13:54:09 +01:00
public void switch_timeline (int32 timeline_no) {
2020-05-29 14:19:35 +02:00
timeline_switcher.set_active (timeline_no);
2018-10-28 13:54:09 +01:00
}
2019-03-07 17:16:52 +01:00
2018-05-20 23:37:33 +02:00
private void update_theme () {
2020-05-29 14:19:35 +02:00
Gtk.Settings.get_default ().gtk_application_prefer_dark_theme = settings.dark_theme;
2018-05-20 23:37:33 +02:00
}
2019-03-07 17:16:52 +01:00
2018-05-29 14:25:56 +02:00
private void update_header () {
bool primary_mode = get_visible_id () == 0;
2020-05-29 14:19:35 +02:00
timeline_switcher.sensitive = primary_mode;
timeline_switcher.opacity = primary_mode ? 1 : 0; //Prevent HeaderBar height jitter
compose_button.visible = primary_mode;
back_button.visible = !primary_mode;
}
2018-10-28 13:54:09 +01:00
private void on_mode_changed (Widget widget) {
2020-05-29 14:19:35 +02:00
var visible = timeline_stack.get_visible_child () as Views.Base;
2018-10-28 13:54:09 +01:00
visible.current = false;
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
timeline_stack.set_visible_child_name (timeline_switcher.selected.to_string ());
2019-03-07 17:16:52 +01:00
2020-05-29 14:19:35 +02:00
visible = timeline_stack.get_visible_child () as Views.Base;
2018-10-28 13:54:09 +01:00
visible.current = true;
visible.on_set_current ();
}
2018-04-14 14:09:06 +02:00
}