AbstractView.cpp 22 KB

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