IconView.cpp 30 KB

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