IconView.cpp 32 KB

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