Skip to content

Commit

Permalink
Add shortcuts for history panels
Browse files Browse the repository at this point in the history
Allow each element to define its own shortcut
  • Loading branch information
albfan committed Jun 6, 2020
1 parent 408f96b commit f2c07f0
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
38 changes: 37 additions & 1 deletion gitg/gitg-application.vala
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}

}
Expand Down
7 changes: 7 additions & 0 deletions gitg/gitg-window.vala
Expand Up @@ -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
Expand Down
26 changes: 24 additions & 2 deletions gitg/history/gitg-history.vala
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions gitg/resources/ui/gitg-shortcuts.ui
Expand Up @@ -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>
Expand Down
11 changes: 11 additions & 0 deletions libgitg-ext/gitg-ext-ui-element.vala
Expand Up @@ -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.
*
Expand Down
5 changes: 5 additions & 0 deletions plugins/diff/gitg-diff.vala
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions plugins/files/gitg-files.vala
Expand Up @@ -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"; }
Expand Down

0 comments on commit f2c07f0

Please sign in to comment.