IconView.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Utf8View.h>
  8. #include <LibCore/Timer.h>
  9. #include <LibGUI/IconView.h>
  10. #include <LibGUI/Model.h>
  11. #include <LibGUI/ModelEditingDelegate.h>
  12. #include <LibGUI/Painter.h>
  13. #include <LibGUI/Scrollbar.h>
  14. #include <LibGfx/Palette.h>
  15. REGISTER_WIDGET(GUI, IconView);
  16. namespace GUI {
  17. IconView::IconView()
  18. {
  19. set_fill_with_background_color(true);
  20. set_background_role(ColorRole::Base);
  21. set_foreground_role(ColorRole::BaseText);
  22. horizontal_scrollbar().set_visible(false);
  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(ModelIndex const& 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(Gfx::IntPoint const& 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(Gfx::IntPoint const& 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::Primary)
  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::Primary) {
  206. m_rubber_banding = false;
  207. set_automatic_scrolling_timer(false);
  208. update(to_widget_rect(Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current)));
  209. }
  210. AbstractView::mouseup_event(event);
  211. }
  212. bool IconView::update_rubber_banding(Gfx::IntPoint const& input_position)
  213. {
  214. auto adjusted_position = to_content_position(input_position.constrained(widget_inner_rect()));
  215. if (m_rubber_band_current != adjusted_position) {
  216. auto prev_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  217. auto prev_rubber_band_fill_rect = prev_rect.shrunken(1, 1);
  218. m_rubber_band_current = adjusted_position;
  219. auto rubber_band_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  220. auto rubber_band_fill_rect = rubber_band_rect.shrunken(1, 1);
  221. for (auto& rect : prev_rubber_band_fill_rect.shatter(rubber_band_fill_rect))
  222. update(to_widget_rect(rect.inflated(1, 1)));
  223. for (auto& rect : rubber_band_fill_rect.shatter(prev_rubber_band_fill_rect))
  224. update(to_widget_rect(rect.inflated(1, 1)));
  225. // If the rectangle width or height is 0, we still want to be able
  226. // to match the items in the path. An easy work-around for this
  227. // is to simply set the width or height to 1
  228. auto ensure_rect = [](Gfx::IntRect& rect) {
  229. if (rect.width() <= 0)
  230. rect.set_width(1);
  231. if (rect.height() <= 0)
  232. rect.set_height(1);
  233. };
  234. ensure_rect(prev_rect);
  235. ensure_rect(rubber_band_rect);
  236. // Clearing the entire selection every time is very expensive,
  237. // determine what items may need to be deselected and what new
  238. // items may need to be selected. Avoid a ton of allocations.
  239. auto deselect_area = prev_rect.shatter(rubber_band_rect);
  240. auto select_area = rubber_band_rect.shatter(prev_rect);
  241. // Initialize all candidate's toggle flag. We need to know which
  242. // items we touched because the various rectangles likely will
  243. // contain the same item more than once
  244. for_each_item_intersecting_rects(deselect_area, [](ItemData& item_data) -> IterationDecision {
  245. item_data.selection_toggled = false;
  246. return IterationDecision::Continue;
  247. });
  248. for_each_item_intersecting_rects(select_area, [](ItemData& item_data) -> IterationDecision {
  249. item_data.selection_toggled = false;
  250. return IterationDecision::Continue;
  251. });
  252. // We're changing the selection and invalidating those items, so
  253. // no need to trigger a full re-render for each item
  254. set_suppress_update_on_selection_change(true);
  255. // Now toggle all items that are no longer in the selected area, once only
  256. for_each_item_intersecting_rects(deselect_area, [&](ItemData& item_data) -> IterationDecision {
  257. if (!item_data.selection_toggled && item_data.is_intersecting(prev_rect) && !item_data.is_intersecting(rubber_band_rect)) {
  258. item_data.selection_toggled = true;
  259. toggle_selection(item_data);
  260. update(to_widget_rect(item_data.rect()));
  261. }
  262. return IterationDecision::Continue;
  263. });
  264. // Now toggle all items that are in the new selected area, once only
  265. for_each_item_intersecting_rects(select_area, [&](ItemData& item_data) -> IterationDecision {
  266. if (!item_data.selection_toggled && !item_data.is_intersecting(prev_rect) && item_data.is_intersecting(rubber_band_rect)) {
  267. item_data.selection_toggled = true;
  268. toggle_selection(item_data);
  269. update(to_widget_rect(item_data.rect()));
  270. }
  271. return IterationDecision::Continue;
  272. });
  273. set_suppress_update_on_selection_change(false);
  274. return true;
  275. }
  276. return false;
  277. }
  278. void IconView::mousemove_event(MouseEvent& event)
  279. {
  280. if (!model())
  281. return AbstractView::mousemove_event(event);
  282. m_rubber_band_scroll_delta = automatic_scroll_delta_from_position(event.position());
  283. if (m_rubber_banding) {
  284. m_out_of_view_position = event.position();
  285. set_automatic_scrolling_timer(!m_rubber_band_scroll_delta.is_null());
  286. if (update_rubber_banding(event.position()))
  287. return;
  288. }
  289. AbstractView::mousemove_event(event);
  290. }
  291. void IconView::on_automatic_scrolling_timer_fired()
  292. {
  293. AbstractView::on_automatic_scrolling_timer_fired();
  294. if (m_rubber_band_scroll_delta.is_null())
  295. return;
  296. vertical_scrollbar().increase_slider_by(m_rubber_band_scroll_delta.y());
  297. horizontal_scrollbar().increase_slider_by(m_rubber_band_scroll_delta.x());
  298. update_rubber_banding(m_out_of_view_position);
  299. }
  300. void IconView::update_item_rects(int item_index, ItemData& item_data) const
  301. {
  302. auto item_rect = this->item_rect(item_index);
  303. item_data.icon_rect.center_within(item_rect);
  304. item_data.icon_rect.translate_by(0, item_data.icon_offset_y);
  305. item_data.text_rect.center_horizontally_within(item_rect);
  306. item_data.text_rect.set_top(item_rect.y() + item_data.text_offset_y);
  307. }
  308. Gfx::IntRect IconView::content_rect(ModelIndex const& index) const
  309. {
  310. if (!index.is_valid())
  311. return {};
  312. auto& item_data = get_item_data(index.row());
  313. return item_data.rect();
  314. }
  315. Gfx::IntRect IconView::editing_rect(ModelIndex const& index) const
  316. {
  317. if (!index.is_valid())
  318. return {};
  319. auto& item_data = get_item_data(index.row());
  320. auto editing_rect = item_data.text_rect;
  321. editing_rect.set_height(font_for_index(index)->glyph_height() + 8);
  322. editing_rect.set_y(item_data.text_rect.y() - 2);
  323. return editing_rect;
  324. }
  325. void IconView::editing_widget_did_change(ModelIndex const& index)
  326. {
  327. if (m_editing_delegate->value().is_string()) {
  328. auto text_width = font_for_index(index)->width(m_editing_delegate->value().as_string());
  329. m_edit_widget_content_rect.set_width(min(text_width + 8, effective_item_size().width()));
  330. m_edit_widget_content_rect.center_horizontally_within(editing_rect(index).translated(frame_thickness(), frame_thickness()));
  331. update_edit_widget_position();
  332. }
  333. }
  334. Gfx::IntRect
  335. IconView::paint_invalidation_rect(ModelIndex const& index) const
  336. {
  337. if (!index.is_valid())
  338. return {};
  339. auto& item_data = get_item_data(index.row());
  340. return item_data.rect(true);
  341. }
  342. void IconView::did_change_hovered_index(ModelIndex const& old_index, ModelIndex const& new_index)
  343. {
  344. AbstractView::did_change_hovered_index(old_index, new_index);
  345. if (old_index.is_valid())
  346. get_item_rects(old_index.row(), get_item_data(old_index.row()), font_for_index(old_index));
  347. if (new_index.is_valid())
  348. get_item_rects(new_index.row(), get_item_data(new_index.row()), font_for_index(new_index));
  349. }
  350. void IconView::did_change_cursor_index(ModelIndex const& old_index, ModelIndex const& new_index)
  351. {
  352. AbstractView::did_change_cursor_index(old_index, new_index);
  353. if (old_index.is_valid())
  354. get_item_rects(old_index.row(), get_item_data(old_index.row()), font_for_index(old_index));
  355. if (new_index.is_valid())
  356. get_item_rects(new_index.row(), get_item_data(new_index.row()), font_for_index(new_index));
  357. }
  358. void IconView::get_item_rects(int item_index, ItemData& item_data, Gfx::Font const& font) const
  359. {
  360. auto item_rect = this->item_rect(item_index);
  361. item_data.icon_rect = Gfx::IntRect(0, 0, 32, 32).centered_within(item_rect);
  362. item_data.icon_offset_y = -font.glyph_height() - 6;
  363. item_data.icon_rect.translate_by(0, item_data.icon_offset_y);
  364. int unwrapped_text_width = font.width(item_data.text);
  365. int available_width = item_rect.width() - 6;
  366. item_data.text_rect = { 0, item_data.icon_rect.bottom() + 6 + 1, 0, font.glyph_height() };
  367. item_data.wrapped_text_lines.clear();
  368. 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)) {
  369. int current_line_width = 0;
  370. int current_line_start = 0;
  371. int widest_line_width = 0;
  372. Utf8View utf8_view(item_data.text);
  373. auto it = utf8_view.begin();
  374. for (; it != utf8_view.end(); ++it) {
  375. auto code_point = *it;
  376. auto glyph_width = font.glyph_width(code_point);
  377. if ((current_line_width + glyph_width + font.glyph_spacing()) > available_width) {
  378. item_data.wrapped_text_lines.append(item_data.text.substring_view(current_line_start, utf8_view.byte_offset_of(it) - current_line_start));
  379. current_line_start = utf8_view.byte_offset_of(it);
  380. current_line_width = glyph_width;
  381. } else {
  382. current_line_width += glyph_width + font.glyph_spacing();
  383. }
  384. widest_line_width = max(widest_line_width, current_line_width);
  385. }
  386. if (current_line_width > 0) {
  387. item_data.wrapped_text_lines.append(item_data.text.substring_view(current_line_start, utf8_view.byte_offset_of(it) - current_line_start));
  388. }
  389. item_data.text_rect.set_width(widest_line_width);
  390. item_data.text_rect.center_horizontally_within(item_rect);
  391. item_data.text_rect.intersect(item_rect);
  392. item_data.text_rect.set_height(font.glyph_height() * item_data.wrapped_text_lines.size());
  393. item_data.text_rect.inflate(6, 6);
  394. item_data.text_rect_wrapped = item_data.text_rect;
  395. } else {
  396. item_data.text_rect.set_width(unwrapped_text_width);
  397. item_data.text_rect.inflate(6, 6);
  398. if (item_data.text_rect.width() > available_width)
  399. item_data.text_rect.set_width(available_width);
  400. item_data.text_rect.center_horizontally_within(item_rect);
  401. }
  402. item_data.text_rect.intersect(item_rect);
  403. item_data.text_offset_y = item_data.text_rect.y() - item_rect.y();
  404. }
  405. void IconView::second_paint_event(PaintEvent& event)
  406. {
  407. if (!m_rubber_banding)
  408. return;
  409. Painter painter(*this);
  410. painter.add_clip_rect(event.rect());
  411. painter.add_clip_rect(widget_inner_rect());
  412. painter.translate(frame_thickness(), frame_thickness());
  413. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  414. auto rubber_band_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  415. painter.fill_rect(rubber_band_rect, palette().rubber_band_fill());
  416. painter.draw_rect(rubber_band_rect, palette().rubber_band_border());
  417. }
  418. void IconView::paint_event(PaintEvent& event)
  419. {
  420. Color widget_background_color = palette().color(background_role());
  421. Frame::paint_event(event);
  422. Painter painter(*this);
  423. painter.add_clip_rect(widget_inner_rect());
  424. painter.add_clip_rect(event.rect());
  425. painter.fill_rect(event.rect(), fill_with_background_color() ? widget_background_color : Color::Transparent);
  426. if (!model())
  427. return;
  428. painter.translate(frame_thickness(), frame_thickness());
  429. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  430. auto selection_color = is_focused() ? palette().selection() : palette().inactive_selection();
  431. for_each_item_intersecting_rect(to_content_rect(event.rect()), [&](auto& item_data) -> IterationDecision {
  432. Color background_color;
  433. if (item_data.selected) {
  434. background_color = selection_color;
  435. } else {
  436. if (fill_with_background_color())
  437. background_color = widget_background_color;
  438. }
  439. auto icon = item_data.index.data(ModelRole::Icon);
  440. if (icon.is_icon()) {
  441. if (auto bitmap = icon.as_icon().bitmap_for_size(item_data.icon_rect.width())) {
  442. Gfx::IntRect destination = bitmap->rect();
  443. destination.center_within(item_data.icon_rect);
  444. if (item_data.selected) {
  445. auto tint = selection_color.with_alpha(100);
  446. painter.blit_filtered(destination.location(), *bitmap, bitmap->rect(), [&](auto src) { return src.blend(tint); });
  447. } else if (m_hovered_index.is_valid() && m_hovered_index == item_data.index) {
  448. painter.blit_brightened(destination.location(), *bitmap, bitmap->rect());
  449. } else {
  450. auto opacity = item_data.index.data(ModelRole::IconOpacity).as_float_or(1.0f);
  451. painter.blit(destination.location(), *bitmap, bitmap->rect(), opacity);
  452. }
  453. }
  454. }
  455. auto font = font_for_index(item_data.index);
  456. const auto& text_rect = item_data.text_rect;
  457. if (m_edit_index != item_data.index)
  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. size_t previous_line_lengths = 0;
  468. for (size_t line_index = 0; line_index < number_of_text_lines; ++line_index) {
  469. Gfx::IntRect line_rect;
  470. line_rect.set_width(text_rect.width());
  471. line_rect.set_height(font->glyph_height());
  472. line_rect.center_horizontally_within(item_data.text_rect);
  473. line_rect.set_y(3 + item_data.text_rect.y() + line_index * font->glyph_height());
  474. line_rect.inflate(6, 0);
  475. // Shrink the line_rect on the last line to apply elision if there are more lines.
  476. if (number_of_text_lines - 1 == line_index && lines.size() > number_of_text_lines)
  477. line_rect.inflate(-(6 + 2 * font->max_glyph_width()), 0);
  478. 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);
  479. previous_line_lengths += lines[line_index].length();
  480. }
  481. } else {
  482. draw_item_text(painter, item_data.index, item_data.selected, item_data.text_rect, item_data.text, font, Gfx::TextAlignment::Center, Gfx::TextElision::Right);
  483. }
  484. if (has_pending_drop() && item_data.index == drop_candidate_index()) {
  485. // FIXME: This visualization is not great, as it's also possible to drop things on the text label..
  486. painter.draw_rect(item_data.icon_rect.inflated(8, 8), palette().selection(), true);
  487. }
  488. return IterationDecision::Continue;
  489. });
  490. }
  491. int IconView::item_count() const
  492. {
  493. if (!model())
  494. return 0;
  495. return model()->row_count();
  496. }
  497. void IconView::did_update_selection()
  498. {
  499. AbstractView::did_update_selection();
  500. if (m_changing_selection)
  501. return;
  502. // Selection was modified externally, we need to synchronize our cache
  503. do_clear_selection();
  504. selection().for_each_index([&](ModelIndex const& index) {
  505. if (index.is_valid()) {
  506. auto item_index = model_index_to_item_index(index);
  507. if ((size_t)item_index < m_item_data_cache.size())
  508. do_add_selection(get_item_data(item_index));
  509. }
  510. });
  511. }
  512. void IconView::do_clear_selection()
  513. {
  514. for (size_t item_index = m_first_selected_hint; item_index < m_item_data_cache.size(); item_index++) {
  515. if (m_selected_count_cache == 0)
  516. break;
  517. auto& item_data = m_item_data_cache[item_index];
  518. if (!item_data.selected)
  519. continue;
  520. item_data.selected = false;
  521. m_selected_count_cache--;
  522. }
  523. m_first_selected_hint = 0;
  524. VERIFY(m_selected_count_cache == 0);
  525. }
  526. void IconView::clear_selection()
  527. {
  528. TemporaryChange change(m_changing_selection, true);
  529. AbstractView::clear_selection();
  530. do_clear_selection();
  531. }
  532. bool IconView::do_add_selection(ItemData& item_data)
  533. {
  534. if (!item_data.selected) {
  535. item_data.selected = true;
  536. m_selected_count_cache++;
  537. int item_index = &item_data - &m_item_data_cache[0];
  538. if (m_first_selected_hint > item_index)
  539. m_first_selected_hint = item_index;
  540. return true;
  541. }
  542. return false;
  543. }
  544. void IconView::add_selection(ItemData& item_data)
  545. {
  546. if (do_add_selection(item_data))
  547. AbstractView::add_selection(item_data.index);
  548. }
  549. void IconView::add_selection(ModelIndex const& new_index)
  550. {
  551. TemporaryChange change(m_changing_selection, true);
  552. auto item_index = model_index_to_item_index(new_index);
  553. add_selection(get_item_data(item_index));
  554. }
  555. void IconView::toggle_selection(ItemData& item_data)
  556. {
  557. if (!item_data.selected)
  558. add_selection(item_data);
  559. else
  560. remove_item_selection(item_data);
  561. }
  562. void IconView::toggle_selection(ModelIndex const& new_index)
  563. {
  564. TemporaryChange change(m_changing_selection, true);
  565. auto item_index = model_index_to_item_index(new_index);
  566. toggle_selection(get_item_data(item_index));
  567. }
  568. void IconView::remove_item_selection(ItemData& item_data)
  569. {
  570. if (!item_data.selected)
  571. return;
  572. TemporaryChange change(m_changing_selection, true);
  573. item_data.selected = false;
  574. VERIFY(m_selected_count_cache > 0);
  575. m_selected_count_cache--;
  576. int item_index = &item_data - &m_item_data_cache[0];
  577. if (m_first_selected_hint == item_index) {
  578. m_first_selected_hint = 0;
  579. while ((size_t)item_index < m_item_data_cache.size()) {
  580. if (m_item_data_cache[item_index].selected) {
  581. m_first_selected_hint = item_index;
  582. break;
  583. }
  584. item_index++;
  585. }
  586. }
  587. AbstractView::remove_selection(item_data.index);
  588. }
  589. void IconView::set_selection(ModelIndex const& new_index)
  590. {
  591. TemporaryChange change(m_changing_selection, true);
  592. do_clear_selection();
  593. auto item_index = model_index_to_item_index(new_index);
  594. auto& item_data = get_item_data(item_index);
  595. item_data.selected = true;
  596. m_selected_count_cache = 1;
  597. if (item_index < m_first_selected_hint)
  598. m_first_selected_hint = item_index;
  599. AbstractView::set_selection(new_index);
  600. }
  601. int IconView::items_per_page() const
  602. {
  603. if (m_flow_direction == FlowDirection::LeftToRight)
  604. return (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  605. return (visible_content_rect().width() / effective_item_size().width()) * m_visual_row_count;
  606. }
  607. void IconView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  608. {
  609. if (!model())
  610. return;
  611. auto& model = *this->model();
  612. if (!cursor_index().is_valid()) {
  613. set_cursor(model.index(0, model_column()), SelectionUpdate::Set);
  614. return;
  615. }
  616. auto new_row = cursor_index().row();
  617. switch (movement) {
  618. case CursorMovement::Right:
  619. if (m_flow_direction == FlowDirection::LeftToRight)
  620. new_row += 1;
  621. else
  622. new_row += m_visual_row_count;
  623. break;
  624. case CursorMovement::Left:
  625. if (m_flow_direction == FlowDirection::LeftToRight)
  626. new_row -= 1;
  627. else
  628. new_row -= m_visual_row_count;
  629. break;
  630. case CursorMovement::Up:
  631. if (m_flow_direction == FlowDirection::LeftToRight)
  632. new_row -= m_visual_column_count;
  633. else
  634. new_row -= 1;
  635. break;
  636. case CursorMovement::Down:
  637. if (m_flow_direction == FlowDirection::LeftToRight)
  638. new_row += m_visual_column_count;
  639. else
  640. new_row += 1;
  641. break;
  642. case CursorMovement::PageUp:
  643. new_row = max(0, cursor_index().row() - items_per_page());
  644. break;
  645. case CursorMovement::PageDown:
  646. new_row = min(model.row_count() - 1, cursor_index().row() + items_per_page());
  647. break;
  648. case CursorMovement::Home:
  649. new_row = 0;
  650. break;
  651. case CursorMovement::End:
  652. new_row = model.row_count() - 1;
  653. break;
  654. default:
  655. return;
  656. }
  657. auto new_index = model.index(new_row, cursor_index().column());
  658. if (new_index.is_valid())
  659. set_cursor(new_index, selection_update);
  660. }
  661. void IconView::set_flow_direction(FlowDirection flow_direction)
  662. {
  663. if (m_flow_direction == flow_direction)
  664. return;
  665. m_flow_direction = flow_direction;
  666. m_item_data_cache.clear();
  667. m_item_data_cache_valid = false;
  668. update();
  669. }
  670. template<typename Function>
  671. inline IterationDecision IconView::for_each_item_intersecting_rect(Gfx::IntRect const& rect, Function f) const
  672. {
  673. VERIFY(model());
  674. if (rect.is_empty())
  675. return IterationDecision::Continue;
  676. int begin_row, begin_column;
  677. column_row_from_content_position(rect.top_left(), begin_row, begin_column);
  678. int end_row, end_column;
  679. column_row_from_content_position(rect.bottom_right(), end_row, end_column);
  680. int items_per_flow_axis_step;
  681. int item_index;
  682. int last_index;
  683. if (m_flow_direction == FlowDirection::LeftToRight) {
  684. items_per_flow_axis_step = end_column - begin_column + 1;
  685. item_index = max(0, begin_row * m_visual_column_count + begin_column);
  686. last_index = min(item_count(), end_row * m_visual_column_count + end_column + 1);
  687. } else {
  688. items_per_flow_axis_step = end_row - begin_row + 1;
  689. item_index = max(0, begin_column * m_visual_row_count + begin_row);
  690. last_index = min(item_count(), end_column * m_visual_row_count + end_row + 1);
  691. }
  692. while (item_index < last_index) {
  693. for (int i = item_index; i < min(item_index + items_per_flow_axis_step, last_index); i++) {
  694. auto& item_data = get_item_data(i);
  695. if (item_data.is_intersecting(rect)) {
  696. auto decision = f(item_data);
  697. if (decision != IterationDecision::Continue)
  698. return decision;
  699. }
  700. }
  701. item_index += (m_flow_direction == FlowDirection::LeftToRight) ? m_visual_column_count : m_visual_row_count;
  702. };
  703. return IterationDecision::Continue;
  704. }
  705. template<typename Function>
  706. inline IterationDecision IconView::for_each_item_intersecting_rects(Vector<Gfx::IntRect> const& rects, Function f) const
  707. {
  708. for (auto& rect : rects) {
  709. auto decision = for_each_item_intersecting_rect(rect, f);
  710. if (decision != IterationDecision::Continue)
  711. return decision;
  712. }
  713. return IterationDecision::Continue;
  714. }
  715. }