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            .focus_in(|style| style.bg(color_3))
 61            .child(
 62                div()
 63                    .track_focus(&self.child_1_focus)
 64                    .key_context("child-1")
 65                    .on_action(|_, action: &ActionB, cx| {
 66                        println!("Action B dispatched on child 1 during");
 67                    })
 68                    .w_full()
 69                    .h_6()
 70                    .bg(color_4)
 71                    .focus(|style| style.bg(color_5))
 72                    .in_focus(|style| style.bg(color_6))
 73                    .on_focus(|_, _, _| println!("Child 1 focused"))
 74                    .on_blur(|_, _, _| println!("Child 1 blurred"))
 75                    .on_focus_in(|_, _, _| println!("Child 1 focus_in"))
 76                    .on_focus_out(|_, _, _| println!("Child 1 focus_out"))
 77                    .on_key_down(|_, event, phase, _| println!("Key down on child 1 {:?}", event))
 78                    .on_key_up(|_, event, phase, _| println!("Key up on child 1 {:?}", event))
 79                    .child("Child 1"),
 80            )
 81            .child(
 82                div()
 83                    .track_focus(&self.child_2_focus)
 84                    .key_context("child-2")
 85                    .on_action(|_, action: &ActionC, cx| {
 86                        println!("Action C dispatched on child 2");
 87                    })
 88                    .w_full()
 89                    .h_6()
 90                    .bg(color_4)
 91                    .on_focus(|_, _, _| println!("Child 2 focused"))
 92                    .on_blur(|_, _, _| println!("Child 2 blurred"))
 93                    .on_focus_in(|_, _, _| println!("Child 2 focus_in"))
 94                    .on_focus_out(|_, _, _| println!("Child 2 focus_out"))
 95                    .on_key_down(|_, event, phase, _| println!("Key down on child 2 {:?}", event))
 96                    .on_key_up(|_, event, phase, _| println!("Key up on child 2 {:?}", event))
 97                    .child("Child 2"),
 98            )
 99    }
100}