AbstractView.cpp 24 KB

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