[balsa/popover] mailbox-node: Fix open and close



commit f99796f7e9189a1cf6f9b551d2908ff03d806a5a
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Tue May 12 14:44:59 2020 -0400

    mailbox-node: Fix open and close
    
    They both must be in the menu, but one should be disabled.

 src/mailbox-node.c | 69 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 55 insertions(+), 14 deletions(-)
---
diff --git a/src/mailbox-node.c b/src/mailbox-node.c
index e0b80018f..d572e9356 100644
--- a/src/mailbox-node.c
+++ b/src/mailbox-node.c
@@ -102,7 +102,8 @@ struct _BalsaMailboxNode {
     int delim; /* IMAP delimiter so that we do not need to check it
                 * too often. */
 
-    GtkWidget *context_menu_popover;
+    GtkWidget *context_menu;
+    GtkWidget *relative_to;
 
     unsigned subscribed:1;     /* Used only by remote */
     unsigned list_inbox:1;     /* Used only by remote */
@@ -955,6 +956,26 @@ delete_activated(GSimpleAction *action,
         folder_conf_delete(mbnode);
 }
 
+static void
+context_menu_set_enabled(BalsaMailboxNode *mbnode)
+{
+    gboolean is_open;
+    GActionGroup *action_group;
+    GActionMap *action_map;
+    GAction *action;
+
+    action_group = gtk_widget_get_action_group(mbnode->relative_to, "mbnode");
+    action_map = G_ACTION_MAP(action_group);
+
+    is_open = MAILBOX_OPEN(mbnode->mailbox);
+
+    action = g_action_map_lookup_action(action_map, "open");
+    g_simple_action_set_enabled(G_SIMPLE_ACTION(action), !is_open);
+
+    action = g_action_map_lookup_action(action_map, "close");
+    g_simple_action_set_enabled(G_SIMPLE_ACTION(action), is_open);
+}
+
 static void
 open_activated(GSimpleAction *action,
                GVariant      *parameter,
@@ -963,6 +984,7 @@ open_activated(GSimpleAction *action,
     BalsaMailboxNode *mbnode = user_data;
 
     balsa_mblist_open_mailbox(mbnode->mailbox);
+    context_menu_set_enabled(mbnode);
 }
 
 static void
@@ -973,6 +995,7 @@ close_activated(GSimpleAction *action,
     BalsaMailboxNode *mbnode = user_data;
 
     balsa_window_close_mbnode(balsa_app.main_window, mbnode);
+    context_menu_set_enabled(mbnode);
 }
 
 static void
@@ -1043,9 +1066,9 @@ filters_activated(GSimpleAction *action,
     }
 }
 
-static void
-create_context_menu_popover(BalsaMailboxNode *mbnode,
-                            GtkWidget        *relative_to)
+static GtkWidget *
+create_context_menu(BalsaMailboxNode *mbnode,
+                    GtkWidget        *relative_to)
 {
     static const GActionEntry entries[] = {
         {"add-mbox", add_mbox_activated},
@@ -1071,6 +1094,7 @@ create_context_menu_popover(BalsaMailboxNode *mbnode,
     GMenu *section;
     LibBalsaMailbox *mailbox;
     gboolean special;
+    GtkWidget *context_menu;
 
     simple = g_simple_action_group_new();
     g_action_map_add_action_entries(G_ACTION_MAP(simple),
@@ -1107,6 +1131,8 @@ create_context_menu_popover(BalsaMailboxNode *mbnode,
     if (mbnode == NULL) {/* clicked on the empty space */
         g_menu_append(section, _("_Rescan"), "mbnode.rescan");
     } else {
+        mbnode->relative_to = relative_to;
+
         /* If we didn't click on a mailbox node then there is only one option. */
         if (g_signal_has_handler_pending(mbnode,
                                          balsa_mailbox_node_signals
@@ -1116,16 +1142,15 @@ create_context_menu_popover(BalsaMailboxNode *mbnode,
         if (g_signal_has_handler_pending(mbnode,
                                          balsa_mailbox_node_signals[APPEND_SUBTREE],
                                          0, FALSE))
-            g_menu_append(section, _("_Rescan"), "rescan");
+            g_menu_append(section, _("_Rescan"), "mbnode.rescan");
 
         if (mbnode->config_prefix != NULL)
             g_menu_append(section, _("_Delete"), "mbnode.delete");
 
         if ((mailbox = mbnode->mailbox) != NULL) {
-            if (!MAILBOX_OPEN(mailbox))
-                g_menu_append(section, _("_Open"), "mbnode.open");
-            else
-                g_menu_append(section, _("_Close"), "mbnode.close");
+            g_menu_append(section, _("_Open"), "mbnode.open");
+            g_menu_append(section, _("_Close"), "mbnode.close");
+            context_menu_set_enabled(mbnode);
 
             special = (   mailbox == balsa_app.inbox
                        || mailbox == balsa_app.sentbox
@@ -1161,19 +1186,35 @@ create_context_menu_popover(BalsaMailboxNode *mbnode,
     g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
     g_object_unref(section);
 
-    mbnode->context_menu_popover =
-        gtk_popover_new_from_model(relative_to, G_MENU_MODEL(menu));
+    context_menu = gtk_popover_new_from_model(relative_to, G_MENU_MODEL(menu));
     g_object_unref(menu);
+
+    return context_menu;
 }
 
 GtkWidget *
 balsa_mailbox_node_get_context_menu(BalsaMailboxNode *mbnode,
                                     GtkWidget        *relative_to)
 {
-    if (mbnode->context_menu_popover == NULL)
-        create_context_menu_popover(mbnode, relative_to);
+    GtkWidget *context_menu;
+
+    if (mbnode == NULL) {
+        static GtkWidget *null_context_menu;
+
+        if (null_context_menu == NULL) {
+            null_context_menu =
+                create_context_menu(NULL, relative_to);
+        }
+        context_menu = null_context_menu;
+    } else {
+        if (mbnode->context_menu == NULL) {
+            mbnode->context_menu =
+                create_context_menu(mbnode, relative_to);
+        }
+        context_menu = mbnode->context_menu;
+    }
 
-    return mbnode->context_menu_popover;
+    return context_menu;
 }
 
 /* ---------------------------------------------------------------------


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