Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 69 additions & 70 deletions plugins/fuzzy-search/fuzzy-search-popover.vala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
*/

public class Scratch.FuzzySearchPopover : Gtk.Popover {
public signal void open_file (string filepath);
public signal void close_search ();

public Scratch.MainWindow current_window { get; construct; }
public Scratch.Services.FuzzySearchIndexer search_indexer { get; construct; }
public bool sidebar_is_visible { get; set; }

private Gtk.SearchEntry search_term_entry;
private Services.FuzzyFinder fuzzy_finder;
private Gtk.ListBox search_result_container;
Expand All @@ -20,12 +27,6 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
private Gtk.EventControllerKey search_term_entry_key_controller;
private Gtk.Label title_label;
private string current_doc_project;
public Scratch.MainWindow current_window { get; construct; }
public Scratch.Services.FuzzySearchIndexer search_indexer { get; construct; }
public bool sidebar_is_visible { get; set; }

public signal void open_file (string filepath);
public signal void close_search ();

public FuzzySearchPopover (Scratch.Services.FuzzySearchIndexer search_indexer, Scratch.MainWindow window) {
Object (
Expand All @@ -34,58 +35,54 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
);
}

private void calculate_scroll_offset (int old_position, int new_position) {
// Shortcut if jumping from first to last or the other way round
if (new_position == 0 && old_position > new_position) {
scrolled.vadjustment.value = 0;
return;
} else if (old_position == 0 && new_position == items.size - 1) {
scrolled.vadjustment.value = scrolled.vadjustment.get_upper ();
return;
}

var size_box = scrolled.vadjustment.get_upper () / items.size;
var current_top = scrolled.vadjustment.value;
var current_bottom = current_top + size_box * (max_items - 2);
if (old_position < new_position) {
// Down movement
var new_adjust = size_box * (preselected_index);
if (new_adjust >= current_bottom) {
scrolled.vadjustment.value = size_box * (preselected_index - (max_items - 1));
}
} else if (old_position > new_position) {
// Up movement
var new_adjust = size_box * (preselected_index);
if (new_adjust < current_top) {
scrolled.vadjustment.value = new_adjust;
}
}
}

construct {
modal = true;
relative_to = current_window.document_view;
width_request = 500;
pointing_to = { 0, 32, 1, 1 };
get_style_context ().add_class ("fuzzy-popover");

this.get_style_context ().add_class ("fuzzy-popover");

title_label = new Gtk.Label (_("Find project files"));
title_label.halign = Gtk.Align.START;
title_label.get_style_context ().add_class ("h4");
title_label = new Granite.HeaderLabel (_("Find project files"));

search_term_entry = new Gtk.SearchEntry ();
search_term_entry.halign = Gtk.Align.FILL;
search_term_entry.hexpand = true;
search_term_entry = new Gtk.SearchEntry () {
hexpand = true,
valign = START
};

search_result_container = new Gtk.ListBox () {
selection_mode = Gtk.SelectionMode.NONE,
selection_mode = NONE,
activate_on_single_click = true,
can_focus = false
};

search_result_container.get_style_context ().add_class ("fuzzy-list");

var entry_layout = new Gtk.Box (VERTICAL, 0) {
valign = START
};
entry_layout.add (title_label);
entry_layout.add (search_term_entry);

scrolled = new Gtk.ScrolledWindow (null, null) {
propagate_natural_height = true,
hexpand = true,
vexpand = true,
child = search_result_container
};

var box = new Gtk.Box (VERTICAL, 0);
box.add (entry_layout);
box.add (scrolled);
box.show_all ();

scrolled.hide ();

add (box);

fuzzy_finder = new Services.FuzzyFinder (search_indexer.project_paths);
indexer = search_indexer;
items = new Gee.ArrayList<FileItem> ();
cancellables = new Gee.LinkedList<GLib.Cancellable> ();

search_result_container.row_activated.connect ((row) => {
var file_item = row as FileItem;
if (file_item == null) {
Expand Down Expand Up @@ -225,32 +222,6 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
}
});

var entry_layout = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
entry_layout.valign = Gtk.Align.START;

entry_layout.add (title_label);
entry_layout.add (search_term_entry);
search_term_entry.valign = Gtk.Align.START;

scrolled = new Gtk.ScrolledWindow (null, null) {
propagate_natural_height = true,
hexpand = true,
child = search_result_container
};

var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
box.pack_start (entry_layout, false, false);
box.pack_end (scrolled, true, true);
box.show_all ();

scrolled.hide ();
this.add (box);

fuzzy_finder = new Services.FuzzyFinder (search_indexer.project_paths);
indexer = search_indexer;
items = new Gee.ArrayList<FileItem> ();
cancellables = new Gee.LinkedList<GLib.Cancellable> ();

search_term_entry.realize.connect_after (() => {
int height;
current_window.get_size (null, out height);
Expand Down Expand Up @@ -285,6 +256,34 @@ public class Scratch.FuzzySearchPopover : Gtk.Popover {
});
}

private void calculate_scroll_offset (int old_position, int new_position) {
// Shortcut if jumping from first to last or the other way round
if (new_position == 0 && old_position > new_position) {
scrolled.vadjustment.value = 0;
return;
} else if (old_position == 0 && new_position == items.size - 1) {
scrolled.vadjustment.value = scrolled.vadjustment.get_upper ();
return;
}

var size_box = scrolled.vadjustment.get_upper () / items.size;
var current_top = scrolled.vadjustment.value;
var current_bottom = current_top + size_box * (max_items - 2);
if (old_position < new_position) {
// Down movement
var new_adjust = size_box * (preselected_index);
if (new_adjust >= current_bottom) {
scrolled.vadjustment.value = size_box * (preselected_index - (max_items - 1));
}
} else if (old_position > new_position) {
// Up movement
var new_adjust = size_box * (preselected_index);
if (new_adjust < current_top) {
scrolled.vadjustment.value = new_adjust;
}
}
}

private void handle_item_selection (int index) {
var item = items.get (index);
open_file (item.filepath.strip ());
Expand Down