AbstractView.cpp 22 KB

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