tootle-linux-client/src/Widgets/AccountsButton.vala

173 lines
4.6 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
2020-05-29 14:19:35 +02:00
[GtkTemplate (ui = "/com/github/bleakgrey/tootle/ui/widgets/accounts_button.ui")]
public class Tootle.Widgets.AccountsButton : Gtk.MenuButton, IAccountListener {
[GtkTemplate (ui = "/com/github/bleakgrey/tootle/ui/widgets/accounts_button_item.ui")]
class Item : Grid {
2020-05-29 14:19:35 +02:00
[GtkChild]
Widgets.Avatar avatar;
2020-05-29 14:19:35 +02:00
[GtkChild]
Label title;
2020-05-29 14:19:35 +02:00
[GtkChild]
Label handle;
2020-05-29 14:19:35 +02:00
[GtkChild]
Button profile;
2020-05-29 14:19:35 +02:00
[GtkChild]
Button forget;
2020-05-29 14:19:35 +02:00
public Item (InstanceAccount acc, AccountsButton _self) {
avatar.url = acc.avatar;
2020-05-30 18:48:20 +02:00
title.label = acc.display_name;
2020-05-29 14:19:35 +02:00
handle.label = acc.handle;
profile.clicked.connect (() => {
Views.Profile.open_from_id (acc.id);
_self.active = false;
});
2020-05-30 18:48:20 +02:00
forget.clicked.connect (() => {
2020-05-29 14:19:35 +02:00
_self.active = false;
accounts.remove (acc);
});
2018-04-15 12:03:40 +02:00
}
2019-03-09 12:42:27 +01:00
2020-05-29 14:19:35 +02:00
public Item.add_new () {
2020-05-30 18:48:20 +02:00
title.label = _("New Account");
2020-05-29 14:19:35 +02:00
handle.label = _("Click to add");
profile.destroy ();
2020-05-30 18:48:20 +02:00
forget.destroy ();
2018-05-27 18:25:16 +02:00
}
2018-04-15 12:03:40 +02:00
}
2018-04-14 14:09:06 +02:00
bool invalidated = true;
2020-05-29 14:19:35 +02:00
[GtkChild]
Widgets.Avatar avatar;
2020-05-29 14:19:35 +02:00
[GtkChild]
ListBox account_list;
2020-05-29 14:19:35 +02:00
[GtkChild]
ModelButton item_accounts;
2020-05-29 14:19:35 +02:00
[GtkChild]
ModelButton item_prefs;
2020-05-29 14:19:35 +02:00
[GtkChild]
ModelButton item_refresh;
2020-05-29 14:19:35 +02:00
[GtkChild]
ModelButton item_search;
2020-05-29 14:19:35 +02:00
[GtkChild]
Button item_favs;
2020-05-29 14:19:35 +02:00
[GtkChild]
Button item_conversations;
[GtkChild]
Button item_bookmarks;
2020-07-28 20:30:45 +02:00
[GtkChild]
Button item_lists;
2020-05-29 14:19:35 +02:00
construct {
2020-07-10 16:22:38 +02:00
account_listener_init ();
2019-03-09 12:42:27 +01:00
item_refresh.clicked.connect (() => {
app.refresh ();
});
2019-03-09 12:42:27 +01:00
Desktop.set_hotkey_tooltip (item_refresh, null, app.ACCEL_REFRESH);
item_favs.clicked.connect (() => {
window.open_view (new Views.Favorites ());
popover.popdown ();
});
item_conversations.clicked.connect (() => {
window.open_view (new Views.Conversations ());
popover.popdown ();
});
item_bookmarks.clicked.connect (() => {
window.open_view (new Views.Bookmarks ());
popover.popdown ();
});
2020-07-28 20:30:45 +02:00
item_lists.clicked.connect (() => {
window.open_view (new Views.Lists ());
popover.popdown ();
});
item_search.clicked.connect (() => {
window.open_view (new Views.Search ());
popover.popdown ();
});
item_prefs.clicked.connect (() => {
Dialogs.Preferences.open ();
popover.popdown ();
});
2020-05-29 14:19:35 +02:00
on_account_changed (null);
2019-03-09 12:42:27 +01:00
2020-05-29 14:19:35 +02:00
notify["active"].connect (() => {
if (active && invalidated)
rebuild ();
2018-05-27 18:25:16 +02:00
});
2019-03-09 12:42:27 +01:00
account_list.row_activated.connect (on_selection_changed);
2018-05-27 18:25:16 +02:00
}
2020-07-10 16:22:38 +02:00
~AccountsButton () {
account_listener_free ();
}
2019-03-09 12:42:27 +01:00
2020-05-29 14:19:35 +02:00
protected void on_selection_changed (ListBoxRow r) {
var i = r.get_index ();
if (i >= accounts.saved.size) {
active = false;
window.open_view (new Views.NewAccount (true));
popover.popdown ();
2020-05-29 14:19:35 +02:00
return;
}
var account = accounts.saved.@get (i);
if (accounts.active == account)
return;
accounts.switch_account (i);
popover.popdown ();
2018-04-14 14:09:06 +02:00
}
2019-03-09 12:42:27 +01:00
2020-05-29 14:19:35 +02:00
public virtual void on_accounts_changed (Gee.ArrayList<InstanceAccount> accounts) {
invalidated = true;
if (active)
rebuild ();
2018-05-27 18:25:16 +02:00
}
2020-05-29 14:19:35 +02:00
public virtual void on_account_changed (InstanceAccount? account) {
if (account == null) {
avatar.url = null;
item_accounts.text = "<b>" + _("No active account") + "</b>";
}
else {
avatar.url = account.avatar;
item_accounts.text = @"<b>$(account.display_name)</b>\n$(account.handle) ";
}
item_accounts.use_markup = true;
}
void rebuild () {
2020-05-29 14:19:35 +02:00
account_list.@foreach (w => account_list.remove (w));
accounts.saved.@foreach (acc => {
var item = new Item (acc, this);
var row = new ListBoxRow ();
row.add (item);
row.show ();
account_list.insert (row, -1);
if (accounts.active == acc)
row.activate ();
return true;
});
var new_row = new ListBoxRow ();
new_row.add (new Item.add_new ());
new_row.selectable = false;
new_row.show ();
account_list.insert (new_row, -1);
invalidated = false;
2018-05-31 14:13:21 +02:00
}
2018-04-14 14:09:06 +02:00
}