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<Stateful<Div>>;
 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(cx.listener(|_, action: &ActionA, cx| {
 46                println!("Action A dispatched on parent");
 47            }))
 48            .on_action(cx.listener(|_, action: &ActionB, cx| {
 49                println!("Action B dispatched on parent");
 50            }))
 51            .on_focus(cx.listener(|_, _, _| println!("Parent focused")))
 52            .on_blur(cx.listener(|_, _, _| println!("Parent blurred")))
 53            .on_focus_in(cx.listener(|_, _, _| println!("Parent focus_in")))
 54            .on_focus_out(cx.listener(|_, _, _| println!("Parent focus_out")))
 55            .on_key_down(cx.listener(|_, event, _| println!("Key down on parent {:?}", event)))
 56            .on_key_up(cx.listener(|_, event, _| 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(cx.listener(|_, 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(cx.listener(|_, _, _| println!("Child 1 focused")))
 73                    .on_blur(cx.listener(|_, _, _| println!("Child 1 blurred")))
 74                    .on_focus_in(cx.listener(|_, _, _| println!("Child 1 focus_in")))
 75                    .on_focus_out(cx.listener(|_, _, _| println!("Child 1 focus_out")))
 76                    .on_key_down(
 77                        cx.listener(|_, event, _| println!("Key down on child 1 {:?}", event)),
 78                    )
 79                    .on_key_up(cx.listener(|_, event, _| println!("Key up on child 1 {:?}", event)))
 80                    .child("Child 1"),
 81            )
 82            .child(
 83                div()
 84                    .track_focus(&self.child_2_focus)
 85                    .key_context("child-2")
 86                    .on_action(cx.listener(|_, action: &ActionC, cx| {
 87                        println!("Action C dispatched on child 2");
 88                    }))
 89                    .w_full()
 90                    .h_6()
 91                    .bg(color_4)
 92                    .on_focus(cx.listener(|_, _, _| println!("Child 2 focused")))
 93                    .on_blur(cx.listener(|_, _, _| println!("Child 2 blurred")))
 94                    .on_focus_in(cx.listener(|_, _, _| println!("Child 2 focus_in")))
 95                    .on_focus_out(cx.listener(|_, _, _| println!("Child 2 focus_out")))
 96                    .on_key_down(
 97                        cx.listener(|_, event, _| println!("Key down on child 2 {:?}", event)),
 98                    )
 99                    .on_key_up(cx.listener(|_, event, _| println!("Key up on child 2 {:?}", event)))
100                    .child("Child 2"),
101            )
102    }
103}