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

172 lines
5.6 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
public class Tootle.AccountsButton : Gtk.MenuButton{
const int AVATAR_SIZE = 24;
2018-04-14 14:09:06 +02:00
Granite.Widgets.Avatar avatar;
Gtk.Grid grid;
Gtk.Popover menu;
2018-05-27 18:25:16 +02:00
Gtk.ListBox list;
2018-05-02 14:28:46 +02:00
Gtk.ModelButton item_settings;
2018-05-03 10:11:31 +02:00
Gtk.ModelButton item_refresh;
2018-05-14 16:43:10 +02:00
Gtk.ModelButton item_search;
2018-05-02 14:28:46 +02:00
Gtk.ModelButton item_favs;
2018-06-17 11:48:58 +02:00
Gtk.ModelButton item_direct;
2018-07-14 10:37:41 +02:00
Gtk.ModelButton item_watchlist;
2018-04-15 12:03:40 +02:00
2018-10-27 10:29:25 +02:00
private class AccountItemView : Gtk.ListBoxRow{
2018-04-15 12:03:40 +02:00
2018-05-27 18:25:16 +02:00
private Gtk.Grid grid;
2018-04-15 13:29:55 +02:00
public Gtk.Label display_name;
2018-05-27 18:25:16 +02:00
public Gtk.Label instance;
public Gtk.Button button;
public int id = -1;
2018-04-15 12:03:40 +02:00
construct {
2018-05-27 18:25:16 +02:00
can_default = false;
grid = new Gtk.Grid ();
grid.margin = 6;
grid.margin_start = 14;
2018-04-15 12:03:40 +02:00
2018-05-27 18:25:16 +02:00
display_name = new Gtk.Label ("");
2018-04-15 13:29:55 +02:00
display_name.hexpand = true;
display_name.halign = Gtk.Align.START;
display_name.use_markup = true;
2018-05-27 18:25:16 +02:00
instance = new Gtk.Label ("");
instance.halign = Gtk.Align.START;
button = new Gtk.Button.from_icon_name ("close-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
button.receives_default = false;
button.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);
2018-04-15 12:03:40 +02:00
2018-05-27 18:25:16 +02:00
grid.attach(display_name, 1, 0, 1, 1);
grid.attach(instance, 1, 1, 1, 1);
grid.attach(button, 2, 0, 2, 2);
add (grid);
show_all ();
2018-04-15 12:03:40 +02:00
}
2018-10-27 10:29:25 +02:00
public AccountItemView (){
2018-05-29 19:04:04 +02:00
button.clicked.connect (() => accounts.remove (id));
2018-05-27 18:25:16 +02:00
}
2018-04-15 12:03:40 +02:00
}
2018-04-14 14:09:06 +02:00
construct{
avatar = new Granite.Widgets.Avatar.with_default_icon (AVATAR_SIZE);
2018-04-14 14:09:06 +02:00
avatar.button_press_event.connect(event => {
return false;
});
2018-05-27 18:25:16 +02:00
list = new Gtk.ListBox ();
2018-04-15 12:03:40 +02:00
2018-04-14 14:09:06 +02:00
var item_separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
item_separator.hexpand = true;
2018-05-03 10:11:31 +02:00
item_refresh = new Gtk.ModelButton ();
item_refresh.text = _("Refresh");
2018-05-29 19:04:04 +02:00
item_refresh.clicked.connect (() => app.refresh ());
2018-05-03 10:11:31 +02:00
2018-05-02 14:28:46 +02:00
item_favs = new Gtk.ModelButton ();
item_favs.text = _("Favorites");
2018-05-29 19:04:04 +02:00
item_favs.clicked.connect (() => window.open_view (new FavoritesView ()));
2018-06-17 11:48:58 +02:00
item_direct = new Gtk.ModelButton ();
item_direct.text = _("Direct Messages");
item_direct.clicked.connect (() => window.open_view (new DirectView ()));
2018-05-02 14:28:46 +02:00
2018-05-14 16:43:10 +02:00
item_search = new Gtk.ModelButton ();
item_search.text = _("Search");
2018-05-29 19:04:04 +02:00
item_search.clicked.connect (() => window.open_view (new SearchView ()));
2018-05-14 16:43:10 +02:00
2018-07-14 10:37:41 +02:00
item_watchlist = new Gtk.ModelButton ();
item_watchlist.text = _("Watchlist");
item_watchlist.clicked.connect (() => WatchlistDialog.open ());
2018-05-02 14:28:46 +02:00
item_settings = new Gtk.ModelButton ();
2018-04-14 19:53:09 +02:00
item_settings.text = _("Settings");
2018-05-20 14:43:42 +02:00
item_settings.clicked.connect (() => SettingsDialog.open ());
2018-04-14 14:09:06 +02:00
grid = new Gtk.Grid ();
grid.orientation = Gtk.Orientation.VERTICAL;
grid.width_request = 200;
2018-05-27 18:25:16 +02:00
grid.attach(list, 0, 1, 1, 1);
grid.attach(item_separator, 0, 3, 1, 1);
grid.attach(item_favs, 0, 4, 1, 1);
2018-06-17 11:48:58 +02:00
grid.attach(item_direct, 0, 5, 1, 1);
grid.attach(new Gtk.Separator (Gtk.Orientation.HORIZONTAL), 0, 6, 1, 1);
grid.attach(item_refresh, 0, 7, 1, 1);
grid.attach(item_search, 0, 8, 1, 1);
2018-07-14 10:37:41 +02:00
grid.attach(item_watchlist, 0, 9, 1, 1);
grid.attach(item_settings, 0, 10, 1, 1);
2018-04-14 14:09:06 +02:00
grid.show_all ();
menu = new Gtk.Popover (null);
menu.add (grid);
get_style_context ().add_class ("button_avatar");
popover = menu;
2018-10-27 10:29:25 +02:00
add (avatar);
2018-04-14 14:09:06 +02:00
show_all ();
2018-04-14 19:18:42 +02:00
2018-05-29 14:25:56 +02:00
accounts.updated.connect (accounts_updated);
accounts.switched.connect (account_switched);
2018-05-27 18:25:16 +02:00
list.row_activated.connect (row => {
2018-10-27 10:29:25 +02:00
var widget = row as AccountItemView;
2018-05-27 18:25:16 +02:00
if (widget.id == -1) {
NewAccountDialog.open ();
return;
2018-04-15 12:03:40 +02:00
}
2018-10-27 10:29:25 +02:00
if (widget.id == settings.current_account)
AccountView.open_from_id (accounts.current.id);
2018-05-27 18:25:16 +02:00
else
2018-05-29 19:04:04 +02:00
accounts.switch_account (widget.id);
2018-10-27 10:29:25 +02:00
menu.popdown ();
2018-04-14 19:18:42 +02:00
});
2018-04-14 14:09:06 +02:00
}
2018-05-27 18:25:16 +02:00
private void accounts_updated (GenericArray<InstanceAccount> accounts) {
list.forall (widget => widget.destroy ());
int i = -1;
accounts.foreach (account => {
i++;
2018-10-27 10:29:25 +02:00
var widget = new AccountItemView ();
2018-05-27 18:25:16 +02:00
widget.id = i;
widget.display_name.label = "<b>@"+account.username+"</b>";
2018-05-30 16:51:55 +02:00
widget.instance.label = account.get_pretty_instance ();
2018-05-27 18:25:16 +02:00
list.add (widget);
});
2018-10-27 10:29:25 +02:00
var add_account = new AccountItemView ();
2018-05-27 18:25:16 +02:00
add_account.display_name.label = _("<b>New Account</b>");
add_account.instance.label = _("Click to add");
add_account.button.hide ();
list.add (add_account);
update_selection ();
}
private void account_switched (Account? account) {
if (account == null)
avatar.show_default (AVATAR_SIZE);
2018-05-27 18:25:16 +02:00
else
network.load_avatar (account.avatar, avatar, get_avatar_size ());
2018-04-14 14:09:06 +02:00
}
2018-05-27 18:25:16 +02:00
private void update_selection () {
2018-10-27 10:29:25 +02:00
var id = settings.current_account;
2018-05-27 18:25:16 +02:00
var row = list.get_row_at_index (id);
if (row != null)
list.select_row (row);
}
public int get_avatar_size () {
return AVATAR_SIZE * get_style_context ().get_scale ();
}
2018-05-31 14:13:21 +02:00
public AccountsButton() {
account_switched (accounts.current);
}
2018-04-14 14:09:06 +02:00
}