diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 310b28b..a303c8b 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -6,14 +6,6 @@ */ public class Maps.MainWindow : Adw.ApplicationWindow { - private class PlaceListBoxRow : Gtk.ListBoxRow { - public Geocode.Place place { get; construct; } - - public PlaceListBoxRow (Geocode.Place place) { - Object (place: place); - } - } - [Flags] private enum BusyReason { /** Busy with locating */ @@ -36,7 +28,6 @@ public class Maps.MainWindow : Adw.ApplicationWindow { private Gtk.Button current_location; private Gtk.Spinner spinner; private Gtk.SearchEntry search_entry; - private Gtk.ListBox search_res_list; private Gtk.Popover search_res_popover; private MapWidget map_widget; @@ -70,25 +61,34 @@ public class Maps.MainWindow : Adw.ApplicationWindow { margin_end = 12 }; - search_res_list = new Gtk.ListBox () { - selection_mode = Gtk.SelectionMode.BROWSE + var list_factory = new Gtk.SignalListItemFactory (); + list_factory.setup.connect (setup_factory); + list_factory.bind.connect (bind_factory); + + var selection_model = new Gtk.NoSelection (location_store); + + var search_listview = new Gtk.ListView (selection_model, list_factory) { + single_click_activate = true }; - search_res_list.add_css_class (Granite.STYLE_CLASS_RICH_LIST); - search_res_list.bind_model (location_store, construct_search_res); - search_res_list.set_placeholder (search_placeholder); + search_listview.add_css_class (Granite.STYLE_CLASS_RICH_LIST); var search_res_list_scrolled = new Gtk.ScrolledWindow () { - child = search_res_list, + child = search_listview, hscrollbar_policy = Gtk.PolicyType.NEVER, max_content_height = 500, propagate_natural_height = true }; + var search_stack = new Gtk.Stack () { + vhomogeneous = false + }; + search_stack.add_child (search_res_list_scrolled); + search_stack.add_child (search_placeholder); + search_res_popover = new Gtk.Popover () { width_request = 400, has_arrow = false, - child = search_res_list_scrolled, - default_widget = search_res_list + child = search_stack }; search_res_popover.set_parent (search_entry); @@ -154,6 +154,14 @@ public class Maps.MainWindow : Adw.ApplicationWindow { map_widget.go_to_current (); }); + selection_model.items_changed.connect (() => { + if (selection_model.get_n_items () == 0) { + search_stack.visible_child = search_placeholder; + } else { + search_stack.visible_child = search_res_list_scrolled; + } + }); + search_entry.search_changed.connect (() => { if (search_entry.text == "") { search_res_popover.popdown (); @@ -164,13 +172,9 @@ public class Maps.MainWindow : Adw.ApplicationWindow { search_location.begin (search_entry.text, location_store); }); - search_res_list.row_activated.connect ((row) => { - unowned var place_row = row as PlaceListBoxRow; - if (place_row == null) { - return; - } - - map_widget.go_to_place (place_row.place); + search_listview.activate.connect ((pos) => { + var place = (Geocode.Place) selection_model.get_item (pos); + map_widget.go_to_place (place); search_res_popover.popdown (); }); @@ -254,6 +258,8 @@ public class Maps.MainWindow : Adw.ApplicationWindow { answer_count = 10 }; + loc_store.remove_all (); + var places = new List (); try { places = yield forward.search_async (search_cancellable); @@ -262,23 +268,22 @@ public class Maps.MainWindow : Adw.ApplicationWindow { return; } - loc_store.remove_all (); foreach (unowned var place in places) { loc_store.append (place); } } - private Gtk.Widget construct_search_res (Object item) { - unowned var place = item as Geocode.Place; + private void setup_factory (Object object) { + var search_result_item = new SearchResultItem (); - var result_item = new Maps.SearchResultItem () { - place = place - }; + var list_item = (Gtk.ListItem) object; + list_item.child = search_result_item; + } - var row = new PlaceListBoxRow (place) { - child = result_item - }; + private void bind_factory (Object object) { + var list_item = (Gtk.ListItem) object; - return row; + var search_result_item = (SearchResultItem) list_item.child; + search_result_item.place = (Geocode.Place) list_item.item; } }