Fix Input avoid copy, cut a empty into into clipboard.

This commit is contained in:
Jason Lee 2024-07-02 17:35:06 +08:00
parent 88dcca590d
commit 537dbfab9e
3 changed files with 19 additions and 2 deletions

View file

@ -14,6 +14,7 @@ actions!(
[ [
Backspace, Backspace,
Delete, Delete,
Enter,
Left, Left,
Right, Right,
SelectLeft, SelectLeft,
@ -33,12 +34,14 @@ actions!(
pub enum TextEvent { pub enum TextEvent {
Input { text: SharedString }, Input { text: SharedString },
PressEnter,
} }
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.bind_keys([ cx.bind_keys([
KeyBinding::new("backspace", Backspace, None), KeyBinding::new("backspace", Backspace, None),
KeyBinding::new("delete", Delete, None), KeyBinding::new("delete", Delete, None),
KeyBinding::new("enter", Enter, None),
KeyBinding::new("left", Left, None), KeyBinding::new("left", Left, None),
KeyBinding::new("right", Right, None), KeyBinding::new("right", Right, None),
KeyBinding::new("shift-left", SelectLeft, None), KeyBinding::new("shift-left", SelectLeft, None),
@ -236,16 +239,28 @@ impl TextInput {
self.replace_text_in_range(None, "", cx) self.replace_text_in_range(None, "", cx)
} }
fn enter(&mut self, _: &Enter, cx: &mut ViewContext<Self>) {
cx.emit(TextEvent::PressEnter);
}
fn show_character_palette(&mut self, _: &ShowCharacterPalette, cx: &mut ViewContext<Self>) { fn show_character_palette(&mut self, _: &ShowCharacterPalette, cx: &mut ViewContext<Self>) {
cx.show_character_palette(); cx.show_character_palette();
} }
fn copy(&mut self, _: &Copy, cx: &mut ViewContext<Self>) { fn copy(&mut self, _: &Copy, cx: &mut ViewContext<Self>) {
if self.selected_range.is_empty() {
return;
}
let selected_text = self.text[self.selected_range.clone()].to_string(); let selected_text = self.text[self.selected_range.clone()].to_string();
cx.write_to_clipboard(ClipboardItem::new(selected_text)); cx.write_to_clipboard(ClipboardItem::new(selected_text));
} }
fn cut(&mut self, _: &Cut, cx: &mut ViewContext<Self>) { fn cut(&mut self, _: &Cut, cx: &mut ViewContext<Self>) {
if self.selected_range.is_empty() {
return;
}
let selected_text = self.text[self.selected_range.clone()].to_string(); let selected_text = self.text[self.selected_range.clone()].to_string();
cx.write_to_clipboard(ClipboardItem::new(selected_text)); cx.write_to_clipboard(ClipboardItem::new(selected_text));
self.replace_text_in_range(Some(self.selected_range.clone()), "", cx); 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<Self>) { pub fn paste(&mut self, _: &Paste, cx: &mut ViewContext<Self>) {
if let Some(clipboard) = cx.read_from_clipboard() { 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| { .when(!self.disabled, |this| {
this.on_action(cx.listener(Self::backspace)) this.on_action(cx.listener(Self::backspace))
.on_action(cx.listener(Self::delete)) .on_action(cx.listener(Self::delete))
.on_action(cx.listener(Self::enter))
}) })
.on_action(cx.listener(Self::left)) .on_action(cx.listener(Self::left))
.on_action(cx.listener(Self::right)) .on_action(cx.listener(Self::right))

View file

@ -534,6 +534,7 @@ impl<D: PickerDelegate> Picker<D> {
self.set_query(text, cx); self.set_query(text, cx);
self.refresh(cx); self.refresh(cx);
} }
_ => {}
} }
} }
} }

View file

@ -141,7 +141,6 @@ impl RenderOnce for Switch {
self.on_click.filter(|_| !self.disabled), self.on_click.filter(|_| !self.disabled),
|this, on_click| { |this, on_click| {
this.on_click(move |ev, cx| { this.on_click(move |ev, cx| {
dbg!("---- switch clicked");
cx.stop_propagation(); cx.stop_propagation();
on_click(ev, cx); on_click(ev, cx);
}) })