diff options
author | Andreas Kling <kling@serenityos.org> | 2020-08-27 17:47:19 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-27 17:47:19 +0200 |
commit | 0b9d765f6bc080eda22594120ed95815ad839d10 (patch) | |
tree | 3958f2775c24685e94c6fbec64947ee24cdd684d /Libraries/LibGUI/TreeView.cpp | |
parent | 1b3169f405ac9250b65ee3608e2962f51d2d8e3c (diff) | |
download | serenity-0b9d765f6bc080eda22594120ed95815ad839d10.zip |
LibGUI: Add AbstractView::move_cursor() and share some movement logic
A view can now be told to move its cursor in one of multiple directions
as specified by the CursorMovement enum.
View subclasses can override move_cursor(CursorMovement) to implement
their own cursor behavior. By default, AbstractView::move_cursor() is
a no-op.
This patch improves code sharing between TableView and TreeView. :^)
Diffstat (limited to 'Libraries/LibGUI/TreeView.cpp')
-rw-r--r-- | Libraries/LibGUI/TreeView.cpp | 104 |
1 files changed, 67 insertions, 37 deletions
diff --git a/Libraries/LibGUI/TreeView.cpp b/Libraries/LibGUI/TreeView.cpp index 0984bd0628..b19f89f51b 100644 --- a/Libraries/LibGUI/TreeView.cpp +++ b/Libraries/LibGUI/TreeView.cpp @@ -418,43 +418,6 @@ void TreeView::keydown_event(KeyEvent& event) return; } - if (event.key() == KeyCode::Key_Up) { - ModelIndex previous_index; - ModelIndex found_index; - traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) { - if (index == cursor_index) { - found_index = previous_index; - return IterationDecision::Break; - } - previous_index = index; - return IterationDecision::Continue; - }); - if (found_index.is_valid()) { - selection().set(found_index); - scroll_into_view(selection().first(), Orientation::Vertical); - update(); - } - return; - } - if (event.key() == KeyCode::Key_Down) { - ModelIndex previous_index; - ModelIndex found_index; - traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) { - if (previous_index == cursor_index) { - found_index = index; - return IterationDecision::Break; - } - previous_index = index; - return IterationDecision::Continue; - }); - if (found_index.is_valid()) { - selection().set(found_index); - scroll_into_view(selection().first(), Orientation::Vertical); - update(); - } - return; - } - auto open_tree_node = [&](bool open, MetadataForIndex& metadata) { if (on_toggle) on_toggle(cursor_index, open); @@ -509,6 +472,73 @@ void TreeView::keydown_event(KeyEvent& event) return; } } + + AbstractTableView::keydown_event(event); +} + +void TreeView::move_cursor(CursorMovement movement) +{ + auto cursor_index = selection().first(); + + switch (movement) { + case CursorMovement::Up: { + ModelIndex previous_index; + ModelIndex found_index; + traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) { + if (index == cursor_index) { + found_index = previous_index; + return IterationDecision::Break; + } + previous_index = index; + return IterationDecision::Continue; + }); + if (found_index.is_valid()) { + selection().set(found_index); + scroll_into_view(selection().first(), Orientation::Vertical); + update(); + } + break; + } + case CursorMovement::Down: { + ModelIndex previous_index; + ModelIndex found_index; + traverse_in_paint_order([&](const ModelIndex& index, const Gfx::IntRect&, const Gfx::IntRect&, int) { + if (previous_index == cursor_index) { + found_index = index; + return IterationDecision::Break; + } + previous_index = index; + return IterationDecision::Continue; + }); + if (found_index.is_valid()) { + selection().set(found_index); + scroll_into_view(selection().first(), Orientation::Vertical); + update(); + } + return; + } + + case CursorMovement::Home: + // FIXME: Implement. + break; + + case CursorMovement::End: + // FIXME: Implement. + break; + + case CursorMovement::PageUp: + // FIXME: Implement. + break; + + case CursorMovement::PageDown: + // FIXME: Implement. + break; + + case CursorMovement::Left: + case CursorMovement::Right: + // There is no left/right in a treeview, those keys expand/collapse items instead. + break; + } } int TreeView::item_count() const |