[geary/mjog/mutiple-main-windows: 1/14] Remove GearyApplication.instance global var



commit 419bc6d051c76c0172d9866958aa978f91dfbaa1
Author: Michael Gratton <mike vee net>
Date:   Wed Nov 13 10:32:00 2019 +1100

    Remove GearyApplication.instance global var

 src/client/application/application-controller.vala |  2 +-
 src/client/application/geary-application.vala      | 13 -----
 src/client/components/icon-factory.vala            | 22 +++-----
 src/client/components/main-window.vala             |  7 ++-
 src/client/components/search-bar.vala              | 14 +++--
 .../conversation-list-cell-renderer.vala           |  7 ++-
 .../conversation-list/conversation-list-store.vala | 11 ++--
 .../conversation-list/conversation-list-view.vala  | 59 ++++++++++++----------
 .../formatted-conversation-data.vala               | 18 ++++---
 .../folder-list/folder-list-folder-entry.vala      |  5 +-
 .../folder-list/folder-list-search-branch.vala     | 20 +++++++-
 src/client/sidebar/sidebar-entry.vala              |  2 +-
 src/client/sidebar/sidebar-tree.vala               |  9 +++-
 13 files changed, 110 insertions(+), 79 deletions(-)
---
diff --git a/src/client/application/application-controller.vala 
b/src/client/application/application-controller.vala
index c483fb31..9b17ed00 100644
--- a/src/client/application/application-controller.vala
+++ b/src/client/application/application-controller.vala
@@ -185,7 +185,7 @@ public class Application.Controller : Geary.BaseObject {
         // This initializes the IconFactory, important to do before
         // the actions are created (as they refer to some of Geary's
         // custom icons)
-        IconFactory.instance.init();
+        IconFactory.init(application.get_resource_directory());
 
         // Create DB upgrade dialog.
         this.upgrade_dialog = new UpgradeDialog();
diff --git a/src/client/application/geary-application.vala b/src/client/application/geary-application.vala
index 53db7dc5..4ede37e1 100644
--- a/src/client/application/geary-application.vala
+++ b/src/client/application/geary-application.vala
@@ -156,18 +156,6 @@ public class GearyApplication : Gtk.Application {
 
     }
 
-    [Version (deprecated = true)]
-    public static GearyApplication instance {
-        get { return _instance; }
-        private set {
-            // Ensure singleton behavior.
-            assert (_instance == null);
-            _instance = value;
-        }
-    }
-    private static GearyApplication _instance = null;
-
-
     /**
      * The global controller for this application instance.
      *
@@ -347,7 +335,6 @@ public class GearyApplication : Gtk.Application {
             )
         );
         this.add_main_option_entries(OPTION_ENTRIES);
-        _instance = this;
     }
 
     public override bool local_command_line(ref unowned string[] args,
diff --git a/src/client/components/icon-factory.vala b/src/client/components/icon-factory.vala
index c7950bf3..267ac3e4 100644
--- a/src/client/components/icon-factory.vala
+++ b/src/client/components/icon-factory.vala
@@ -9,18 +9,15 @@ public class IconFactory {
     public const Gtk.IconSize ICON_TOOLBAR = Gtk.IconSize.LARGE_TOOLBAR;
     public const Gtk.IconSize ICON_SIDEBAR = Gtk.IconSize.MENU;
 
-    private static IconFactory? _instance = null;
-    public static IconFactory instance {
-        get {
-            if (_instance == null)
-                _instance = new IconFactory();
 
-            return _instance;
-        }
+    public static IconFactory? instance { get; private set; }
+
 
-        private set { _instance = value; }
+    public static void init(GLib.File resource_directory) {
+        IconFactory.instance = new IconFactory(resource_directory);
     }
 
+
     public const int UNREAD_ICON_SIZE = 16;
     public const int STAR_ICON_SIZE = 16;
 
@@ -29,17 +26,12 @@ public class IconFactory {
     private File icons_dir;
 
     // Creates the icon factory.
-    private IconFactory() {
-        icons_dir = GearyApplication.instance.get_resource_directory().get_child("icons");
+    private IconFactory(GLib.File resource_directory) {
+        icons_dir = resource_directory.get_child("icons");
         icon_theme = Gtk.IconTheme.get_default();
         icon_theme.append_search_path(icons_dir.get_path());
     }
 
-    public void init() {
-        // perform any additional initialization here; at this time, everything is done in the
-        // constructor
-    }
-
     private int icon_size_to_pixels(Gtk.IconSize icon_size) {
         switch (icon_size) {
             case ICON_SIDEBAR:
diff --git a/src/client/components/main-window.vala b/src/client/components/main-window.vala
index 02e5936d..216c2340 100644
--- a/src/client/components/main-window.vala
+++ b/src/client/components/main-window.vala
@@ -451,7 +451,8 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
                 this.progress_monitor.add(this.conversations.progress_monitor);
 
                 conversations_model = new ConversationListStore(
-                    this.conversations
+                    this.conversations, this.application.config
+
                 );
                 this.progress_monitor.add(conversations_model.preview_monitor);
                 this.conversation_list_view.set_model(conversations_model);
@@ -839,7 +840,9 @@ public class MainWindow : Gtk.ApplicationWindow, Geary.BaseInterface {
         this.folder_list_scrolled.add(this.folder_list);
 
         // Conversation list
-        this.conversation_list_view = new ConversationListView(this);
+        this.conversation_list_view = new ConversationListView(
+            this.application.config
+        );
         this.conversation_list_view.load_more.connect(on_load_more);
         this.conversation_list_view.mark_conversations.connect(on_mark_conversations);
         this.conversation_list_view.conversations_selected.connect(on_conversations_selected);
diff --git a/src/client/components/search-bar.vala b/src/client/components/search-bar.vala
index d79cc8f4..7a2471ce 100644
--- a/src/client/components/search-bar.vala
+++ b/src/client/components/search-bar.vala
@@ -110,9 +110,17 @@ public class SearchBar : Gtk.SearchBar {
     }
 
     private void on_information_changed() {
-        set_search_placeholder_text(current_account == null ||
-            GearyApplication.instance.controller.get_num_accounts() == 1 ? DEFAULT_SEARCH_TEXT :
-            _("Search %s account").printf(current_account.information.display_name));
+        MainWindow? main = get_toplevel() as MainWindow;
+        if (main != null) {
+            set_search_placeholder_text(
+                current_account == null ||
+                main.application.controller.get_num_accounts() == 1
+                ? DEFAULT_SEARCH_TEXT :
+                _("Search %s account").printf(
+                    current_account.information.display_name
+                )
+            );
+        }
     }
 
     private void on_search_mode_changed() {
diff --git a/src/client/conversation-list/conversation-list-cell-renderer.vala 
b/src/client/conversation-list/conversation-list-cell-renderer.vala
index d8cccfb2..98e4ddab 100644
--- a/src/client/conversation-list/conversation-list-cell-renderer.vala
+++ b/src/client/conversation-list/conversation-list-cell-renderer.vala
@@ -46,8 +46,11 @@ public class ConversationListCellRenderer : Gtk.CellRenderer {
     // Recalculates size when the style changed.
     // Note: this must be called by the parent TreeView.
     public static void style_changed(Gtk.Widget widget) {
-        if (example_data == null) {
-            example_data = new FormattedConversationData.create_example();
+        MainWindow? window = widget.get_toplevel() as MainWindow;
+        if (window != null && example_data == null) {
+            example_data = new FormattedConversationData.create_example(
+                window.application.config
+            );
         }
 
         example_data.calculate_sizes(widget);
diff --git a/src/client/conversation-list/conversation-list-store.vala 
b/src/client/conversation-list/conversation-list-store.vala
index 3917f2ca..a4542867 100644
--- a/src/client/conversation-list/conversation-list-store.vala
+++ b/src/client/conversation-list/conversation-list-store.vala
@@ -91,6 +91,8 @@ public class ConversationListStore : Gtk.ListStore {
     public Geary.ProgressMonitor preview_monitor { get; private set; default =
         new Geary.SimpleProgressMonitor(Geary.ProgressType.ACTIVITY); }
 
+    private Application.Configuration config;
+
     private Gee.HashMap<Geary.App.Conversation, RowWrapper> row_map = new Gee.HashMap<
         Geary.App.Conversation, RowWrapper>();
     private Geary.App.EmailStore? email_store = null;
@@ -101,7 +103,8 @@ public class ConversationListStore : Gtk.ListStore {
     public signal void conversations_added(bool start);
     public signal void conversations_removed(bool start);
 
-    public ConversationListStore(Geary.App.ConversationMonitor conversations) {
+    public ConversationListStore(Geary.App.ConversationMonitor conversations,
+                                 Application.Configuration config) {
         set_column_types(Column.get_types());
         set_default_sort_func(ConversationListStore.sort_by_date);
         set_sort_column_id(Gtk.SortColumn.DEFAULT, Gtk.SortType.DESCENDING);
@@ -110,7 +113,8 @@ public class ConversationListStore : Gtk.ListStore {
         this.email_store = new Geary.App.EmailStore(
             conversations.base_folder.account
         );
-        GearyApplication.instance.config.settings.changed[
+        this.config = config;
+        this.config.settings.changed[
             Application.Configuration.DISPLAY_PREVIEW_KEY
         ].connect(on_display_preview_changed);
 
@@ -175,7 +179,7 @@ public class ConversationListStore : Gtk.ListStore {
 
     // should only be called by refresh_previews_async()
     private async void do_refresh_previews_async(Geary.App.ConversationMonitor conversation_monitor) {
-        if (conversation_monitor == null || !GearyApplication.instance.config.display_preview)
+        if (conversation_monitor == null || !this.config.display_preview)
             return;
 
         Gee.Set<Geary.EmailIdentifier> needing_previews = get_emails_needing_previews();
@@ -283,6 +287,7 @@ public class ConversationListStore : Gtk.ListStore {
 
     private void set_row(Gtk.TreeIter iter, Geary.App.Conversation conversation, Geary.Email preview) {
         FormattedConversationData conversation_data = new FormattedConversationData(
+            this.config,
             conversation,
             preview,
             this.conversations.base_folder,
diff --git a/src/client/conversation-list/conversation-list-view.vala 
b/src/client/conversation-list/conversation-list-view.vala
index 7eb3dcb7..e95b7284 100644
--- a/src/client/conversation-list/conversation-list-view.vala
+++ b/src/client/conversation-list/conversation-list-view.vala
@@ -8,8 +8,7 @@ public class ConversationListView : Gtk.TreeView, Geary.BaseInterface {
     const int LOAD_MORE_HEIGHT = 100;
 
 
-    // Used to be able to refer to the action names of the MainWindow
-    private weak MainWindow main_window;
+    private Application.Configuration config;
 
     private bool enable_load_more = true;
 
@@ -34,11 +33,12 @@ public class ConversationListView : Gtk.TreeView, Geary.BaseInterface {
     public signal void visible_conversations_changed(Gee.Set<Geary.App.Conversation> visible);
 
 
-    public ConversationListView(MainWindow parent) {
+    public ConversationListView(Application.Configuration config) {
         base_ref();
         set_show_expanders(false);
         set_headers_visible(false);
-        this.main_window = parent;
+
+        this.config = config;
 
         append_column(create_column(ConversationListStore.Column.CONVERSATION_DATA,
             new ConversationListCellRenderer(), ConversationListStore.Column.CONVERSATION_DATA.to_string(),
@@ -56,7 +56,7 @@ public class ConversationListView : Gtk.TreeView, Geary.BaseInterface {
         Gtk.drag_source_set(this, Gdk.ModifierType.BUTTON1_MASK, FolderList.Tree.TARGET_ENTRY_LIST,
             Gdk.DragAction.COPY | Gdk.DragAction.MOVE);
 
-        GearyApplication.instance.config.settings.changed[
+        this.config.settings.changed[
             Application.Configuration.DISPLAY_PREVIEW_KEY
         ].connect(on_display_preview_changed);
 
@@ -193,7 +193,7 @@ public class ConversationListView : Gtk.TreeView, Geary.BaseInterface {
         // Select the first conversation, if autoselect is enabled,
         // nothing has been selected yet and we're not showing a
         // composer.
-        if (GearyApplication.instance.config.autoselect &&
+        if (this.config.autoselect &&
             get_selection().count_selected_rows() == 0) {
             MainWindow? parent = get_toplevel() as MainWindow;
             if (parent != null && !parent.has_composer) {
@@ -222,7 +222,7 @@ public class ConversationListView : Gtk.TreeView, Geary.BaseInterface {
     }
 
     private void on_conversations_removed(bool start) {
-        if (!GearyApplication.instance.config.autoselect) {
+        if (!this.config.autoselect) {
             Gtk.SelectionMode mode = start
                 // Stop GtkTreeView from automatically selecting the
                 // next row after the removed rows
@@ -262,7 +262,7 @@ public class ConversationListView : Gtk.TreeView, Geary.BaseInterface {
             // Click positions depend on whether the preview is enabled.
             bool read_clicked = false;
             bool star_clicked = false;
-            if (GearyApplication.instance.config.display_preview) {
+            if (this.config.display_preview) {
                 read_clicked = cell_x < 25 && cell_y >= 14 && cell_y <= 30;
                 star_clicked = cell_x < 25 && cell_y >= 40 && cell_y <= 62;
             } else {
@@ -306,26 +306,29 @@ public class ConversationListView : Gtk.TreeView, Geary.BaseInterface {
             Geary.App.Conversation conversation = get_model().get_conversation_at_path(path);
 
             GLib.Menu context_menu_model = new GLib.Menu();
-            if (!this.main_window.is_shift_down) {
-                context_menu_model.append(
-                    /// Translators: Context menu item
-                    ngettext(
-                        "Move conversation to _Trash",
-                        "Move conversations to _Trash",
-                        this.selected.size
-                    ),
-                    "win." + MainWindow.ACTION_ARCHIVE_CONVERSATION
-                );
-            } else {
-                context_menu_model.append(
-                    /// Translators: Context menu item
-                    ngettext(
-                        "_Delete conversation",
-                        "_Delete conversations",
-                        this.selected.size
-                    ),
-                    "win." + MainWindow.ACTION_DELETE_CONVERSATION
-                );
+            MainWindow? main = get_toplevel() as MainWindow;
+            if (main != null) {
+                if (main.is_shift_down) {
+                    context_menu_model.append(
+                        /// Translators: Context menu item
+                        ngettext(
+                            "Move conversation to _Trash",
+                            "Move conversations to _Trash",
+                            this.selected.size
+                        ),
+                        "win." + MainWindow.ACTION_ARCHIVE_CONVERSATION
+                    );
+                } else {
+                    context_menu_model.append(
+                        /// Translators: Context menu item
+                        ngettext(
+                            "_Delete conversation",
+                            "_Delete conversations",
+                            this.selected.size
+                        ),
+                        "win." + MainWindow.ACTION_DELETE_CONVERSATION
+                    );
+                }
             }
 
             if (conversation.is_unread())
diff --git a/src/client/conversation-list/formatted-conversation-data.vala 
b/src/client/conversation-list/formatted-conversation-data.vala
index fda44ad0..7dcfa162 100644
--- a/src/client/conversation-list/formatted-conversation-data.vala
+++ b/src/client/conversation-list/formatted-conversation-data.vala
@@ -103,6 +103,8 @@ public class FormattedConversationData : Geary.BaseObject {
     public int num_emails { get; set; }
     public Geary.Email? preview { get; private set; default = null; }
 
+    private Application.Configuration config;
+
     private Geary.App.Conversation? conversation = null;
     private Gee.List<Geary.RFC822.MailboxAddress>? account_owner_emails = null;
     private bool use_to = true;
@@ -111,9 +113,12 @@ public class FormattedConversationData : Geary.BaseObject {
     private Participants participants = Participants(){markup = null};
 
     // Creates a formatted message data from an e-mail.
-    public FormattedConversationData(Geary.App.Conversation conversation, Geary.Email preview,
-        Geary.Folder folder, Gee.List<Geary.RFC822.MailboxAddress> account_owner_emails) {
-
+    public FormattedConversationData(Application.Configuration config,
+                                     Geary.App.Conversation conversation,
+                                     Geary.Email preview,
+                                     Geary.Folder folder,
+                                     Gee.List<Geary.RFC822.MailboxAddress> account_owner_emails) {
+        this.config = config;
         this.conversation = conversation;
         this.account_owner_emails = account_owner_emails;
         use_to = (folder != null) && folder.special_folder_type.is_outgoing();
@@ -150,7 +155,7 @@ public class FormattedConversationData : Geary.BaseObject {
         // Date:
         string new_date = Util.Date.pretty_print(
             latest.properties.date_received.to_local(),
-            GearyApplication.instance.config.clock_format
+            this.config.clock_format
         );
         if (new_date == date)
             return false;
@@ -161,7 +166,8 @@ public class FormattedConversationData : Geary.BaseObject {
     }
 
     // Creates an example message (used internally for styling calculations.)
-    public FormattedConversationData.create_example() {
+    public FormattedConversationData.create_example(Application.Configuration config) {
+        this.config = config;
         this.is_unread = false;
         this.is_flagged = false;
         this.date = STYLE_EXAMPLE;
@@ -287,7 +293,7 @@ public class FormattedConversationData : Geary.BaseObject {
     private void render_internal(Gtk.Widget widget, Gdk.Rectangle? cell_area,
         Cairo.Context? ctx, Gtk.CellRendererState flags, bool recalc_dims,
         bool hover_select) {
-        bool display_preview = GearyApplication.instance.config.display_preview;
+        bool display_preview = this.config.display_preview;
         int y = LINE_SPACING + (cell_area != null ? cell_area.y : 0);
 
         bool selected = (flags & Gtk.CellRendererState.SELECTED) != 0;
diff --git a/src/client/folder-list/folder-list-folder-entry.vala 
b/src/client/folder-list/folder-list-folder-entry.vala
index 3eda0554..a047ce0e 100644
--- a/src/client/folder-list/folder-list-folder-entry.vala
+++ b/src/client/folder-list/folder-list-folder-entry.vala
@@ -99,12 +99,13 @@ public class FolderList.FolderEntry : FolderList.AbstractFolderEntry, Sidebar.In
         is_emphasized_changed(has_new);
     }
 
-    public bool internal_drop_received(Gdk.DragContext context, Gtk.SelectionData data) {
+    public bool internal_drop_received(MainWindow main_window,
+                                       Gdk.DragContext context,
+                                       Gtk.SelectionData data) {
         // Copy or move?
         Gdk.ModifierType mask;
         double[] axes = new double[2];
         context.get_device().get_state(context.get_dest_window(), axes, out mask);
-        MainWindow main_window = GearyApplication.instance.controller.main_window;
         if ((mask & Gdk.ModifierType.CONTROL_MASK) != 0) {
             main_window.folder_list.copy_conversation(folder);
         } else {
diff --git a/src/client/folder-list/folder-list-search-branch.vala 
b/src/client/folder-list/folder-list-search-branch.vala
index dc3c9ad4..a6fb4b0e 100644
--- a/src/client/folder-list/folder-list-search-branch.vala
+++ b/src/client/folder-list/folder-list-search-branch.vala
@@ -18,9 +18,18 @@ public class FolderList.SearchBranch : Sidebar.RootOnlyBranch {
 }
 
 public class FolderList.SearchEntry : FolderList.AbstractFolderEntry {
+
+    private int account_count = 0;
+
     public SearchEntry(Geary.SearchFolder folder) {
         base(folder);
 
+        try {
+            this.account_count = Geary.Engine.instance.get_accounts().size;
+        } catch (GLib.Error error) {
+            debug("Failed to get account count: %s", error.message);
+        }
+
         Geary.Engine.instance.account_available.connect(on_accounts_changed);
         Geary.Engine.instance.account_unavailable.connect(on_accounts_changed);
         folder.properties.notify[Geary.FolderProperties.PROP_NAME_EMAIL_TOTAL].connect(
@@ -35,8 +44,9 @@ public class FolderList.SearchEntry : FolderList.AbstractFolderEntry {
     }
 
     public override string get_sidebar_name() {
-        return GearyApplication.instance.controller.get_num_accounts() == 1 ? _("Search") :
-            _("Search %s account").printf(folder.account.information.display_name);
+        return this.account_count == 1
+        ? _("Search")
+        : _("Search %s account").printf(folder.account.information.display_name);
     }
 
     public override string? get_sidebar_tooltip() {
@@ -55,6 +65,12 @@ public class FolderList.SearchEntry : FolderList.AbstractFolderEntry {
     private void on_accounts_changed() {
         sidebar_name_changed(get_sidebar_name());
         sidebar_tooltip_changed(get_sidebar_tooltip());
+
+        try {
+            this.account_count = Geary.Engine.instance.get_accounts().size;
+        } catch (GLib.Error error) {
+            debug("Failed to get account count: %s", error.message);
+        }
     }
 
     private void on_email_total_changed() {
diff --git a/src/client/sidebar/sidebar-entry.vala b/src/client/sidebar/sidebar-entry.vala
index fa1baac5..ae78261a 100644
--- a/src/client/sidebar/sidebar-entry.vala
+++ b/src/client/sidebar/sidebar-entry.vala
@@ -56,7 +56,7 @@ public interface Sidebar.DestroyableEntry : Sidebar.Entry {
 
 public interface Sidebar.InternalDropTargetEntry : Sidebar.Entry {
     // Returns true if drop was successful
-    public abstract bool internal_drop_received(Gdk.DragContext context, Gtk.SelectionData data);
+    public abstract bool internal_drop_received(MainWindow main, Gdk.DragContext context, Gtk.SelectionData 
data);
 }
 
 public interface Sidebar.InternalDragSourceEntry : Sidebar.Entry {
diff --git a/src/client/sidebar/sidebar-tree.vala b/src/client/sidebar/sidebar-tree.vala
index 6a61b3c1..58afb195 100644
--- a/src/client/sidebar/sidebar-tree.vala
+++ b/src/client/sidebar/sidebar-tree.vala
@@ -1069,7 +1069,14 @@ public class Sidebar.Tree : Gtk.TreeView {
             return;
         }
 
-        bool success = targetable.internal_drop_received(context, selection_data);
+        bool success = false;
+
+        MainWindow? main = get_toplevel() as MainWindow;
+        if (main != null) {
+            success = targetable.internal_drop_received(
+                main, context, selection_data
+            );
+        }
 
         Gtk.drag_finish(context, success, false, time);
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]