| 1 |
/* collection-view.vala |
| 2 |
* |
| 3 |
* Copyright (C) Red Hat, Inc |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* Author: Felipe Borges <[email protected]> |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
namespace Connections { |
| 23 |
[GtkTemplate (ui = "/org/gnome/Connections/ui/collection-view.ui")] |
| 24 |
public class CollectionView : Gtk.ScrolledWindow { |
| 25 |
[GtkChild] |
| 26 |
private unowned Gtk.FlowBox flowbox; |
| 27 |
[GtkChild] |
| 28 |
public unowned Gtk.SearchBar search_bar; |
| 29 |
[GtkChild] |
| 30 |
private unowned Gtk.SearchEntry search_entry; |
| 31 |
|
| 32 |
private Connections.ActionsPopover popover; |
| 33 |
|
| 34 |
construct { |
| 35 |
popover = new Connections.ActionsPopover (); |
| 36 |
|
| 37 |
flowbox.set_filter_func (model_filter); |
| 38 |
} |
| 39 |
|
| 40 |
public void bind_model (ListModel model) { |
| 41 |
flowbox.bind_model (model, create_child); |
| 42 |
} |
| 43 |
|
| 44 |
private Gtk.Widget create_child (Object item) { |
| 45 |
var child = new Gtk.FlowBoxChild (); |
| 46 |
child.halign = Gtk.Align.START; |
| 47 |
|
| 48 |
var box = new CollectionViewChild (item as Connection); |
| 49 |
child.add (box); |
| 50 |
|
| 51 |
return child; |
| 52 |
} |
| 53 |
|
| 54 |
[GtkCallback] |
| 55 |
private void on_child_activated (Gtk.FlowBoxChild child) { |
| 56 |
var item = child.get_child () as CollectionViewChild; |
| 57 |
if (item == null) |
| 58 |
return; |
| 59 |
|
| 60 |
Application.application.open_connection (item.connection); |
| 61 |
} |
| 62 |
|
| 63 |
[GtkCallback] |
| 64 |
private bool on_button_release_event (Gdk.EventButton event) { |
| 65 |
if (event.type != Gdk.EventType.BUTTON_RELEASE || event.button != 3) |
| 66 |
return false; |
| 67 |
|
| 68 |
var child = flowbox.get_child_at_pos ((int) event.x, (int) event.y); |
| 69 |
|
| 70 |
return launch_context_popover_for_child (child); |
| 71 |
} |
| 72 |
|
| 73 |
private bool launch_context_popover_for_child (Gtk.FlowBoxChild child) { |
| 74 |
var item = child.get_child () as CollectionViewChild; |
| 75 |
if (item == null) |
| 76 |
return false; |
| 77 |
|
| 78 |
popover.update_for_item (item.connection); |
| 79 |
popover.set_relative_to (item.thumbnail); |
| 80 |
popover.show (); |
| 81 |
|
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
private bool model_filter (Gtk.FlowBoxChild child) { |
| 86 |
if (child == null) |
| 87 |
return false; |
| 88 |
|
| 89 |
var item = child.get_child () as CollectionViewChild; |
| 90 |
if (item == null) |
| 91 |
return false; |
| 92 |
|
| 93 |
return item.connection.uri.contains (search_entry.text) || |
| 94 |
item.connection.display_name.contains (search_entry.text); |
| 95 |
} |
| 96 |
|
| 97 |
[GtkCallback] |
| 98 |
private void on_search_changed () { |
| 99 |
flowbox.invalidate_filter (); |
| 100 |
} |
| 101 |
} |
| 102 |
} |