IconView.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Utf8View.h>
  7. #include <LibCore/Timer.h>
  8. #include <LibGUI/IconView.h>
  9. #include <LibGUI/Model.h>
  10. #include <LibGUI/ModelEditingDelegate.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/Scrollbar.h>
  13. #include <LibGfx/Palette.h>
  14. REGISTER_WIDGET(GUI, IconView);
  15. namespace GUI {
  16. IconView::IconView()
  17. {
  18. set_fill_with_background_color(true);
  19. set_background_role(ColorRole::Base);
  20. set_foreground_role(ColorRole::BaseText);
  21. horizontal_scrollbar().set_visible(false);
  22. }
  23. IconView::~IconView()
  24. {
  25. }
  26. void IconView::select_all()
  27. {
  28. for (int item_index = 0; item_index < item_count(); ++item_index) {
  29. auto& item_data = m_item_data_cache[item_index];
  30. if (!item_data.selected) {
  31. if (item_data.is_valid())
  32. add_selection(item_data);
  33. else
  34. add_selection(model()->index(item_index, model_column()));
  35. }
  36. }
  37. }
  38. void IconView::scroll_into_view(const ModelIndex& index, bool scroll_horizontally, bool scroll_vertically)
  39. {
  40. if (!index.is_valid())
  41. return;
  42. AbstractScrollableWidget::scroll_into_view(item_rect(index.row()), scroll_horizontally, scroll_vertically);
  43. }
  44. void IconView::resize_event(ResizeEvent& event)
  45. {
  46. AbstractView::resize_event(event);
  47. update_content_size();
  48. if (!m_had_valid_size) {
  49. m_had_valid_size = true;
  50. if (!selection().is_empty())
  51. scroll_into_view(selection().first());
  52. }
  53. }
  54. void IconView::rebuild_item_cache() const
  55. {
  56. auto prev_item_count = m_item_data_cache.size();
  57. size_t new_item_count = item_count();
  58. auto items_to_invalidate = min(prev_item_count, new_item_count);
  59. // if the new number of items is less, check if any of the
  60. // ones not in the list anymore was selected
  61. for (size_t i = new_item_count; i < m_item_data_cache.size(); i++) {
  62. auto& item_data = m_item_data_cache[i];
  63. if (item_data.selected) {
  64. VERIFY(m_selected_count_cache > 0);
  65. m_selected_count_cache--;
  66. }
  67. }
  68. if ((size_t)m_first_selected_hint >= new_item_count)
  69. m_first_selected_hint = 0;
  70. m_item_data_cache.resize(new_item_count);
  71. for (size_t i = 0; i < items_to_invalidate; i++) {
  72. auto& item_data = m_item_data_cache[i];
  73. // TODO: It's unfortunate that we have no way to know whether any
  74. // data actually changed, so we have to invalidate *everyone*
  75. if (item_data.is_valid() /* && !model()->is_valid(item_data.index)*/)
  76. item_data.invalidate();
  77. if (item_data.selected && i < (size_t)m_first_selected_hint)
  78. m_first_selected_hint = (int)i;
  79. }
  80. m_item_data_cache_valid = true;
  81. }
  82. auto IconView::get_item_data(int item_index) const -> ItemData&
  83. {
  84. if (!m_item_data_cache_valid)
  85. rebuild_item_cache();
  86. auto& item_data = m_item_data_cache[item_index];
  87. if (item_data.is_valid())
  88. return item_data;
  89. item_data.index = model()->index(item_index, model_column());
  90. item_data.text = item_data.index.data().to_string();
  91. get_item_rects(item_index, item_data, font_for_index(item_data.index));
  92. item_data.valid = true;
  93. return item_data;
  94. }
  95. auto IconView::item_data_from_content_position(const Gfx::IntPoint& content_position) const -> ItemData*
  96. {
  97. if (!m_visual_row_count || !m_visual_column_count)
  98. return nullptr;
  99. int row, column;
  100. column_row_from_content_position(content_position, row, column);
  101. int item_index = (m_flow_direction == FlowDirection::LeftToRight)
  102. ? row * m_visual_column_count + column
  103. : column * m_visual_row_count + row;
  104. if (item_index < 0 || item_index >= item_count())
  105. return nullptr;
  106. return &get_item_data(item_index);
  107. }
  108. void IconView::model_did_update(unsigned flags)
  109. {
  110. AbstractView::model_did_update(flags);
  111. if (!model() || (flags & GUI::Model::InvalidateAllIndices)) {
  112. m_item_data_cache.clear();
  113. AbstractView::clear_selection();
  114. m_selected_count_cache = 0;
  115. m_first_selected_hint = 0;
  116. }
  117. m_item_data_cache_valid = false;
  118. update_content_size();
  119. update();
  120. }
  121. void IconView::update_content_size()
  122. {
  123. if (!model())
  124. return set_content_size({});
  125. int content_width;
  126. int content_height;
  127. if (m_flow_direction == FlowDirection::LeftToRight) {
  128. m_visual_column_count = max(1, available_size().width() / effective_item_size().width());
  129. if (m_visual_column_count)
  130. m_visual_row_count = ceil_div(model()->row_count(), m_visual_column_count);
  131. else
  132. m_visual_row_count = 0;
  133. content_width = m_visual_column_count * effective_item_size().width();
  134. content_height = m_visual_row_count * effective_item_size().height();
  135. } else {
  136. m_visual_row_count = max(1, available_size().height() / effective_item_size().height());
  137. if (m_visual_row_count)
  138. m_visual_column_count = ceil_div(model()->row_count(), m_visual_row_count);
  139. else
  140. m_visual_column_count = 0;
  141. content_width = m_visual_column_count * effective_item_size().width();
  142. content_height = available_size().height();
  143. }
  144. set_content_size({ content_width, content_height });
  145. if (!m_item_data_cache_valid)
  146. rebuild_item_cache();
  147. for (int item_index = 0; item_index < item_count(); item_index++) {
  148. auto& item_data = m_item_data_cache[item_index];
  149. if (item_data.is_valid())
  150. update_item_rects(item_index, item_data);
  151. }
  152. }
  153. Gfx::IntRect IconView::item_rect(int item_index) const
  154. {
  155. if (!m_visual_row_count || !m_visual_column_count)
  156. return {};
  157. int visual_row_index;
  158. int visual_column_index;
  159. if (m_flow_direction == FlowDirection::LeftToRight) {
  160. visual_row_index = item_index / m_visual_column_count;
  161. visual_column_index = item_index % m_visual_column_count;
  162. } else {
  163. visual_row_index = item_index % m_visual_row_count;
  164. visual_column_index = item_index / m_visual_row_count;
  165. }
  166. return {
  167. visual_column_index * effective_item_size().width(),
  168. visual_row_index * effective_item_size().height(),
  169. effective_item_size().width(),
  170. effective_item_size().height()
  171. };
  172. }
  173. ModelIndex IconView::index_at_event_position(const Gfx::IntPoint& position) const
  174. {
  175. VERIFY(model());
  176. auto adjusted_position = to_content_position(position);
  177. if (auto item_data = item_data_from_content_position(adjusted_position)) {
  178. if (item_data->is_containing(adjusted_position))
  179. return item_data->index;
  180. }
  181. return {};
  182. }
  183. void IconView::mousedown_event(MouseEvent& event)
  184. {
  185. if (!model())
  186. return AbstractView::mousedown_event(event);
  187. if (event.button() != MouseButton::Primary)
  188. return AbstractView::mousedown_event(event);
  189. auto index = index_at_event_position(event.position());
  190. if (index.is_valid()) {
  191. // We might start dragging this item, but not rubber-banding.
  192. return AbstractView::mousedown_event(event);
  193. }
  194. if (!(event.modifiers() & Mod_Ctrl)) {
  195. clear_selection();
  196. }
  197. auto adjusted_position = to_content_position(event.position());
  198. m_might_drag = false;
  199. if (selection_mode() == SelectionMode::MultiSelection) {
  200. m_rubber_banding = true;
  201. m_rubber_band_origin = adjusted_position;
  202. m_rubber_band_current = adjusted_position;
  203. }
  204. }
  205. void IconView::mouseup_event(MouseEvent& event)
  206. {
  207. if (m_rubber_banding && event.button() == MouseButton::Primary) {
  208. m_rubber_banding = false;
  209. if (m_out_of_view_timer)
  210. m_out_of_view_timer->stop();
  211. update(to_widget_rect(Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current)));
  212. }
  213. AbstractView::mouseup_event(event);
  214. }
  215. bool IconView::update_rubber_banding(const Gfx::IntPoint& input_position)
  216. {
  217. auto adjusted_position = to_content_position(input_position.constrained(widget_inner_rect()));
  218. if (m_rubber_band_current != adjusted_position) {
  219. auto prev_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  220. auto prev_rubber_band_fill_rect = prev_rect.shrunken(1, 1);
  221. m_rubber_band_current = adjusted_position;
  222. auto rubber_band_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  223. auto rubber_band_fill_rect = rubber_band_rect.shrunken(1, 1);
  224. for (auto& rect : prev_rubber_band_fill_rect.shatter(rubber_band_fill_rect))
  225. update(to_widget_rect(rect.inflated(1, 1)));
  226. for (auto& rect : rubber_band_fill_rect.shatter(prev_rubber_band_fill_rect))
  227. update(to_widget_rect(rect.inflated(1, 1)));
  228. // If the rectangle width or height is 0, we still want to be able
  229. // to match the items in the path. An easy work-around for this
  230. // is to simply set the width or height to 1
  231. auto ensure_rect = [](Gfx::IntRect& rect) {
  232. if (rect.width() <= 0)
  233. rect.set_width(1);
  234. if (rect.height() <= 0)
  235. rect.set_height(1);
  236. };
  237. ensure_rect(prev_rect);
  238. ensure_rect(rubber_band_rect);
  239. // Clearing the entire selection every time is very expensive,
  240. // determine what items may need to be deselected and what new
  241. // items may need to be selected. Avoid a ton of allocations.
  242. auto deselect_area = prev_rect.shatter(rubber_band_rect);
  243. auto select_area = rubber_band_rect.shatter(prev_rect);
  244. // Initialize all candidate's toggle flag. We need to know which
  245. // items we touched because the various rectangles likely will
  246. // contain the same item more than once
  247. for_each_item_intersecting_rects(deselect_area, [](ItemData& item_data) -> IterationDecision {
  248. item_data.selection_toggled = false;
  249. return IterationDecision::Continue;
  250. });
  251. for_each_item_intersecting_rects(select_area, [](ItemData& item_data) -> IterationDecision {
  252. item_data.selection_toggled = false;
  253. return IterationDecision::Continue;
  254. });
  255. // We're changing the selection and invalidating those items, so
  256. // no need to trigger a full re-render for each item
  257. set_suppress_update_on_selection_change(true);
  258. // Now toggle all items that are no longer in the selected area, once only
  259. for_each_item_intersecting_rects(deselect_area, [&](ItemData& item_data) -> IterationDecision {
  260. if (!item_data.selection_toggled && item_data.is_intersecting(prev_rect) && !item_data.is_intersecting(rubber_band_rect)) {
  261. item_data.selection_toggled = true;
  262. toggle_selection(item_data);
  263. update(to_widget_rect(item_data.rect()));
  264. }
  265. return IterationDecision::Continue;
  266. });
  267. // Now toggle all items that are in the new selected area, once only
  268. for_each_item_intersecting_rects(select_area, [&](ItemData& item_data) -> IterationDecision {
  269. if (!item_data.selection_toggled && !item_data.is_intersecting(prev_rect) && item_data.is_intersecting(rubber_band_rect)) {
  270. item_data.selection_toggled = true;
  271. toggle_selection(item_data);
  272. update(to_widget_rect(item_data.rect()));
  273. }
  274. return IterationDecision::Continue;
  275. });
  276. set_suppress_update_on_selection_change(false);
  277. return true;
  278. }
  279. return false;
  280. }
  281. #define SCROLL_OUT_OF_VIEW_HOT_MARGIN 20
  282. void IconView::mousemove_event(MouseEvent& event)
  283. {
  284. if (!model())
  285. return AbstractView::mousemove_event(event);
  286. if (m_rubber_banding) {
  287. auto in_view_rect = widget_inner_rect();
  288. in_view_rect.shrink(SCROLL_OUT_OF_VIEW_HOT_MARGIN, SCROLL_OUT_OF_VIEW_HOT_MARGIN);
  289. if (!in_view_rect.contains(event.position())) {
  290. if (!m_out_of_view_timer) {
  291. m_out_of_view_timer = add<Core::Timer>();
  292. m_out_of_view_timer->set_interval(100);
  293. m_out_of_view_timer->on_timeout = [this] {
  294. scroll_out_of_view_timer_fired();
  295. };
  296. }
  297. m_out_of_view_position = event.position();
  298. if (!m_out_of_view_timer->is_active())
  299. m_out_of_view_timer->start();
  300. } else {
  301. if (m_out_of_view_timer)
  302. m_out_of_view_timer->stop();
  303. }
  304. if (update_rubber_banding(event.position()))
  305. return;
  306. }
  307. AbstractView::mousemove_event(event);
  308. }
  309. void IconView::scroll_out_of_view_timer_fired()
  310. {
  311. auto scroll_to = to_content_position(m_out_of_view_position);
  312. // Adjust the scroll-to position by SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2
  313. // depending on which direction we're scrolling. This allows us to
  314. // start scrolling before we actually leave the visible area, which
  315. // is important when there is no space to further move the mouse. The
  316. // speed of scrolling is determined by the distance between the mouse
  317. // pointer and the widget's inner rect shrunken by the hot margin
  318. auto in_view_rect = widget_inner_rect().shrunken(SCROLL_OUT_OF_VIEW_HOT_MARGIN, SCROLL_OUT_OF_VIEW_HOT_MARGIN);
  319. int adjust_x = 0, adjust_y = 0;
  320. if (m_out_of_view_position.y() > in_view_rect.bottom())
  321. adjust_y = (SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2) + min(SCROLL_OUT_OF_VIEW_HOT_MARGIN, m_out_of_view_position.y() - in_view_rect.bottom());
  322. else if (m_out_of_view_position.y() < in_view_rect.top())
  323. adjust_y = -(SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2) + max(-SCROLL_OUT_OF_VIEW_HOT_MARGIN, m_out_of_view_position.y() - in_view_rect.top());
  324. if (m_out_of_view_position.x() > in_view_rect.right())
  325. adjust_x = (SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2) + min(SCROLL_OUT_OF_VIEW_HOT_MARGIN, m_out_of_view_position.x() - in_view_rect.right());
  326. else if (m_out_of_view_position.x() < in_view_rect.left())
  327. adjust_x = -(SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2) + max(-SCROLL_OUT_OF_VIEW_HOT_MARGIN, m_out_of_view_position.x() - in_view_rect.left());
  328. AbstractScrollableWidget::scroll_into_view({ scroll_to.translated(adjust_x, adjust_y), { 1, 1 } }, true, true);
  329. update_rubber_banding(m_out_of_view_position);
  330. }
  331. void IconView::update_item_rects(int item_index, ItemData& item_data) const
  332. {
  333. auto item_rect = this->item_rect(item_index);
  334. item_data.icon_rect.center_within(item_rect);
  335. item_data.icon_rect.translate_by(0, item_data.icon_offset_y);
  336. item_data.text_rect.center_horizontally_within(item_rect);
  337. item_data.text_rect.set_top(item_rect.y() + item_data.text_offset_y);
  338. }
  339. Gfx::IntRect IconView::content_rect(ModelIndex const& index) const
  340. {
  341. if (!index.is_valid())
  342. return {};
  343. auto& item_data = get_item_data(index.row());
  344. return item_data.rect();
  345. }
  346. Gfx::IntRect IconView::editing_rect(ModelIndex const& index) const
  347. {
  348. if (!index.is_valid())
  349. return {};
  350. auto& item_data = get_item_data(index.row());
  351. auto editing_rect = item_data.text_rect;
  352. editing_rect.set_height(font_for_index(index)->glyph_height() + 8);
  353. editing_rect.set_y(item_data.text_rect.y() - 2);
  354. return editing_rect;
  355. }
  356. void IconView::editing_widget_did_change(const ModelIndex& index)
  357. {
  358. if (m_editing_delegate->value().is_string()) {
  359. auto text_width = font_for_index(index)->width(m_editing_delegate->value().as_string());
  360. m_edit_widget_content_rect.set_width(min(text_width + 8, effective_item_size().width()));
  361. m_edit_widget_content_rect.center_horizontally_within(editing_rect(index).translated(frame_thickness(), frame_thickness()));
  362. update_edit_widget_position();
  363. }
  364. }
  365. Gfx::IntRect
  366. IconView::paint_invalidation_rect(const ModelIndex& index) const
  367. {
  368. if (!index.is_valid())
  369. return {};
  370. auto& item_data = get_item_data(index.row());
  371. return item_data.rect(true);
  372. }
  373. void IconView::did_change_hovered_index(const ModelIndex& old_index, const ModelIndex& new_index)
  374. {
  375. AbstractView::did_change_hovered_index(old_index, new_index);
  376. if (old_index.is_valid())
  377. get_item_rects(old_index.row(), get_item_data(old_index.row()), font_for_index(old_index));
  378. if (new_index.is_valid())
  379. get_item_rects(new_index.row(), get_item_data(new_index.row()), font_for_index(new_index));
  380. }
  381. void IconView::did_change_cursor_index(const ModelIndex& old_index, const ModelIndex& new_index)
  382. {
  383. AbstractView::did_change_cursor_index(old_index, new_index);
  384. if (old_index.is_valid())
  385. get_item_rects(old_index.row(), get_item_data(old_index.row()), font_for_index(old_index));
  386. if (new_index.is_valid())
  387. get_item_rects(new_index.row(), get_item_data(new_index.row()), font_for_index(new_index));
  388. }
  389. void IconView::get_item_rects(int item_index, ItemData& item_data, const Gfx::Font& font) const
  390. {
  391. auto item_rect = this->item_rect(item_index);
  392. item_data.icon_rect = Gfx::IntRect(0, 0, 32, 32).centered_within(item_rect);
  393. item_data.icon_offset_y = -font.glyph_height() - 6;
  394. item_data.icon_rect.translate_by(0, item_data.icon_offset_y);
  395. int unwrapped_text_width = font.width(item_data.text);
  396. int available_width = item_rect.width() - 6;
  397. item_data.text_rect = { 0, item_data.icon_rect.bottom() + 6 + 1, 0, font.glyph_height() };
  398. item_data.wrapped_text_lines.clear();
  399. if ((unwrapped_text_width > available_width) && (item_data.selected || m_hovered_index == item_data.index || cursor_index() == item_data.index || m_always_wrap_item_labels)) {
  400. int current_line_width = 0;
  401. int current_line_start = 0;
  402. int widest_line_width = 0;
  403. Utf8View utf8_view(item_data.text);
  404. auto it = utf8_view.begin();
  405. for (; it != utf8_view.end(); ++it) {
  406. auto code_point = *it;
  407. auto glyph_width = font.glyph_width(code_point);
  408. if ((current_line_width + glyph_width + font.glyph_spacing()) > available_width) {
  409. item_data.wrapped_text_lines.append(item_data.text.substring_view(current_line_start, utf8_view.byte_offset_of(it) - current_line_start));
  410. current_line_start = utf8_view.byte_offset_of(it);
  411. current_line_width = glyph_width;
  412. } else {
  413. current_line_width += glyph_width + font.glyph_spacing();
  414. }
  415. widest_line_width = max(widest_line_width, current_line_width);
  416. }
  417. if (current_line_width > 0) {
  418. item_data.wrapped_text_lines.append(item_data.text.substring_view(current_line_start, utf8_view.byte_offset_of(it) - current_line_start));
  419. }
  420. item_data.text_rect.set_width(widest_line_width);
  421. item_data.text_rect.center_horizontally_within(item_rect);
  422. item_data.text_rect.intersect(item_rect);
  423. item_data.text_rect.set_height(font.glyph_height() * item_data.wrapped_text_lines.size());
  424. item_data.text_rect.inflate(6, 6);
  425. item_data.text_rect_wrapped = item_data.text_rect;
  426. } else {
  427. item_data.text_rect.set_width(unwrapped_text_width);
  428. item_data.text_rect.inflate(6, 6);
  429. if (item_data.text_rect.width() > available_width)
  430. item_data.text_rect.set_width(available_width);
  431. item_data.text_rect.center_horizontally_within(item_rect);
  432. }
  433. item_data.text_rect.intersect(item_rect);
  434. item_data.text_offset_y = item_data.text_rect.y() - item_rect.y();
  435. }
  436. void IconView::second_paint_event(PaintEvent& event)
  437. {
  438. if (!m_rubber_banding)
  439. return;
  440. Painter painter(*this);
  441. painter.add_clip_rect(event.rect());
  442. painter.add_clip_rect(widget_inner_rect());
  443. painter.translate(frame_thickness(), frame_thickness());
  444. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  445. auto rubber_band_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  446. painter.fill_rect(rubber_band_rect, palette().rubber_band_fill());
  447. painter.draw_rect(rubber_band_rect, palette().rubber_band_border());
  448. }
  449. void IconView::paint_event(PaintEvent& event)
  450. {
  451. Color widget_background_color = palette().color(background_role());
  452. Frame::paint_event(event);
  453. Painter painter(*this);
  454. painter.add_clip_rect(widget_inner_rect());
  455. painter.add_clip_rect(event.rect());
  456. painter.fill_rect(event.rect(), fill_with_background_color() ? widget_background_color : Color::Transparent);
  457. if (!model())
  458. return;
  459. painter.translate(frame_thickness(), frame_thickness());
  460. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  461. auto selection_color = is_focused() ? palette().selection() : palette().inactive_selection();
  462. for_each_item_intersecting_rect(to_content_rect(event.rect()), [&](auto& item_data) -> IterationDecision {
  463. Color background_color;
  464. if (item_data.selected) {
  465. background_color = selection_color;
  466. } else {
  467. if (fill_with_background_color())
  468. background_color = widget_background_color;
  469. }
  470. auto icon = item_data.index.data(ModelRole::Icon);
  471. if (icon.is_icon()) {
  472. if (auto bitmap = icon.as_icon().bitmap_for_size(item_data.icon_rect.width())) {
  473. Gfx::IntRect destination = bitmap->rect();
  474. destination.center_within(item_data.icon_rect);
  475. if (item_data.selected) {
  476. auto tint = selection_color.with_alpha(100);
  477. painter.blit_filtered(destination.location(), *bitmap, bitmap->rect(), [&](auto src) { return src.blend(tint); });
  478. } else if (m_hovered_index.is_valid() && m_hovered_index == item_data.index) {
  479. painter.blit_brightened(destination.location(), *bitmap, bitmap->rect());
  480. } else {
  481. auto opacity = item_data.index.data(ModelRole::IconOpacity).as_float_or(1.0f);
  482. painter.blit(destination.location(), *bitmap, bitmap->rect(), opacity);
  483. }
  484. }
  485. }
  486. auto font = font_for_index(item_data.index);
  487. const auto& text_rect = item_data.text_rect;
  488. if (m_edit_index != item_data.index)
  489. painter.fill_rect(text_rect, background_color);
  490. if (is_focused() && item_data.index == cursor_index()) {
  491. painter.draw_rect(text_rect, widget_background_color);
  492. painter.draw_focus_rect(text_rect, palette().focus_outline());
  493. }
  494. if (!item_data.wrapped_text_lines.is_empty()) {
  495. // Item text would not fit in the item text rect, let's break it up into lines..
  496. const auto& lines = item_data.wrapped_text_lines;
  497. size_t number_of_text_lines = min((size_t)text_rect.height() / font->glyph_height(), lines.size());
  498. size_t previous_line_lengths = 0;
  499. for (size_t line_index = 0; line_index < number_of_text_lines; ++line_index) {
  500. Gfx::IntRect line_rect;
  501. line_rect.set_width(text_rect.width());
  502. line_rect.set_height(font->glyph_height());
  503. line_rect.center_horizontally_within(item_data.text_rect);
  504. line_rect.set_y(3 + item_data.text_rect.y() + line_index * font->glyph_height());
  505. line_rect.inflate(6, 0);
  506. // Shrink the line_rect on the last line to apply elision if there are more lines.
  507. if (number_of_text_lines - 1 == line_index && lines.size() > number_of_text_lines)
  508. line_rect.inflate(-(6 + 2 * font->max_glyph_width()), 0);
  509. draw_item_text(painter, item_data.index, item_data.selected, line_rect, lines[line_index], font, Gfx::TextAlignment::Center, Gfx::TextElision::Right, previous_line_lengths);
  510. previous_line_lengths += lines[line_index].length();
  511. }
  512. } else {
  513. draw_item_text(painter, item_data.index, item_data.selected, item_data.text_rect, item_data.text, font, Gfx::TextAlignment::Center, Gfx::TextElision::Right);
  514. }
  515. if (has_pending_drop() && item_data.index == drop_candidate_index()) {
  516. // FIXME: This visualization is not great, as it's also possible to drop things on the text label..
  517. painter.draw_rect(item_data.icon_rect.inflated(8, 8), palette().selection(), true);
  518. }
  519. return IterationDecision::Continue;
  520. });
  521. }
  522. int IconView::item_count() const
  523. {
  524. if (!model())
  525. return 0;
  526. return model()->row_count();
  527. }
  528. void IconView::did_update_selection()
  529. {
  530. AbstractView::did_update_selection();
  531. if (m_changing_selection)
  532. return;
  533. // Selection was modified externally, we need to synchronize our cache
  534. do_clear_selection();
  535. selection().for_each_index([&](const ModelIndex& index) {
  536. if (index.is_valid()) {
  537. auto item_index = model_index_to_item_index(index);
  538. if ((size_t)item_index < m_item_data_cache.size())
  539. do_add_selection(get_item_data(item_index));
  540. }
  541. });
  542. }
  543. void IconView::do_clear_selection()
  544. {
  545. for (size_t item_index = m_first_selected_hint; item_index < m_item_data_cache.size(); item_index++) {
  546. if (m_selected_count_cache == 0)
  547. break;
  548. auto& item_data = m_item_data_cache[item_index];
  549. if (!item_data.selected)
  550. continue;
  551. item_data.selected = false;
  552. m_selected_count_cache--;
  553. }
  554. m_first_selected_hint = 0;
  555. VERIFY(m_selected_count_cache == 0);
  556. }
  557. void IconView::clear_selection()
  558. {
  559. TemporaryChange change(m_changing_selection, true);
  560. AbstractView::clear_selection();
  561. do_clear_selection();
  562. }
  563. bool IconView::do_add_selection(ItemData& item_data)
  564. {
  565. if (!item_data.selected) {
  566. item_data.selected = true;
  567. m_selected_count_cache++;
  568. int item_index = &item_data - &m_item_data_cache[0];
  569. if (m_first_selected_hint > item_index)
  570. m_first_selected_hint = item_index;
  571. return true;
  572. }
  573. return false;
  574. }
  575. void IconView::add_selection(ItemData& item_data)
  576. {
  577. if (do_add_selection(item_data))
  578. AbstractView::add_selection(item_data.index);
  579. }
  580. void IconView::add_selection(const ModelIndex& new_index)
  581. {
  582. TemporaryChange change(m_changing_selection, true);
  583. auto item_index = model_index_to_item_index(new_index);
  584. add_selection(get_item_data(item_index));
  585. }
  586. void IconView::toggle_selection(ItemData& item_data)
  587. {
  588. if (!item_data.selected)
  589. add_selection(item_data);
  590. else
  591. remove_item_selection(item_data);
  592. }
  593. void IconView::toggle_selection(const ModelIndex& new_index)
  594. {
  595. TemporaryChange change(m_changing_selection, true);
  596. auto item_index = model_index_to_item_index(new_index);
  597. toggle_selection(get_item_data(item_index));
  598. }
  599. void IconView::remove_item_selection(ItemData& item_data)
  600. {
  601. if (!item_data.selected)
  602. return;
  603. TemporaryChange change(m_changing_selection, true);
  604. item_data.selected = false;
  605. VERIFY(m_selected_count_cache > 0);
  606. m_selected_count_cache--;
  607. int item_index = &item_data - &m_item_data_cache[0];
  608. if (m_first_selected_hint == item_index) {
  609. m_first_selected_hint = 0;
  610. while ((size_t)item_index < m_item_data_cache.size()) {
  611. if (m_item_data_cache[item_index].selected) {
  612. m_first_selected_hint = item_index;
  613. break;
  614. }
  615. item_index++;
  616. }
  617. }
  618. AbstractView::remove_selection(item_data.index);
  619. }
  620. void IconView::set_selection(const ModelIndex& new_index)
  621. {
  622. TemporaryChange change(m_changing_selection, true);
  623. do_clear_selection();
  624. auto item_index = model_index_to_item_index(new_index);
  625. auto& item_data = get_item_data(item_index);
  626. item_data.selected = true;
  627. m_selected_count_cache = 1;
  628. if (item_index < m_first_selected_hint)
  629. m_first_selected_hint = item_index;
  630. AbstractView::set_selection(new_index);
  631. }
  632. int IconView::items_per_page() const
  633. {
  634. if (m_flow_direction == FlowDirection::LeftToRight)
  635. return (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  636. return (visible_content_rect().width() / effective_item_size().width()) * m_visual_row_count;
  637. }
  638. void IconView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  639. {
  640. if (!model())
  641. return;
  642. auto& model = *this->model();
  643. if (!cursor_index().is_valid()) {
  644. set_cursor(model.index(0, model_column()), SelectionUpdate::Set);
  645. return;
  646. }
  647. auto new_row = cursor_index().row();
  648. switch (movement) {
  649. case CursorMovement::Right:
  650. if (m_flow_direction == FlowDirection::LeftToRight)
  651. new_row += 1;
  652. else
  653. new_row += m_visual_row_count;
  654. break;
  655. case CursorMovement::Left:
  656. if (m_flow_direction == FlowDirection::LeftToRight)
  657. new_row -= 1;
  658. else
  659. new_row -= m_visual_row_count;
  660. break;
  661. case CursorMovement::Up:
  662. if (m_flow_direction == FlowDirection::LeftToRight)
  663. new_row -= m_visual_column_count;
  664. else
  665. new_row -= 1;
  666. break;
  667. case CursorMovement::Down:
  668. if (m_flow_direction == FlowDirection::LeftToRight)
  669. new_row += m_visual_column_count;
  670. else
  671. new_row += 1;
  672. break;
  673. case CursorMovement::PageUp:
  674. new_row = max(0, cursor_index().row() - items_per_page());
  675. break;
  676. case CursorMovement::PageDown:
  677. new_row = min(model.row_count() - 1, cursor_index().row() + items_per_page());
  678. break;
  679. case CursorMovement::Home:
  680. new_row = 0;
  681. break;
  682. case CursorMovement::End:
  683. new_row = model.row_count() - 1;
  684. break;
  685. default:
  686. return;
  687. }
  688. auto new_index = model.index(new_row, cursor_index().column());
  689. if (new_index.is_valid())
  690. set_cursor(new_index, selection_update);
  691. }
  692. void IconView::set_flow_direction(FlowDirection flow_direction)
  693. {
  694. if (m_flow_direction == flow_direction)
  695. return;
  696. m_flow_direction = flow_direction;
  697. m_item_data_cache.clear();
  698. m_item_data_cache_valid = false;
  699. update();
  700. }
  701. template<typename Function>
  702. inline IterationDecision IconView::for_each_item_intersecting_rect(const Gfx::IntRect& rect, Function f) const
  703. {
  704. VERIFY(model());
  705. if (rect.is_empty())
  706. return IterationDecision::Continue;
  707. int begin_row, begin_column;
  708. column_row_from_content_position(rect.top_left(), begin_row, begin_column);
  709. int end_row, end_column;
  710. column_row_from_content_position(rect.bottom_right(), end_row, end_column);
  711. int items_per_flow_axis_step;
  712. int item_index;
  713. int last_index;
  714. if (m_flow_direction == FlowDirection::LeftToRight) {
  715. items_per_flow_axis_step = end_column - begin_column + 1;
  716. item_index = max(0, begin_row * m_visual_column_count + begin_column);
  717. last_index = min(item_count(), end_row * m_visual_column_count + end_column + 1);
  718. } else {
  719. items_per_flow_axis_step = end_row - begin_row + 1;
  720. item_index = max(0, begin_column * m_visual_row_count + begin_row);
  721. last_index = min(item_count(), end_column * m_visual_row_count + end_row + 1);
  722. }
  723. while (item_index < last_index) {
  724. for (int i = item_index; i < min(item_index + items_per_flow_axis_step, last_index); i++) {
  725. auto& item_data = get_item_data(i);
  726. if (item_data.is_intersecting(rect)) {
  727. auto decision = f(item_data);
  728. if (decision != IterationDecision::Continue)
  729. return decision;
  730. }
  731. }
  732. item_index += (m_flow_direction == FlowDirection::LeftToRight) ? m_visual_column_count : m_visual_row_count;
  733. };
  734. return IterationDecision::Continue;
  735. }
  736. template<typename Function>
  737. inline IterationDecision IconView::for_each_item_intersecting_rects(const Vector<Gfx::IntRect>& rects, Function f) const
  738. {
  739. for (auto& rect : rects) {
  740. auto decision = for_each_item_intersecting_rect(rect, f);
  741. if (decision != IterationDecision::Continue)
  742. return decision;
  743. }
  744. return IterationDecision::Continue;
  745. }
  746. }