AbstractView.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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 const> 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. if (widget_inner_rect().contains(event.position())) {
  256. auto hovered_index = index_at_event_position(event.position());
  257. set_hovered_index(hovered_index);
  258. }
  259. auto data_type = m_model->drag_data_type();
  260. if (data_type.is_null())
  261. return AbstractScrollableWidget::mousemove_event(event);
  262. if (!m_might_drag)
  263. return AbstractScrollableWidget::mousemove_event(event);
  264. if (!(event.buttons() & MouseButton::Primary) || m_selection.is_empty()) {
  265. m_might_drag = false;
  266. return AbstractScrollableWidget::mousemove_event(event);
  267. }
  268. auto diff = event.position() - m_left_mousedown_position;
  269. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  270. constexpr int drag_distance_threshold = 5;
  271. if (distance_travelled_squared <= drag_distance_threshold)
  272. return AbstractScrollableWidget::mousemove_event(event);
  273. VERIFY(!data_type.is_null());
  274. if (m_is_dragging)
  275. return;
  276. // An event might sneak in between us constructing the drag operation and the
  277. // event loop exec at the end of `drag_operation->exec()' if the user is fast enough.
  278. // Prevent this by just ignoring later drag initiations (until the current drag operation ends).
  279. TemporaryChange dragging { m_is_dragging, true };
  280. dbgln_if(DRAG_DEBUG, "Initiate drag!");
  281. auto drag_operation = DragOperation::construct();
  282. drag_operation->set_mime_data(m_model->mime_data(m_selection));
  283. auto outcome = drag_operation->exec();
  284. switch (outcome) {
  285. case DragOperation::Outcome::Accepted:
  286. dbgln_if(DRAG_DEBUG, "Drag was accepted!");
  287. break;
  288. case DragOperation::Outcome::Cancelled:
  289. dbgln_if(DRAG_DEBUG, "Drag was cancelled!");
  290. m_might_drag = false;
  291. break;
  292. default:
  293. VERIFY_NOT_REACHED();
  294. break;
  295. }
  296. }
  297. void AbstractView::mouseup_event(MouseEvent& event)
  298. {
  299. AbstractScrollableWidget::mouseup_event(event);
  300. if (!model())
  301. return;
  302. set_automatic_scrolling_timer_active(false);
  303. if (m_might_drag) {
  304. // We were unsure about unselecting items other than the current one
  305. // in mousedown_event(), because we could be seeing a start of a drag.
  306. // Since we're here, it was not that; so fix up the selection now.
  307. auto index = index_at_event_position(event.position());
  308. if (index.is_valid()) {
  309. set_cursor(index, SelectionUpdate::Set, true);
  310. } else
  311. clear_selection();
  312. m_might_drag = false;
  313. update();
  314. }
  315. if (activates_on_selection())
  316. activate_selected();
  317. }
  318. void AbstractView::doubleclick_event(MouseEvent& event)
  319. {
  320. if (!model())
  321. return;
  322. if (event.button() != MouseButton::Primary)
  323. return;
  324. m_might_drag = false;
  325. auto index = index_at_event_position(event.position());
  326. if (!index.is_valid()) {
  327. clear_selection();
  328. return;
  329. }
  330. if (!m_selection.contains(index))
  331. set_selection(index);
  332. if (is_editable() && edit_triggers() & EditTrigger::DoubleClicked)
  333. begin_editing(cursor_index());
  334. else
  335. activate(cursor_index());
  336. }
  337. void AbstractView::context_menu_event(ContextMenuEvent& event)
  338. {
  339. if (!model())
  340. return;
  341. auto index = index_at_event_position(event.position());
  342. if (index.is_valid())
  343. add_selection(index);
  344. else
  345. clear_selection();
  346. if (on_context_menu_request)
  347. on_context_menu_request(index, event);
  348. }
  349. void AbstractView::drop_event(DropEvent& event)
  350. {
  351. event.accept();
  352. if (!model())
  353. return;
  354. auto index = index_at_event_position(event.position());
  355. if (on_drop)
  356. on_drop(index, event);
  357. }
  358. void AbstractView::set_selection_mode(SelectionMode selection_mode)
  359. {
  360. if (m_selection_mode == selection_mode)
  361. return;
  362. m_selection_mode = selection_mode;
  363. if (m_selection_mode == SelectionMode::NoSelection)
  364. m_selection.clear();
  365. else if (m_selection_mode != SelectionMode::SingleSelection && m_selection.size() > 1) {
  366. auto first_selected = m_selection.first();
  367. m_selection.clear();
  368. m_selection.set(first_selected);
  369. }
  370. update();
  371. }
  372. void AbstractView::set_key_column_and_sort_order(int column, SortOrder sort_order)
  373. {
  374. m_key_column = column;
  375. m_sort_order = sort_order;
  376. if (model())
  377. model()->sort(column, sort_order);
  378. update();
  379. }
  380. void AbstractView::select_range(ModelIndex const& index)
  381. {
  382. auto min_row = min(selection_start_index().row(), index.row());
  383. auto max_row = max(selection_start_index().row(), index.row());
  384. auto min_column = min(selection_start_index().column(), index.column());
  385. auto max_column = max(selection_start_index().column(), index.column());
  386. clear_selection();
  387. for (auto row = min_row; row <= max_row; ++row) {
  388. for (auto column = min_column; column <= max_column; ++column) {
  389. auto new_index = model()->index(row, column);
  390. if (new_index.is_valid())
  391. toggle_selection(new_index);
  392. }
  393. }
  394. }
  395. void AbstractView::set_cursor(ModelIndex index, SelectionUpdate selection_update, bool scroll_cursor_into_view)
  396. {
  397. if (!model() || !index.is_valid() || selection_mode() == SelectionMode::NoSelection) {
  398. m_cursor_index = {};
  399. stop_highlighted_search_timer();
  400. return;
  401. }
  402. if (!m_cursor_index.is_valid() || model()->parent_index(m_cursor_index) != model()->parent_index(index))
  403. stop_highlighted_search_timer();
  404. if (selection_mode() == SelectionMode::SingleSelection && (selection_update == SelectionUpdate::Ctrl || selection_update == SelectionUpdate::Shift))
  405. selection_update = SelectionUpdate::Set;
  406. if (model()->is_within_range(index)) {
  407. if (selection_update == SelectionUpdate::Set) {
  408. set_selection(index);
  409. set_selection_start_index(index);
  410. } else if (selection_update == SelectionUpdate::Ctrl) {
  411. toggle_selection(index);
  412. } else if (selection_update == SelectionUpdate::ClearIfNotSelected) {
  413. if (!m_selection.contains(index))
  414. clear_selection();
  415. } else if (selection_update == SelectionUpdate::Shift) {
  416. if (!selection_start_index().is_valid())
  417. set_selection_start_index(index);
  418. select_range(index);
  419. }
  420. // FIXME: Support the other SelectionUpdate types
  421. auto old_cursor_index = m_cursor_index;
  422. m_cursor_index = index;
  423. did_change_cursor_index(old_cursor_index, m_cursor_index);
  424. if (scroll_cursor_into_view)
  425. scroll_into_view(index, true, true);
  426. update();
  427. }
  428. }
  429. void AbstractView::set_edit_triggers(unsigned triggers)
  430. {
  431. m_edit_triggers = triggers;
  432. }
  433. void AbstractView::hide_event(HideEvent& event)
  434. {
  435. stop_editing();
  436. AbstractScrollableWidget::hide_event(event);
  437. }
  438. void AbstractView::keydown_event(KeyEvent& event)
  439. {
  440. if (event.alt()) {
  441. event.ignore();
  442. return;
  443. }
  444. if (event.key() == KeyCode::Key_F2) {
  445. if (is_editable() && edit_triggers() & EditTrigger::EditKeyPressed) {
  446. begin_editing(cursor_index());
  447. event.accept();
  448. return;
  449. }
  450. }
  451. if (event.key() == KeyCode::Key_Return) {
  452. activate_selected();
  453. event.accept();
  454. return;
  455. }
  456. SelectionUpdate selection_update = SelectionUpdate::Set;
  457. if (event.modifiers() == KeyModifier::Mod_Shift) {
  458. selection_update = SelectionUpdate::Shift;
  459. }
  460. if (event.key() == KeyCode::Key_Left) {
  461. move_cursor(CursorMovement::Left, selection_update);
  462. event.accept();
  463. return;
  464. }
  465. if (event.key() == KeyCode::Key_Right) {
  466. move_cursor(CursorMovement::Right, selection_update);
  467. event.accept();
  468. return;
  469. }
  470. if (event.key() == KeyCode::Key_Up) {
  471. move_cursor(CursorMovement::Up, selection_update);
  472. event.accept();
  473. return;
  474. }
  475. if (event.key() == KeyCode::Key_Down) {
  476. move_cursor(CursorMovement::Down, selection_update);
  477. event.accept();
  478. return;
  479. }
  480. if (event.key() == KeyCode::Key_Home) {
  481. move_cursor(CursorMovement::Home, selection_update);
  482. event.accept();
  483. return;
  484. }
  485. if (event.key() == KeyCode::Key_End) {
  486. move_cursor(CursorMovement::End, selection_update);
  487. event.accept();
  488. return;
  489. }
  490. if (event.key() == KeyCode::Key_PageUp) {
  491. move_cursor(CursorMovement::PageUp, selection_update);
  492. event.accept();
  493. return;
  494. }
  495. if (event.key() == KeyCode::Key_PageDown) {
  496. move_cursor(CursorMovement::PageDown, selection_update);
  497. event.accept();
  498. return;
  499. }
  500. if (is_searchable()) {
  501. if (event.key() == KeyCode::Key_Backspace) {
  502. if (!m_highlighted_search.is_null()) {
  503. // if (event.modifiers() == Mod_Ctrl) {
  504. // TODO: delete last word
  505. // }
  506. Utf8View view(m_highlighted_search);
  507. size_t n_code_points = view.length();
  508. if (n_code_points > 1) {
  509. n_code_points--;
  510. StringBuilder sb;
  511. for (auto it = view.begin(); it != view.end(); ++it) {
  512. if (n_code_points == 0)
  513. break;
  514. n_code_points--;
  515. sb.append_code_point(*it);
  516. }
  517. auto index = find_next_search_match(sb.string_view());
  518. if (index.is_valid()) {
  519. m_highlighted_search = sb.to_deprecated_string();
  520. highlight_search(index);
  521. start_highlighted_search_timer();
  522. }
  523. } else {
  524. stop_highlighted_search_timer();
  525. }
  526. event.accept();
  527. return;
  528. }
  529. } else if (event.key() == KeyCode::Key_Escape) {
  530. if (!m_highlighted_search.is_null()) {
  531. stop_highlighted_search_timer();
  532. event.accept();
  533. return;
  534. }
  535. } else if (event.key() != KeyCode::Key_Tab && !event.ctrl() && !event.alt() && event.code_point() != 0) {
  536. StringBuilder sb;
  537. sb.append(m_highlighted_search);
  538. sb.append_code_point(event.code_point());
  539. auto index = find_next_search_match(sb.string_view());
  540. if (index.is_valid()) {
  541. m_highlighted_search = sb.to_deprecated_string();
  542. highlight_search(index);
  543. start_highlighted_search_timer();
  544. set_cursor(index, SelectionUpdate::None, true);
  545. }
  546. event.accept();
  547. return;
  548. }
  549. }
  550. AbstractScrollableWidget::keydown_event(event);
  551. }
  552. void AbstractView::stop_highlighted_search_timer()
  553. {
  554. m_highlighted_search = nullptr;
  555. if (m_highlighted_search_timer)
  556. m_highlighted_search_timer->stop();
  557. if (m_highlighted_search_index.is_valid()) {
  558. m_highlighted_search_index = {};
  559. update();
  560. }
  561. }
  562. void AbstractView::start_highlighted_search_timer()
  563. {
  564. if (!m_highlighted_search_timer) {
  565. m_highlighted_search_timer = add<Core::Timer>();
  566. m_highlighted_search_timer->set_single_shot(true);
  567. m_highlighted_search_timer->on_timeout = [this] {
  568. stop_highlighted_search_timer();
  569. };
  570. }
  571. m_highlighted_search_timer->set_interval(5 * 1000);
  572. m_highlighted_search_timer->restart();
  573. }
  574. ModelIndex AbstractView::find_next_search_match(StringView const search)
  575. {
  576. if (search.is_empty())
  577. return {};
  578. auto found_indices = model()->matches(search, Model::MatchesFlag::FirstMatchOnly | Model::MatchesFlag::MatchAtStart | Model::MatchesFlag::CaseInsensitive, model()->parent_index(cursor_index()));
  579. if (found_indices.is_empty())
  580. return {};
  581. return found_indices[0];
  582. }
  583. void AbstractView::highlight_search(ModelIndex const index)
  584. {
  585. m_highlighted_search_index = index;
  586. set_selection(index);
  587. scroll_into_view(index);
  588. update();
  589. }
  590. bool AbstractView::is_searchable() const
  591. {
  592. if (!m_searchable || !model())
  593. return false;
  594. return model()->is_searchable();
  595. }
  596. void AbstractView::set_searchable(bool searchable)
  597. {
  598. if (m_searchable == searchable)
  599. return;
  600. m_searchable = searchable;
  601. if (!m_searchable)
  602. stop_highlighted_search_timer();
  603. }
  604. 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)
  605. {
  606. if (m_edit_index == index)
  607. return;
  608. Color text_color;
  609. if (!is_enabled())
  610. text_color = palette().color(Gfx::ColorRole::DisabledText);
  611. else if (is_selected)
  612. text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  613. else
  614. text_color = index.data(ModelRole::ForegroundColor).to_color(palette().color(foreground_role()));
  615. if (index == m_highlighted_search_index) {
  616. Utf8View searching_text(m_highlighted_search);
  617. auto searching_length = searching_text.length();
  618. if (searching_length > search_highlighting_offset)
  619. searching_length -= search_highlighting_offset;
  620. else if (search_highlighting_offset > 0)
  621. searching_length = 0;
  622. // Highlight the text background first
  623. auto background_searching_length = searching_length;
  624. painter.draw_text([&](Gfx::FloatRect const& rect, Utf8CodePointIterator&) {
  625. if (background_searching_length > 0) {
  626. background_searching_length--;
  627. painter.fill_rect(rect.to_type<int>().inflated(0, 2), palette().highlight_searching());
  628. }
  629. },
  630. text_rect, item_text, font, alignment, elision);
  631. // Then draw the text
  632. auto text_searching_length = searching_length;
  633. auto highlight_text_color = palette().highlight_searching_text();
  634. searching_length = searching_text.length();
  635. painter.draw_text([&](auto const& rect, Utf8CodePointIterator& it) {
  636. if (text_searching_length > 0) {
  637. text_searching_length--;
  638. painter.draw_glyph_or_emoji(rect.location(), it, font, highlight_text_color);
  639. } else {
  640. painter.draw_glyph_or_emoji(rect.location(), it, font, text_color);
  641. }
  642. },
  643. text_rect, item_text, font, alignment, elision);
  644. } else {
  645. if (m_draw_item_text_with_shadow) {
  646. painter.draw_text(text_rect.translated(1, 1), item_text, font, alignment, Color::Black, elision);
  647. painter.draw_text(text_rect, item_text, font, alignment, Color::White, elision);
  648. } else {
  649. painter.draw_text(text_rect, item_text, font, alignment, text_color, elision);
  650. }
  651. }
  652. }
  653. void AbstractView::focusin_event(FocusEvent& event)
  654. {
  655. AbstractScrollableWidget::focusin_event(event);
  656. if (model() && !cursor_index().is_valid()) {
  657. move_cursor(CursorMovement::Home, SelectionUpdate::None);
  658. clear_selection();
  659. }
  660. }
  661. void AbstractView::drag_enter_event(DragEvent& event)
  662. {
  663. if (!model())
  664. return;
  665. if (!is_editable())
  666. return;
  667. // NOTE: Right now, AbstractView accepts drags since we won't get "drag move" events
  668. // unless we accept the "drag enter" event.
  669. // We might be able to reduce event traffic by communicating the set of drag-accepting
  670. // rects in this widget to the windowing system somehow.
  671. event.accept();
  672. dbgln_if(DRAG_DEBUG, "accepting drag of {}", event.mime_types().first());
  673. }
  674. void AbstractView::drag_move_event(DragEvent& event)
  675. {
  676. if (!model())
  677. return;
  678. auto index = index_at_event_position(event.position());
  679. ModelIndex new_drop_candidate_index;
  680. bool acceptable = model()->accepts_drag(index, event.mime_types());
  681. if (acceptable && index.is_valid())
  682. new_drop_candidate_index = index;
  683. if (acceptable) {
  684. m_automatic_scroll_delta = automatic_scroll_delta_from_position(event.position());
  685. set_automatic_scrolling_timer_active(!m_automatic_scroll_delta.is_zero());
  686. }
  687. if (m_drop_candidate_index != new_drop_candidate_index) {
  688. m_drop_candidate_index = new_drop_candidate_index;
  689. update();
  690. }
  691. if (m_drop_candidate_index.is_valid())
  692. event.accept();
  693. }
  694. void AbstractView::drag_leave_event(Event&)
  695. {
  696. if (m_drop_candidate_index.is_valid()) {
  697. m_drop_candidate_index = {};
  698. update();
  699. }
  700. set_automatic_scrolling_timer_active(false);
  701. }
  702. void AbstractView::automatic_scrolling_timer_did_fire()
  703. {
  704. if (m_automatic_scroll_delta.is_zero())
  705. return;
  706. vertical_scrollbar().increase_slider_by(m_automatic_scroll_delta.y());
  707. horizontal_scrollbar().increase_slider_by(m_automatic_scroll_delta.x());
  708. }
  709. }