AbstractView.cpp 26 KB

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