[gitg/wip/albfan/history-panel-shorcuts: 2/2] Add shortcuts for history panels



commit f2c07f0ef63bb946fcad03e928c56771b65fc83d
Author: Alberto Fanjul <albertofanjul gmail com>
Date:   Wed Feb 5 01:42:36 2020 +0100

    Add shortcuts for history panels
    
    Allow each element to define its own shortcut

 gitg/gitg-application.vala           | 38 +++++++++++++++++++++++++++++++++++-
 gitg/gitg-window.vala                |  7 +++++++
 gitg/history/gitg-history.vala       | 26 ++++++++++++++++++++++--
 gitg/resources/ui/gitg-shortcuts.ui  |  6 ++++++
 libgitg-ext/gitg-ext-ui-element.vala | 11 +++++++++++
 plugins/diff/gitg-diff.vala          |  5 +++++
 plugins/files/gitg-files.vala        |  5 +++++
 7 files changed, 95 insertions(+), 3 deletions(-)
---
diff --git a/gitg/gitg-application.vala b/gitg/gitg-application.vala
index 527deddc..5b8c8e24 100644
--- a/gitg/gitg-application.vala
+++ b/gitg/gitg-application.vala
@@ -281,7 +281,24 @@ public class Application : Gtk.Application
                // Create shortcuts window if needed
                if (d_shortcuts == null)
                {
-                       d_shortcuts = Builder.load_object<Gtk.ShortcutsWindow>("ui/gitg-shortcuts.ui", 
"shortcuts-gitg");
+                       var shortcuts_ui_objects = GitgExt.UI.from_builder("ui/gitg-shortcuts.ui", 
"shortcuts-gitg", "history-shortcuts-group");
+                       d_shortcuts = (Gtk.ShortcutsWindow) shortcuts_ui_objects["shortcuts-gitg"];
+
+                       if(plugins_accel != null)
+                       {
+                               var history_shortcuts_group = (Gtk.ShortcutsGroup) 
shortcuts_ui_objects["history-shortcuts-group"];
+                               history_shortcuts_group.set_visible(true);
+
+                               foreach(var element in plugins_accel)
+                               {
+                                       var shortcut = (Gtk.ShortcutsShortcut) 
Object.new(typeof(Gtk.ShortcutsShortcut),
+                                                       "title", element.name,
+                                                       "accelerator", "<Alt>" + element.shortcut,
+                                                       null);
+                                       shortcut.set_visible(true);
+                                       history_shortcuts_group.add(shortcut);
+                               }
+                       }
 
                        d_shortcuts.destroy.connect((w) => {
                                d_shortcuts = null;
@@ -336,6 +353,14 @@ public class Application : Gtk.Application
                string[] accels;
        }
 
+       struct PluginAccel
+       {
+               string name;
+               string shortcut;
+       }
+
+       private List<PluginAccel?> plugins_accel;
+
        private void init_error(string msg)
        {
                var dlg = new Gtk.MessageDialog(null,
@@ -570,6 +595,17 @@ public class Application : Gtk.Application
                w.set_environment(app_command_line.get_environ());
                w.present(activity, command_lines);
        }
+
+       public void register_shortcut(string name, uint shortcut)
+       {
+               if(plugins_accel == null)
+               {
+                       plugins_accel = new List<PluginAccel?>();
+               }
+
+               PluginAccel plugin_accel = { name, Gdk.keyval_name(shortcut) };
+               plugins_accel.append(plugin_accel);
+       }
 }
 
 }
diff --git a/gitg/gitg-window.vala b/gitg/gitg-window.vala
index c3d93d9d..3ce9726d 100644
--- a/gitg/gitg-window.vala
+++ b/gitg/gitg-window.vala
@@ -944,6 +944,13 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable
                                                                             extset,
                                                                             d_stack_activities);
 
+               var history_panels = ((GitgHistory.Activity) builtins[0]).d_panels;
+
+               foreach(var element in history_panels.get_available_elements())
+               {
+                       app.register_shortcut(element.display_name, element.shortcut);
+               };
+
                d_activities.notify["current"].connect(on_current_activity_changed);
 
                // Setup window geometry saving
diff --git a/gitg/history/gitg-history.vala b/gitg/history/gitg-history.vala
index 38dfdd1e..02582e2e 100644
--- a/gitg/history/gitg-history.vala
+++ b/gitg/history/gitg-history.vala
@@ -58,7 +58,12 @@ namespace GitgHistory
                private string[] d_mainline;
                private bool d_ignore_external;
 
-               private Gitg.UIElements<GitgExt.HistoryPanel> d_panels;
+               private Gitg.UIElements<GitgExt.HistoryPanel> _d_panels;
+
+               public Gitg.UIElements<GitgExt.HistoryPanel> d_panels
+               {
+                       get { return _d_panels; }
+               }
 
                public Activity(GitgExt.Application application)
                {
@@ -552,6 +557,23 @@ namespace GitgHistory
                        });
                }
 
+               public bool on_key_pressed (Gdk.EventKey event) {
+                       var mmask = Gtk.accelerator_get_default_mod_mask();
+
+                       if ((mmask & event.state) == Gdk.ModifierType.MOD1_MASK)
+                       {
+                               foreach(var element in d_panels.get_available_elements()) {
+                                        GitgExt.HistoryPanel panel = (GitgExt.HistoryPanel)element;
+                                       uint? key = panel.shortcut;
+                                       if (key != null && key == Gdk.keyval_to_lower(event.keyval)) {
+                                               panel.activate();
+                                               return true;
+                                       }
+                               };
+                       }
+                       return false;
+               }
+
                private void build_ui()
                {
                        d_main = new Paned();
@@ -583,7 +605,7 @@ namespace GitgHistory
                                                           "application",
                                                           application);
 
-                       d_panels = new Gitg.UIElements<GitgExt.HistoryPanel>(extset,
+                       _d_panels = new Gitg.UIElements<GitgExt.HistoryPanel>(extset,
                                                                             d_main.stack_panel);
 
                        d_refs_list_popup = new Gitg.PopupMenu(d_main.refs_list);
diff --git a/gitg/resources/ui/gitg-shortcuts.ui b/gitg/resources/ui/gitg-shortcuts.ui
index 319535e0..10c8b0d3 100644
--- a/gitg/resources/ui/gitg-shortcuts.ui
+++ b/gitg/resources/ui/gitg-shortcuts.ui
@@ -187,6 +187,12 @@
             </child>
           </object>
         </child>
+        <child>
+          <object class="GtkShortcutsGroup" id="history-shortcuts-group">
+            <property name="visible">false</property>
+            <property name="title" translatable="yes" context="shortcut window">History Activity</property>
+          </object>
+        </child>
       </object>
     </child>
   </object>
diff --git a/libgitg-ext/gitg-ext-ui-element.vala b/libgitg-ext/gitg-ext-ui-element.vala
index 8230172f..bdab4567 100644
--- a/libgitg-ext/gitg-ext-ui-element.vala
+++ b/libgitg-ext/gitg-ext-ui-element.vala
@@ -80,6 +80,17 @@ public interface UIElement : Object
                owned get { return null; }
        }
 
+       /**
+        * The ui element shortcut.
+        *
+        * If provided, the key to mix with Gdk.ModifierType.MOD1_MASK to enable
+        * this element
+        */
+       public virtual uint? shortcut
+       {
+               owned get { return null; }
+       }
+
        /**
         * Check whether the ui element is available in the current application state.
         *
diff --git a/plugins/diff/gitg-diff.vala b/plugins/diff/gitg-diff.vala
index 51d18706..324780fb 100644
--- a/plugins/diff/gitg-diff.vala
+++ b/plugins/diff/gitg-diff.vala
@@ -32,6 +32,11 @@ namespace GitgDiff
 
                private ulong d_selection_changed_id;
 
+               public virtual uint? shortcut
+               {
+                       owned get { return Gdk.Key.d; }
+               }
+
                protected override void constructed()
                {
                        base.constructed();
diff --git a/plugins/files/gitg-files.vala b/plugins/files/gitg-files.vala
index 9adb63d2..b878fc7a 100644
--- a/plugins/files/gitg-files.vala
+++ b/plugins/files/gitg-files.vala
@@ -48,6 +48,11 @@ namespace GitgFiles
                        history.selection_changed.connect(on_selection_changed);
                }
 
+               public virtual uint? shortcut
+               {
+                       owned get { return Gdk.Key.f; }
+               }
+
                public string id
                {
                        owned get { return "/org/gnome/gitg/Panels/Files"; }


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