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

185 lines
5.7 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-06-01 02:02:17 +02:00
public const string ZOOM_CLASS = "app-scalable";
2020-05-29 14:19:35 +02:00
[GtkChild]
protected Stack view_stack;
[GtkChild]
protected Stack timeline_stack;
[GtkChild]
2020-06-29 23:43:45 +02:00
public HeaderBar header;
[GtkChild]
protected Revealer view_navigation;
2020-05-29 14:19:35 +02:00
[GtkChild]
protected Button back_button;
[GtkChild]
protected Button compose_button;
[GtkChild]
protected Hdy.ViewSwitcherTitle timeline_switcher;
2020-05-29 14:19:35 +02:00
[GtkChild]
protected Hdy.ViewSwitcherBar switcher_navbar;
[GtkChild]
2020-05-29 14:19:35 +02:00
protected Widgets.AccountsButton accounts_button;
2020-05-31 20:55:40 +02:00
2020-05-31 12:28:35 +02:00
Views.Base? last_view = null;
2018-04-14 14:09:06 +02:00
2020-06-01 02:02:17 +02:00
CssProvider zoom_css_provider = new CssProvider ();
2019-03-07 17:16:52 +01:00
2020-06-01 02:02:17 +02:00
construct {
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-31 12:28:35 +02:00
timeline_switcher.stack = timeline_stack;
timeline_switcher.valign = Align.FILL;
timeline_stack.notify["visible-child"].connect (on_timeline_changed);
2019-03-12 09:12:53 +01:00
2020-05-31 12:28:35 +02:00
add_timeline_view (new Views.Home (), app.ACCEL_TIMELINE_0, 0);
add_timeline_view (new Views.Notifications (), app.ACCEL_TIMELINE_1, 1);
add_timeline_view (new Views.Local (), app.ACCEL_TIMELINE_2, 2);
add_timeline_view (new Views.Federated (), app.ACCEL_TIMELINE_3, 3);
2019-03-07 17:16:52 +01:00
2020-05-31 21:56:03 +02:00
settings.bind_property ("dark-theme", Gtk.Settings.get_default (), "gtk-application-prefer-dark-theme", BindingFlags.SYNC_CREATE);
2020-06-01 02:02:17 +02:00
settings.notify["post-text-size"].connect (() => on_zoom_level_changed ());
var provider = new Gtk.CssProvider ();
provider.load_from_resource (@"$(Build.RESOURCES)app.css");
StyleContext.add_provider_for_screen (Screen.get_default (), provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
StyleContext.add_provider_for_screen (Screen.get_default (), zoom_css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
on_zoom_level_changed ();
2020-05-31 21:56:03 +02:00
2020-05-29 14:19:35 +02:00
button_press_event.connect (on_button_press);
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) {
2020-05-31 12:28:35 +02:00
Object (
application: app,
icon_name: Build.DOMAIN,
resizable: true,
window_position: WindowPosition.CENTER
);
2020-05-29 14:19:35 +02:00
if (accounts.is_empty ())
open_view (new Views.NewAccount (false));
2019-03-11 13:28:51 +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) {
2020-06-29 23:43:45 +02:00
var curr = view_stack.visible_child as Views.Base;
if (curr != null)
curr.current = false;
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 ();
2020-06-29 23:43:45 +02:00
widget.current = true;
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 ());
2020-06-29 23:43:45 +02:00
(child as Views.Base).current = false;
2018-05-29 12:05:04 +02:00
child.destroy ();
2018-05-29 14:25:56 +02:00
update_header ();
2020-06-29 23:43:45 +02:00
var curr = view_stack.visible_child as Views.Base;
if (curr != null)
curr.current = true;
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 (() => {
2020-05-31 21:56:03 +02:00
if (!settings.work_in_background || accounts.is_empty ())
2018-10-23 12:05:24 +02:00
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
2020-05-31 12:28:35 +02:00
public void switch_timeline (int32 num) {
timeline_stack.visible_child_name = num.to_string ();
2018-10-28 13:54:09 +01:00
}
2019-03-07 17:16:52 +01:00
2020-05-31 12:28:35 +02:00
bool on_button_press (EventButton ev) {
if (ev.button == 8)
return back ();
return false;
}
void add_timeline_view (Views.Base view, string[] accelerators, int32 num) {
timeline_stack.add_titled (view, num.to_string (), view.label);
timeline_stack.child_set_property (view, "icon-name", view.icon);
view.notify["needs-attention"].connect (() => {
timeline_stack.child_set_property (view, "needs-attention", view.needs_attention);
});
}
void update_header () {
2018-05-29 14:25:56 +02:00
bool primary_mode = get_visible_id () == 0;
switcher_navbar.visible = timeline_switcher.sensitive = primary_mode;
2020-05-29 14:19:35 +02:00
timeline_switcher.opacity = primary_mode ? 1 : 0; //Prevent HeaderBar height jitter
2020-06-29 23:43:45 +02:00
view_navigation.reveal_child = !primary_mode;
if (primary_mode)
header.custom_title = timeline_switcher;
}
2018-10-28 13:54:09 +01:00
2020-05-31 12:28:35 +02:00
void on_timeline_changed (ParamSpec spec) {
var view = timeline_stack.visible_child as Views.Base;
2019-03-07 17:16:52 +01:00
2020-05-31 12:28:35 +02:00
if (last_view != null)
last_view.current = false;
2019-03-07 17:16:52 +01:00
2020-05-31 12:28:35 +02:00
if (view != null) {
view.current = true;
last_view = view;
}
2018-10-28 13:54:09 +01:00
}
2020-06-01 02:02:17 +02:00
void on_zoom_level_changed () {
var css ="""
2020-06-02 11:35:29 +02:00
.%s label {
2020-06-01 02:02:17 +02:00
font-size: %i%;
}
""".printf (ZOOM_CLASS, settings.post_text_size);
try {
zoom_css_provider.load_from_data (css);
}
catch (Error e) {
warning (@"Can't set zoom level: $(e.message)");
}
}
2018-04-14 14:09:06 +02:00
}