From bc0359a4745d5d4a84cde74889c65043b0286696 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 9 Jul 2024 08:33:29 -0600 Subject: [PATCH] gpui: Input example log keystrokes (#13963) Release Notes: - N/A --- crates/gpui/examples/input.rs | 53 +++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/crates/gpui/examples/input.rs b/crates/gpui/examples/input.rs index e690202e43faa00414134c04cdcc1bb76c927516..38d4ee48cd76e69250df67f9b2a95a532c99442f 100644 --- a/crates/gpui/examples/input.rs +++ b/crates/gpui/examples/input.rs @@ -451,6 +451,7 @@ impl Render for TextInput { .flex() .key_context("TextInput") .track_focus(&self.focus_handle) + .cursor(CursorStyle::IBeam) .on_action(cx.listener(Self::backspace)) .on_action(cx.listener(Self::delete)) .on_action(cx.listener(Self::left)) @@ -482,6 +483,39 @@ impl Render for TextInput { } } +impl FocusableView for TextInput { + fn focus_handle(&self, _: &AppContext) -> FocusHandle { + self.focus_handle.clone() + } +} + +struct InputExample { + text_input: View, + recent_keystrokes: Vec, +} + +impl Render for InputExample { + fn render(&mut self, _: &mut ViewContext) -> impl IntoElement { + div() + .bg(rgb(0xaaaaaa)) + .flex() + .flex_col() + .size_full() + .child(self.text_input.clone()) + .children(self.recent_keystrokes.iter().rev().map(|ks| { + format!( + "{:} {}", + ks, + if let Some(ime_key) = ks.ime_key.as_ref() { + format!("-> {}", ime_key) + } else { + "".to_owned() + } + ) + })) + } +} + fn main() { App::new().run(|cx: &mut AppContext| { let bounds = Bounds::centered(None, size(px(300.0), px(300.0)), cx); @@ -504,7 +538,7 @@ fn main() { ..Default::default() }, |cx| { - cx.new_view(|cx| TextInput { + let text_input = cx.new_view(|cx| TextInput { focus_handle: cx.focus_handle(), content: "".into(), selected_range: 0..0, @@ -513,14 +547,27 @@ fn main() { last_layout: None, last_bounds: None, is_selecting: false, + }); + cx.new_view(|_| InputExample { + text_input, + recent_keystrokes: vec![], }) }, ) .unwrap(); + cx.observe_keystrokes(move |ev, cx| { + window + .update(cx, |view, cx| { + view.recent_keystrokes.push(ev.keystroke.clone()); + cx.notify(); + }) + .unwrap(); + }) + .detach(); window .update(cx, |view, cx| { - view.focus_handle.focus(cx); - cx.activate(true) + cx.focus_view(&view.text_input); + cx.activate(true); }) .unwrap(); });