From ea603401e2cbd074aba4dc0d457ba381a0f66957 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 7 Nov 2023 13:48:32 -0800 Subject: [PATCH] Get actions + focus working on picker, now that it's a view Co-authored-by: Marshall --- crates/editor2/src/editor.rs | 2 +- crates/picker2/src/picker2.rs | 15 +++++++++++---- crates/storybook2/src/stories/picker.rs | 15 ++++++--------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/crates/editor2/src/editor.rs b/crates/editor2/src/editor.rs index c63c531cebddcfa1f807ee5460a54b1976d9c175..52ded35d57473192a22828a4e3344ab273530c6c 100644 --- a/crates/editor2/src/editor.rs +++ b/crates/editor2/src/editor.rs @@ -9186,7 +9186,7 @@ impl Editor { // supports // } - fn focus(&self, cx: &mut WindowContext) { + pub fn focus(&self, cx: &mut WindowContext) { cx.focus(&self.focus_handle) } } diff --git a/crates/picker2/src/picker2.rs b/crates/picker2/src/picker2.rs index 5b454d6edd2fbb39657547a1e047e16132d4ee3f..e80801229d06101d17fcfb5c8fe2008e7c080b13 100644 --- a/crates/picker2/src/picker2.rs +++ b/crates/picker2/src/picker2.rs @@ -1,7 +1,8 @@ use editor::Editor; use gpui::{ - div, uniform_list, Component, Div, ParentElement, Render, StatelessInteractive, Styled, - UniformListScrollHandle, View, ViewContext, VisualContext, + div, uniform_list, Component, Div, FocusEnabled, ParentElement, Render, StatefulInteractivity, + StatelessInteractive, Styled, UniformListScrollHandle, View, ViewContext, VisualContext, + WindowContext, }; use std::cmp; @@ -41,6 +42,10 @@ impl Picker { } } + pub fn focus(&self, cx: &mut WindowContext) { + self.editor.update(cx, |editor, cx| editor.focus(cx)); + } + fn select_next(&mut self, _: &menu::SelectNext, cx: &mut ViewContext) { let count = self.delegate.match_count(); if count > 0 { @@ -91,12 +96,14 @@ impl Picker { } impl Render for Picker { - type Element = Div; + type Element = Div, FocusEnabled>; fn render(&mut self, _cx: &mut ViewContext) -> Self::Element { div() - .size_full() .context("picker") + .id("picker-container") + .focusable() + .size_full() .on_action(Self::select_next) .on_action(Self::select_prev) .on_action(Self::select_first) diff --git a/crates/storybook2/src/stories/picker.rs b/crates/storybook2/src/stories/picker.rs index e256d0f53c11f3136adc2ba14b1c00416ddf424a..0a0ee0c1d4513df617c55b16561352d1e90948e8 100644 --- a/crates/storybook2/src/stories/picker.rs +++ b/crates/storybook2/src/stories/picker.rs @@ -1,13 +1,12 @@ use gpui::{ - div, Component, Div, FocusHandle, KeyBinding, ParentElement, Render, SharedString, - StatelessInteractive, Styled, View, VisualContext, WindowContext, + div, Component, Div, KeyBinding, ParentElement, Render, SharedString, StatelessInteractive, + Styled, View, VisualContext, WindowContext, }; use picker::{Picker, PickerDelegate}; use theme2::ActiveTheme; pub struct PickerStory { picker: View>, - focus_handle: FocusHandle, } struct Delegate { @@ -86,13 +85,9 @@ impl PickerStory { KeyBinding::new("ctrl-c", menu::Cancel, Some("picker")), ]); - let focus_handle = cx.focus_handle(); - cx.focus(&focus_handle); - PickerStory { - focus_handle, picker: cx.build_view(|cx| { - Picker::new( + let picker = Picker::new( Delegate { candidates: vec![ "Baguette (France)".into(), @@ -148,7 +143,9 @@ impl PickerStory { selected_ix: 0, }, cx, - ) + ); + picker.focus(cx); + picker }), } })