1use crate::prelude::*;
2use gpui::{AnyElement, FocusHandle, ScrollAnchor, ScrollHandle};
3
4/// An element that can be navigated through via keyboard. Intended for use with scrollable views that want to use
5pub struct Navigable {
6 child: AnyElement,
7 selectable_children: Vec<NavigableEntry>,
8}
9
10/// An entry of [Navigable] that can be navigated to.
11#[derive(Clone)]
12pub struct NavigableEntry {
13 #[allow(missing_docs)]
14 pub focus_handle: FocusHandle,
15 #[allow(missing_docs)]
16 pub scroll_anchor: Option<ScrollAnchor>,
17}
18
19impl NavigableEntry {
20 /// Creates a new [NavigableEntry] for a given scroll handle.
21 pub fn new(scroll_handle: &ScrollHandle, cx: &WindowContext<'_>) -> Self {
22 Self {
23 focus_handle: cx.focus_handle(),
24 scroll_anchor: Some(ScrollAnchor::for_handle(scroll_handle.clone())),
25 }
26 }
27 /// Create a new [NavigableEntry] that cannot be scrolled to.
28 pub fn focusable(cx: &WindowContext<'_>) -> Self {
29 Self {
30 focus_handle: cx.focus_handle(),
31 scroll_anchor: None,
32 }
33 }
34}
35impl Navigable {
36 /// Creates new empty [Navigable] wrapper.
37 pub fn new(child: AnyElement) -> Self {
38 Self {
39 child,
40 selectable_children: vec![],
41 }
42 }
43
44 /// Add a new entry that can be navigated to via keyboard.
45 /// The order of calls to [Navigable::entry] determines the order of traversal of elements via successive
46 /// uses of [menu:::SelectNext]/[menu::SelectPrev]
47 pub fn entry(mut self, child: NavigableEntry) -> Self {
48 self.selectable_children.push(child);
49 self
50 }
51
52 fn find_focused(
53 selectable_children: &[NavigableEntry],
54 cx: &mut WindowContext<'_>,
55 ) -> Option<usize> {
56 selectable_children
57 .iter()
58 .position(|entry| entry.focus_handle.contains_focused(cx))
59 }
60}
61impl RenderOnce for Navigable {
62 fn render(self, _: &mut WindowContext<'_>) -> impl crate::IntoElement {
63 div()
64 .on_action({
65 let children = self.selectable_children.clone();
66
67 move |_: &menu::SelectNext, cx| {
68 let target = Self::find_focused(&children, cx)
69 .and_then(|index| {
70 index.checked_add(1).filter(|index| *index < children.len())
71 })
72 .unwrap_or(0);
73 if let Some(entry) = children.get(target) {
74 entry.focus_handle.focus(cx);
75 if let Some(anchor) = &entry.scroll_anchor {
76 anchor.scroll_to(cx);
77 }
78 }
79 }
80 })
81 .on_action({
82 let children = self.selectable_children;
83 move |_: &menu::SelectPrev, cx| {
84 let target = Self::find_focused(&children, cx)
85 .and_then(|index| index.checked_sub(1))
86 .or(children.len().checked_sub(1));
87 if let Some(entry) = target.and_then(|target| children.get(target)) {
88 entry.focus_handle.focus(cx);
89 if let Some(anchor) = &entry.scroll_anchor {
90 anchor.scroll_to(cx);
91 }
92 }
93 }
94 })
95 .size_full()
96 .child(self.child)
97 }
98}