IconView.cpp 30 KB

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