-
Notifications
You must be signed in to change notification settings - Fork 131
/
GamesList.vala
123 lines (102 loc) · 2.95 KB
/
GamesList.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
This file is part of GameHub.
Copyright (C) 2018-2019 Anatoliy Kashkin
GameHub is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GameHub is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GameHub. If not, see <https://www.gnu.org/licenses/>.
*/
using Gtk;
using Gdk;
using Gee;
using GameHub.Data;
using GameHub.Data.Adapters;
using GameHub.Data.DB;
using GameHub.Utils;
using GameHub.UI.Widgets;
namespace GameHub.UI.Views.GamesView.List
{
public class GamesList: ListBox
{
public signal void game_selected(Game? game);
public signal void multiple_games_selected(ArrayList<Game> games);
private GamesAdapter? adapter = null;
public ScrolledWindow scrolled;
public GamesList()
{
Object(selection_mode: SelectionMode.MULTIPLE);
}
construct
{
selected_rows_changed.connect(() => {
var rows = get_selected_rows();
if(rows.length() == 1)
{
game_selected(((GameListRow) rows.data).game);
}
else if(rows.length() > 1)
{
var selected = new ArrayList<Game>();
foreach(var row in rows)
{
var game = ((GameListRow) row).game;
if(game != null) selected.add(game);
}
multiple_games_selected(selected);
}
});
}
public void attach(GamesAdapter adapter)
{
this.adapter = adapter;
adapter.attach_list(this);
}
public void select(int index, bool grab_focus=false)
{
unselect_all();
var row = get_row_at_index(index);
if(row != null)
{
select_row(row);
if(grab_focus) row.grab_focus();
}
}
private void update_scroll()
{
var scroll = scrolled.vadjustment.value;
var height = scrolled.vadjustment.page_size;
var viewport_top = scroll;
var viewport_bottom = scroll + height;
this.foreach(w => {
var row = (GameListRow) w;
if(!row.visible) return;
Allocation alloc;
row.get_allocation(out alloc);
if(alloc.x < 0 || alloc.y < 0 || alloc.width < 1 || alloc.height < 1) return;
var row_top = alloc.y;
var row_bottom = alloc.y + alloc.height;
var is_before_viewport = row_bottom < viewport_top;
var is_after_viewport = row_top > viewport_bottom;
row.icon_is_visible = !is_before_viewport && !is_after_viewport;
});
}
public ScrolledWindow wrapped()
{
scrolled = new ScrolledWindow(null, null);
scrolled.hscrollbar_policy = PolicyType.NEVER;
scrolled.set_size_request(220, -1);
scrolled.add(this);
scrolled.vadjustment.value_changed.connect(update_scroll);
scrolled.size_allocate.connect(update_scroll);
scrolled.show_all();
show_all();
return scrolled;
}
}
}