@@ -19,37 +19,19 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
1919 private Gee . LinkedList<GLib . Cancellable > cancellables;
2020 private Gtk . EventControllerKey search_term_entry_key_controller;
2121 private Gtk . Label title_label;
22+ private string current_doc_project;
2223 public Scratch . MainWindow current_window { get ; construct; }
24+ public Scratch . Services . FuzzySearchIndexer search_indexer { get ; construct; }
2325 public bool sidebar_is_visible { get ; set ; }
2426
2527 public signal void open_file (string filepath );
2628 public signal void close_search ();
2729
2830 public FuzzySearchPopover (Scratch .Services .FuzzySearchIndexer search_indexer , Scratch .MainWindow window ) {
2931 Object (
30- modal: true ,
31- relative_to: window. document_view,
32- width_request: 500 ,
33- current_window: window
32+ current_window: window,
33+ search_indexer: search_indexer
3434 );
35-
36- int height;
37- current_window. get_size (null , out height);
38- window_height = height;
39-
40- fuzzy_finder = new Services .FuzzyFinder (search_indexer. project_paths);
41- indexer = search_indexer;
42- items = new Gee .ArrayList<FileItem > ();
43- cancellables = new Gee .LinkedList<GLib . Cancellable > ();
44-
45- // Limit the shown results if the window height is too small
46- if (window_height > 400 ) {
47- max_items = 5 ;
48- } else {
49- max_items = 3 ;
50- }
51-
52- scrolled. set_max_content_height (45 /* height */ * max_items);
5335 }
5436
5537 private void calculate_scroll_offset (int old_position , int new_position ) {
@@ -81,7 +63,11 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
8163 }
8264
8365 construct {
66+ modal = true ;
67+ relative_to = current_window. document_view;
68+ width_request = 500 ;
8469 pointing_to = { 0 , 32 , 1 , 1 };
70+
8571 this . get_style_context (). add_class (" fuzzy-popover" );
8672
8773 title_label = new Gtk .Label (_(" Find project files" ));
@@ -183,7 +169,7 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
183169 }
184170
185171 fuzzy_finder. fuzzy_find_async. begin (term, dir_length,
186- get_current_project () ,
172+ current_doc_project ,
187173 next_cancellable,
188174 (obj, res) = > {
189175 if (next_cancellable. is_cancelled ()) {
@@ -260,6 +246,44 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
260246
261247 scrolled. hide ();
262248 this . add (box);
249+
250+ fuzzy_finder = new Services .FuzzyFinder (search_indexer. project_paths);
251+ indexer = search_indexer;
252+ items = new Gee .ArrayList<FileItem > ();
253+ cancellables = new Gee .LinkedList<GLib . Cancellable > ();
254+
255+ search_term_entry. realize. connect_after (() = > {
256+ int height;
257+ current_window. get_size (null , out height);
258+
259+ // Limit the shown results if the window height is too small
260+ if (height > 400 ) {
261+ max_items = height / 80 ;
262+ } else {
263+ max_items = 3 ;
264+ }
265+
266+ scrolled. set_max_content_height (45 /* height */ * max_items);
267+
268+ current_doc_project = get_current_project (); // This will not change while popover is showing
269+ search_result_container. set_sort_func ((a , b) = > {
270+ var result_a = ((FileItem )a). result;
271+ var result_b = ((FileItem )b). result;
272+ var project_a_is_current = result_a. project == current_doc_project;
273+ var project_b_is_current = result_b. project == current_doc_project;
274+ if (project_a_is_current && ! project_b_is_current) {
275+ return 1 ;
276+ } else if (project_b_is_current && ! project_a_is_current) {
277+ return - 1 ;
278+ } else if (result_a. score > result_b. score) {
279+ return - 1 ;
280+ } else if (result_b. score > result_a. score) {
281+ return 1 ;
282+ } else {
283+ return strcmp (((FileItem )a). result. full_path, ((FileItem )b). result. full_path);
284+ }
285+ });
286+ });
263287 }
264288
265289 private void handle_item_selection (int index ) {
0 commit comments