Skip to content

Commit aa0a95e

Browse files
danirabbitjeremypw
andauthored
FuzzySearchPopover: code style and GTK4 prep (#1766)
Co-authored-by: Jeremy Wootten <jeremywootten@gmail.com>
1 parent f7d2001 commit aa0a95e

1 file changed

Lines changed: 69 additions & 70 deletions

File tree

plugins/fuzzy-search/fuzzy-search-popover.vala

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
*/
88

99
public class Scratch.FuzzySearchPopover : Gtk.Popover {
10+
public signal void open_file (string filepath);
11+
public signal void close_search ();
12+
13+
public Scratch.MainWindow current_window { get; construct; }
14+
public Scratch.Services.FuzzySearchIndexer search_indexer { get; construct; }
15+
public bool sidebar_is_visible { get; set; }
16+
1017
private Gtk.SearchEntry search_term_entry;
1118
private Services.FuzzyFinder fuzzy_finder;
1219
private Gtk.ListBox search_result_container;
@@ -20,12 +27,6 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
2027
private Gtk.EventControllerKey search_term_entry_key_controller;
2128
private Gtk.Label title_label;
2229
private string current_doc_project;
23-
public Scratch.MainWindow current_window { get; construct; }
24-
public Scratch.Services.FuzzySearchIndexer search_indexer { get; construct; }
25-
public bool sidebar_is_visible { get; set; }
26-
27-
public signal void open_file (string filepath);
28-
public signal void close_search ();
2930

3031
public FuzzySearchPopover (Scratch.Services.FuzzySearchIndexer search_indexer, Scratch.MainWindow window) {
3132
Object (
@@ -34,58 +35,54 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
3435
);
3536
}
3637

37-
private void calculate_scroll_offset (int old_position, int new_position) {
38-
// Shortcut if jumping from first to last or the other way round
39-
if (new_position == 0 && old_position > new_position) {
40-
scrolled.vadjustment.value = 0;
41-
return;
42-
} else if (old_position == 0 && new_position == items.size - 1) {
43-
scrolled.vadjustment.value = scrolled.vadjustment.get_upper ();
44-
return;
45-
}
46-
47-
var size_box = scrolled.vadjustment.get_upper () / items.size;
48-
var current_top = scrolled.vadjustment.value;
49-
var current_bottom = current_top + size_box * (max_items - 2);
50-
if (old_position < new_position) {
51-
// Down movement
52-
var new_adjust = size_box * (preselected_index);
53-
if (new_adjust >= current_bottom) {
54-
scrolled.vadjustment.value = size_box * (preselected_index - (max_items - 1));
55-
}
56-
} else if (old_position > new_position) {
57-
// Up movement
58-
var new_adjust = size_box * (preselected_index);
59-
if (new_adjust < current_top) {
60-
scrolled.vadjustment.value = new_adjust;
61-
}
62-
}
63-
}
64-
6538
construct {
6639
modal = true;
6740
relative_to = current_window.document_view;
6841
width_request = 500;
6942
pointing_to = { 0, 32, 1, 1 };
43+
get_style_context ().add_class ("fuzzy-popover");
7044

71-
this.get_style_context ().add_class ("fuzzy-popover");
72-
73-
title_label = new Gtk.Label (_("Find project files"));
74-
title_label.halign = Gtk.Align.START;
75-
title_label.get_style_context ().add_class ("h4");
45+
title_label = new Granite.HeaderLabel (_("Find project files"));
7646

77-
search_term_entry = new Gtk.SearchEntry ();
78-
search_term_entry.halign = Gtk.Align.FILL;
79-
search_term_entry.hexpand = true;
47+
search_term_entry = new Gtk.SearchEntry () {
48+
hexpand = true,
49+
valign = START
50+
};
8051

8152
search_result_container = new Gtk.ListBox () {
82-
selection_mode = Gtk.SelectionMode.NONE,
53+
selection_mode = NONE,
8354
activate_on_single_click = true,
8455
can_focus = false
8556
};
86-
8757
search_result_container.get_style_context ().add_class ("fuzzy-list");
8858

59+
var entry_layout = new Gtk.Box (VERTICAL, 0) {
60+
valign = START
61+
};
62+
entry_layout.add (title_label);
63+
entry_layout.add (search_term_entry);
64+
65+
scrolled = new Gtk.ScrolledWindow (null, null) {
66+
propagate_natural_height = true,
67+
hexpand = true,
68+
vexpand = true,
69+
child = search_result_container
70+
};
71+
72+
var box = new Gtk.Box (VERTICAL, 0);
73+
box.add (entry_layout);
74+
box.add (scrolled);
75+
box.show_all ();
76+
77+
scrolled.hide ();
78+
79+
add (box);
80+
81+
fuzzy_finder = new Services.FuzzyFinder (search_indexer.project_paths);
82+
indexer = search_indexer;
83+
items = new Gee.ArrayList<FileItem> ();
84+
cancellables = new Gee.LinkedList<GLib.Cancellable> ();
85+
8986
search_result_container.row_activated.connect ((row) => {
9087
var file_item = row as FileItem;
9188
if (file_item == null) {
@@ -225,32 +222,6 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
225222
}
226223
});
227224

228-
var entry_layout = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
229-
entry_layout.valign = Gtk.Align.START;
230-
231-
entry_layout.add (title_label);
232-
entry_layout.add (search_term_entry);
233-
search_term_entry.valign = Gtk.Align.START;
234-
235-
scrolled = new Gtk.ScrolledWindow (null, null) {
236-
propagate_natural_height = true,
237-
hexpand = true,
238-
child = search_result_container
239-
};
240-
241-
var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
242-
box.pack_start (entry_layout, false, false);
243-
box.pack_end (scrolled, true, true);
244-
box.show_all ();
245-
246-
scrolled.hide ();
247-
this.add (box);
248-
249-
fuzzy_finder = new Services.FuzzyFinder (search_indexer.project_paths);
250-
indexer = search_indexer;
251-
items = new Gee.ArrayList<FileItem> ();
252-
cancellables = new Gee.LinkedList<GLib.Cancellable> ();
253-
254225
search_term_entry.realize.connect_after (() => {
255226
int height;
256227
current_window.get_size (null, out height);
@@ -285,6 +256,34 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
285256
});
286257
}
287258

259+
private void calculate_scroll_offset (int old_position, int new_position) {
260+
// Shortcut if jumping from first to last or the other way round
261+
if (new_position == 0 && old_position > new_position) {
262+
scrolled.vadjustment.value = 0;
263+
return;
264+
} else if (old_position == 0 && new_position == items.size - 1) {
265+
scrolled.vadjustment.value = scrolled.vadjustment.get_upper ();
266+
return;
267+
}
268+
269+
var size_box = scrolled.vadjustment.get_upper () / items.size;
270+
var current_top = scrolled.vadjustment.value;
271+
var current_bottom = current_top + size_box * (max_items - 2);
272+
if (old_position < new_position) {
273+
// Down movement
274+
var new_adjust = size_box * (preselected_index);
275+
if (new_adjust >= current_bottom) {
276+
scrolled.vadjustment.value = size_box * (preselected_index - (max_items - 1));
277+
}
278+
} else if (old_position > new_position) {
279+
// Up movement
280+
var new_adjust = size_box * (preselected_index);
281+
if (new_adjust < current_top) {
282+
scrolled.vadjustment.value = new_adjust;
283+
}
284+
}
285+
}
286+
288287
private void handle_item_selection (int index) {
289288
var item = items.get (index);
290289
open_file (item.filepath.strip ());

0 commit comments

Comments
 (0)