Add keyboard support for Picker.

This commit is contained in:
Jason Lee 2024-06-26 16:20:21 +08:00
parent 5c93282e1d
commit a4601782f8
2 changed files with 25 additions and 5 deletions

View file

@ -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

View file

@ -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<D: PickerDelegate> Render for Picker<D> {
.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);
}
_ => {}
}
}))
}
}