IconView.cpp 31 KB

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