Update KeyBinding for Picker.

This commit is contained in:
Jason Lee 2024-06-27 18:20:24 +08:00
parent 8489a3f7f7
commit 10564f5140
3 changed files with 19 additions and 22 deletions

View file

@ -29,3 +29,9 @@ pub use selectable::*;
pub use icon::*;
pub use stock::*;
/// Initialize the UI module.
pub fn init(cx: &mut gpui::AppContext) {
input::init(cx);
picker::init(cx);
}

View file

@ -4,7 +4,7 @@ use anyhow::Result;
use gpui::{
actions, div, list, prelude::FluentBuilder as _, px, rems, uniform_list, AppContext,
ClickEvent, DismissEvent, Div, EventEmitter, FocusHandle, FocusableView, InteractiveElement,
IntoElement, Length, ListSizingBehavior, ListState, MouseButton, MouseUpEvent,
IntoElement, KeyBinding, Length, ListSizingBehavior, ListState, MouseButton, MouseUpEvent,
ParentElement as _, Render, SharedString, StatefulInteractiveElement as _, Styled as _, Task,
UniformListScrollHandle, View, ViewContext, VisualContext as _, WindowContext,
};
@ -23,11 +23,19 @@ actions!(
]
);
pub fn init(cx: &mut AppContext) {
cx.bind_keys([
KeyBinding::new("enter", Confirm, None),
KeyBinding::new("escape", Cancel, None),
KeyBinding::new("up", SelectPrev, None),
KeyBinding::new("down", SelectNext, None),
]);
}
use crate::{
divider::Divider,
empty::Empty,
input::{TextEvent, TextInput},
label::Label,
stock::*,
theme::ActiveTheme,
StyledExt as _,
@ -516,23 +524,5 @@ impl<D: PickerDelegate> Render for Picker<D> {
.child("No matched."),
)
})
.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);
}
_ => {}
}
}))
}
}

View file

@ -6,6 +6,7 @@ use ui::{
button::ButtonSize,
input,
label::Label,
picker,
switch::{LabelSide, Switch},
theme::{ActiveTheme, Theme},
title_bar::TitleBar,
@ -40,9 +41,9 @@ impl Workspace {
app_state: Arc<AppState>,
cx: &mut AppContext,
) -> Task<anyhow::Result<WindowHandle<Workspace>>> {
let window_bounds = Bounds::centered(None, size(px(1200.0), px(900.0)), cx);
ui::init(cx);
input::init(cx);
let window_bounds = Bounds::centered(None, size(px(1200.0), px(900.0)), cx);
cx.spawn(|mut cx| async move {
let options = WindowOptions {