AbstractView.cpp 25 KB

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