From 90ea9c4da4aa885faf7a74ce85c5ec9b1841db2e Mon Sep 17 00:00:00 2001
From: fiaxh <git@lightrise.org>
Date: Fri, 21 Jun 2024 17:58:00 +0200
Subject: Preferences dialog: Various improvements

- Only show avatar remove button if one is set, fixes #1589
- Only show account picker if user has more than one account
---
 .../account_preferences_subpage.vala               |  8 ++-
 .../encryption_preferences_page.vala               | 58 ++++++++++++++--------
 2 files changed, 43 insertions(+), 23 deletions(-)

diff --git a/main/src/windows/preferences_window/account_preferences_subpage.vala b/main/src/windows/preferences_window/account_preferences_subpage.vala
index 01cf5584..a1966e34 100644
--- a/main/src/windows/preferences_window/account_preferences_subpage.vala
+++ b/main/src/windows/preferences_window/account_preferences_subpage.vala
@@ -123,8 +123,14 @@ public class Dino.Ui.AccountPreferencesSubpage : Gtk.Box {
                     return true;
                 });
 
+                // Only show avatar removal button if an avatar is set
+                var avatar_model = model.selected_account.avatar_model.tiles.get_item(0) as ViewModel.AvatarPictureTileModel;
+                avatar_model.notify["image-file"].connect(() => {
+                    remove_avatar_button.visible = avatar_model.image_file != null;
+                });
+                remove_avatar_button.visible = avatar_model.image_file != null;
+
                 model.selected_account.notify["connection-error"].connect(() => {
-                    // TODO doesn't work
                     if (model.selected_account.connection_error != null) {
                         connection_status.add_css_class("error");
                     } else {
diff --git a/main/src/windows/preferences_window/encryption_preferences_page.vala b/main/src/windows/preferences_window/encryption_preferences_page.vala
index 2222584d..7477e6cd 100644
--- a/main/src/windows/preferences_window/encryption_preferences_page.vala
+++ b/main/src/windows/preferences_window/encryption_preferences_page.vala
@@ -18,36 +18,50 @@ public class Dino.Ui.PreferencesWindowEncryption : Adw.PreferencesPage {
 
         this.notify["model"].connect(() => {
             this.model.update.connect(() => {
-                if (drop_down != null) {
-                    accounts_group.remove(drop_down);
-                    drop_down = null;
-                }
-
-                if (model.active_accounts_selection.get_n_items() > 0) {
-                    drop_down = new DropDown(model.active_accounts_selection, null) { halign=Align.CENTER };
-                    drop_down.factory = new BuilderListItemFactory.from_resource(null, "/im/dino/Dino/account_picker_row.ui");
-
-                    drop_down.notify["selected-item"].connect(() => {
-                        var account_details = (ViewModel.AccountDetails) drop_down.selected_item;
-                        if (account_details == null) return;
-                        set_account(account_details.account);
-                    });
-
-                    drop_down.selected = 0;
-                    set_account(((ViewModel.AccountDetails)model.active_accounts_selection.get_item(0)).account);
-                } else {
-                    drop_down = new DropDown.from_strings(new string[] { _("No active accounts")}) { halign=Align.CENTER };
-                }
-                accounts_group.add(drop_down);
+                repopulate_account_selector();
             });
         });
     }
 
-    public void set_account(Account account) {
+    private void repopulate_account_selector() {
+        // Remove current selector
+        if (drop_down != null) {
+            accounts_group.remove(drop_down);
+            drop_down = null;
+        }
+
+        // Don't show selector if the user has only one account (active + inactive)
+        accounts_group.visible = model.account_details.size != 1;
+
+        // Populate selector
+        if (model.active_accounts_selection.get_n_items() > 0) {
+            drop_down = new DropDown(model.active_accounts_selection, null) { halign=Align.CENTER };
+            drop_down.factory = new BuilderListItemFactory.from_resource(null, "/im/dino/Dino/account_picker_row.ui");
+
+            drop_down.notify["selected-item"].connect(() => {
+                var account_details = (ViewModel.AccountDetails) drop_down.selected_item;
+                if (account_details == null) return;
+                set_account(account_details.account);
+            });
+
+            drop_down.selected = 0;
+            set_account(((ViewModel.AccountDetails)model.active_accounts_selection.get_item(0)).account);
+        } else {
+            drop_down = new DropDown.from_strings(new string[] { _("No active accounts")}) { halign=Align.CENTER };
+            unset_account();
+        }
+        accounts_group.add(drop_down);
+    }
+
+    private void unset_account() {
         foreach (var widget in added_widgets) {
             this.remove(widget);
         }
         added_widgets.clear();
+    }
+
+    private void set_account(Account account) {
+        unset_account();
 
         Application app = GLib.Application.get_default() as Application;
         foreach (Plugins.EncryptionPreferencesEntry e in app.plugin_registry.encryption_preferences_entries) {
-- 
cgit v1.2.3-70-g09d2