Skip to content

Commit

Permalink
CalendarButton: Add a simple search (#356)
Browse files Browse the repository at this point in the history
* CalendarButton: Add a simple search

* Add placeholder and clear entry on row activated

* Activate when search is focused

* unselect rows when filter is changed

* Move focus to search bar after top of the list
  • Loading branch information
danirabbit authored and Jeremy Wootten committed Jan 5, 2019
1 parent 5083f96 commit 1da543c
Showing 1 changed file with 57 additions and 4 deletions.
61 changes: 57 additions & 4 deletions src/Widgets/CalendarButton.vala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Maya.View.Widgets.CalendarButton : Gtk.MenuButton {
}
}

private Gtk.SearchEntry search_entry;
private CalendarGrid calendar_grid;

construct {
Expand Down Expand Up @@ -65,22 +66,39 @@ public class Maya.View.Widgets.CalendarButton : Gtk.MenuButton {

current_source = registry.default_calendar;

search_entry = new Gtk.SearchEntry ();
search_entry.margin = 12;
search_entry.margin_bottom = 6;
search_entry.placeholder_text = _("Search Calendars");

var placeholder = new Granite.Widgets.AlertView (
_("No Results"),
_("Try changing search terms."),
""
);
placeholder.show_all ();

var list_box = new Gtk.ListBox ();
list_box.activate_on_single_click = true;
list_box.set_placeholder (placeholder);

var scrolled = new Gtk.ScrolledWindow (null, null);
scrolled.hscrollbar_policy = Gtk.PolicyType.NEVER;
scrolled.add (list_box);
scrolled.margin_top = 6;
scrolled.margin_bottom = 6;
scrolled.max_content_height = 300;
scrolled.propagate_natural_height = true;
scrolled.show_all ();

var popover_grid = new Gtk.Grid ();
popover_grid.margin_bottom = 6;
popover_grid.attach (search_entry, 0, 0);
popover_grid.attach (scrolled, 0, 1);
popover_grid.show_all ();

popover = new Gtk.Popover (this);
popover.width_request = 310;
popover.add (scrolled);
popover.add (popover_grid);

list_box.set_filter_func (filter_function);
list_box.set_header_func (header_update_func);

list_box.set_sort_func ((row1, row2) => {
Expand All @@ -94,11 +112,35 @@ public class Maya.View.Widgets.CalendarButton : Gtk.MenuButton {
}
});

list_box.move_cursor.connect ((mvmt, count) => {
var row = list_box.get_selected_row ().get_index ();
if (row == 0 && count == -1) {
search_entry.grab_focus ();
}
});

list_box.row_activated.connect ((row) => {
current_source = ((CalendarGrid)row.get_child ()).source;
popover.popdown ();
});

popover.unmap.connect (() => {
search_entry.text = "";
});

search_entry.activate.connect (() => {
foreach (unowned Gtk.Widget child in list_box.get_children ()) {
if (child.get_child_visible ()) {
((Gtk.ListBoxRow) child).activate ();
}
}
});

search_entry.search_changed.connect (() => {
list_box.invalidate_filter ();
list_box.unselect_all ();
});

foreach (var source in sources) {
var calgrid = new CalendarGrid (source);
calgrid.margin = 6;
Expand All @@ -116,6 +158,17 @@ public class Maya.View.Widgets.CalendarButton : Gtk.MenuButton {
}
}

[CCode (instance_pos = -1)]
private bool filter_function (Gtk.ListBoxRow row) {
var search_term = search_entry.text.down ();

if (search_term in ((CalendarGrid)row.get_child ()).label.down ()) {
return true;
}

return false;
}

private void header_update_func (Gtk.ListBoxRow row, Gtk.ListBoxRow? before) {
var row_location = ((CalendarGrid)row.get_child ()).location;
if (before != null) {
Expand Down

0 comments on commit 1da543c

Please sign in to comment.