AbstractView.cpp 23 KB

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