[file-roller/wip/jtojnar/gtk4: 3/3] wip: Try to port GtkMenu to GMenu




commit c846956252238a934d431cb909f83ac19df1faee
Author: Jan Tojnar <jtojnar gmail com>
Date:   Sun Dec 12 23:21:36 2021 +0100

    wip: Try to port GtkMenu to GMenu

 src/fr-file-selector-dialog.c | 62 ++++++++++++++++++++++++++++---------------
 src/ui/file-selector.ui       | 51 +++++++++++++----------------------
 2 files changed, 58 insertions(+), 55 deletions(-)
---
diff --git a/src/fr-file-selector-dialog.c b/src/fr-file-selector-dialog.c
index 5c4e036a..384fcee6 100644
--- a/src/fr-file-selector-dialog.c
+++ b/src/fr-file-selector-dialog.c
@@ -574,7 +574,9 @@ files_treeview_button_press_event_cb (GtkWidget      *widget,
        FrFileSelectorDialog *self = user_data;
 
        if (event->button == 3) {
-               gtk_menu_popup_at_pointer (GTK_MENU (GET_WIDGET ("file_list_context_menu")),  (GdkEvent *) 
event);
+               GtkWidget *popover = GET_WIDGET ("file_list_context_menu");
+               gtk_widget_set_parent(popover, widget);
+               gtk_popover_popdown (GTK_POPOVER (popover));
 
                return TRUE;
        }
@@ -604,16 +606,18 @@ select_all_files (FrFileSelectorDialog *self,
 
 
 static void
-select_all_menuitem_activate_cb (GtkMenuItem *menu_item,
-                                gpointer     user_data)
+select_all_activate_cb (GSimpleAction *action,
+                         GVariant *state,
+                         gpointer user_data)
 {
        select_all_files (FR_FILE_SELECTOR_DIALOG (user_data), TRUE);
 }
 
 
 static void
-unselect_all_menuitem_activate_cb (GtkMenuItem *menu_item,
-                                  gpointer     user_data)
+unselect_all_activate_cb (GSimpleAction *action,
+                                  GVariant *state,
+                                   gpointer user_data)
 {
        select_all_files (FR_FILE_SELECTOR_DIALOG (user_data), FALSE);
 }
@@ -626,14 +630,15 @@ _set_current_folder (FrFileSelectorDialog *self,
 
 
 static void
-show_hidden_files_menuitem_toggled_cb (GtkCheckMenuItem *checkmenuitem,
-                                      gpointer          user_data)
+show_hidden_files_toggled_cb (GSimpleAction *action,
+                                      GVariant *state,
+                                       gpointer user_data)
 {
        FrFileSelectorDialog *self = user_data;
        GFile                *folder;
        GList                *selected_files;
 
-       self->show_hidden = gtk_check_menu_item_get_active (checkmenuitem);
+       self->show_hidden = g_variant_get_boolean (state);
        folder = fr_file_selector_dialog_get_current_folder (self);
        selected_files = fr_file_selector_dialog_get_selected_files (self);
        _set_current_folder (self, folder, selected_files);
@@ -643,6 +648,26 @@ show_hidden_files_menuitem_toggled_cb (GtkCheckMenuItem *checkmenuitem,
 }
 
 
+static void
+activate_toggle (GSimpleAction *action,
+                 GVariant      *parameter,
+                 gpointer       user_data)
+{
+       GVariant *state;
+
+       state = g_action_get_state (G_ACTION (action));
+       g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
+       g_variant_unref (state);
+}
+
+
+static GActionEntry win_entries[] = {
+  { "select-all", select_all_activate_cb, NULL, NULL, NULL },
+  { "deselect-all", unselect_all_activate_cb, NULL, NULL, NULL },
+  { "show-hidden", activate_toggle, NULL, "false", show_hidden_files_toggled_cb }
+};
+
+
 static void
 fr_file_selector_dialog_init (FrFileSelectorDialog *self)
 {
@@ -660,8 +685,6 @@ fr_file_selector_dialog_init (FrFileSelectorDialog *self)
        gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (GET_WIDGET ("files_liststore")), 
FILE_LIST_COLUMN_MODIFIED, files_modified_column_sort_func, self, NULL);
        gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (GET_WIDGET ("files_liststore")), 
FILE_LIST_COLUMN_NAME, GTK_SORT_ASCENDING);
 
-       gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (GET_WIDGET ("show_hidden_files_menuitem")), 
self->show_hidden);
-
        g_signal_connect (GET_WIDGET ("is_selected_cellrenderertoggle"),
                          "toggled",
                          G_CALLBACK (is_selected_cellrenderertoggle_toggled_cb),
@@ -682,18 +705,13 @@ fr_file_selector_dialog_init (FrFileSelectorDialog *self)
                          "button-press-event",
                          G_CALLBACK (files_treeview_button_press_event_cb),
                          self);
-       g_signal_connect (GET_WIDGET ("select_all_menuitem"),
-                         "activate",
-                         G_CALLBACK (select_all_menuitem_activate_cb),
-                         self);
-       g_signal_connect (GET_WIDGET ("unselect_all_menuitem"),
-                         "activate",
-                         G_CALLBACK (unselect_all_menuitem_activate_cb),
-                         self);
-       g_signal_connect (GET_WIDGET ("show_hidden_files_menuitem"),
-                         "toggled",
-                         G_CALLBACK (show_hidden_files_menuitem_toggled_cb),
-                         self);
+
+       // TODO: this fails with “invalid cast from 'FrFileSelectorDialog' to 'GActionMap'” since GtkDialog 
is not GActionMap
+       g_action_map_add_action_entries (G_ACTION_MAP (self),
+                       win_entries, G_N_ELEMENTS (win_entries),
+                       self);
+
+       g_simple_action_set_state (G_SIMPLE_ACTION (g_action_map_lookup_action (G_ACTION_MAP (self), 
"win.show-hidden")), g_variant_new_boolean (self->show_hidden));
 
        _fr_file_selector_dialog_update_size (self);
        gtk_widget_grab_focus (GET_WIDGET ("files_treeview"));
diff --git a/src/ui/file-selector.ui b/src/ui/file-selector.ui
index 5bd32864..1a16c84f 100644
--- a/src/ui/file-selector.ui
+++ b/src/ui/file-selector.ui
@@ -1,39 +1,24 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <!-- interface-requires gtk+ 3.10 -->
-  <object class="GtkMenu" id="file_list_context_menu">
-    <property name="visible">True</property>
-    <property name="can_focus">False</property>
-    <child>
-      <object class="GtkMenuItem" id="select_all_menuitem">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="label" translatable="yes">_Select All</property>
-        <property name="use_underline">True</property>
-      </object>
-    </child>
-    <child>
-      <object class="GtkMenuItem" id="unselect_all_menuitem">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="label" translatable="yes">Dese_lect All</property>
-        <property name="use_underline">True</property>
-      </object>
-    </child>
-    <child>
-      <object class="GtkSeparatorMenuItem" id="menuitem3">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-      </object>
-    </child>
-    <child>
-      <object class="GtkCheckMenuItem" id="show_hidden_files_menuitem">
-        <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="label" translatable="yes">Show Hidden Files</property>
-      </object>
-    </child>
-  </object>
+  <menu id="file_list_context_menu_model">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Select All</attribute>
+        <attribute name="action">win.select-all</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Dese_lect All</attribute>
+        <attribute name="action">win.deselect-all</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">Show Hidden Files</attribute>
+        <attribute name="action">app.save</attribute>
+      </item>
+    </section>
+  </menu>
   <object class="GtkListStore" id="files_liststore">
     <columns>
       <!-- column-name icon -->


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