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

183 lines
3.9 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")]
2021-02-20 13:46:57 +01:00
public class Tootle.Widgets.AccountsButton : Gtk.MenuButton, IAccountHolder {
protected InstanceAccount? account { get; set; default = null; }
2020-05-29 14:19:35 +02:00
2020-08-01 01:42:14 +02:00
[GtkTemplate (ui = "/com/github/bleakgrey/tootle/ui/widgets/accounts_button_item.ui")]
class Item : ListBoxRow {
AccountsButton button;
InstanceAccount account;
[GtkChild]
Stack stack;
[GtkChild]
Widgets.Avatar avatar;
[GtkChild]
Label title;
[GtkChild]
Label handle;
[GtkChild]
Button forget_button;
public Item (InstanceAccount account, AccountsButton btn) {
this.account = account;
this.button = btn;
2020-10-18 22:35:59 +02:00
avatar.account = account;
2020-08-01 01:42:14 +02:00
title.label = account.display_name;
handle.label = account.handle;
}
public Item.add_new () {
forget_button.destroy ();
stack.visible_child_name = "new";
selectable = false;
}
[GtkCallback]
void forget () {
var forget = app.question (
_("Forget %s?".printf (handle.label)),
_("This account will be removed from the application."),
window
);
if (forget) {
button.active = false;
2021-02-12 16:26:37 +01:00
try {
accounts.remove (account);
}
catch (Error e) {
warning (e.message);
app.inform (Gtk.MessageType.ERROR, _("Error"), e.message);
}
2020-08-01 01:42:14 +02:00
}
}
[GtkCallback]
void open_profile () {
button.active = false;
2020-08-01 17:40:56 +02:00
account.resolve_open (accounts.active);
2020-08-01 01:42:14 +02:00
}
}
bool invalidated = true;
[GtkChild]
Widgets.Avatar avatar;
[GtkChild]
ListBox account_list;
[GtkChild]
ModelButton item_accounts;
[GtkChild]
ModelButton item_prefs;
[GtkChild]
ModelButton item_refresh;
[GtkChild]
Button item_favs;
[GtkChild]
Button item_conversations;
[GtkChild]
Button item_bookmarks;
[GtkChild]
Button item_lists;
construct {
account_listener_init ();
get_style_context ().add_class ("image-button");
2020-08-01 01:42:14 +02:00
item_refresh.clicked.connect (() => {
app.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 ();
});
item_lists.clicked.connect (() => {
window.open_view (new Views.Lists ());
popover.popdown ();
});
item_prefs.clicked.connect (() => {
Dialogs.Preferences.open ();
popover.popdown ();
});
notify["active"].connect (() => {
if (active && invalidated)
rebuild ();
});
account_list.row_activated.connect (on_selection_changed);
}
2020-07-10 16:22:38 +02:00
~AccountsButton () {
account_listener_free ();
}
2019-03-09 12:42:27 +01:00
2020-08-01 01:42:14 +02:00
protected void on_selection_changed (ListBoxRow r) {
var i = r.get_index ();
if (i >= accounts.saved.size) {
active = false;
2020-09-10 19:10:24 +02:00
new Dialogs.NewAccount ();
2020-08-01 01:42:14 +02:00
popover.popdown ();
return;
}
var account = accounts.saved.@get (i);
if (accounts.active == account)
return;
2021-02-12 16:26:37 +01:00
else
accounts.activate (account);
2020-08-01 01:42:14 +02:00
popover.popdown ();
}
public virtual void on_accounts_changed (Gee.ArrayList<InstanceAccount> accounts) {
invalidated = true;
if (active)
rebuild ();
}
public virtual void on_account_changed (InstanceAccount? account) {
if (account == null) {
2020-10-18 22:35:59 +02:00
avatar.account = null;
2020-08-01 01:42:14 +02:00
item_accounts.text = "<b><span size=\"large\">%s</span></b>\n%s".printf (
_("Anonymous"),
_("No active account"));
}
else {
2020-10-18 22:35:59 +02:00
avatar.account = account;
2020-08-01 01:42:14 +02:00
item_accounts.text = @"<b><span size=\"large\">$(account.display_name)</span></b>\n$(account.handle)";
}
item_accounts.use_markup = true;
}
void rebuild () {
account_list.@foreach (w => account_list.remove (w));
accounts.saved.@foreach (acc => {
var row = new Item (acc, this);
account_list.insert (row, -1);
if (accounts.active == acc)
row.activate ();
return true;
});
account_list.insert (new Item.add_new (), -1);
invalidated = false;
}
2018-04-14 14:09:06 +02:00
}