IconView.cpp 30 KB

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