focus.rs

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