From 537dbfab9e5f461e3065e543100353fec4c12925 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 2 Jul 2024 17:35:06 +0800 Subject: [PATCH] Fix Input avoid copy, cut a empty into into clipboard. --- crates/ui/src/input.rs | 19 ++++++++++++++++++- crates/ui/src/picker.rs | 1 + crates/ui/src/switch.rs | 1 - 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/ui/src/input.rs b/crates/ui/src/input.rs index 7680566b..6fbc386e 100644 --- a/crates/ui/src/input.rs +++ b/crates/ui/src/input.rs @@ -14,6 +14,7 @@ actions!( [ Backspace, Delete, + Enter, Left, Right, SelectLeft, @@ -33,12 +34,14 @@ actions!( pub enum TextEvent { Input { text: SharedString }, + PressEnter, } pub fn init(cx: &mut AppContext) { cx.bind_keys([ KeyBinding::new("backspace", Backspace, None), KeyBinding::new("delete", Delete, None), + KeyBinding::new("enter", Enter, None), KeyBinding::new("left", Left, None), KeyBinding::new("right", Right, None), KeyBinding::new("shift-left", SelectLeft, None), @@ -236,16 +239,28 @@ impl TextInput { self.replace_text_in_range(None, "", cx) } + fn enter(&mut self, _: &Enter, cx: &mut ViewContext) { + cx.emit(TextEvent::PressEnter); + } + fn show_character_palette(&mut self, _: &ShowCharacterPalette, cx: &mut ViewContext) { cx.show_character_palette(); } fn copy(&mut self, _: &Copy, cx: &mut ViewContext) { + if self.selected_range.is_empty() { + return; + } + let selected_text = self.text[self.selected_range.clone()].to_string(); cx.write_to_clipboard(ClipboardItem::new(selected_text)); } fn cut(&mut self, _: &Cut, cx: &mut ViewContext) { + if self.selected_range.is_empty() { + return; + } + let selected_text = self.text[self.selected_range.clone()].to_string(); cx.write_to_clipboard(ClipboardItem::new(selected_text)); self.replace_text_in_range(Some(self.selected_range.clone()), "", cx); @@ -253,7 +268,8 @@ impl TextInput { pub fn paste(&mut self, _: &Paste, cx: &mut ViewContext) { if let Some(clipboard) = cx.read_from_clipboard() { - self.replace_text_in_range(Some(self.selected_range.clone()), clipboard.text(), cx); + let new_text = clipboard.text().replace('\n', ""); + self.replace_text_in_range(Some(self.selected_range.clone()), &new_text, cx); } } @@ -639,6 +655,7 @@ impl Render for TextInput { .when(!self.disabled, |this| { this.on_action(cx.listener(Self::backspace)) .on_action(cx.listener(Self::delete)) + .on_action(cx.listener(Self::enter)) }) .on_action(cx.listener(Self::left)) .on_action(cx.listener(Self::right)) diff --git a/crates/ui/src/picker.rs b/crates/ui/src/picker.rs index a3bff37e..394b5b6d 100644 --- a/crates/ui/src/picker.rs +++ b/crates/ui/src/picker.rs @@ -534,6 +534,7 @@ impl Picker { self.set_query(text, cx); self.refresh(cx); } + _ => {} } } } diff --git a/crates/ui/src/switch.rs b/crates/ui/src/switch.rs index ab5c929c..0da0251a 100644 --- a/crates/ui/src/switch.rs +++ b/crates/ui/src/switch.rs @@ -141,7 +141,6 @@ impl RenderOnce for Switch { self.on_click.filter(|_| !self.disabled), |this, on_click| { this.on_click(move |ev, cx| { - dbg!("---- switch clicked"); cx.stop_propagation(); on_click(ev, cx); })