Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 23346a7

Browse files
sfesenkomdh34
authored andcommitted
Add 'Find in page' searchbar (#63)
1 parent f377e33 commit 23346a7

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

src/Application.vala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public class Docs : Gtk.Application {
4848
tab_switch.activate.connect (() => {
4949
window.change_tab ();
5050
});
51+
52+
var search_action = new SimpleAction ("find", null);
53+
add_action (search_action);
54+
set_accels_for_action ("app.find", {"<Control>F"});
55+
56+
search_action.activate.connect (window.toggle_search);
5157
}
5258

5359
public static int main (string[] args) {

src/MainWindow.vala

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
public class MainWindow : Gtk.Window {
2323
Gtk.Stack stack;
24+
Gtk.SearchBar search_bar;
2425
public MainWindow (Gtk.Application application) {
2526
Object (application: application,
2627
icon_name: "com.github.mdh34.quickdocs",
@@ -135,7 +136,13 @@ public class MainWindow : Gtk.Window {
135136
header.pack_end (theme_switch);
136137
header.pack_end (offline_button);
137138

138-
add (stack);
139+
search_bar = create_search_bar ();
140+
141+
var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
142+
vbox.pack_start (stack, true, true, 0);
143+
vbox.pack_start (search_bar, false, true, 0);
144+
145+
add (vbox);
139146
init_theme ();
140147

141148
string style = "@define-color colorPrimary #403757;";
@@ -187,6 +194,74 @@ public class MainWindow : Gtk.Window {
187194
}
188195
}
189196

197+
public void toggle_search () {
198+
var disabled = !search_bar.search_mode_enabled;
199+
search_bar.search_mode_enabled = disabled;
200+
if (disabled) {
201+
var view = get_current_view ();
202+
view.search_finish ();
203+
}
204+
}
205+
206+
private View? get_current_view () {
207+
var v = stack.get_visible_child ();
208+
return (v is View)
209+
? v as View
210+
: null;
211+
}
212+
213+
private Gtk.SearchBar create_search_bar () {
214+
var search_entry = new Gtk.SearchEntry();
215+
search_entry.placeholder_text = _("Find in page...");
216+
search_entry.set_width_chars(60);
217+
218+
search_entry.search_changed.connect (() => {
219+
var v = get_current_view ();
220+
if (v != null) {
221+
v.search (search_entry.text);
222+
}
223+
});
224+
225+
search_entry.activate.connect (() => {
226+
var v = get_current_view ();
227+
if (v != null ) {
228+
v.search_next ();
229+
}
230+
});
231+
search_entry.show();
232+
233+
var next_search = new Gtk.Button.from_icon_name ("go-down-symbolic", Gtk.IconSize.MENU);
234+
next_search.clicked.connect (() => {
235+
var v = get_current_view ();
236+
if (v != null ) {
237+
v.search_next ();
238+
}
239+
});
240+
241+
next_search.show();
242+
243+
var previous_search = new Gtk.Button.from_icon_name ("go-up-symbolic", Gtk.IconSize.MENU);
244+
previous_search.clicked.connect (() => {
245+
var v = get_current_view ();
246+
if (v != null ) {
247+
v.search_previous ();
248+
}
249+
});
250+
previous_search.show();
251+
252+
var hbox = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 5);
253+
hbox.pack_start (search_entry, false, false, 0);
254+
hbox.pack_start (previous_search, false, false, 0);
255+
hbox.pack_start (next_search, false, false, 0);
256+
hbox.show();
257+
258+
var search_bar = new Gtk.SearchBar();
259+
search_bar.connect_entry(search_entry);
260+
search_bar.add(hbox);
261+
262+
return search_bar;
263+
}
264+
190265
private bool check_online () {
191266
var host = "valadoc.org";
192267
try {

src/Widgets/View.vala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,21 @@ public class View : WebKit.WebView {
5656
cookies.set_accept_policy (WebKit.CookieAcceptPolicy.ALWAYS);
5757
cookies.set_persistent_storage (path, WebKit.CookiePersistentStorage.SQLITE);
5858
}
59-
}
59+
60+
public void search (string text) {
61+
var fc = get_find_controller ();
62+
fc.search (text, WebKit.FindOptions.CASE_INSENSITIVE, 100);
63+
}
64+
65+
public void search_next () {
66+
get_find_controller ().search_next ();
67+
}
68+
69+
public void search_previous () {
70+
get_find_controller ().search_previous ();
71+
}
72+
73+
public void search_finish () {
74+
get_find_controller () .search_finish ();
75+
}
76+
}

0 commit comments

Comments
 (0)