AbstractView.cpp 21 KB

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