AbstractView.cpp 22 KB

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