AbstractView.cpp 25 KB

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