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