From 10564f5140ff64053e42780f0611f03a6fba4719 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 27 Jun 2024 18:20:24 +0800 Subject: [PATCH] Update KeyBinding for Picker. --- crates/ui/src/lib.rs | 6 ++++++ crates/ui/src/picker.rs | 30 ++++++++++-------------------- crates/workspace/src/lib.rs | 5 +++-- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/crates/ui/src/lib.rs b/crates/ui/src/lib.rs index 252b566f..5be2f43d 100644 --- a/crates/ui/src/lib.rs +++ b/crates/ui/src/lib.rs @@ -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); +} diff --git a/crates/ui/src/picker.rs b/crates/ui/src/picker.rs index 3b2245eb..860cb16d 100644 --- a/crates/ui/src/picker.rs +++ b/crates/ui/src/picker.rs @@ -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 Render for Picker { .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); - } - _ => {} - } - })) } } diff --git a/crates/workspace/src/lib.rs b/crates/workspace/src/lib.rs index aed54c8f..eeb0a6b6 100644 --- a/crates/workspace/src/lib.rs +++ b/crates/workspace/src/lib.rs @@ -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, cx: &mut AppContext, ) -> Task>> { - 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 {