IconView.cpp 30 KB

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