IconView.cpp 24 KB

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