Tooth/src/Views/Base.vala

121 lines
3.7 KiB
Vala
Raw Normal View History

2020-05-29 12:19:35 +00:00
using Gtk;
[GtkTemplate (ui = "/dev/geopjr/Tooth/ui/views/base.ui")]
2022-11-13 20:57:43 +00:00
public class Tooth.Views.Base : Box {
2020-05-29 12:19:35 +00:00
public static string STATUS_EMPTY = _("Nothing to see here");
2020-05-29 12:19:35 +00:00
2020-05-31 10:28:35 +00:00
public string? icon { get; set; default = null; }
public string label { get; set; default = ""; }
public bool needs_attention { get; set; default = false; }
public bool current { get; set; default = false; }
public bool is_main { get; set; default = false; }
2022-11-29 01:00:33 +00:00
public bool is_profile { get; set; default = false; }
public bool is_sidebar_item { get; set; default = false; }
2022-11-18 01:59:31 +00:00
public int badge_number { get; set; default = 0; }
2021-07-23 11:41:03 +00:00
protected SimpleActionGroup actions { get; set; default = new SimpleActionGroup (); }
[GtkChild] protected unowned Adw.HeaderBar header;
[GtkChild] protected unowned Button back_button;
[GtkChild] protected unowned ScrolledWindow scrolled;
[GtkChild] protected unowned Box view;
[GtkChild] protected unowned Adw.Clamp clamp;
[GtkChild] protected unowned Box column_view;
[GtkChild] protected unowned Stack states;
[GtkChild] protected unowned Box content_box;
[GtkChild] protected unowned Button status_button;
[GtkChild] unowned Stack status_stack;
[GtkChild] unowned Label status_title_label;
[GtkChild] unowned Label status_message_label;
public string state { get; set; default = "status"; }
public string status_title { get; set; default = STATUS_EMPTY; }
public string status_message { get; set; default = STATUS_EMPTY; }
public bool status_loading { get; set; default = false; }
construct {
2021-07-23 11:41:03 +00:00
build_actions ();
build_header ();
status_button.label = _("Reload");
bind_property ("state", states, "visible-child-name", BindingFlags.SYNC_CREATE);
bind_property ("status-loading", status_stack, "visible-child-name", BindingFlags.SYNC_CREATE, (b, src, ref target) => {
target.set_string (src.get_boolean () ? "spinner" : "message");
return true;
});
bind_property ("status-message", status_message_label, "label", BindingFlags.SYNC_CREATE, (b, src, ref target) => {
var src_s = src.get_string ();
target.set_string (src_s);
status_message_label.visible = src_s != STATUS_EMPTY && src_s != "";
return true;
});
bind_property ("status-title", status_title_label, "label", BindingFlags.SYNC_CREATE, (b, src, ref target) => {
var src_s = src.get_string ();
target.set_string (src_s);
status_message = STATUS_EMPTY;
status_loading = src_s == "";
return true;
});
notify["status-title"].connect (() => {
status_title_label.label = status_title;
status_message = STATUS_EMPTY;
});
notify["current"].connect (() => {
if (current)
on_shown ();
else
on_hidden ();
});
scrolled.get_style_context ().add_class (Dialogs.MainWindow.ZOOM_CLASS);
2021-07-23 11:41:03 +00:00
}
~Base () {
message ("Destroying base "+label);
}
2021-07-23 11:41:03 +00:00
public override void dispose () {
actions.dispose ();
base.dispose ();
}
2021-07-23 11:41:03 +00:00
protected virtual void build_actions () {}
2021-07-23 11:41:03 +00:00
protected virtual void build_header () {
2022-11-17 18:32:26 +00:00
var title = new Adw.WindowTitle (label, "");
2021-07-23 11:41:03 +00:00
bind_property ("label", title, "title", BindingFlags.SYNC_CREATE);
header.title_widget = title;
}
2021-07-23 11:41:03 +00:00
public virtual void clear () {
state = "status";
}
public virtual void on_shown () {
2022-12-11 00:23:29 +00:00
if (app != null && app.main_window != null)
app.main_window.insert_action_group ("view", actions);
}
public virtual void on_hidden () {
2022-12-11 00:23:29 +00:00
if (app != null && app.main_window != null)
app.main_window.insert_action_group ("view", null);
}
2021-07-23 11:41:03 +00:00
public virtual void on_content_changed () {}
public virtual void on_error (int32 code, string reason) {
status_title = _("An Error Occurred");
status_message = reason;
status_button.visible = true;
status_button.sensitive = true;
state = "status";
}
[GtkCallback]
void on_close () {
2021-07-23 11:41:03 +00:00
app.main_window.back ();
}
2020-05-29 12:19:35 +00:00
}