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

212 lines
7.3 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
2019-03-11 15:14:37 +01:00
public class Tootle.Dialogs.MainWindow: Gtk.Window, ISavedWindow {
2019-03-07 17:16:52 +01: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;
2019-03-07 17:16:52 +01:00
2018-06-13 15:13:41 +02:00
public HeaderBar header;
2018-10-30 17:24:29 +01:00
public Granite.Widgets.ModeButton button_mode;
2019-03-11 15:14:37 +01:00
private Widgets.AccountsButton button_accounts;
2018-05-29 14:25:56 +02:00
private Spinner spinner;
private Button button_toot;
private Button button_back;
2019-03-07 17:16:52 +01:00
2019-03-11 15:14:37 +01:00
public Views.Home home = new Views.Home ();
public Views.Notifications notifications = new Views.Notifications ();
public Views.Local local = new Views.Local ();
public Views.Federated federated = new Views.Federated ();
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);
2019-03-07 17:16:52 +01:00
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");
2019-03-11 13:28:51 +01:00
primary_stack.hexpand = primary_stack.vexpand = true;
2019-03-07 17:16:52 +01:00
2018-05-29 14:25:56 +02:00
spinner = new Spinner ();
spinner.active = true;
2019-03-11 15:14:37 +01:00
button_accounts = new Widgets.AccountsButton ();
2019-03-07 17:16:52 +01:00
2018-05-29 14:25:56 +02:00
button_back = new Button ();
2018-10-28 13:57:03 +01:00
button_back.valign = Align.CENTER;
2018-05-29 14:25:56 +02:00
button_back.label = _("Back");
button_back.get_style_context ().add_class (Granite.STYLE_CLASS_BACK_BUTTON);
button_back.clicked.connect (() => back ());
2019-03-09 12:42:27 +01:00
Desktop.set_hotkey_tooltip (button_back, null, app.ACCEL_BACK);
2019-03-07 17:16:52 +01:00
2018-05-29 14:25:56 +02:00
button_toot = new Button ();
2018-10-28 13:57:03 +01:00
button_toot.valign = Align.CENTER;
2018-10-28 13:54:09 +01:00
button_toot.image = new Image.from_icon_name ("document-edit-symbolic", IconSize.LARGE_TOOLBAR);
2019-03-11 15:14:37 +01:00
button_toot.clicked.connect (() => Dialogs.Compose.open ());
2019-03-09 12:42:27 +01:00
Desktop.set_hotkey_tooltip (button_toot, _("Toot"), app.ACCEL_NEW_POST);
2018-05-29 14:25:56 +02:00
button_mode = new Granite.Widgets.ModeButton ();
button_mode.get_style_context ().add_class ("mode");
2018-10-23 12:05:24 +02:00
button_mode.vexpand = true;
2018-10-28 13:54:09 +01:00
button_mode.valign = Align.FILL;
button_mode.mode_changed.connect (on_mode_changed);
2018-05-29 14:25:56 +02:00
button_mode.show ();
2019-03-07 17:16:52 +01:00
2018-10-28 13:54:09 +01:00
header = new HeaderBar ();
2018-10-24 10:55:36 +02:00
header.get_style_context ().add_class ("compact");
2018-05-29 14:25:56 +02:00
header.show_close_button = true;
2018-10-28 13:54:09 +01:00
header.title = _("Tootle");
2018-05-29 14:25:56 +02:00
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 ();
2019-03-07 17:16:52 +01:00
2018-10-28 13:54:09 +01:00
grid = new Grid ();
2018-04-29 18:25:42 +02:00
grid.attach (primary_stack, 0, 0, 1, 1);
2019-03-07 17:16:52 +01:00
2019-03-09 12:42:27 +01:00
add_header_view (home, app.ACCEL_TIMELINE_0, 0);
add_header_view (notifications, app.ACCEL_TIMELINE_1, 1);
add_header_view (local, app.ACCEL_TIMELINE_2, 2);
add_header_view (federated, app.ACCEL_TIMELINE_3, 3);
2018-05-29 14:25:56 +02:00
button_mode.set_active (0);
2019-03-07 17:16:52 +01:00
2018-05-31 14:52:06 +02:00
toast = new Granite.Widgets.Toast ("");
2018-10-28 13:54:09 +01:00
overlay = new Overlay ();
2018-05-31 14:52:06 +02:00
overlay.add_overlay (grid);
overlay.add_overlay (toast);
overlay.set_size_request (450, 600);
add (overlay);
2019-03-11 13:28:51 +01:00
2019-03-09 19:20:11 +01:00
restore_state ();
2018-04-14 14:09:06 +02:00
show_all ();
2018-04-20 13:51:18 +02:00
}
2019-03-07 17:16:52 +01:00
2018-10-23 12:05:24 +02:00
public MainWindow (Gtk.Application _app) {
application = _app;
icon_name = "com.github.bleakgrey.tootle";
resizable = true;
2018-04-20 13:51:18 +02:00
window_position = WindowPosition.CENTER;
2018-10-24 10:55:36 +02:00
set_titlebar (header);
update_header ();
2019-03-07 17:16:52 +01: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);
2019-03-11 13:28:51 +01:00
button_press_event.connect (on_button_press);
}
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
2019-03-11 15:14:37 +01:00
private void add_header_view (Views.Abstract 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);
2018-05-29 14:25:56 +02:00
button_mode.append (img);
2018-05-05 17:38:01 +02:00
view.image = img;
2019-03-09 12:42:27 +01:00
secondary_stack.add_named (view, num.to_string ());
2019-03-07 17:16:52 +01:00
2019-03-11 15:14:37 +01:00
if (view is Views.Notifications)
2018-05-05 17:38:01 +02:00
img.pixel_size = 20; // For some reason Notifications icon is too small without this
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 () {
return int.parse (primary_stack.get_visible_child_name ());
}
2019-03-07 17:16:52 +01:00
2019-03-11 15:14:37 +01:00
public bool open_view (Views.Abstract 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 ();
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 ();
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
2018-05-29 12:05:04 +02:00
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 ();
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
2018-05-03 10:56:04 +02:00
public override bool delete_event (Gdk.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) {
button_mode.set_active (timeline_no);
}
2019-03-07 17:16:52 +01: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;
}
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;
2018-10-23 12:05:24 +02:00
button_mode.sensitive = primary_mode;
button_mode.opacity = primary_mode ? 1 : 0; //Prevent HeaderBar height jitter
2018-05-29 14:25:56 +02:00
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
2018-10-28 13:54:09 +01:00
private void on_toast (string msg){
toast.title = msg;
toast.send_notification ();
}
2018-10-28 13:54:09 +01:00
private void on_mode_changed (Widget widget) {
2019-03-11 15:14:37 +01:00
var visible = secondary_stack.get_visible_child () as Views.Abstract;
2018-10-28 13:54:09 +01:00
visible.current = false;
2019-03-07 17:16:52 +01:00
2019-03-09 12:42:27 +01:00
secondary_stack.set_visible_child_name (button_mode.selected.to_string ());
2019-03-07 17:16:52 +01:00
2019-03-11 15:14:37 +01:00
visible = secondary_stack.get_visible_child () as Views.Abstract;
2018-10-28 13:54:09 +01:00
visible.current = true;
visible.on_set_current ();
}
2018-04-14 14:09:06 +02:00
}