From a4601782f8d045c67ebfc00580c3629fad3f884b Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 26 Jun 2024 16:20:21 +0800 Subject: [PATCH] Add keyboard support for Picker. --- README.md | 3 +++ crates/ui/src/picker.rs | 27 ++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 684b3daf..1aa0bf2e 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ https://github.com/huacnlee/gpui-app/assets/5518/ad103f02-697a-40ed-a876-8b13e42 - [ ] Radio & RadioGroup - [ ] Dropdown - [ ] Combobox + - [ ] Picker + - [x] Picker List + - [x] Use keyword to select next, prev, enter to select, esc to cancel. - [x] Tabs - [x] Tab - [x] TabBar diff --git a/crates/ui/src/picker.rs b/crates/ui/src/picker.rs index ace04ccf..975419cd 100644 --- a/crates/ui/src/picker.rs +++ b/crates/ui/src/picker.rs @@ -3,11 +3,10 @@ use std::time::Duration; use anyhow::Result; use gpui::{ actions, div, list, prelude::FluentBuilder as _, px, rems, uniform_list, AppContext, - ClickEvent, DismissEvent, Div, EventEmitter, FocusHandle, FocusableView, - InteractiveElement as _, IntoElement, Length, ListSizingBehavior, ListState, MouseButton, - MouseUpEvent, ParentElement as _, Render, SharedString, StatefulInteractiveElement as _, - Styled as _, Task, UniformListScrollHandle, View, ViewContext, VisualContext as _, - WindowContext, + ClickEvent, DismissEvent, Div, EventEmitter, FocusHandle, FocusableView, InteractiveElement, + IntoElement, Length, ListSizingBehavior, ListState, MouseButton, MouseUpEvent, + ParentElement as _, Render, SharedString, StatefulInteractiveElement as _, Styled as _, Task, + UniformListScrollHandle, View, ViewContext, VisualContext as _, WindowContext, }; actions!( @@ -486,5 +485,23 @@ impl Render for Picker { .child(div().child(Label::new("No matched.").text_color(cx.theme().muted))), ) }) + .on_key_down(cx.listener(|this, ev: &gpui::KeyDownEvent, cx| { + let key = ev.keystroke.key.as_str(); + match key { + "escape" => { + this.cancel(&Cancel, cx); + } + "enter" => { + this.confirm(&Confirm, cx); + } + "down" => { + this.select_next(&SelectNext, cx); + } + "up" => { + this.select_prev(&SelectPrev, cx); + } + _ => {} + } + })) } }