IconView.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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.text = item_data.index.data().to_string();
  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 = (m_flow_direction == FlowDirection::LeftToRight)
  118. ? row * m_visual_column_count + column
  119. : column * m_visual_row_count + row;
  120. if (item_index < 0 || item_index >= item_count())
  121. return nullptr;
  122. return &get_item_data(item_index);
  123. }
  124. void IconView::model_did_update(unsigned flags)
  125. {
  126. AbstractView::model_did_update(flags);
  127. if (!model() || (flags & GUI::Model::InvalidateAllIndexes)) {
  128. m_item_data_cache.clear();
  129. AbstractView::clear_selection();
  130. m_selected_count_cache = 0;
  131. m_first_selected_hint = 0;
  132. }
  133. m_item_data_cache_valid = false;
  134. update_content_size();
  135. update();
  136. }
  137. void IconView::update_content_size()
  138. {
  139. if (!model())
  140. return set_content_size({});
  141. int content_width;
  142. int content_height;
  143. if (m_flow_direction == FlowDirection::LeftToRight) {
  144. m_visual_column_count = max(1, available_size().width() / effective_item_size().width());
  145. if (m_visual_column_count)
  146. m_visual_row_count = ceil_div(model()->row_count(), m_visual_column_count);
  147. else
  148. m_visual_row_count = 0;
  149. content_width = available_size().width();
  150. content_height = m_visual_row_count * effective_item_size().height();
  151. } else {
  152. m_visual_row_count = max(1, available_size().height() / effective_item_size().height());
  153. if (m_visual_row_count)
  154. m_visual_column_count = ceil_div(model()->row_count(), m_visual_row_count);
  155. else
  156. m_visual_column_count = 0;
  157. content_width = m_visual_column_count * effective_item_size().width();
  158. content_height = available_size().height();
  159. }
  160. set_content_size({ content_width, content_height });
  161. if (!m_item_data_cache_valid)
  162. reinit_item_cache();
  163. for (int item_index = 0; item_index < item_count(); item_index++) {
  164. auto& item_data = m_item_data_cache[item_index];
  165. if (item_data.is_valid())
  166. update_item_rects(item_index, item_data);
  167. }
  168. }
  169. Gfx::IntRect IconView::item_rect(int item_index) const
  170. {
  171. if (!m_visual_row_count || !m_visual_column_count)
  172. return {};
  173. int visual_row_index;
  174. int visual_column_index;
  175. if (m_flow_direction == FlowDirection::LeftToRight) {
  176. visual_row_index = item_index / m_visual_column_count;
  177. visual_column_index = item_index % m_visual_column_count;
  178. } else {
  179. visual_row_index = item_index % m_visual_row_count;
  180. visual_column_index = item_index / m_visual_row_count;
  181. }
  182. return {
  183. visual_column_index * effective_item_size().width(),
  184. visual_row_index * effective_item_size().height(),
  185. effective_item_size().width(),
  186. effective_item_size().height()
  187. };
  188. }
  189. ModelIndex IconView::index_at_event_position(const Gfx::IntPoint& position) const
  190. {
  191. ASSERT(model());
  192. auto adjusted_position = to_content_position(position);
  193. if (auto item_data = item_data_from_content_position(adjusted_position)) {
  194. if (item_data->is_containing(adjusted_position))
  195. return item_data->index;
  196. }
  197. return {};
  198. }
  199. void IconView::mousedown_event(MouseEvent& event)
  200. {
  201. if (!model())
  202. return AbstractView::mousedown_event(event);
  203. if (event.button() != MouseButton::Left)
  204. return AbstractView::mousedown_event(event);
  205. auto index = index_at_event_position(event.position());
  206. if (index.is_valid()) {
  207. // We might start dragging this item, but not rubber-banding.
  208. return AbstractView::mousedown_event(event);
  209. }
  210. if (event.modifiers() & Mod_Ctrl) {
  211. m_rubber_banding_store_selection = true;
  212. } else {
  213. clear_selection();
  214. m_rubber_banding_store_selection = false;
  215. }
  216. auto adjusted_position = to_content_position(event.position());
  217. m_might_drag = false;
  218. if (is_multi_select()) {
  219. m_rubber_banding = true;
  220. m_rubber_band_origin = adjusted_position;
  221. m_rubber_band_current = adjusted_position;
  222. }
  223. }
  224. void IconView::mouseup_event(MouseEvent& event)
  225. {
  226. if (m_rubber_banding && event.button() == MouseButton::Left) {
  227. m_rubber_banding = false;
  228. if (m_out_of_view_timer)
  229. m_out_of_view_timer->stop();
  230. update();
  231. }
  232. AbstractView::mouseup_event(event);
  233. }
  234. void IconView::drag_move_event(DragEvent& event)
  235. {
  236. auto index = index_at_event_position(event.position());
  237. ModelIndex new_drop_candidate_index;
  238. if (index.is_valid()) {
  239. bool acceptable = model()->accepts_drag(index, event.data_type());
  240. #ifdef DRAGDROP_DEBUG
  241. dbg() << "Drag of type '" << event.data_type() << "' moving over " << index << ", acceptable: " << acceptable;
  242. #endif
  243. if (acceptable)
  244. new_drop_candidate_index = index;
  245. }
  246. if (m_drop_candidate_index != new_drop_candidate_index) {
  247. m_drop_candidate_index = new_drop_candidate_index;
  248. update();
  249. }
  250. event.accept();
  251. }
  252. bool IconView::update_rubber_banding(const Gfx::IntPoint& position)
  253. {
  254. auto adjusted_position = to_content_position(position);
  255. if (m_rubber_band_current != adjusted_position) {
  256. auto prev_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  257. m_rubber_band_current = adjusted_position;
  258. auto rubber_band_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  259. // If the rectangle width or height is 0, we still want to be able
  260. // to match the items in the path. An easy work-around for this
  261. // is to simply set the width or height to 1
  262. auto ensure_rect = [](Gfx::IntRect& rect) {
  263. if (rect.width() <= 0)
  264. rect.set_width(1);
  265. if (rect.height() <= 0)
  266. rect.set_height(1);
  267. };
  268. ensure_rect(prev_rect);
  269. ensure_rect(rubber_band_rect);
  270. // Clearing the entire selection every time is very expensive,
  271. // determine what items may need to be deselected and what new
  272. // items may need to be selected. Avoid a ton of allocations.
  273. auto deselect_area = prev_rect.shatter(rubber_band_rect);
  274. auto select_area = rubber_band_rect.shatter(prev_rect);
  275. // Initialize all candidate's toggle flag. We need to know which
  276. // items we touched because the various rectangles likely will
  277. // contain the same item more than once
  278. for_each_item_intersecting_rects(deselect_area, [](ItemData& item_data) -> IterationDecision {
  279. item_data.selection_toggled = false;
  280. return IterationDecision::Continue;
  281. });
  282. for_each_item_intersecting_rects(select_area, [](ItemData& item_data) -> IterationDecision {
  283. item_data.selection_toggled = false;
  284. return IterationDecision::Continue;
  285. });
  286. // Now toggle all items that are no longer in the selected area, once only
  287. for_each_item_intersecting_rects(deselect_area, [&](ItemData& item_data) -> IterationDecision {
  288. if (!item_data.selection_toggled && item_data.is_intersecting(prev_rect) && !item_data.is_intersecting(rubber_band_rect)) {
  289. item_data.selection_toggled = true;
  290. toggle_selection(item_data);
  291. }
  292. return IterationDecision::Continue;
  293. });
  294. // Now toggle all items that are in the new selected area, once only
  295. for_each_item_intersecting_rects(select_area, [&](ItemData& item_data) -> IterationDecision {
  296. if (!item_data.selection_toggled && !item_data.is_intersecting(prev_rect) && item_data.is_intersecting(rubber_band_rect)) {
  297. item_data.selection_toggled = true;
  298. toggle_selection(item_data);
  299. }
  300. return IterationDecision::Continue;
  301. });
  302. update();
  303. return true;
  304. }
  305. return false;
  306. }
  307. #define SCROLL_OUT_OF_VIEW_HOT_MARGIN 20
  308. void IconView::mousemove_event(MouseEvent& event)
  309. {
  310. if (!model())
  311. return AbstractView::mousemove_event(event);
  312. if (m_rubber_banding) {
  313. auto in_view_rect = widget_inner_rect();
  314. in_view_rect.shrink(SCROLL_OUT_OF_VIEW_HOT_MARGIN, SCROLL_OUT_OF_VIEW_HOT_MARGIN);
  315. if (!in_view_rect.contains(event.position())) {
  316. if (!m_out_of_view_timer) {
  317. m_out_of_view_timer = add<Core::Timer>();
  318. m_out_of_view_timer->set_interval(100);
  319. m_out_of_view_timer->on_timeout = [this] {
  320. scroll_out_of_view_timer_fired();
  321. };
  322. }
  323. m_out_of_view_position = event.position();
  324. if (!m_out_of_view_timer->is_active())
  325. m_out_of_view_timer->start();
  326. } else {
  327. if (m_out_of_view_timer)
  328. m_out_of_view_timer->stop();
  329. }
  330. if (update_rubber_banding(event.position()))
  331. return;
  332. }
  333. AbstractView::mousemove_event(event);
  334. }
  335. void IconView::scroll_out_of_view_timer_fired()
  336. {
  337. auto scroll_to = to_content_position(m_out_of_view_position);
  338. // Adjust the scroll-to position by SCROLL_OUT_OF_VIEW_HOT_MARGIN / 2
  339. // depending on which direction we're scrolling. This allows us to
  340. // start scrolling before we actually leave the visible area, which
  341. // is important when there is no space to further move the mouse. The
  342. // speed of scrolling is determined by the distance between the mouse
  343. // pointer and the widget's inner rect shrunken by the hot margin
  344. auto in_view_rect = widget_inner_rect().shrunken(SCROLL_OUT_OF_VIEW_HOT_MARGIN, SCROLL_OUT_OF_VIEW_HOT_MARGIN);
  345. int adjust_x = 0, adjust_y = 0;
  346. if (m_out_of_view_position.y() > in_view_rect.bottom())
  347. 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());
  348. else if (m_out_of_view_position.y() < in_view_rect.top())
  349. 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());
  350. if (m_out_of_view_position.x() > in_view_rect.right())
  351. 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());
  352. else if (m_out_of_view_position.x() < in_view_rect.left())
  353. 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());
  354. ScrollableWidget::scroll_into_view({ scroll_to.translated(adjust_x, adjust_y), { 1, 1 } }, true, true);
  355. update_rubber_banding(m_out_of_view_position);
  356. }
  357. void IconView::update_item_rects(int item_index, ItemData& item_data) const
  358. {
  359. auto item_rect = this->item_rect(item_index);
  360. item_data.icon_rect.center_within(item_rect);
  361. item_data.icon_rect.move_by(0, item_data.icon_offset_y);
  362. item_data.text_rect.center_horizontally_within(item_rect);
  363. item_data.text_rect.set_top(item_rect.y() + item_data.text_offset_y);
  364. }
  365. Gfx::IntRect IconView::content_rect(const ModelIndex& index) const
  366. {
  367. if (!index.is_valid())
  368. return {};
  369. auto& item_data = get_item_data(index.row());
  370. return item_data.text_rect.inflated(4, 4);
  371. }
  372. void IconView::did_change_hovered_index(const ModelIndex& old_index, const ModelIndex& new_index)
  373. {
  374. AbstractView::did_change_hovered_index(old_index, new_index);
  375. if (old_index.is_valid())
  376. get_item_rects(old_index.row(), get_item_data(old_index.row()), font_for_index(old_index));
  377. if (new_index.is_valid())
  378. get_item_rects(new_index.row(), get_item_data(new_index.row()), font_for_index(new_index));
  379. }
  380. void IconView::did_change_cursor_index(const ModelIndex& old_index, const ModelIndex& new_index)
  381. {
  382. AbstractView::did_change_cursor_index(old_index, new_index);
  383. if (old_index.is_valid())
  384. get_item_rects(old_index.row(), get_item_data(old_index.row()), font_for_index(old_index));
  385. if (new_index.is_valid())
  386. get_item_rects(new_index.row(), get_item_data(new_index.row()), font_for_index(new_index));
  387. }
  388. void IconView::get_item_rects(int item_index, ItemData& item_data, const Gfx::Font& font) const
  389. {
  390. auto item_rect = this->item_rect(item_index);
  391. item_data.icon_rect = { 0, 0, 32, 32 };
  392. item_data.icon_rect.center_within(item_rect);
  393. item_data.icon_offset_y = -font.glyph_height() - 6;
  394. item_data.icon_rect.move_by(0, item_data.icon_offset_y);
  395. int unwrapped_text_width = font.width(item_data.text);
  396. int available_width = item_rect.width() - 6;
  397. item_data.text_rect = { 0, item_data.icon_rect.bottom() + 6 + 1, 0, font.glyph_height() };
  398. item_data.wrapped_text_lines.clear();
  399. if ((unwrapped_text_width > available_width) && (item_data.selected || m_hovered_index == item_data.index || cursor_index() == item_data.index)) {
  400. int current_line_width = 0;
  401. int current_line_start = 0;
  402. int widest_line_width = 0;
  403. Utf8View utf8_view(item_data.text);
  404. auto it = utf8_view.begin();
  405. for (; it != utf8_view.end(); ++it) {
  406. auto codepoint = *it;
  407. auto glyph_width = font.glyph_width(codepoint);
  408. if ((current_line_width + glyph_width + font.glyph_spacing()) > available_width) {
  409. item_data.wrapped_text_lines.append(item_data.text.substring_view(current_line_start, utf8_view.byte_offset_of(it) - current_line_start));
  410. current_line_start = utf8_view.byte_offset_of(it);
  411. current_line_width = glyph_width;
  412. } else {
  413. current_line_width += glyph_width + font.glyph_spacing();
  414. }
  415. widest_line_width = max(widest_line_width, current_line_width);
  416. }
  417. if (current_line_width > 0) {
  418. item_data.wrapped_text_lines.append(item_data.text.substring_view(current_line_start, utf8_view.byte_offset_of(it) - current_line_start));
  419. }
  420. item_data.text_rect.set_width(widest_line_width);
  421. item_data.text_rect.center_horizontally_within(item_rect);
  422. item_data.text_rect.intersect(item_rect);
  423. item_data.text_rect.set_height(font.glyph_height() * item_data.wrapped_text_lines.size());
  424. item_data.text_rect.inflate(6, 4);
  425. } else {
  426. item_data.text_rect.set_width(unwrapped_text_width);
  427. item_data.text_rect.inflate(6, 4);
  428. item_data.text_rect.center_horizontally_within(item_rect);
  429. }
  430. item_data.text_rect.intersect(item_rect);
  431. item_data.text_offset_y = item_data.text_rect.y() - item_rect.y();
  432. }
  433. void IconView::second_paint_event(PaintEvent& event)
  434. {
  435. if (!m_rubber_banding)
  436. return;
  437. Painter painter(*this);
  438. painter.add_clip_rect(event.rect());
  439. painter.add_clip_rect(widget_inner_rect());
  440. painter.translate(frame_thickness(), frame_thickness());
  441. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  442. auto rubber_band_rect = Gfx::IntRect::from_two_points(m_rubber_band_origin, m_rubber_band_current);
  443. painter.fill_rect(rubber_band_rect, palette().rubber_band_fill());
  444. painter.draw_rect(rubber_band_rect, palette().rubber_band_border());
  445. }
  446. void IconView::paint_event(PaintEvent& event)
  447. {
  448. Color widget_background_color = palette().color(background_role());
  449. Frame::paint_event(event);
  450. Painter painter(*this);
  451. painter.add_clip_rect(widget_inner_rect());
  452. painter.add_clip_rect(event.rect());
  453. if (fill_with_background_color())
  454. painter.fill_rect(event.rect(), widget_background_color);
  455. painter.translate(frame_thickness(), frame_thickness());
  456. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  457. auto selection_color = is_focused() ? palette().selection() : palette().inactive_selection();
  458. for_each_item_intersecting_rect(to_content_rect(event.rect()), [&](auto& item_data) -> IterationDecision {
  459. Color background_color;
  460. if (item_data.selected) {
  461. background_color = selection_color;
  462. } else {
  463. if (fill_with_background_color())
  464. background_color = widget_background_color;
  465. }
  466. auto icon = item_data.index.data(ModelRole::Icon);
  467. if (icon.is_icon()) {
  468. if (auto bitmap = icon.as_icon().bitmap_for_size(item_data.icon_rect.width())) {
  469. Gfx::IntRect destination = bitmap->rect();
  470. destination.center_within(item_data.icon_rect);
  471. if (item_data.selected) {
  472. auto tint = selection_color.with_alpha(100);
  473. painter.blit_filtered(destination.location(), *bitmap, bitmap->rect(), [&](auto src) { return src.blend(tint); });
  474. } else if (m_hovered_index.is_valid() && m_hovered_index == item_data.index) {
  475. painter.blit_brightened(destination.location(), *bitmap, bitmap->rect());
  476. } else {
  477. painter.blit(destination.location(), *bitmap, bitmap->rect());
  478. }
  479. }
  480. }
  481. auto font = font_for_index(item_data.index);
  482. const auto& text_rect = item_data.text_rect;
  483. painter.fill_rect(text_rect, background_color);
  484. if (is_focused() && item_data.index == cursor_index()) {
  485. painter.draw_rect(text_rect, widget_background_color);
  486. painter.draw_focus_rect(text_rect, palette().focus_outline());
  487. }
  488. if (!item_data.wrapped_text_lines.is_empty()) {
  489. // Item text would not fit in the item text rect, let's break it up into lines..
  490. const auto& lines = item_data.wrapped_text_lines;
  491. size_t number_of_text_lines = min((size_t)text_rect.height() / font->glyph_height(), lines.size());
  492. for (size_t line_index = 0; line_index < number_of_text_lines; ++line_index) {
  493. Gfx::IntRect line_rect;
  494. line_rect.set_width(text_rect.width());
  495. line_rect.set_height(font->glyph_height());
  496. line_rect.center_horizontally_within(item_data.text_rect);
  497. line_rect.set_y(2 + item_data.text_rect.y() + line_index * font->glyph_height());
  498. line_rect.inflate(6, 0);
  499. // Shrink the line_rect on the last line to apply elision if there are more lines.
  500. if (number_of_text_lines - 1 == line_index && lines.size() > number_of_text_lines)
  501. line_rect.inflate(-(6 + 2 * font->max_glyph_width()), 0);
  502. draw_item_text(painter, item_data.index, item_data.selected, line_rect, lines[line_index], font, Gfx::TextAlignment::Center, Gfx::TextElision::Right);
  503. }
  504. } else {
  505. draw_item_text(painter, item_data.index, item_data.selected, item_data.text_rect, item_data.text, font, Gfx::TextAlignment::Center, Gfx::TextElision::Right);
  506. }
  507. if (item_data.index == m_drop_candidate_index) {
  508. // FIXME: This visualization is not great, as it's also possible to drop things on the text label..
  509. painter.draw_rect(item_data.icon_rect.inflated(8, 8), palette().selection(), true);
  510. }
  511. return IterationDecision::Continue;
  512. });
  513. }
  514. int IconView::item_count() const
  515. {
  516. if (!model())
  517. return 0;
  518. return model()->row_count();
  519. }
  520. void IconView::did_update_selection()
  521. {
  522. AbstractView::did_update_selection();
  523. if (m_changing_selection)
  524. return;
  525. // Selection was modified externally, we need to synchronize our cache
  526. do_clear_selection();
  527. selection().for_each_index([&](const ModelIndex& index) {
  528. if (index.is_valid()) {
  529. auto item_index = model_index_to_item_index(index);
  530. if ((size_t)item_index < m_item_data_cache.size())
  531. do_add_selection(get_item_data(item_index));
  532. }
  533. });
  534. }
  535. void IconView::do_clear_selection()
  536. {
  537. for (size_t item_index = m_first_selected_hint; item_index < m_item_data_cache.size(); item_index++) {
  538. if (m_selected_count_cache == 0)
  539. break;
  540. auto& item_data = m_item_data_cache[item_index];
  541. if (!item_data.selected)
  542. continue;
  543. item_data.selected = false;
  544. m_selected_count_cache--;
  545. }
  546. m_first_selected_hint = 0;
  547. ASSERT(m_selected_count_cache == 0);
  548. }
  549. void IconView::clear_selection()
  550. {
  551. TemporaryChange change(m_changing_selection, true);
  552. AbstractView::clear_selection();
  553. do_clear_selection();
  554. }
  555. bool IconView::do_add_selection(ItemData& item_data)
  556. {
  557. if (!item_data.selected) {
  558. item_data.selected = true;
  559. m_selected_count_cache++;
  560. int item_index = &item_data - &m_item_data_cache[0];
  561. if (m_first_selected_hint > item_index)
  562. m_first_selected_hint = item_index;
  563. return true;
  564. }
  565. return false;
  566. }
  567. void IconView::add_selection(ItemData& item_data)
  568. {
  569. if (do_add_selection(item_data))
  570. AbstractView::add_selection(item_data.index);
  571. }
  572. void IconView::add_selection(const ModelIndex& new_index)
  573. {
  574. TemporaryChange change(m_changing_selection, true);
  575. auto item_index = model_index_to_item_index(new_index);
  576. add_selection(get_item_data(item_index));
  577. }
  578. void IconView::toggle_selection(ItemData& item_data)
  579. {
  580. if (!item_data.selected)
  581. add_selection(item_data);
  582. else
  583. remove_selection(item_data);
  584. }
  585. void IconView::toggle_selection(const ModelIndex& new_index)
  586. {
  587. TemporaryChange change(m_changing_selection, true);
  588. auto item_index = model_index_to_item_index(new_index);
  589. toggle_selection(get_item_data(item_index));
  590. }
  591. void IconView::remove_selection(ItemData& item_data)
  592. {
  593. if (!item_data.selected)
  594. return;
  595. TemporaryChange change(m_changing_selection, true);
  596. item_data.selected = false;
  597. ASSERT(m_selected_count_cache > 0);
  598. m_selected_count_cache--;
  599. int item_index = &item_data - &m_item_data_cache[0];
  600. if (m_first_selected_hint == item_index) {
  601. m_first_selected_hint = 0;
  602. while ((size_t)item_index < m_item_data_cache.size()) {
  603. if (m_item_data_cache[item_index].selected) {
  604. m_first_selected_hint = item_index;
  605. break;
  606. }
  607. item_index++;
  608. }
  609. }
  610. AbstractView::remove_selection(item_data.index);
  611. }
  612. void IconView::set_selection(const ModelIndex& new_index)
  613. {
  614. TemporaryChange change(m_changing_selection, true);
  615. do_clear_selection();
  616. auto item_index = model_index_to_item_index(new_index);
  617. auto& item_data = get_item_data(item_index);
  618. item_data.selected = true;
  619. m_selected_count_cache = 1;
  620. if (item_index < m_first_selected_hint)
  621. m_first_selected_hint = item_index;
  622. AbstractView::set_selection(new_index);
  623. }
  624. int IconView::items_per_page() const
  625. {
  626. if (m_flow_direction == FlowDirection::LeftToRight)
  627. return (visible_content_rect().height() / effective_item_size().height()) * m_visual_column_count;
  628. return (visible_content_rect().width() / effective_item_size().width()) * m_visual_row_count;
  629. }
  630. void IconView::move_cursor(CursorMovement movement, SelectionUpdate selection_update)
  631. {
  632. if (!model())
  633. return;
  634. auto& model = *this->model();
  635. if (!cursor_index().is_valid()) {
  636. set_cursor(model.index(0, model_column()), SelectionUpdate::Set);
  637. return;
  638. }
  639. auto new_row = cursor_index().row();
  640. switch (movement) {
  641. case CursorMovement::Right:
  642. if (m_flow_direction == FlowDirection::LeftToRight)
  643. new_row += 1;
  644. else
  645. new_row += m_visual_row_count;
  646. break;
  647. case CursorMovement::Left:
  648. if (m_flow_direction == FlowDirection::LeftToRight)
  649. new_row -= 1;
  650. else
  651. new_row -= m_visual_row_count;
  652. break;
  653. case CursorMovement::Up:
  654. if (m_flow_direction == FlowDirection::LeftToRight)
  655. new_row -= m_visual_column_count;
  656. else
  657. new_row -= 1;
  658. break;
  659. case CursorMovement::Down:
  660. if (m_flow_direction == FlowDirection::LeftToRight)
  661. new_row += m_visual_column_count;
  662. else
  663. new_row += 1;
  664. break;
  665. case CursorMovement::PageUp:
  666. new_row = max(0, cursor_index().row() - items_per_page());
  667. break;
  668. case CursorMovement::PageDown:
  669. new_row = min(model.row_count() - 1, cursor_index().row() + items_per_page());
  670. break;
  671. case CursorMovement::Home:
  672. new_row = 0;
  673. break;
  674. case CursorMovement::End:
  675. new_row = model.row_count() - 1;
  676. break;
  677. default:
  678. return;
  679. }
  680. auto new_index = model.index(new_row, cursor_index().column());
  681. if (new_index.is_valid())
  682. set_cursor(new_index, selection_update);
  683. }
  684. void IconView::set_flow_direction(FlowDirection flow_direction)
  685. {
  686. if (m_flow_direction == flow_direction)
  687. return;
  688. m_flow_direction = flow_direction;
  689. m_item_data_cache.clear();
  690. m_item_data_cache_valid = false;
  691. update();
  692. }
  693. template<typename Function>
  694. inline IterationDecision IconView::for_each_item_intersecting_rect(const Gfx::IntRect& rect, Function f) const
  695. {
  696. ASSERT(model());
  697. if (rect.is_empty())
  698. return IterationDecision::Continue;
  699. int begin_row, begin_column;
  700. column_row_from_content_position(rect.top_left(), begin_row, begin_column);
  701. int end_row, end_column;
  702. column_row_from_content_position(rect.bottom_right(), end_row, end_column);
  703. int items_per_flow_axis_step;
  704. int item_index;
  705. int last_index;
  706. if (m_flow_direction == FlowDirection::LeftToRight) {
  707. items_per_flow_axis_step = end_column - begin_column + 1;
  708. item_index = max(0, begin_row * m_visual_column_count + begin_column);
  709. last_index = min(item_count(), end_row * m_visual_column_count + end_column + 1);
  710. } else {
  711. items_per_flow_axis_step = end_row - begin_row + 1;
  712. item_index = max(0, begin_column * m_visual_row_count + begin_row);
  713. last_index = min(item_count(), end_column * m_visual_row_count + end_row + 1);
  714. }
  715. while (item_index < last_index) {
  716. for (int i = item_index; i < min(item_index + items_per_flow_axis_step, last_index); i++) {
  717. auto& item_data = get_item_data(i);
  718. if (item_data.is_intersecting(rect)) {
  719. auto decision = f(item_data);
  720. if (decision != IterationDecision::Continue)
  721. return decision;
  722. }
  723. }
  724. item_index += m_visual_column_count;
  725. };
  726. return IterationDecision::Continue;
  727. }
  728. template<typename Function>
  729. inline IterationDecision IconView::for_each_item_intersecting_rects(const Vector<Gfx::IntRect>& rects, Function f) const
  730. {
  731. for (auto& rect : rects) {
  732. auto decision = for_each_item_intersecting_rect(rect, f);
  733. if (decision != IterationDecision::Continue)
  734. return decision;
  735. }
  736. return IterationDecision::Continue;
  737. }
  738. }