Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2022, the SerenityOS developers.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <LibGUI/ListView.h>
9#include <LibGUI/Model.h>
10#include <LibGUI/Painter.h>
11#include <LibGUI/Scrollbar.h>
12#include <LibGfx/Palette.h>
13
14REGISTER_WIDGET(GUI, ListView)
15
16namespace GUI {
17
18ListView::ListView()
19{
20 set_fill_with_background_color(true);
21 set_background_role(ColorRole::Base);
22 set_foreground_role(ColorRole::BaseText);
23 set_searchable(true);
24}
25
26ListView::~ListView() = default;
27
28void ListView::select_all()
29{
30 selection().clear();
31 for (int item_index = 0; item_index < item_count(); ++item_index) {
32 auto index = model()->index(item_index, m_model_column);
33 selection().add(index);
34 }
35}
36
37void ListView::update_content_size()
38{
39 if (!model())
40 return set_content_size({});
41
42 int content_width = 0;
43 for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
44 auto text = model()->index(row, m_model_column).data();
45 content_width = max(content_width, font().width(text.to_deprecated_string()) + horizontal_padding() * 2);
46 }
47 m_max_item_width = content_width;
48 content_width = max(content_width, widget_inner_rect().width());
49
50 int content_height = item_count() * item_height();
51 set_content_size({ content_width, content_height });
52}
53
54void ListView::resize_event(ResizeEvent& event)
55{
56 update_content_size();
57 AbstractView::resize_event(event);
58}
59
60void ListView::layout_relevant_change_occurred()
61{
62 update_content_size();
63 AbstractView::layout_relevant_change_occurred();
64}
65
66void ListView::model_did_update(unsigned flags)
67{
68 AbstractView::model_did_update(flags);
69 update_content_size();
70 update();
71}
72
73Gfx::IntRect ListView::content_rect(int row) const
74{
75 return { 0, row * item_height(), content_width(), item_height() };
76}
77
78Gfx::IntRect ListView::content_rect(ModelIndex const& index) const
79{
80 return content_rect(index.row());
81}
82
83ModelIndex ListView::index_at_event_position(Gfx::IntPoint point) const
84{
85 VERIFY(model());
86
87 auto adjusted_position = this->adjusted_position(point);
88 for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
89 if (!content_rect(row).contains(adjusted_position))
90 continue;
91 return model()->index(row, m_model_column);
92 }
93 return {};
94}
95
96Gfx::IntPoint ListView::adjusted_position(Gfx::IntPoint position) const
97{
98 return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
99}
100
101void ListView::paint_list_item(Painter& painter, int row_index, int painted_item_index)
102{
103 bool is_selected_row = selection().contains_row(row_index);
104
105 int y = painted_item_index * item_height();
106
107 Color background_color;
108 if (is_selected_row) {
109 background_color = is_focused() ? palette().selection() : palette().inactive_selection();
110 } else {
111 Color row_fill_color = palette().color(background_role());
112 if (alternating_row_colors() && (painted_item_index % 2)) {
113 background_color = row_fill_color.darkened(0.8f);
114 } else {
115 background_color = row_fill_color;
116 }
117 }
118
119 Gfx::IntRect row_rect(0, y, content_width(), item_height());
120 painter.fill_rect(row_rect, background_color);
121 auto index = model()->index(row_index, m_model_column);
122 auto data = index.data();
123 auto font = font_for_index(index);
124 if (data.is_bitmap()) {
125 painter.blit(row_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
126 } else if (data.is_icon()) {
127 if (auto bitmap = data.as_icon().bitmap_for_size(16)) {
128 auto opacity = index.data(ModelRole::IconOpacity).as_float_or(1.0f);
129 painter.blit(row_rect.location(), *bitmap, bitmap->rect(), opacity);
130 }
131 } else {
132 auto text_rect = row_rect;
133 text_rect.translate_by(horizontal_padding(), 0);
134 text_rect.set_width(text_rect.width() - horizontal_padding() * 2);
135 auto text_alignment = index.data(ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterLeft);
136 draw_item_text(painter, index, is_selected_row, text_rect, data.to_deprecated_string(), font, text_alignment, Gfx::TextElision::None);
137 }
138}
139
140void ListView::paint_event(PaintEvent& event)
141{
142 Frame::paint_event(event);
143
144 if (!model())
145 return;
146
147 Painter painter(*this);
148 painter.add_clip_rect(frame_inner_rect());
149 painter.add_clip_rect(event.rect());
150 painter.translate(frame_thickness(), frame_thickness());
151 painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
152
153 int exposed_width = max(content_size().width(), width());
154 int painted_item_index = 0;
155
156 for (int row_index = 0; row_index < model()->row_count(); ++row_index) {
157 paint_list_item(painter, row_index, painted_item_index);
158 ++painted_item_index;
159 };
160
161 Gfx::IntRect unpainted_rect(0, painted_item_index * item_height(), exposed_width, height());
162 if (fill_with_background_color())
163 painter.fill_rect(unpainted_rect, palette().color(background_role()));
164}
165
166int ListView::item_count() const
167{
168 if (!model())
169 return 0;
170 return model()->row_count();
171}
172
173void ListView::mousemove_event(MouseEvent& event)
174{
175 auto previous_hovered_index = m_hovered_index;
176 AbstractView::mousemove_event(event);
177 if (hover_highlighting() && previous_hovered_index != m_hovered_index)
178 set_cursor(m_hovered_index, SelectionUpdate::Set);
179}
180
181void ListView::keydown_event(KeyEvent& event)
182{
183 if (!model())
184 return AbstractView::keydown_event(event);
185
186 if (event.key() == KeyCode::Key_Escape) {
187 if (on_escape_pressed)
188 on_escape_pressed();
189 return;
190 }
191 AbstractView::keydown_event(event);
192}
193
194void ListView::move_cursor_relative(int steps, SelectionUpdate selection_update)
195{
196 if (!model())
197 return;
198 auto& model = *this->model();
199 ModelIndex new_index;
200 if (cursor_index().is_valid()) {
201 auto row = cursor_index().row();
202 if (steps > 0) {
203 if (row + steps >= model.row_count())
204 row = model.row_count() - 1;
205 else
206 row += steps;
207 } else if (steps < 0) {
208 if (row < -steps)
209 row = 0;
210 else
211 row += steps;
212 }
213 new_index = model.index(row, cursor_index().column());
214 } else {
215 new_index = model.index(0, 0);
216 }
217 set_cursor(new_index, selection_update);
218}
219
220void ListView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
221{
222 if (!model())
223 return;
224 auto& model = *this->model();
225
226 if (!cursor_index().is_valid()) {
227 set_cursor(model.index(0, 0), SelectionUpdate::Set);
228 return;
229 }
230
231 ModelIndex new_index;
232
233 switch (movement) {
234 case CursorMovement::Up:
235 new_index = model.index(cursor_index().row() - 1, cursor_index().column());
236 break;
237 case CursorMovement::Down:
238 new_index = model.index(cursor_index().row() + 1, cursor_index().column());
239 break;
240 case CursorMovement::Home:
241 new_index = model.index(0, 0);
242 break;
243 case CursorMovement::End:
244 new_index = model.index(model.row_count() - 1, 0);
245 break;
246 case CursorMovement::PageUp: {
247 int items_per_page = visible_content_rect().height() / item_height();
248 new_index = model.index(max(0, cursor_index().row() - items_per_page), cursor_index().column());
249 break;
250 }
251 case CursorMovement::PageDown: {
252 int items_per_page = visible_content_rect().height() / item_height();
253 new_index = model.index(min(model.row_count() - 1, cursor_index().row() + items_per_page), cursor_index().column());
254 break;
255 }
256 default:
257 break;
258 }
259
260 if (new_index.is_valid())
261 set_cursor(new_index, selection_update);
262}
263
264void ListView::scroll_into_view(ModelIndex const& index, bool scroll_horizontally, bool scroll_vertically)
265{
266 if (!model())
267 return;
268 AbstractScrollableWidget::scroll_into_view(content_rect(index.row()), scroll_horizontally, scroll_vertically);
269}
270
271Optional<UISize> ListView::calculated_min_size() const
272{
273 auto min_width = horizontal_scrollbar().effective_min_size().width().as_int() + vertical_scrollbar().width() + frame_thickness() * 2;
274 auto min_height = vertical_scrollbar().effective_min_size().height().as_int() + horizontal_scrollbar().height() + frame_thickness() * 2;
275 auto content_exceeds_minimums = content_width() > min_width && content_height() > min_height;
276 auto scrollbars_are_visible = horizontal_scrollbar().is_visible() && vertical_scrollbar().is_visible();
277 if (content_exceeds_minimums || scrollbars_are_visible)
278 return AbstractScrollableWidget::calculated_min_size();
279 return { { m_max_item_width + frame_thickness() * 2, content_height() + frame_thickness() * 2 } };
280}
281
282}