IconView.cpp 26 KB

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