IconView.cpp 30 KB

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