From 287e925cbcc9947dce002707b38bdb2ce6d47f0e Mon Sep 17 00:00:00 2001 From: Adrien Dorsaz Date: Mon, 1 Apr 2024 21:17:59 +0000 Subject: [PATCH] Add shortcuts and buttons to navigate through search results --- gitg/gitg-window.vala | 42 +++++++++++++++++++++++++ gitg/history/gitg-history.vala | 30 ++++++++++++++++++ gitg/resources/ui/gitg-shortcuts.ui | 14 +++++++++ gitg/resources/ui/gitg-window.ui | 47 +++++++++++++++++++++++++--- libgitg-ext/gitg-ext-searchable.vala | 2 ++ 5 files changed, 131 insertions(+), 4 deletions(-) diff --git a/gitg/gitg-window.vala b/gitg/gitg-window.vala index f77a7f56..76df26cd 100644 --- a/gitg/gitg-window.vala +++ b/gitg/gitg-window.vala @@ -90,6 +90,10 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable private unowned Gtk.SearchBar d_search_bar; [GtkChild] private unowned Gtk.SearchEntry d_search_entry; + [GtkChild] + private unowned Gtk.Button d_search_up_button; + [GtkChild] + private unowned Gtk.Button d_search_down_button; [GtkChild] private unowned Gtk.Stack d_main_stack; @@ -247,12 +251,25 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable d_search_entry.text = searchable.search_text; searchable.search_visible = true; searchable.search_entry = d_search_entry; + var has_text = d_search_entry.text.length > 0; + d_search_up_button.set_sensitive(has_text); + d_search_down_button.set_sensitive(has_text); } else { searchable.search_visible = false; searchable.search_entry = null; + d_search_up_button.set_sensitive(false); + d_search_down_button.set_sensitive(false); } + + var show_buttons = false; + if (current_activity is GitgExt.Searchable) + { + show_buttons = searchable.show_buttons(); + } + d_search_up_button.set_visible(show_buttons); + d_search_down_button.set_visible(show_buttons); } [GtkCallback] @@ -264,6 +281,9 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable if (ntext != searchable.search_text) { searchable.search_text = ntext; + var has_text = ntext.length > 0; + d_search_up_button.set_sensitive(has_text); + d_search_down_button.set_sensitive(has_text); } } @@ -278,6 +298,18 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable return ret; } + [GtkCallback] + private void search_up_clicked(Gtk.Button button) + { + search_move(true); + } + + [GtkCallback] + private void search_down_clicked(Gtk.Button button) + { + search_move(false); + } + construct { if (Gitg.PlatformSupport.use_native_window_controls()) @@ -1364,6 +1396,16 @@ public class Window : Gtk.ApplicationWindow, GitgExt.Application, Initable { owned get { return d_notifications; } } + + private void search_move(bool up) + { + if (current_activity is GitgExt.Searchable) + { + var searchable = current_activity as GitgExt.Searchable; + var key = d_search_entry.text; + searchable.search_move(key, up); + } + } } } diff --git a/gitg/history/gitg-history.vala b/gitg/history/gitg-history.vala index 5ef69bfd..3f4513a0 100644 --- a/gitg/history/gitg-history.vala +++ b/gitg/history/gitg-history.vala @@ -1257,6 +1257,36 @@ namespace GitgHistory public string search_text { owned get; set; default = ""; } public bool search_visible { get; set; } + + public override void search_move(string key, bool up) + { + // Move the tree selection by sending key press event, + // see: https://gitlab.gnome.org/GNOME/gtk/merge_requests/1167 + var search_entry = d_main.commit_list_view.get_search_entry(); + var keyval = up ? Gdk.Key.Up : Gdk.Key.Down; + + Gdk.KeymapKey[] keys; + if(!Gdk.Keymap.get_for_display(search_entry.get_display()).get_entries_for_keyval(keyval, out keys)) + { + return; + } + + search_entry.grab_focus(); + + Gdk.EventKey* event = new Gdk.Event(Gdk.EventType.KEY_PRESS); + event->window = search_entry.get_window(); + event->keyval = keyval; + event->hardware_keycode = (uint16) keys[0].keycode; + event->group = (uint8) keys[0].group; + ((Gdk.Event*) event)->put(); + + return; + } + + public override bool show_buttons() + { + return true; + } } } diff --git a/gitg/resources/ui/gitg-shortcuts.ui b/gitg/resources/ui/gitg-shortcuts.ui index a8f299c2..76e971d1 100644 --- a/gitg/resources/ui/gitg-shortcuts.ui +++ b/gitg/resources/ui/gitg-shortcuts.ui @@ -61,6 +61,20 @@ Find + + + true + <primary>G + Move to next search result + + + + + true + <primary><shift>G + Move to previous search result + + true diff --git a/gitg/resources/ui/gitg-window.ui b/gitg/resources/ui/gitg-window.ui index 436c560a..9b18c85b 100644 --- a/gitg/resources/ui/gitg-window.ui +++ b/gitg/resources/ui/gitg-window.ui @@ -247,11 +247,50 @@ False False - + True - True - 500 - + horizontal + + + + True + True + 500 + + + + + + True + False + + + + True + False + 1 + go-up-symbolic + + + + + + + True + False + + + + True + False + 1 + go-down-symbolic + + + + diff --git a/libgitg-ext/gitg-ext-searchable.vala b/libgitg-ext/gitg-ext-searchable.vala index f49d5d6b..d32307c2 100644 --- a/libgitg-ext/gitg-ext-searchable.vala +++ b/libgitg-ext/gitg-ext-searchable.vala @@ -32,6 +32,8 @@ public interface Searchable : Object, Activity public abstract bool search_visible { get; set; } public abstract bool search_available { get; } public abstract Gtk.Entry? search_entry { set; } + public virtual void search_move(string key, bool up) {} + public virtual bool show_buttons() { return false; } } }