AbstractView.cpp 24 KB

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