1use gpui::{
2 App, Entity, FocusHandle, KeyBinding, Render, Subscription, Window, actions, div, prelude::*,
3};
4use ui::prelude::*;
5
6actions!(focus, [ActionA, ActionB, ActionC]);
7
8pub struct FocusStory {
9 parent_focus: FocusHandle,
10 child_1_focus: FocusHandle,
11 child_2_focus: FocusHandle,
12 _focus_subscriptions: Vec<Subscription>,
13}
14
15impl FocusStory {
16 pub fn model(window: &mut Window, cx: &mut App) -> Entity<Self> {
17 cx.bind_keys([
18 KeyBinding::new("cmd-a", ActionA, Some("parent")),
19 KeyBinding::new("cmd-a", ActionB, Some("child-1")),
20 KeyBinding::new("cmd-c", ActionC, None),
21 ]);
22
23 cx.new(|cx| {
24 let parent_focus = cx.focus_handle();
25 let child_1_focus = cx.focus_handle();
26 let child_2_focus = cx.focus_handle();
27 let _focus_subscriptions = vec![
28 cx.on_focus(&parent_focus, window, |_, _, _| {
29 println!("Parent focused");
30 }),
31 cx.on_blur(&parent_focus, window, |_, _, _| {
32 println!("Parent blurred");
33 }),
34 cx.on_focus(&child_1_focus, window, |_, _, _| {
35 println!("Child 1 focused");
36 }),
37 cx.on_blur(&child_1_focus, window, |_, _, _| {
38 println!("Child 1 blurred");
39 }),
40 cx.on_focus(&child_2_focus, window, |_, _, _| {
41 println!("Child 2 focused");
42 }),
43 cx.on_blur(&child_2_focus, window, |_, _, _| {
44 println!("Child 2 blurred");
45 }),
46 ];
47
48 Self {
49 parent_focus,
50 child_1_focus,
51 child_2_focus,
52 _focus_subscriptions,
53 }
54 })
55 }
56}
57
58impl Render for FocusStory {
59 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
60 let theme = cx.theme();
61 let color_1 = theme.status().created;
62 let color_2 = theme.status().modified;
63 let color_4 = theme.status().conflict;
64 let color_5 = theme.status().ignored;
65 let color_6 = theme.status().renamed;
66 let color_7 = theme.status().hint;
67
68 div()
69 .id("parent")
70 .active(|style| style.bg(color_7))
71 .track_focus(&self.parent_focus)
72 .key_context("parent")
73 .on_action(cx.listener(|_, _action: &ActionA, _window, _cx| {
74 println!("Action A dispatched on parent");
75 }))
76 .on_action(cx.listener(|_, _action: &ActionB, _window, _cx| {
77 println!("Action B dispatched on parent");
78 }))
79 .on_key_down(cx.listener(|_, event, _, _| println!("Key down on parent {:?}", event)))
80 .on_key_up(cx.listener(|_, event, _, _| println!("Key up on parent {:?}", event)))
81 .size_full()
82 .bg(color_1)
83 .focus(|style| style.bg(color_2))
84 .child(
85 div()
86 .track_focus(&self.child_1_focus)
87 .key_context("child-1")
88 .on_action(cx.listener(|_, _action: &ActionB, _window, _cx| {
89 println!("Action B dispatched on child 1 during");
90 }))
91 .w_full()
92 .h_6()
93 .bg(color_4)
94 .focus(|style| style.bg(color_5))
95 .in_focus(|style| style.bg(color_6))
96 .on_key_down(
97 cx.listener(|_, event, _, _| println!("Key down on child 1 {:?}", event)),
98 )
99 .on_key_up(
100 cx.listener(|_, event, _, _| println!("Key up on child 1 {:?}", event)),
101 )
102 .child("Child 1"),
103 )
104 .child(
105 div()
106 .track_focus(&self.child_2_focus)
107 .key_context("child-2")
108 .on_action(cx.listener(|_, _action: &ActionC, _window, _cx| {
109 println!("Action C dispatched on child 2");
110 }))
111 .w_full()
112 .h_6()
113 .bg(color_4)
114 .on_key_down(
115 cx.listener(|_, event, _, _| println!("Key down on child 2 {:?}", event)),
116 )
117 .on_key_up(
118 cx.listener(|_, event, _, _| println!("Key up on child 2 {:?}", event)),
119 )
120 .child("Child 2"),
121 )
122 }
123}