IconView.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 = item_data.index.data();
  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. if (is_multi_select()) {
  195. m_rubber_banding = true;
  196. m_rubber_band_origin = adjusted_position;
  197. m_rubber_band_current = adjusted_position;
  198. }
  199. }
  200. void IconView::mouseup_event(MouseEvent& event)
  201. {
  202. if (m_rubber_banding && event.button() == MouseButton::Left) {
  203. m_rubber_banding = false;
  204. if (m_out_of_view_timer)
  205. m_out_of_view_timer->stop();
  206. update();
  207. }
  208. AbstractView::mouseup_event(event);
  209. }
  210. void IconView::drag_move_event(DragEvent& event)
  211. {
  212. auto index = index_at_event_position(event.position());
  213. ModelIndex new_drop_candidate_index;
  214. if (index.is_valid()) {
  215. bool acceptable = model()->accepts_drag(index, event.data_type());
  216. #ifdef DRAGDROP_DEBUG
  217. dbg() << "Drag of type '" << event.data_type() << "' moving over " << index << ", acceptable: " << acceptable;
  218. #endif
  219. if (acceptable)
  220. new_drop_candidate_index = index;
  221. }
  222. if (m_drop_candidate_index != new_drop_candidate_index) {
  223. m_drop_candidate_index = new_drop_candidate_index;
  224. update();
  225. }
  226. event.accept();
  227. }
  228. bool IconView::update_rubber_banding(const Gfx::IntPoint& position)
  229. {
  230. auto adjusted_position = to_content_position(position);
  231. if (m_rubber_band_current != adjusted_position) {
  232. auto prev_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  233. m_rubber_band_current = adjusted_position;
  234. auto rubber_band_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  235. // If the rectangle width or height is 0, we still want to be able
  236. // to match the items in the path. An easy work-around for this
  237. // is to simply set the width or height to 1
  238. auto ensure_rect = [](Gfx::IntRect& rect) {
  239. if (rect.width() <= 0)
  240. rect.set_width(1);
  241. if (rect.height() <= 0)
  242. rect.set_height(1);
  243. };
  244. ensure_rect(prev_rect);
  245. ensure_rect(rubber_band_rect);
  246. // Clearing the entire selection every time is very expensive,
  247. // determine what items may need to be deselected and what new
  248. // items may need to be selected. Avoid a ton of allocations.
  249. auto deselect_area = prev_rect.shatter(rubber_band_rect);
  250. auto select_area = rubber_band_rect.shatter(prev_rect);
  251. // Initialize all candidate's toggle flag. We need to know which
  252. // items we touched because the various rectangles likely will
  253. // contain the same item more than once
  254. for_each_item_intersecting_rects(deselect_area, [](ItemData& item_data) -> IterationDecision {
  255. item_data.selection_toggled = false;
  256. return IterationDecision::Continue;
  257. });
  258. for_each_item_intersecting_rects(select_area, [](ItemData& item_data) -> IterationDecision {
  259. item_data.selection_toggled = false;
  260. return IterationDecision::Continue;
  261. });
  262. // Now toggle all items that are no longer in the selected area, once only
  263. for_each_item_intersecting_rects(deselect_area, [&](ItemData& item_data) -> IterationDecision {
  264. if (!item_data.selection_toggled && item_data.is_intersecting(prev_rect) && !item_data.is_intersecting(rubber_band_rect)) {
  265. item_data.selection_toggled = true;
  266. toggle_selection(item_data);
  267. }
  268. return IterationDecision::Continue;
  269. });
  270. // Now toggle all items that are in the new selected area, once only
  271. for_each_item_intersecting_rects(select_area, [&](ItemData& item_data) -> IterationDecision {
  272. if (!item_data.selection_toggled && !item_data.is_intersecting(prev_rect) && item_data.is_intersecting(rubber_band_rect)) {
  273. item_data.selection_toggled = true;
  274. toggle_selection(item_data);
  275. }
  276. return IterationDecision::Continue;
  277. });
  278. update();
  279. return true;
  280. }
  281. return false;
  282. }
  283. #define SCROLL_OUT_OF_VIEW_HOT_MARGIN 20
  284. void IconView::mousemove_event(MouseEvent& event)
  285. {
  286. if (!model())
  287. return AbstractView::mousemove_event(event);
  288. if (m_rubber_banding) {
  289. auto in_view_rect = widget_inner_rect();
  290. in_view_rect.shrink(SCROLL_OUT_OF_VIEW_HOT_MARGIN, SCROLL_OUT_OF_VIEW_HOT_MARGIN);
  291. if (!in_view_rect.contains(event.position())) {
  292. if (!m_out_of_view_timer) {
  293. m_out_of_view_timer = add<Core::Timer>();
  294. m_out_of_view_timer->set_interval(100);
  295. m_out_of_view_timer->on_timeout = [this] {
  296. scroll_out_of_view_timer_fired();
  297. };
  298. }
  299. m_out_of_view_position = event.position();
  300. if (!m_out_of_view_timer->is_active())
  301. m_out_of_view_timer->start();
  302. } else {
  303. if (m_out_of_view_timer)
  304. m_out_of_view_timer->stop();
  305. }
  306. if (update_rubber_banding(event.position()))
  307. return;
  308. }
  309. AbstractView::mousemove_event(event);
  310. }
  311. void IconView::scroll_out_of_view_timer_fired()
  312. {
  313. auto scroll_to = to_content_position(m_out_of_view_position);
  314. // Adjust the scroll-to position by SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2
  315. // depending on which direction we're scrolling. This allows us to
  316. // start scrolling before we actually leave the visible area, which
  317. // is important when there is no space to further move the mouse. The
  318. // speed of scrolling is determined by the distance between the mouse
  319. // pointer and the widget's inner rect shrunken by the hot margin
  320. auto in_view_rect = widget_inner_rect().shrunken(SCROLL_OUT_OF_VIEW_HOT_MARGIN, SCROLL_OUT_OF_VIEW_HOT_MARGIN);
  321. int adjust_x = 0, adjust_y = 0;
  322. if (m_out_of_view_position.y() > in_view_rect.bottom())
  323. 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());
  324. else if (m_out_of_view_position.y() < in_view_rect.top())
  325. 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());
  326. if (m_out_of_view_position.x() > in_view_rect.right())
  327. 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());
  328. else if (m_out_of_view_position.x() < in_view_rect.left())
  329. 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());
  330. ScrollableWidget::scroll_into_view({ scroll_to.translated(adjust_x, adjust_y), { 1, 1 } }, true, true);
  331. update_rubber_banding(m_out_of_view_position);
  332. }
  333. void IconView::update_item_rects(int item_index, ItemData& item_data) const
  334. {
  335. auto item_rect = this->item_rect(item_index);
  336. item_data.icon_rect.center_within(item_rect);
  337. item_data.icon_rect.move_by(0, item_data.icon_offset_y);
  338. item_data.text_rect.center_horizontally_within(item_rect);
  339. item_data.text_rect.set_top(item_rect.y() + item_data.text_offset_y);
  340. }
  341. void IconView::get_item_rects(int item_index, ItemData& item_data, const Gfx::Font& font) const
  342. {
  343. auto item_rect = this->item_rect(item_index);
  344. item_data.icon_rect = { 0, 0, 32, 32 };
  345. item_data.icon_rect.center_within(item_rect);
  346. item_data.icon_offset_y = -font.glyph_height() - 6;
  347. item_data.icon_rect.move_by(0, item_data.icon_offset_y);
  348. item_data.text_rect = { 0, item_data.icon_rect.bottom() + 6 + 1, font.width(item_data.data.to_string()), font.glyph_height() };
  349. item_data.text_rect.center_horizontally_within(item_rect);
  350. item_data.text_rect.inflate(6, 4);
  351. item_data.text_rect.intersect(item_rect);
  352. item_data.text_offset_y = item_data.text_rect.y() - item_rect.y();
  353. }
  354. void IconView::second_paint_event(PaintEvent& event)
  355. {
  356. if (!m_rubber_banding)
  357. return;
  358. Painter painter(*this);
  359. painter.add_clip_rect(event.rect());
  360. painter.translate(frame_thickness(), frame_thickness());
  361. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  362. auto rubber_band_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  363. painter.fill_rect(rubber_band_rect, palette().rubber_band_fill());
  364. painter.draw_rect(rubber_band_rect, palette().rubber_band_border());
  365. }
  366. void IconView::paint_event(PaintEvent& event)
  367. {
  368. Color widget_background_color = palette().color(background_role());
  369. Frame::paint_event(event);
  370. Painter painter(*this);
  371. painter.add_clip_rect(widget_inner_rect());
  372. painter.add_clip_rect(event.rect());
  373. if (fill_with_background_color())
  374. painter.fill_rect(event.rect(), widget_background_color);
  375. painter.translate(frame_thickness(), frame_thickness());
  376. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  377. auto translation = painter.translation().translated(-relative_position().x(), -relative_position().y());
  378. for_each_item_intersecting_rect(painter.clip_rect().translated(-translation.x(), -translation.y()), [&](auto& item_data) -> IterationDecision {
  379. Color background_color;
  380. if (item_data.selected) {
  381. background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  382. } else {
  383. background_color = widget_background_color;
  384. }
  385. auto icon = item_data.index.data(ModelRole::Icon);
  386. auto item_text = item_data.index.data();
  387. if (icon.is_icon()) {
  388. if (auto bitmap = icon.as_icon().bitmap_for_size(item_data.icon_rect.width())) {
  389. Gfx::IntRect destination = bitmap->rect();
  390. destination.center_within(item_data.icon_rect);
  391. if (m_hovered_index.is_valid() && m_hovered_index == item_data.index) {
  392. painter.blit_brightened(destination.location(), *bitmap, bitmap->rect());
  393. } else {
  394. painter.blit(destination.location(), *bitmap, bitmap->rect());
  395. }
  396. }
  397. }
  398. Color text_color;
  399. if (item_data.selected)
  400. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  401. else
  402. text_color = item_data.index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
  403. painter.fill_rect(item_data.text_rect, background_color);
  404. 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);
  405. if (item_data.index == m_drop_candidate_index) {
  406. // FIXME: This visualization is not great, as it's also possible to drop things on the text label..
  407. painter.draw_rect(item_data.icon_rect.inflated(8, 8), palette().selection(), true);
  408. }
  409. return IterationDecision::Continue;
  410. });
  411. }
  412. int IconView::item_count() const
  413. {
  414. if (!model())
  415. return 0;
  416. return model()->row_count();
  417. }
  418. void IconView::did_update_selection()
  419. {
  420. AbstractView::did_update_selection();
  421. if (m_changing_selection)
  422. return;
  423. // Selection was modified externally, we need to synchronize our cache
  424. do_clear_selection();
  425. selection().for_each_index([&](const ModelIndex& index) {
  426. if (index.is_valid()) {
  427. auto item_index = model_index_to_item_index(index);
  428. if ((size_t)item_index < m_item_data_cache.size())
  429. do_add_selection(get_item_data(item_index));
  430. }
  431. });
  432. }
  433. void IconView::do_clear_selection()
  434. {
  435. for (size_t item_index = m_first_selected_hint; item_index < m_item_data_cache.size(); item_index++) {
  436. if (m_selected_count_cache == 0)
  437. break;
  438. auto& item_data = m_item_data_cache[item_index];
  439. if (!item_data.selected)
  440. continue;
  441. item_data.selected = false;
  442. m_selected_count_cache--;
  443. }
  444. m_first_selected_hint = 0;
  445. ASSERT(m_selected_count_cache == 0);
  446. }
  447. void IconView::clear_selection()
  448. {
  449. TemporaryChange change(m_changing_selection, true);
  450. AbstractView::clear_selection();
  451. do_clear_selection();
  452. }
  453. bool IconView::do_add_selection(ItemData& item_data)
  454. {
  455. if (!item_data.selected) {
  456. item_data.selected = true;
  457. m_selected_count_cache++;
  458. int item_index = &item_data - &m_item_data_cache[0];
  459. if (m_first_selected_hint > item_index)
  460. m_first_selected_hint = item_index;
  461. return true;
  462. }
  463. return false;
  464. }
  465. void IconView::add_selection(ItemData& item_data)
  466. {
  467. if (do_add_selection(item_data))
  468. AbstractView::add_selection(item_data.index);
  469. }
  470. void IconView::add_selection(const ModelIndex& new_index)
  471. {
  472. TemporaryChange change(m_changing_selection, true);
  473. auto item_index = model_index_to_item_index(new_index);
  474. add_selection(get_item_data(item_index));
  475. }
  476. void IconView::toggle_selection(ItemData& item_data)
  477. {
  478. if (!item_data.selected)
  479. add_selection(item_data);
  480. else
  481. remove_selection(item_data);
  482. }
  483. void IconView::toggle_selection(const ModelIndex& new_index)
  484. {
  485. TemporaryChange change(m_changing_selection, true);
  486. auto item_index = model_index_to_item_index(new_index);
  487. toggle_selection(get_item_data(item_index));
  488. }
  489. void IconView::remove_selection(ItemData& item_data)
  490. {
  491. if (!item_data.selected)
  492. return;
  493. TemporaryChange change(m_changing_selection, true);
  494. item_data.selected = false;
  495. ASSERT(m_selected_count_cache > 0);
  496. m_selected_count_cache--;
  497. int item_index = &item_data - &m_item_data_cache[0];
  498. if (m_first_selected_hint == item_index) {
  499. m_first_selected_hint = 0;
  500. while ((size_t)item_index < m_item_data_cache.size()) {
  501. if (m_item_data_cache[item_index].selected) {
  502. m_first_selected_hint = item_index;
  503. break;
  504. }
  505. item_index++;
  506. }
  507. }
  508. AbstractView::remove_selection(item_data.index);
  509. }
  510. void IconView::set_selection(const ModelIndex& new_index)
  511. {
  512. TemporaryChange change(m_changing_selection, true);
  513. do_clear_selection();
  514. auto item_index = model_index_to_item_index(new_index);
  515. auto& item_data = get_item_data(item_index);
  516. item_data.selected = true;
  517. m_selected_count_cache = 1;
  518. if (item_index < m_first_selected_hint)
  519. m_first_selected_hint = item_index;
  520. AbstractView::set_selection(new_index);
  521. }
  522. void IconView::keydown_event(KeyEvent& event)
  523. {
  524. if (!model())
  525. return;
  526. if (!m_visual_row_count || !m_visual_column_count)
  527. return;
  528. auto& model = *this->model();
  529. if (event.key() == KeyCode::Key_Return) {
  530. activate_selected();
  531. return;
  532. }
  533. if (event.key() == KeyCode::Key_Home) {
  534. auto new_index = model.index(0, 0);
  535. if (model.is_valid(new_index)) {
  536. set_selection(new_index);
  537. scroll_into_view(new_index, Orientation::Vertical);
  538. update();
  539. }
  540. return;
  541. }
  542. if (event.key() == KeyCode::Key_End) {
  543. auto new_index = model.index(model.row_count() - 1, 0);
  544. if (model.is_valid(new_index)) {
  545. set_selection(new_index);
  546. scroll_into_view(new_index, Orientation::Vertical);
  547. update();
  548. }
  549. return;
  550. }
  551. if (event.key() == KeyCode::Key_Up) {
  552. ModelIndex new_index;
  553. if (!selection().is_empty()) {
  554. auto old_index = selection().first();
  555. new_index = model.index(old_index.row() - m_visual_column_count, old_index.column());
  556. } else {
  557. new_index = model.index(0, 0);
  558. }
  559. if (model.is_valid(new_index)) {
  560. set_selection(new_index);
  561. scroll_into_view(new_index, Orientation::Vertical);
  562. update();
  563. }
  564. return;
  565. }
  566. if (event.key() == KeyCode::Key_Down) {
  567. ModelIndex new_index;
  568. if (!selection().is_empty()) {
  569. auto old_index = selection().first();
  570. new_index = model.index(old_index.row() + m_visual_column_count, old_index.column());
  571. } else {
  572. new_index = model.index(0, 0);
  573. }
  574. if (model.is_valid(new_index)) {
  575. set_selection(new_index);
  576. scroll_into_view(new_index, Orientation::Vertical);
  577. update();
  578. }
  579. return;
  580. }
  581. if (event.key() == KeyCode::Key_Left) {
  582. ModelIndex new_index;
  583. if (!selection().is_empty()) {
  584. auto old_index = selection().first();
  585. new_index = model.index(old_index.row() - 1, old_index.column());
  586. } else {
  587. new_index = model.index(0, 0);
  588. }
  589. if (model.is_valid(new_index)) {
  590. set_selection(new_index);
  591. scroll_into_view(new_index, Orientation::Vertical);
  592. update();
  593. }
  594. return;
  595. }
  596. if (event.key() == KeyCode::Key_Right) {
  597. ModelIndex new_index;
  598. if (!selection().is_empty()) {
  599. auto old_index = selection().first();
  600. new_index = model.index(old_index.row() + 1, old_index.column());
  601. } else {
  602. new_index = model.index(0, 0);
  603. }
  604. if (model.is_valid(new_index)) {
  605. set_selection(new_index);
  606. scroll_into_view(new_index, Orientation::Vertical);
  607. update();
  608. }
  609. return;
  610. }
  611. if (event.key() == KeyCode::Key_PageUp) {
  612. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  613. auto old_index = selection().first();
  614. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  615. if (model.is_valid(new_index)) {
  616. set_selection(new_index);
  617. scroll_into_view(new_index, Orientation::Vertical);
  618. update();
  619. }
  620. return;
  621. }
  622. if (event.key() == KeyCode::Key_PageDown) {
  623. int items_per_page = (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  624. auto old_index = selection().first();
  625. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  626. if (model.is_valid(new_index)) {
  627. set_selection(new_index);
  628. scroll_into_view(new_index, Orientation::Vertical);
  629. update();
  630. }
  631. return;
  632. }
  633. return Widget::keydown_event(event);
  634. }
  635. }