focus.rs

 1use gpui::{
 2    actions, div, prelude::*, Div, FocusHandle, Focusable, KeyBinding, Render, Stateful, View,
 3    WindowContext,
 4};
 5use theme2::ActiveTheme;
 6
 7actions!(ActionA, ActionB, ActionC);
 8
 9pub struct FocusStory {
10    child_1_focus: FocusHandle,
11    child_2_focus: FocusHandle,
12}
13
14impl FocusStory {
15    pub fn view(cx: &mut WindowContext) -> View<Self> {
16        cx.bind_keys([
17            KeyBinding::new("cmd-a", ActionA, Some("parent")),
18            KeyBinding::new("cmd-a", ActionB, Some("child-1")),
19            KeyBinding::new("cmd-c", ActionC, None),
20        ]);
21
22        cx.build_view(move |cx| Self {
23            child_1_focus: cx.focus_handle(),
24            child_2_focus: cx.focus_handle(),
25        })
26    }
27}
28
29impl Render for FocusStory {
30    type Element = Focusable<Self, Stateful<Self, Div<Self>>>;
31
32    fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> Self::Element {
33        let theme = cx.theme();
34        let color_1 = theme.status().created;
35        let color_2 = theme.status().modified;
36        let color_3 = theme.status().deleted;
37        let color_4 = theme.status().conflict;
38        let color_5 = theme.status().ignored;
39        let color_6 = theme.status().renamed;
40
41        div()
42            .id("parent")
43            .focusable()
44            .key_context("parent")
45            .on_action(|_, action: &ActionA, cx| {
46                println!("Action A dispatched on parent");
47            })
48            .on_action(|_, action: &ActionB, cx| {
49                println!("Action B dispatched on parent");
50            })
51            .on_focus(|_, _, _| println!("Parent focused"))
52            .on_blur(|_, _, _| println!("Parent blurred"))
53            .on_focus_in(|_, _, _| println!("Parent focus_in"))
54            .on_focus_out(|_, _, _| println!("Parent focus_out"))
55            .on_key_down(|_, event, phase, _| println!("Key down on parent {:?}", event))
56            .on_key_up(|_, event, phase, _| println!("Key up on parent {:?}", event))
57            .size_full()
58            .bg(color_1)
59            .focus(|style| style.bg(color_2))
60            .child(
61                div()
62                    .track_focus(&self.child_1_focus)
63                    .key_context("child-1")
64                    .on_action(|_, action: &ActionB, cx| {
65                        println!("Action B dispatched on child 1 during");
66                    })
67                    .w_full()
68                    .h_6()
69                    .bg(color_4)
70                    .focus(|style| style.bg(color_5))
71                    .in_focus(|style| style.bg(color_6))
72                    .on_focus(|_, _, _| println!("Child 1 focused"))
73                    .on_blur(|_, _, _| println!("Child 1 blurred"))
74                    .on_focus_in(|_, _, _| println!("Child 1 focus_in"))
75                    .on_focus_out(|_, _, _| println!("Child 1 focus_out"))
76                    .on_key_down(|_, event, phase, _| println!("Key down on child 1 {:?}", event))
77                    .on_key_up(|_, event, phase, _| println!("Key up on child 1 {:?}", event))
78                    .child("Child 1"),
79            )
80            .child(
81                div()
82                    .track_focus(&self.child_2_focus)
83                    .key_context("child-2")
84                    .on_action(|_, action: &ActionC, cx| {
85                        println!("Action C dispatched on child 2");
86                    })
87                    .w_full()
88                    .h_6()
89                    .bg(color_4)
90                    .on_focus(|_, _, _| println!("Child 2 focused"))
91                    .on_blur(|_, _, _| println!("Child 2 blurred"))
92                    .on_focus_in(|_, _, _| println!("Child 2 focus_in"))
93                    .on_focus_out(|_, _, _| println!("Child 2 focus_out"))
94                    .on_key_down(|_, event, phase, _| println!("Key down on child 2 {:?}", event))
95                    .on_key_up(|_, event, phase, _| println!("Key up on child 2 {:?}", event))
96                    .child("Child 2"),
97            )
98    }
99}