IconView.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <AK/Utf8View.h>
  28. #include <LibCore/Timer.h>
  29. #include <LibGUI/DragOperation.h>
  30. #include <LibGUI/IconView.h>
  31. #include <LibGUI/Model.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibGUI/ScrollBar.h>
  34. #include <LibGfx/Palette.h>
  35. //#define DRAGDROP_DEBUG
  36. namespace GUI {
  37. IconView::IconView()
  38. {
  39. set_fill_with_background_color(true);
  40. set_background_role(ColorRole::Base);
  41. set_foreground_role(ColorRole::BaseText);
  42. horizontal_scrollbar().set_visible(false);
  43. }
  44. IconView::~IconView()
  45. {
  46. }
  47. void IconView::select_all()
  48. {
  49. for (int item_index = 0; item_index < item_count(); ++item_index) {
  50. auto& item_data = m_item_data_cache[item_index];
  51. if (!item_data.selected) {
  52. if (item_data.is_valid())
  53. add_selection(item_data);
  54. else
  55. add_selection(model()->index(item_index, model_column()));
  56. }
  57. }
  58. }
  59. void IconView::scroll_into_view(const ModelIndex& index, bool scroll_horizontally, bool scroll_vertically)
  60. {
  61. if (!index.is_valid())
  62. return;
  63. ScrollableWidget::scroll_into_view(item_rect(index.row()), scroll_horizontally, scroll_vertically);
  64. }
  65. void IconView::resize_event(ResizeEvent& event)
  66. {
  67. AbstractView::resize_event(event);
  68. update_content_size();
  69. }
  70. void IconView::reinit_item_cache() const
  71. {
  72. auto prev_item_count = m_item_data_cache.size();
  73. size_t new_item_count = item_count();
  74. auto items_to_invalidate = min(prev_item_count, new_item_count);
  75. // if the new number of items is less, check if any of the
  76. // ones not in the list anymore was selected
  77. for (size_t i = new_item_count; i < m_item_data_cache.size(); i++) {
  78. auto& item_data = m_item_data_cache[i];
  79. if (item_data.selected) {
  80. ASSERT(m_selected_count_cache > 0);
  81. m_selected_count_cache--;
  82. }
  83. }
  84. if ((size_t)m_first_selected_hint >= new_item_count)
  85. m_first_selected_hint = 0;
  86. m_item_data_cache.resize(new_item_count);
  87. for (size_t i = 0; i < items_to_invalidate; i++) {
  88. auto& item_data = m_item_data_cache[i];
  89. // TODO: It's unfortunate that we have no way to know whether any
  90. // data actually changed, so we have to invalidate *everyone*
  91. if (item_data.is_valid() /* && !model()->is_valid(item_data.index)*/)
  92. item_data.invalidate();
  93. if (item_data.selected && i < (size_t)m_first_selected_hint)
  94. m_first_selected_hint = (int)i;
  95. }
  96. m_item_data_cache_valid = true;
  97. }
  98. auto IconView::get_item_data(int item_index) const -> ItemData&
  99. {
  100. if (!m_item_data_cache_valid)
  101. reinit_item_cache();
  102. auto& item_data = m_item_data_cache[item_index];
  103. if (item_data.is_valid())
  104. return item_data;
  105. item_data.index = model()->index(item_index, model_column());
  106. item_data.data = item_data.index.data();
  107. get_item_rects(item_index, item_data, font_for_index(item_data.index));
  108. item_data.valid = true;
  109. return item_data;
  110. }
  111. auto IconView::item_data_from_content_position(const Gfx::IntPoint& content_position) const -> ItemData*
  112. {
  113. if (!m_visual_row_count || !m_visual_column_count)
  114. return nullptr;
  115. int row, column;
  116. column_row_from_content_position(content_position, row, column);
  117. int item_index = row * m_visual_column_count + column;
  118. if (item_index < 0 || item_index >= item_count())
  119. return nullptr;
  120. return &get_item_data(item_index);
  121. }
  122. void IconView::did_update_model(unsigned flags)
  123. {
  124. AbstractView::did_update_model(flags);
  125. if (!model() || (flags & GUI::Model::InvalidateAllIndexes)) {
  126. m_item_data_cache.clear();
  127. AbstractView::clear_selection();
  128. m_selected_count_cache = 0;
  129. m_first_selected_hint = 0;
  130. }
  131. m_item_data_cache_valid = false;
  132. update_content_size();
  133. update();
  134. }
  135. void IconView::update_content_size()
  136. {
  137. if (!model())
  138. return set_content_size({});
  139. m_visual_column_count = max(1, available_size().width() / effective_item_size().width());
  140. if (m_visual_column_count)
  141. m_visual_row_count = ceil_div(model()->row_count(), m_visual_column_count);
  142. else
  143. m_visual_row_count = 0;
  144. int content_width = available_size().width();
  145. int content_height = m_visual_row_count * effective_item_size().height();
  146. set_content_size({ content_width, content_height });
  147. if (!m_item_data_cache_valid)
  148. reinit_item_cache();
  149. for (int item_index = 0; item_index < item_count(); item_index++) {
  150. auto& item_data = m_item_data_cache[item_index];
  151. if (item_data.is_valid())
  152. update_item_rects(item_index, item_data);
  153. }
  154. }
  155. Gfx::IntRect IconView::item_rect(int item_index) const
  156. {
  157. if (!m_visual_row_count || !m_visual_column_count)
  158. return {};
  159. int visual_row_index = item_index / m_visual_column_count;
  160. int visual_column_index = item_index % m_visual_column_count;
  161. return {
  162. visual_column_index * effective_item_size().width(),
  163. visual_row_index * effective_item_size().height(),
  164. effective_item_size().width(),
  165. effective_item_size().height()
  166. };
  167. }
  168. ModelIndex IconView::index_at_event_position(const Gfx::IntPoint& position) const
  169. {
  170. ASSERT(model());
  171. auto adjusted_position = to_content_position(position);
  172. if (auto item_data = item_data_from_content_position(adjusted_position)) {
  173. if (item_data->is_containing(adjusted_position))
  174. return item_data->index;
  175. }
  176. return {};
  177. }
  178. void IconView::mousedown_event(MouseEvent& event)
  179. {
  180. if (!model())
  181. return AbstractView::mousedown_event(event);
  182. if (event.button() != MouseButton::Left)
  183. return AbstractView::mousedown_event(event);
  184. auto index = index_at_event_position(event.position());
  185. if (index.is_valid()) {
  186. // We might start dragging this item, but not rubber-banding.
  187. return AbstractView::mousedown_event(event);
  188. }
  189. if (event.modifiers() & Mod_Ctrl) {
  190. m_rubber_banding_store_selection = true;
  191. } else {
  192. clear_selection();
  193. m_rubber_banding_store_selection = false;
  194. }
  195. auto adjusted_position = to_content_position(event.position());
  196. m_might_drag = false;
  197. if (is_multi_select()) {
  198. m_rubber_banding = true;
  199. m_rubber_band_origin = adjusted_position;
  200. m_rubber_band_current = adjusted_position;
  201. }
  202. }
  203. void IconView::mouseup_event(MouseEvent& event)
  204. {
  205. if (m_rubber_banding && event.button() == MouseButton::Left) {
  206. m_rubber_banding = false;
  207. if (m_out_of_view_timer)
  208. m_out_of_view_timer->stop();
  209. update();
  210. }
  211. AbstractView::mouseup_event(event);
  212. }
  213. void IconView::drag_move_event(DragEvent& event)
  214. {
  215. auto index = index_at_event_position(event.position());
  216. ModelIndex new_drop_candidate_index;
  217. if (index.is_valid()) {
  218. bool acceptable = model()->accepts_drag(index, event.data_type());
  219. #ifdef DRAGDROP_DEBUG
  220. dbg() << "Drag of type '" << event.data_type() << "' moving over " << index << ", acceptable: " << acceptable;
  221. #endif
  222. if (acceptable)
  223. new_drop_candidate_index = index;
  224. }
  225. if (m_drop_candidate_index != new_drop_candidate_index) {
  226. m_drop_candidate_index = new_drop_candidate_index;
  227. update();
  228. }
  229. event.accept();
  230. }
  231. bool IconView::update_rubber_banding(const Gfx::IntPoint& position)
  232. {
  233. auto adjusted_position = to_content_position(position);
  234. if (m_rubber_band_current != adjusted_position) {
  235. auto prev_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  236. m_rubber_band_current = adjusted_position;
  237. auto rubber_band_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  238. // If the rectangle width or height is 0, we still want to be able
  239. // to match the items in the path. An easy work-around for this
  240. // is to simply set the width or height to 1
  241. auto ensure_rect = [](Gfx::IntRect& rect) {
  242. if (rect.width() <= 0)
  243. rect.set_width(1);
  244. if (rect.height() <= 0)
  245. rect.set_height(1);
  246. };
  247. ensure_rect(prev_rect);
  248. ensure_rect(rubber_band_rect);
  249. // Clearing the entire selection every time is very expensive,
  250. // determine what items may need to be deselected and what new
  251. // items may need to be selected. Avoid a ton of allocations.
  252. auto deselect_area = prev_rect.shatter(rubber_band_rect);
  253. auto select_area = rubber_band_rect.shatter(prev_rect);
  254. // Initialize all candidate's toggle flag. We need to know which
  255. // items we touched because the various rectangles likely will
  256. // contain the same item more than once
  257. for_each_item_intersecting_rects(deselect_area, [](ItemData& item_data) -> IterationDecision {
  258. item_data.selection_toggled = false;
  259. return IterationDecision::Continue;
  260. });
  261. for_each_item_intersecting_rects(select_area, [](ItemData& item_data) -> IterationDecision {
  262. item_data.selection_toggled = false;
  263. return IterationDecision::Continue;
  264. });
  265. // Now toggle all items that are no longer in the selected area, once only
  266. for_each_item_intersecting_rects(deselect_area, [&](ItemData& item_data) -> IterationDecision {
  267. if (!item_data.selection_toggled && item_data.is_intersecting(prev_rect) && !item_data.is_intersecting(rubber_band_rect)) {
  268. item_data.selection_toggled = true;
  269. toggle_selection(item_data);
  270. }
  271. return IterationDecision::Continue;
  272. });
  273. // Now toggle all items that are in the new selected area, once only
  274. for_each_item_intersecting_rects(select_area, [&](ItemData& item_data) -> IterationDecision {
  275. if (!item_data.selection_toggled && !item_data.is_intersecting(prev_rect) && item_data.is_intersecting(rubber_band_rect)) {
  276. item_data.selection_toggled = true;
  277. toggle_selection(item_data);
  278. }
  279. return IterationDecision::Continue;
  280. });
  281. update();
  282. return true;
  283. }
  284. return false;
  285. }
  286. #define SCROLL_OUT_OF_VIEW_HOT_MARGIN 20
  287. void IconView::mousemove_event(MouseEvent& event)
  288. {
  289. if (!model())
  290. return AbstractView::mousemove_event(event);
  291. if (m_rubber_banding) {
  292. auto in_view_rect = widget_inner_rect();
  293. in_view_rect.shrink(SCROLL_OUT_OF_VIEW_HOT_MARGIN, SCROLL_OUT_OF_VIEW_HOT_MARGIN);
  294. if (!in_view_rect.contains(event.position())) {
  295. if (!m_out_of_view_timer) {
  296. m_out_of_view_timer = add<Core::Timer>();
  297. m_out_of_view_timer->set_interval(100);
  298. m_out_of_view_timer->on_timeout = [this] {
  299. scroll_out_of_view_timer_fired();
  300. };
  301. }
  302. m_out_of_view_position = event.position();
  303. if (!m_out_of_view_timer->is_active())
  304. m_out_of_view_timer->start();
  305. } else {
  306. if (m_out_of_view_timer)
  307. m_out_of_view_timer->stop();
  308. }
  309. if (update_rubber_banding(event.position()))
  310. return;
  311. }
  312. AbstractView::mousemove_event(event);
  313. }
  314. void IconView::scroll_out_of_view_timer_fired()
  315. {
  316. auto scroll_to = to_content_position(m_out_of_view_position);
  317. // Adjust the scroll-to position by SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2
  318. // depending on which direction we're scrolling. This allows us to
  319. // start scrolling before we actually leave the visible area, which
  320. // is important when there is no space to further move the mouse. The
  321. // speed of scrolling is determined by the distance between the mouse
  322. // pointer and the widget's inner rect shrunken by the hot margin
  323. auto in_view_rect = widget_inner_rect().shrunken(SCROLL_OUT_OF_VIEW_HOT_MARGIN, SCROLL_OUT_OF_VIEW_HOT_MARGIN);
  324. int adjust_x = 0, adjust_y = 0;
  325. if (m_out_of_view_position.y() > in_view_rect.bottom())
  326. adjust_y = (SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2) + min(SCROLL_OUT_OF_VIEW_HOT_MARGIN, m_out_of_view_position.y() - in_view_rect.bottom());
  327. else if (m_out_of_view_position.y() < in_view_rect.top())
  328. adjust_y = -(SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2) + max(-SCROLL_OUT_OF_VIEW_HOT_MARGIN, m_out_of_view_position.y() - in_view_rect.top());
  329. if (m_out_of_view_position.x() > in_view_rect.right())
  330. adjust_x = (SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2) + min(SCROLL_OUT_OF_VIEW_HOT_MARGIN, m_out_of_view_position.x() - in_view_rect.right());
  331. else if (m_out_of_view_position.x() < in_view_rect.left())
  332. adjust_x = -(SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2) + max(-SCROLL_OUT_OF_VIEW_HOT_MARGIN, m_out_of_view_position.x() - in_view_rect.left());
  333. ScrollableWidget::scroll_into_view({ scroll_to.translated(adjust_x, adjust_y), { 1, 1 } }, true, true);
  334. update_rubber_banding(m_out_of_view_position);
  335. }
  336. void IconView::update_item_rects(int item_index, ItemData& item_data) const
  337. {
  338. auto item_rect = this->item_rect(item_index);
  339. item_data.icon_rect.center_within(item_rect);
  340. item_data.icon_rect.move_by(0, item_data.icon_offset_y);
  341. item_data.text_rect.center_horizontally_within(item_rect);
  342. item_data.text_rect.set_top(item_rect.y() + item_data.text_offset_y);
  343. }
  344. Gfx::IntRect IconView::content_rect(const ModelIndex& index) const
  345. {
  346. if (!index.is_valid())
  347. return {};
  348. auto& item_data = get_item_data(index.row());
  349. return item_data.text_rect;
  350. }
  351. void IconView::get_item_rects(int item_index, ItemData& item_data, const Gfx::Font& font) const
  352. {
  353. auto item_rect = this->item_rect(item_index);
  354. item_data.icon_rect = { 0, 0, 32, 32 };
  355. item_data.icon_rect.center_within(item_rect);
  356. item_data.icon_offset_y = -font.glyph_height() - 6;
  357. item_data.icon_rect.move_by(0, item_data.icon_offset_y);
  358. item_data.text_rect = { 0, item_data.icon_rect.bottom() + 6 + 1, font.width(item_data.data.to_string()), font.glyph_height() };
  359. item_data.text_rect.center_horizontally_within(item_rect);
  360. item_data.text_rect.inflate(6, 4);
  361. item_data.text_rect.intersect(item_rect);
  362. item_data.text_offset_y = item_data.text_rect.y() - item_rect.y();
  363. }
  364. void IconView::second_paint_event(PaintEvent& event)
  365. {
  366. if (!m_rubber_banding)
  367. return;
  368. Painter painter(*this);
  369. painter.add_clip_rect(event.rect());
  370. painter.translate(frame_thickness(), frame_thickness());
  371. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  372. auto rubber_band_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  373. painter.fill_rect(rubber_band_rect, palette().rubber_band_fill());
  374. painter.draw_rect(rubber_band_rect, palette().rubber_band_border());
  375. }
  376. void IconView::paint_event(PaintEvent& event)
  377. {
  378. Color widget_background_color = palette().color(background_role());
  379. Frame::paint_event(event);
  380. Painter painter(*this);
  381. painter.add_clip_rect(widget_inner_rect());
  382. painter.add_clip_rect(event.rect());
  383. if (fill_with_background_color())
  384. painter.fill_rect(event.rect(), widget_background_color);
  385. painter.translate(frame_thickness(), frame_thickness());
  386. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  387. auto translation = painter.translation().translated(-relative_position().x(), -relative_position().y());
  388. for_each_item_intersecting_rect(painter.clip_rect().translated(-translation.x(), -translation.y()), [&](auto& item_data) -> IterationDecision {
  389. Color background_color;
  390. if (item_data.selected) {
  391. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  392. } else {
  393. background_color = widget_background_color;
  394. }
  395. auto icon = item_data.index.data(ModelRole::Icon);
  396. auto item_text = item_data.index.data();
  397. if (icon.is_icon()) {
  398. if (auto bitmap = icon.as_icon().bitmap_for_size(item_data.icon_rect.width())) {
  399. Gfx::IntRect destination = bitmap->rect();
  400. destination.center_within(item_data.icon_rect);
  401. if (m_hovered_index.is_valid() && m_hovered_index == item_data.index) {
  402. painter.blit_brightened(destination.location(), *bitmap, bitmap->rect());
  403. } else {
  404. painter.blit(destination.location(), *bitmap, bitmap->rect());
  405. }
  406. }
  407. }
  408. // NOTE: This counteracts the inflate(6, ...) in get_text_rects().
  409. auto available_width = item_data.text_rect.width() - 3;
  410. auto font = font_for_index(item_data.index);
  411. auto text_width = font->width(item_text.to_string());
  412. auto text = item_text.to_string();
  413. if ((item_data.selected || m_hovered_index == item_data.index) && item_data.index != m_edit_index && text_width > available_width) {
  414. // Item text would not fit in the item text rect, let's break it up into lines..
  415. // FIXME: Maybe break this out into some kind of GUI::TextLayout class?
  416. Vector<StringView, 4> lines;
  417. int current_line_width = 0;
  418. int current_line_start = 0;
  419. Utf8View utf8_view(text);
  420. auto it = utf8_view.begin();
  421. for (; it != utf8_view.end(); ++it) {
  422. auto codepoint = *it;
  423. auto glyph_width = font->glyph_width(codepoint);
  424. if (lines.size() < 1 && (current_line_width + glyph_width + font->glyph_spacing()) > available_width) {
  425. lines.append(text.substring_view(current_line_start, utf8_view.byte_offset_of(it) - current_line_start));
  426. current_line_start = utf8_view.byte_offset_of(it);
  427. current_line_width = glyph_width;
  428. } else {
  429. current_line_width += glyph_width + font->glyph_spacing();
  430. }
  431. }
  432. if (current_line_width > 0) {
  433. lines.append(text.substring_view(current_line_start, utf8_view.byte_offset_of(it) - current_line_start));
  434. }
  435. for (size_t line_index = 0; line_index < lines.size(); ++line_index) {
  436. Gfx::IntRect line_rect;
  437. line_rect.set_width(min(available_width, font->width(lines[line_index])));
  438. line_rect.set_height(item_data.text_rect.height());
  439. line_rect.center_horizontally_within(item_data.text_rect);
  440. line_rect.set_y(item_data.text_rect.y() + line_index * item_data.text_rect.height());
  441. line_rect.inflate(6, 0);
  442. painter.fill_rect(line_rect, background_color);
  443. draw_item_text(painter, item_data.index, item_data.selected, line_rect, lines[line_index], font, Gfx::TextAlignment::Center, Gfx::TextElision::Right);
  444. }
  445. } else {
  446. painter.fill_rect(item_data.text_rect, background_color);
  447. draw_item_text(painter, item_data.index, item_data.selected, item_data.text_rect, text, font, Gfx::TextAlignment::Center, Gfx::TextElision::Right);
  448. }
  449. if (item_data.index == m_drop_candidate_index) {
  450. // FIXME: This visualization is not great, as it's also possible to drop things on the text label..
  451. painter.draw_rect(item_data.icon_rect.inflated(8, 8), palette().selection(), true);
  452. }
  453. return IterationDecision::Continue;
  454. });
  455. }
  456. int IconView::item_count() const
  457. {
  458. if (!model())
  459. return 0;
  460. return model()->row_count();
  461. }
  462. void IconView::did_update_selection()
  463. {
  464. AbstractView::did_update_selection();
  465. if (m_changing_selection)
  466. return;
  467. // Selection was modified externally, we need to synchronize our cache
  468. do_clear_selection();
  469. selection().for_each_index([&](const ModelIndex& index) {
  470. if (index.is_valid()) {
  471. auto item_index = model_index_to_item_index(index);
  472. if ((size_t)item_index < m_item_data_cache.size())
  473. do_add_selection(get_item_data(item_index));
  474. }
  475. });
  476. }
  477. void IconView::do_clear_selection()
  478. {
  479. for (size_t item_index = m_first_selected_hint; item_index < m_item_data_cache.size(); item_index++) {
  480. if (m_selected_count_cache == 0)
  481. break;
  482. auto& item_data = m_item_data_cache[item_index];
  483. if (!item_data.selected)
  484. continue;
  485. item_data.selected = false;
  486. m_selected_count_cache--;
  487. }
  488. m_first_selected_hint = 0;
  489. ASSERT(m_selected_count_cache == 0);
  490. }
  491. void IconView::clear_selection()
  492. {
  493. TemporaryChange change(m_changing_selection, true);
  494. AbstractView::clear_selection();
  495. do_clear_selection();
  496. }
  497. bool IconView::do_add_selection(ItemData& item_data)
  498. {
  499. if (!item_data.selected) {
  500. item_data.selected = true;
  501. m_selected_count_cache++;
  502. int item_index = &item_data - &m_item_data_cache[0];
  503. if (m_first_selected_hint > item_index)
  504. m_first_selected_hint = item_index;
  505. return true;
  506. }
  507. return false;
  508. }
  509. void IconView::add_selection(ItemData& item_data)
  510. {
  511. if (do_add_selection(item_data))
  512. AbstractView::add_selection(item_data.index);
  513. }
  514. void IconView::add_selection(const ModelIndex& new_index)
  515. {
  516. TemporaryChange change(m_changing_selection, true);
  517. auto item_index = model_index_to_item_index(new_index);
  518. add_selection(get_item_data(item_index));
  519. }
  520. void IconView::toggle_selection(ItemData& item_data)
  521. {
  522. if (!item_data.selected)
  523. add_selection(item_data);
  524. else
  525. remove_selection(item_data);
  526. }
  527. void IconView::toggle_selection(const ModelIndex& new_index)
  528. {
  529. TemporaryChange change(m_changing_selection, true);
  530. auto item_index = model_index_to_item_index(new_index);
  531. toggle_selection(get_item_data(item_index));
  532. }
  533. void IconView::remove_selection(ItemData& item_data)
  534. {
  535. if (!item_data.selected)
  536. return;
  537. TemporaryChange change(m_changing_selection, true);
  538. item_data.selected = false;
  539. ASSERT(m_selected_count_cache > 0);
  540. m_selected_count_cache--;
  541. int item_index = &item_data - &m_item_data_cache[0];
  542. if (m_first_selected_hint == item_index) {
  543. m_first_selected_hint = 0;
  544. while ((size_t)item_index < m_item_data_cache.size()) {
  545. if (m_item_data_cache[item_index].selected) {
  546. m_first_selected_hint = item_index;
  547. break;
  548. }
  549. item_index++;
  550. }
  551. }
  552. AbstractView::remove_selection(item_data.index);
  553. }
  554. void IconView::set_selection(const ModelIndex& new_index)
  555. {
  556. TemporaryChange change(m_changing_selection, true);
  557. do_clear_selection();
  558. auto item_index = model_index_to_item_index(new_index);
  559. auto& item_data = get_item_data(item_index);
  560. item_data.selected = true;
  561. m_selected_count_cache = 1;
  562. if (item_index < m_first_selected_hint)
  563. m_first_selected_hint = item_index;
  564. AbstractView::set_selection(new_index);
  565. }
  566. void IconView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  567. {
  568. if (!model())
  569. return;
  570. auto& model = *this->model();
  571. if (!cursor_index().is_valid()) {
  572. set_cursor(model.index(0, 0), SelectionUpdate::Set);
  573. return;
  574. }
  575. ModelIndex new_index;
  576. switch (movement) {
  577. case CursorMovement::Right:
  578. new_index = model.index(cursor_index().row() + 1, cursor_index().column());
  579. break;
  580. case CursorMovement::Left:
  581. new_index = model.index(cursor_index().row() - 1, cursor_index().column());
  582. break;
  583. case CursorMovement::Up:
  584. new_index = model.index(cursor_index().row() - m_visual_column_count, cursor_index().column());
  585. break;
  586. case CursorMovement::Down:
  587. new_index = model.index(cursor_index().row() + m_visual_column_count, cursor_index().column());
  588. break;
  589. case CursorMovement::PageUp: {
  590. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  591. new_index = model.index(max(0, cursor_index().row() - items_per_page), cursor_index().column());
  592. break;
  593. }
  594. case CursorMovement::PageDown: {
  595. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  596. new_index = model.index(min(model.row_count() - 1, cursor_index().row() + items_per_page), cursor_index().column());
  597. break;
  598. }
  599. case CursorMovement::Home:
  600. new_index = model.index(0, 0);
  601. break;
  602. case CursorMovement::End:
  603. new_index = model.index(model.row_count() - 1, 0);
  604. break;
  605. default:
  606. break;
  607. }
  608. if (new_index.is_valid())
  609. set_cursor(new_index, selection_update);
  610. }
  611. }