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

147 lines
4.1 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")]
private class Item : Grid {
[GtkChild]
private Widgets.Avatar avatar;
[GtkChild]
2020-05-30 18:48:20 +02:00
private Label title;
2020-05-29 14:19:35 +02:00
[GtkChild]
private Label handle;
[GtkChild]
private Button profile;
[GtkChild]
2020-05-30 18:48:20 +02:00
private 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
2020-05-29 14:19:35 +02:00
private bool invalidated = true;
[GtkChild]
private Widgets.Avatar avatar;
2020-05-30 18:48:20 +02:00
// [GtkChild]
// private Spinner spinner;
2020-05-29 14:19:35 +02:00
[GtkChild]
private ListBox account_list;
[GtkChild]
private ModelButton item_accounts;
[GtkChild]
private ModelButton item_prefs;
[GtkChild]
private ModelButton item_refresh;
[GtkChild]
private ModelButton item_search;
[GtkChild]
private ModelButton item_favs;
[GtkChild]
2020-06-03 14:41:21 +02:00
private ModelButton item_conversations;
2020-05-29 14:19:35 +02:00
construct {
connect_account ();
2019-03-09 12:42:27 +01:00
2018-05-29 19:04:04 +02: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);
2019-03-11 15:14:37 +01:00
item_favs.clicked.connect (() => window.open_view (new Views.Favorites ()));
2020-06-03 14:41:21 +02:00
item_conversations.clicked.connect (() => window.open_view (new Views.Conversations ()));
2019-03-11 15:14:37 +01:00
item_search.clicked.connect (() => window.open_view (new Views.Search ()));
2020-05-29 14:19:35 +02:00
item_prefs.clicked.connect (() => Dialogs.Preferences.open ());
2019-03-09 12:42:27 +01:00
2020-05-29 14:19:35 +02:00
// network.started.connect (() => spinner.show ());
// network.finished.connect (() => spinner.hide ());
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
2020-05-29 14:19:35 +02:00
account_list.row_activated.connect (on_selection_changed) ;
2018-05-27 18:25:16 +02:00
}
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));
return;
}
var account = accounts.saved.@get (i);
if (accounts.active == account)
return;
accounts.switch_account (i);
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;
}
2020-05-29 14:19:35 +02:00
private void rebuild () {
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
}