AbstractView.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 <AK/Vector.h>
  29. #include <LibCore/Timer.h>
  30. #include <LibGUI/AbstractView.h>
  31. #include <LibGUI/DragOperation.h>
  32. #include <LibGUI/Model.h>
  33. #include <LibGUI/ModelEditingDelegate.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/ScrollBar.h>
  36. #include <LibGUI/TextBox.h>
  37. #include <LibGfx/Palette.h>
  38. namespace GUI {
  39. AbstractView::AbstractView()
  40. : m_sort_order(SortOrder::Ascending)
  41. , m_selection(*this)
  42. {
  43. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  44. }
  45. AbstractView::~AbstractView()
  46. {
  47. if (m_searching_timer)
  48. m_searching_timer->stop();
  49. if (m_model)
  50. m_model->unregister_view({}, *this);
  51. }
  52. void AbstractView::set_model(RefPtr<Model> model)
  53. {
  54. if (model == m_model)
  55. return;
  56. if (m_model)
  57. m_model->unregister_view({}, *this);
  58. m_model = move(model);
  59. if (m_model)
  60. m_model->register_view({}, *this);
  61. did_update_model(GUI::Model::InvalidateAllIndexes);
  62. scroll_to_top();
  63. }
  64. void AbstractView::did_update_model(unsigned flags)
  65. {
  66. // FIXME: It's unfortunate that we lose so much view state when the model updates in any way.
  67. stop_editing();
  68. m_edit_index = {};
  69. m_hovered_index = {};
  70. m_cursor_index = {};
  71. if (!model() || (flags & GUI::Model::InvalidateAllIndexes)) {
  72. clear_selection();
  73. } else {
  74. selection().remove_matching([this](auto& index) { return !model()->is_valid(index); });
  75. }
  76. }
  77. void AbstractView::clear_selection()
  78. {
  79. m_selection.clear();
  80. }
  81. void AbstractView::set_selection(const ModelIndex& new_index)
  82. {
  83. m_selection.set(new_index);
  84. }
  85. void AbstractView::add_selection(const ModelIndex& new_index)
  86. {
  87. m_selection.add(new_index);
  88. }
  89. void AbstractView::remove_selection(const ModelIndex& new_index)
  90. {
  91. m_selection.remove(new_index);
  92. }
  93. void AbstractView::toggle_selection(const ModelIndex& new_index)
  94. {
  95. m_selection.toggle(new_index);
  96. }
  97. void AbstractView::did_update_selection()
  98. {
  99. if (!model() || selection().first() != m_edit_index)
  100. stop_editing();
  101. if (model() && on_selection && selection().first().is_valid())
  102. on_selection(selection().first());
  103. }
  104. void AbstractView::did_scroll()
  105. {
  106. update_edit_widget_position();
  107. }
  108. void AbstractView::update_edit_widget_position()
  109. {
  110. if (!m_edit_widget)
  111. return;
  112. m_edit_widget->set_relative_rect(m_edit_widget_content_rect.translated(-horizontal_scrollbar().value(), -vertical_scrollbar().value()));
  113. }
  114. void AbstractView::begin_editing(const ModelIndex& index)
  115. {
  116. ASSERT(is_editable());
  117. ASSERT(model());
  118. if (m_edit_index == index)
  119. return;
  120. if (!model()->is_editable(index))
  121. return;
  122. if (m_edit_widget) {
  123. remove_child(*m_edit_widget);
  124. m_edit_widget = nullptr;
  125. }
  126. m_edit_index = index;
  127. ASSERT(aid_create_editing_delegate);
  128. m_editing_delegate = aid_create_editing_delegate(index);
  129. m_editing_delegate->bind(*model(), index);
  130. m_editing_delegate->set_value(index.data());
  131. m_edit_widget = m_editing_delegate->widget();
  132. add_child(*m_edit_widget);
  133. m_edit_widget->move_to_back();
  134. m_edit_widget_content_rect = content_rect(index).translated(frame_thickness(), frame_thickness());
  135. update_edit_widget_position();
  136. m_edit_widget->set_focus(true);
  137. m_editing_delegate->will_begin_editing();
  138. m_editing_delegate->on_commit = [this] {
  139. ASSERT(model());
  140. model()->set_data(m_edit_index, m_editing_delegate->value());
  141. stop_editing();
  142. };
  143. m_editing_delegate->on_rollback = [this] {
  144. ASSERT(model());
  145. stop_editing();
  146. };
  147. }
  148. void AbstractView::stop_editing()
  149. {
  150. bool take_back_focus = false;
  151. m_edit_index = {};
  152. if (m_edit_widget) {
  153. take_back_focus = m_edit_widget->is_focused();
  154. remove_child(*m_edit_widget);
  155. m_edit_widget = nullptr;
  156. }
  157. if (take_back_focus)
  158. set_focus(true);
  159. }
  160. void AbstractView::activate(const ModelIndex& index)
  161. {
  162. if (on_activation)
  163. on_activation(index);
  164. }
  165. void AbstractView::activate_selected()
  166. {
  167. if (!on_activation)
  168. return;
  169. selection().for_each_index([this](auto& index) {
  170. on_activation(index);
  171. });
  172. }
  173. void AbstractView::notify_selection_changed(Badge<ModelSelection>)
  174. {
  175. did_update_selection();
  176. if (on_selection_change)
  177. on_selection_change();
  178. update();
  179. }
  180. NonnullRefPtr<Gfx::Font> AbstractView::font_for_index(const ModelIndex& index) const
  181. {
  182. if (!model())
  183. return font();
  184. auto font_data = index.data(ModelRole::Font);
  185. if (font_data.is_font())
  186. return font_data.as_font();
  187. return font();
  188. }
  189. void AbstractView::mousedown_event(MouseEvent& event)
  190. {
  191. ScrollableWidget::mousedown_event(event);
  192. if (!model())
  193. return;
  194. if (event.button() == MouseButton::Left)
  195. m_left_mousedown_position = event.position();
  196. auto index = index_at_event_position(event.position());
  197. m_might_drag = false;
  198. if (!index.is_valid()) {
  199. clear_selection();
  200. } else if (event.modifiers() & Mod_Ctrl) {
  201. set_cursor(index, SelectionUpdate::Ctrl);
  202. } else if (event.modifiers() & Mod_Shift) {
  203. set_cursor(index, SelectionUpdate::Shift);
  204. } else if (event.button() == MouseButton::Left && m_selection.contains(index) && !m_model->drag_data_type().is_null()) {
  205. // We might be starting a drag, so don't throw away other selected items yet.
  206. m_might_drag = true;
  207. } else if (event.button() == MouseButton::Right) {
  208. set_cursor(index, SelectionUpdate::ClearIfNotSelected);
  209. } else {
  210. set_cursor(index, SelectionUpdate::Set);
  211. m_might_drag = true;
  212. }
  213. update();
  214. }
  215. void AbstractView::set_hovered_index(const ModelIndex& index)
  216. {
  217. if (m_hovered_index == index)
  218. return;
  219. auto old_index = m_hovered_index;
  220. m_hovered_index = index;
  221. did_change_hovered_index(old_index, index);
  222. update();
  223. }
  224. void AbstractView::leave_event(Core::Event& event)
  225. {
  226. ScrollableWidget::leave_event(event);
  227. set_hovered_index({});
  228. }
  229. void AbstractView::mousemove_event(MouseEvent& event)
  230. {
  231. if (!model())
  232. return ScrollableWidget::mousemove_event(event);
  233. auto hovered_index = index_at_event_position(event.position());
  234. set_hovered_index(hovered_index);
  235. auto data_type = m_model->drag_data_type();
  236. if (data_type.is_null())
  237. return ScrollableWidget::mousemove_event(event);
  238. if (!m_might_drag)
  239. return ScrollableWidget::mousemove_event(event);
  240. if (!(event.buttons() & MouseButton::Left) || m_selection.is_empty()) {
  241. m_might_drag = false;
  242. return ScrollableWidget::mousemove_event(event);
  243. }
  244. auto diff = event.position() - m_left_mousedown_position;
  245. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  246. constexpr int drag_distance_threshold = 5;
  247. if (distance_travelled_squared <= drag_distance_threshold)
  248. return ScrollableWidget::mousemove_event(event);
  249. ASSERT(!data_type.is_null());
  250. dbg() << "Initiate drag!";
  251. auto drag_operation = DragOperation::construct();
  252. RefPtr<Gfx::Bitmap> bitmap;
  253. StringBuilder text_builder;
  254. StringBuilder data_builder;
  255. bool first = true;
  256. m_selection.for_each_index([&](auto& index) {
  257. auto text_data = index.data();
  258. if (!first)
  259. text_builder.append(", ");
  260. text_builder.append(text_data.to_string());
  261. auto drag_data = index.data(ModelRole::DragData);
  262. data_builder.append(drag_data.to_string());
  263. data_builder.append('\n');
  264. first = false;
  265. if (!bitmap) {
  266. Variant icon_data = index.data(ModelRole::Icon);
  267. if (icon_data.is_icon())
  268. bitmap = icon_data.as_icon().bitmap_for_size(32);
  269. }
  270. });
  271. drag_operation->set_text(text_builder.to_string());
  272. drag_operation->set_bitmap(bitmap);
  273. drag_operation->set_data(data_type, data_builder.to_string());
  274. auto outcome = drag_operation->exec();
  275. switch (outcome) {
  276. case DragOperation::Outcome::Accepted:
  277. dbg() << "Drag was accepted!";
  278. break;
  279. case DragOperation::Outcome::Cancelled:
  280. dbg() << "Drag was cancelled!";
  281. break;
  282. default:
  283. ASSERT_NOT_REACHED();
  284. break;
  285. }
  286. }
  287. void AbstractView::mouseup_event(MouseEvent& event)
  288. {
  289. ScrollableWidget::mouseup_event(event);
  290. if (!model())
  291. return;
  292. if (m_might_drag) {
  293. // We were unsure about unselecting items other than the current one
  294. // in mousedown_event(), because we could be seeing a start of a drag.
  295. // Since we're here, it was not that; so fix up the selection now.
  296. auto index = index_at_event_position(event.position());
  297. if (index.is_valid())
  298. set_selection(index);
  299. else
  300. clear_selection();
  301. m_might_drag = false;
  302. update();
  303. }
  304. if (activates_on_selection())
  305. activate_selected();
  306. }
  307. void AbstractView::doubleclick_event(MouseEvent& event)
  308. {
  309. if (!model())
  310. return;
  311. if (event.button() != MouseButton::Left)
  312. return;
  313. m_might_drag = false;
  314. auto index = index_at_event_position(event.position());
  315. if (!index.is_valid()) {
  316. clear_selection();
  317. return;
  318. }
  319. if (!m_selection.contains(index))
  320. set_selection(index);
  321. if (is_editable() && edit_triggers() & EditTrigger::DoubleClicked)
  322. begin_editing(cursor_index());
  323. else
  324. activate(cursor_index());
  325. }
  326. void AbstractView::context_menu_event(ContextMenuEvent& event)
  327. {
  328. if (!model())
  329. return;
  330. auto index = index_at_event_position(event.position());
  331. if (index.is_valid())
  332. add_selection(index);
  333. else
  334. clear_selection();
  335. if (on_context_menu_request)
  336. on_context_menu_request(index, event);
  337. }
  338. void AbstractView::drop_event(DropEvent& event)
  339. {
  340. event.accept();
  341. if (!model())
  342. return;
  343. auto index = index_at_event_position(event.position());
  344. if (on_drop)
  345. on_drop(index, event);
  346. }
  347. void AbstractView::set_multi_select(bool multi_select)
  348. {
  349. if (m_multi_select == multi_select)
  350. return;
  351. m_multi_select = multi_select;
  352. if (!multi_select && m_selection.size() > 1) {
  353. auto first_selected = m_selection.first();
  354. m_selection.clear();
  355. m_selection.set(first_selected);
  356. }
  357. }
  358. void AbstractView::set_key_column_and_sort_order(int column, SortOrder sort_order)
  359. {
  360. m_key_column = column;
  361. m_sort_order = sort_order;
  362. if (model())
  363. model()->sort(column, sort_order);
  364. update();
  365. }
  366. void AbstractView::set_cursor(ModelIndex index, SelectionUpdate selection_update, bool scroll_cursor_into_view)
  367. {
  368. if (!model() || !index.is_valid()) {
  369. m_cursor_index = {};
  370. cancel_searching();
  371. return;
  372. }
  373. if (!m_cursor_index.is_valid() || model()->parent_index(m_cursor_index) != model()->parent_index(index))
  374. cancel_searching();
  375. if (model()->is_valid(index)) {
  376. if (selection_update == SelectionUpdate::Set)
  377. set_selection(index);
  378. else if (selection_update == SelectionUpdate::Ctrl)
  379. toggle_selection(index);
  380. else if (selection_update == SelectionUpdate::ClearIfNotSelected) {
  381. if (!m_selection.contains(index))
  382. clear_selection();
  383. } else if (selection_update == SelectionUpdate::Shift) {
  384. // Toggle all from cursor to new index.
  385. auto min_row = min(cursor_index().row(), index.row());
  386. auto max_row = max(cursor_index().row(), index.row());
  387. auto min_column = min(cursor_index().column(), index.column());
  388. auto max_column = max(cursor_index().column(), index.column());
  389. for (auto row = min_row; row <= max_row; ++row) {
  390. for (auto column = min_column; column <= max_column; ++column) {
  391. auto new_index = model()->index(row, column);
  392. if (new_index.is_valid())
  393. toggle_selection(new_index);
  394. }
  395. }
  396. // Finally toggle the cursor index again to make it go back to its current state.
  397. toggle_selection(cursor_index());
  398. }
  399. // FIXME: Support the other SelectionUpdate types
  400. auto old_cursor_index = m_cursor_index;
  401. m_cursor_index = index;
  402. did_change_cursor_index(old_cursor_index, m_cursor_index);
  403. if (scroll_cursor_into_view)
  404. scroll_into_view(index, true, true);
  405. update();
  406. }
  407. }
  408. void AbstractView::set_edit_triggers(unsigned triggers)
  409. {
  410. m_edit_triggers = triggers;
  411. }
  412. void AbstractView::hide_event(HideEvent& event)
  413. {
  414. stop_editing();
  415. ScrollableWidget::hide_event(event);
  416. }
  417. void AbstractView::keydown_event(KeyEvent& event)
  418. {
  419. if (event.key() == KeyCode::Key_F2) {
  420. if (is_editable() && edit_triggers() & EditTrigger::EditKeyPressed) {
  421. begin_editing(cursor_index());
  422. event.accept();
  423. return;
  424. }
  425. }
  426. if (event.key() == KeyCode::Key_Return) {
  427. activate_selected();
  428. event.accept();
  429. return;
  430. }
  431. SelectionUpdate selection_update = SelectionUpdate::Set;
  432. if (event.modifiers() == KeyModifier::Mod_Shift) {
  433. selection_update = SelectionUpdate::Shift;
  434. }
  435. if (event.key() == KeyCode::Key_Left) {
  436. move_cursor(CursorMovement::Left, selection_update);
  437. event.accept();
  438. return;
  439. }
  440. if (event.key() == KeyCode::Key_Right) {
  441. move_cursor(CursorMovement::Right, selection_update);
  442. event.accept();
  443. return;
  444. }
  445. if (event.key() == KeyCode::Key_Up) {
  446. move_cursor(CursorMovement::Up, selection_update);
  447. event.accept();
  448. return;
  449. }
  450. if (event.key() == KeyCode::Key_Down) {
  451. move_cursor(CursorMovement::Down, selection_update);
  452. event.accept();
  453. return;
  454. }
  455. if (event.key() == KeyCode::Key_Home) {
  456. move_cursor(CursorMovement::Home, selection_update);
  457. event.accept();
  458. return;
  459. }
  460. if (event.key() == KeyCode::Key_End) {
  461. move_cursor(CursorMovement::End, selection_update);
  462. event.accept();
  463. return;
  464. }
  465. if (event.key() == KeyCode::Key_PageUp) {
  466. move_cursor(CursorMovement::PageUp, selection_update);
  467. event.accept();
  468. return;
  469. }
  470. if (event.key() == KeyCode::Key_PageDown) {
  471. move_cursor(CursorMovement::PageDown, selection_update);
  472. event.accept();
  473. return;
  474. }
  475. if (is_searchable()) {
  476. if (event.key() == KeyCode::Key_Backspace) {
  477. if (is_searching()) {
  478. //if (event.modifiers() == Mod_Ctrl) {
  479. // TODO: delete last word
  480. //}
  481. Utf8View view(m_searching);
  482. size_t n_code_points = view.length();
  483. if (n_code_points > 1) {
  484. n_code_points--;
  485. StringBuilder sb;
  486. for (auto it = view.begin(); it != view.end(); ++it) {
  487. if (n_code_points == 0)
  488. break;
  489. n_code_points--;
  490. sb.append_code_point(*it);
  491. }
  492. do_search(sb.to_string());
  493. start_searching_timer();
  494. } else {
  495. cancel_searching();
  496. }
  497. event.accept();
  498. return;
  499. }
  500. } else if (event.key() == KeyCode::Key_Escape) {
  501. if (is_searching()) {
  502. cancel_searching();
  503. event.accept();
  504. return;
  505. }
  506. } else if (event.key() != KeyCode::Key_Tab && !event.ctrl() && !event.alt() && event.code_point() != 0) {
  507. StringBuilder sb;
  508. sb.append(m_searching);
  509. sb.append_code_point(event.code_point());
  510. do_search(sb.to_string());
  511. start_searching_timer();
  512. event.accept();
  513. return;
  514. }
  515. }
  516. ScrollableWidget::keydown_event(event);
  517. }
  518. void AbstractView::cancel_searching()
  519. {
  520. m_searching = nullptr;
  521. if (m_searching_timer)
  522. m_searching_timer->stop();
  523. if (m_highlighted_search_index.is_valid()) {
  524. m_highlighted_search_index = {};
  525. update();
  526. }
  527. }
  528. void AbstractView::start_searching_timer()
  529. {
  530. if (!m_searching_timer) {
  531. m_searching_timer = add<Core::Timer>();
  532. m_searching_timer->set_single_shot(true);
  533. m_searching_timer->on_timeout = [this] {
  534. cancel_searching();
  535. };
  536. }
  537. m_searching_timer->set_interval(5 * 1000);
  538. m_searching_timer->restart();
  539. }
  540. void AbstractView::do_search(String&& searching)
  541. {
  542. if (searching.is_empty() || !model()) {
  543. cancel_searching();
  544. return;
  545. }
  546. auto found_indexes = model()->matches(searching, Model::MatchesFlag::FirstMatchOnly | Model::MatchesFlag::MatchAtStart | Model::MatchesFlag::CaseInsensitive, model()->parent_index(cursor_index()));
  547. if (!found_indexes.is_empty() && found_indexes[0].is_valid()) {
  548. auto& index = found_indexes[0];
  549. m_highlighted_search_index = index;
  550. m_searching = move(searching);
  551. set_selection(index);
  552. scroll_into_view(index);
  553. update();
  554. }
  555. }
  556. bool AbstractView::is_searchable() const
  557. {
  558. if (!m_searchable || !model())
  559. return false;
  560. return model()->is_searchable();
  561. }
  562. void AbstractView::set_searchable(bool searchable)
  563. {
  564. if (m_searchable == searchable)
  565. return;
  566. m_searchable = searchable;
  567. if (!m_searchable)
  568. cancel_searching();
  569. }
  570. bool AbstractView::is_highlighting_searching(const ModelIndex& index) const
  571. {
  572. return index == m_highlighted_search_index;
  573. }
  574. void AbstractView::draw_item_text(Gfx::Painter& painter, const ModelIndex& index, bool is_selected, const Gfx::IntRect& text_rect, const StringView& item_text, const Gfx::Font& font, Gfx::TextAlignment alignment, Gfx::TextElision elision)
  575. {
  576. Color text_color;
  577. if (is_selected)
  578. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  579. else
  580. text_color = index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
  581. if (is_highlighting_searching(index)) {
  582. Utf8View searching_text(searching());
  583. auto searching_length = searching_text.length();
  584. // Highlight the text background first
  585. painter.draw_text([&](const Gfx::IntRect& rect, u32) {
  586. if (searching_length > 0) {
  587. searching_length--;
  588. painter.fill_rect(rect.inflated(0, 2), palette().highlight_searching());
  589. }
  590. },
  591. text_rect, item_text, font, alignment, elision);
  592. // Then draw the text
  593. auto highlight_text_color = palette().highlight_searching_text();
  594. searching_length = searching_text.length();
  595. painter.draw_text([&](const Gfx::IntRect& rect, u32 code_point) {
  596. if (searching_length > 0) {
  597. searching_length--;
  598. painter.draw_glyph_or_emoji(rect.location(), code_point, font, highlight_text_color);
  599. } else {
  600. painter.draw_glyph_or_emoji(rect.location(), code_point, font, text_color);
  601. }
  602. },
  603. text_rect, item_text, font, alignment, elision);
  604. } else {
  605. painter.draw_text(text_rect, item_text, font, alignment, text_color, elision);
  606. }
  607. }
  608. void AbstractView::focusin_event(FocusEvent& event)
  609. {
  610. ScrollableWidget::focusin_event(event);
  611. if (model() && !cursor_index().is_valid()) {
  612. move_cursor(CursorMovement::Home, SelectionUpdate::None);
  613. clear_selection();
  614. }
  615. }
  616. }