[Xfce4-commits] <thunar:jannis/file-menu-custom-actions> Initial work on adding custom and other actions to the file menu.

Jannis Pohlmann noreply at xfce.org
Mon Nov 7 03:30:02 CET 2011


Updating branch refs/heads/jannis/file-menu-custom-actions
         to 36dea6078ac20c1285342ff1d7d09a212e183f5b (commit)
       from b41afa0c255c50d92025540fd072a642c2158d02 (commit)

commit 36dea6078ac20c1285342ff1d7d09a212e183f5b
Author: Jannis Pohlmann <jannis at xfce.org>
Date:   Mon Nov 7 03:25:36 2011 +0100

    Initial work on adding custom and other actions to the file menu.
    
    Adding more items to the file menu would allow users to define keyboard
    shortcuts for them. Unfortunately, the action names generated for menu
    items from plugins are persistent only as long as the order does not
    change. We need to find a better unique name for them, so that adding
    them with gtk_action_group_add_action_with_accel() generates a unique
    AND persistent accel path.

 plugins/thunar-uca/thunar-uca-provider.c |    1 +
 thunar/thunar-launcher.c                 |    4 +-
 thunar/thunar-standard-view-ui.xml       |   13 +++-
 thunar/thunar-standard-view.c            |    8 ++
 thunar/thunar-window-ui.xml              |    9 ++-
 thunar/thunar-window.c                   |  119 ++++++++++++++++++++++++++++++
 6 files changed, 148 insertions(+), 6 deletions(-)

diff --git a/plugins/thunar-uca/thunar-uca-provider.c b/plugins/thunar-uca/thunar-uca-provider.c
index 21e3c24..674af2f 100644
--- a/plugins/thunar-uca/thunar-uca-provider.c
+++ b/plugins/thunar-uca/thunar-uca-provider.c
@@ -216,6 +216,7 @@ thunar_uca_provider_get_file_actions (ThunarxMenuProvider *menu_provider,
                               -1);
 
           /* generate a unique action name */
+          /* FIXME this name is persistent only if actions are not re-ordered in the GUI */
           name = g_strdup_printf ("ThunarUca::action-%d", ++uca_provider->last_action_id);
 
           /* create the new action with the given parameters */
diff --git a/thunar/thunar-launcher.c b/thunar/thunar-launcher.c
index f479cf9..141ce03 100644
--- a/thunar/thunar-launcher.c
+++ b/thunar/thunar-launcher.c
@@ -770,7 +770,7 @@ thunar_launcher_update (ThunarLauncher *launcher)
   /* update the user interface depending on the current selection */
   if (G_LIKELY (n_selected_files == 0 || n_directories > 0))
     {
-      /** CASE 1: nothing selected or atleast one directory in the selection
+      /** CASE 1: nothing selected or at least one directory in the selection
        **
        ** - "Open" and "Open in n New Windows" actions
        **/
@@ -793,7 +793,7 @@ thunar_launcher_update (ThunarLauncher *launcher)
         }
       else
         {
-          /* the "Open" action is sensitive if we have atleast one selected file,
+          /* the "Open" action is sensitive if we have at least one selected file,
            * the label is set to "Open in New Window" if we're not in a regular
            * view (i.e. current_directory is not set) and have only one directory
            * selected to reflect that this action will open a new window.
diff --git a/thunar/thunar-standard-view-ui.xml b/thunar/thunar-standard-view-ui.xml
index 3892f3a..896131c 100644
--- a/thunar/thunar-standard-view-ui.xml
+++ b/thunar/thunar-standard-view-ui.xml
@@ -1,9 +1,8 @@
 <ui>
 
   <!--
-    $Id$
-
     Copyright (c) 2005-2006 Benedikt Meurer <benny at xfce.org>
+    Copyright (c) 2011 Jannis Pohlmann <jannis at xfce.org>
 
     Thunar standard view user interface description file. Do NOT
     simply edit this file if you don't know how the whole system
@@ -16,6 +15,16 @@
         <menuitem action="create-folder" />
         <menuitem action="create-document" />
       </placeholder>
+      <placeholder name="placeholder-clipboard-actions">
+        <menuitem action="cut" />
+        <menuitem action="copy" />
+        <menuitem action="paste-into-folder" />
+        <menuitem action="delete" />
+      </placeholder>
+      <placeholder name="placeholder-edit-actions">
+        <menuitem action="rename" />
+        <menuitem action="restore" />
+      </placeholder>
       <placeholder name="placeholder-file-properties">
         <menuitem action="properties" />
       </placeholder>
diff --git a/thunar/thunar-standard-view.c b/thunar/thunar-standard-view.c
index 2478d7e..68d17a2 100644
--- a/thunar/thunar-standard-view.c
+++ b/thunar/thunar-standard-view.c
@@ -1770,6 +1770,14 @@ thunar_standard_view_merge_custom_actions (ThunarStandardView *standard_view,
           /* add the action to the UI manager */
           if (G_LIKELY (selected_items != NULL))
             {
+              /* add to the file menu */
+              gtk_ui_manager_add_ui (standard_view->ui_manager,
+                                     standard_view->priv->custom_merge_id,
+                                     "/file-menu/placeholder-custom-actions",
+                                     gtk_action_get_name (GTK_ACTION (lp->data)),
+                                     gtk_action_get_name (GTK_ACTION (lp->data)),
+                                     GTK_UI_MANAGER_MENUITEM, FALSE);
+
               /* add to the file context menu */
               gtk_ui_manager_add_ui (standard_view->ui_manager,
                                      standard_view->priv->custom_merge_id,
diff --git a/thunar/thunar-window-ui.xml b/thunar/thunar-window-ui.xml
index f88332e..8e271b9 100644
--- a/thunar/thunar-window-ui.xml
+++ b/thunar/thunar-window-ui.xml
@@ -1,9 +1,8 @@
 <ui>
 
   <!--
-    $Id$
-
     Copyright (c) 2005-2006 Benedikt Meurer <benny at xfce.org>
+    Copyright (c) 2011 Jannis Pohlmann <jannis at xfce.org>
 
     Thunar main window user interface description file. Do NOT
     simply edit this file if you don't know how the whole system
@@ -22,6 +21,12 @@
         <placeholder name="placeholder-sendto-actions" />
       </menu>
       <separator />
+      <placeholder name="placeholder-clipboard-actions" />
+      <separator />
+      <placeholder name="placeholder-edit-actions" />
+      <separator />
+      <placeholder name="placeholder-custom-actions" />
+      <separator />
       <placeholder name="placeholder-file-properties" />
       <separator />
       <menuitem action="empty-trash" />
diff --git a/thunar/thunar-window.c b/thunar/thunar-window.c
index a42d982..7bd2410 100644
--- a/thunar/thunar-window.c
+++ b/thunar/thunar-window.c
@@ -198,6 +198,7 @@ static void     thunar_window_menu_item_selected          (GtkWidget
                                                            ThunarWindow           *window);
 static void     thunar_window_menu_item_deselected        (GtkWidget              *menu_item,
                                                            ThunarWindow           *window);
+static void     thunar_window_update_custom_actions       (ThunarWindow           *window);
 static void     thunar_window_notify_loading              (ThunarView             *view,
                                                            GParamSpec             *pspec,
                                                            ThunarWindow           *window);
@@ -251,6 +252,10 @@ struct _ThunarWindow
   GClosure               *menu_item_selected_closure;
   GClosure               *menu_item_deselected_closure;
 
+  /* custom menu actions for the file menu */
+  GtkActionGroup         *custom_actions;
+  guint                   custom_merge_id;
+
   GtkWidget              *table;
   GtkWidget              *throbber;
   GtkWidget              *paned;
@@ -939,6 +944,10 @@ thunar_window_finalize (GObject *object)
 {
   ThunarWindow *window = THUNAR_WINDOW (object);
 
+  /* drop the custom actions */
+  if (window->custom_actions != NULL)
+    g_object_unref (window->custom_actions);
+
   /* drop our references on the menu_item_selected()/menu_item_deselected() closures */
   g_closure_unref (window->menu_item_deselected_closure);
   g_closure_unref (window->menu_item_selected_closure);
@@ -1920,6 +1929,10 @@ thunar_window_action_view_changed (GtkRadioAction *action,
       if (G_LIKELY (window->statusbar != NULL))
         exo_binding_new (G_OBJECT (window->view), "statusbar-text", G_OBJECT (window->statusbar), "text");
 
+      /* update menu items whenever the selected files change */
+      g_signal_connect_swapped (window->view, "notify::selected-files",
+                                G_CALLBACK (thunar_window_update_custom_actions), window);
+
       /* scroll to the previously visible file in the old view */
       if (G_UNLIKELY (file != NULL))
         thunar_view_scroll_to_file (THUNAR_VIEW (window->view), file, FALSE, TRUE, 0.0f, 0.0f);
@@ -2574,6 +2587,112 @@ thunar_window_menu_item_deselected (GtkWidget    *menu_item,
 
 
 static void
+thunar_window_update_custom_actions (ThunarWindow *window)
+{
+  ThunarFile *folder;
+  GList      *selected_files;
+  GList      *actions = NULL;
+  GList      *lp;
+  GList      *providers;
+  GList      *tmp;
+
+  _thunar_return_if_fail (THUNAR_IS_WINDOW (window));
+
+  /* get a list of selected files */
+  selected_files = thunar_component_get_selected_files (THUNAR_COMPONENT (window->view));
+
+  /* load the menu provides from the provider factory */
+  providers = thunarx_provider_factory_list_providers (window->provider_factory,
+                                                       THUNARX_TYPE_MENU_PROVIDER);
+  if (G_LIKELY (providers != NULL))
+    {
+      /* grab a reference to the current directory of the window */
+      folder = thunar_window_get_current_directory (window);
+
+      /* load the actions offered by the menu providers */
+      for (lp = providers; lp != NULL; lp = lp->next)
+        {
+          if (G_LIKELY (selected_files != NULL))
+            {
+              tmp = thunarx_menu_provider_get_file_actions (lp->data,
+                                                            GTK_WIDGET (window),
+                                                            selected_files);
+            }
+          else if (G_LIKELY (folder != NULL))
+            {
+              tmp =
+                thunarx_menu_provider_get_folder_actions (lp->data,
+                                                          GTK_WIDGET (window),
+                                                          THUNARX_FILE_INFO (folder));
+            }
+          else
+            {
+              tmp = NULL;
+            }
+          actions = g_list_concat (actions, tmp);
+          g_object_unref (G_OBJECT (lp->data));
+        }
+      g_list_free (providers);
+    }
+
+  /* remove previously inserted menu actions from the UI manager */
+  if (window->custom_merge_id != 0)
+    {
+      gtk_ui_manager_remove_ui (window->ui_manager, window->custom_merge_id);
+      gtk_ui_manager_ensure_update (window->ui_manager);
+      window->custom_merge_id = 0;
+    }
+
+  /* drop any previous custom action group */
+  if (window->custom_actions != NULL)
+    {
+      gtk_ui_manager_remove_action_group (window->ui_manager, window->custom_actions);
+      g_object_unref (window->custom_actions);
+      window->custom_actions = NULL;
+    }
+
+  /* add the actions specified by the menu providers */
+  if (G_LIKELY (actions != NULL))
+    {
+      /* allocate the action group and the merge id for the custom actions */
+      window->custom_actions = gtk_action_group_new ("thunar-window-custom-actions");
+      window->custom_merge_id = gtk_ui_manager_new_merge_id (window->ui_manager);
+      gtk_ui_manager_insert_action_group (window->ui_manager, window->custom_actions,
+                                          -1);
+
+      /* add the actions to the UI manager */
+      for (lp = actions; lp != NULL; lp = lp->next)
+        {
+          /* add the action to the action group */
+          gtk_action_group_add_action_with_accel (window->custom_actions,
+                                                  GTK_ACTION (lp->data),
+                                                  NULL);
+
+          /* add the action to the UI manager */
+          /* add to the file menu */
+          gtk_ui_manager_add_ui (window->ui_manager,
+                                 window->custom_merge_id,
+                                 "/main-menu/file-menu/placeholder-custom-actions",
+                                 gtk_action_get_name (GTK_ACTION (lp->data)),
+                                 gtk_action_get_name (GTK_ACTION (lp->data)),
+                                 GTK_UI_MANAGER_MENUITEM, FALSE);
+
+          /* release the reference on the action */
+          g_object_unref (G_OBJECT (lp->data));
+        }
+
+      /* be sure to update the UI manager to avoid flickering */
+      gtk_ui_manager_ensure_update (window->ui_manager);
+
+      /* cleanup */
+      g_list_free (actions);
+
+    }
+}
+
+
+
+static void
 thunar_window_notify_loading (ThunarView   *view,
                               GParamSpec   *pspec,
                               ThunarWindow *window)


More information about the Xfce4-commits mailing list