Merge pull request #2304 from iced-rs/fix/text-input-macos

Prioritize `TextInput` and `TextEditor` commands over text insertion
This commit is contained in:
Héctor Ramón 2024-02-28 09:50:07 +01:00 committed by GitHub
commit 976a57dcbd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 115 additions and 103 deletions

View file

@ -686,6 +686,37 @@ impl Update {
text,
..
} if state.is_focused => {
match key.as_ref() {
keyboard::Key::Named(key::Named::Enter) => {
return edit(Edit::Enter);
}
keyboard::Key::Named(key::Named::Backspace) => {
return edit(Edit::Backspace);
}
keyboard::Key::Named(key::Named::Delete) => {
return edit(Edit::Delete);
}
keyboard::Key::Named(key::Named::Escape) => {
return Some(Self::Unfocus);
}
keyboard::Key::Character("c")
if modifiers.command() =>
{
return Some(Self::Copy);
}
keyboard::Key::Character("x")
if modifiers.command() =>
{
return Some(Self::Cut);
}
keyboard::Key::Character("v")
if modifiers.command() && !modifiers.alt() =>
{
return Some(Self::Paste);
}
_ => {}
}
if let Some(text) = text {
if let Some(c) = text.chars().find(|c| !c.is_control())
{
@ -711,36 +742,7 @@ impl Update {
}
}
match key.as_ref() {
keyboard::Key::Named(key::Named::Enter) => {
edit(Edit::Enter)
}
keyboard::Key::Named(key::Named::Backspace) => {
edit(Edit::Backspace)
}
keyboard::Key::Named(key::Named::Delete) => {
edit(Edit::Delete)
}
keyboard::Key::Named(key::Named::Escape) => {
Some(Self::Unfocus)
}
keyboard::Key::Character("c")
if modifiers.command() =>
{
Some(Self::Copy)
}
keyboard::Key::Character("x")
if modifiers.command() =>
{
Some(Self::Cut)
}
keyboard::Key::Character("v")
if modifiers.command() && !modifiers.alt() =>
{
Some(Self::Paste)
}
_ => None,
}
None
}
_ => None,
},

View file

@ -762,12 +762,92 @@ where
let modifiers = state.keyboard_modifiers;
focus.updated_at = Instant::now();
match key.as_ref() {
keyboard::Key::Character("c")
if state.keyboard_modifiers.command() =>
{
if let Some((start, end)) =
state.cursor.selection(value)
{
clipboard.write(
clipboard::Kind::Standard,
value.select(start, end).to_string(),
);
}
return event::Status::Captured;
}
keyboard::Key::Character("x")
if state.keyboard_modifiers.command() =>
{
if let Some((start, end)) =
state.cursor.selection(value)
{
clipboard.write(
clipboard::Kind::Standard,
value.select(start, end).to_string(),
);
}
let mut editor = Editor::new(value, &mut state.cursor);
editor.delete();
let message = (on_input)(editor.contents());
shell.publish(message);
update_cache(state, value);
return event::Status::Captured;
}
keyboard::Key::Character("v")
if state.keyboard_modifiers.command()
&& !state.keyboard_modifiers.alt() =>
{
let content = match state.is_pasting.take() {
Some(content) => content,
None => {
let content: String = clipboard
.read(clipboard::Kind::Standard)
.unwrap_or_default()
.chars()
.filter(|c| !c.is_control())
.collect();
Value::new(&content)
}
};
let mut editor = Editor::new(value, &mut state.cursor);
editor.paste(content.clone());
let message = if let Some(paste) = &on_paste {
(paste)(editor.contents())
} else {
(on_input)(editor.contents())
};
shell.publish(message);
state.is_pasting = Some(content);
update_cache(state, value);
return event::Status::Captured;
}
keyboard::Key::Character("a")
if state.keyboard_modifiers.command() =>
{
state.cursor.select_all(value);
}
_ => {}
}
if let Some(text) = text {
state.is_pasting = None;
let c = text.chars().next().unwrap_or_default();
if !c.is_control() {
if let Some(c) =
text.chars().next().filter(|c| !c.is_control())
{
let mut editor = Editor::new(value, &mut state.cursor);
editor.insert(c);
@ -880,76 +960,6 @@ where
state.cursor.move_to(value.len());
}
}
keyboard::Key::Character("c")
if state.keyboard_modifiers.command() =>
{
if let Some((start, end)) =
state.cursor.selection(value)
{
clipboard.write(
clipboard::Kind::Standard,
value.select(start, end).to_string(),
);
}
}
keyboard::Key::Character("x")
if state.keyboard_modifiers.command() =>
{
if let Some((start, end)) =
state.cursor.selection(value)
{
clipboard.write(
clipboard::Kind::Standard,
value.select(start, end).to_string(),
);
}
let mut editor = Editor::new(value, &mut state.cursor);
editor.delete();
let message = (on_input)(editor.contents());
shell.publish(message);
update_cache(state, value);
}
keyboard::Key::Character("v")
if state.keyboard_modifiers.command()
&& !state.keyboard_modifiers.alt() =>
{
let content = match state.is_pasting.take() {
Some(content) => content,
None => {
let content: String = clipboard
.read(clipboard::Kind::Standard)
.unwrap_or_default()
.chars()
.filter(|c| !c.is_control())
.collect();
Value::new(&content)
}
};
let mut editor = Editor::new(value, &mut state.cursor);
editor.paste(content.clone());
let message = if let Some(paste) = &on_paste {
(paste)(editor.contents())
} else {
(on_input)(editor.contents())
};
shell.publish(message);
state.is_pasting = Some(content);
update_cache(state, value);
}
keyboard::Key::Character("a")
if state.keyboard_modifiers.command() =>
{
state.cursor.select_all(value);
}
keyboard::Key::Named(key::Named::Escape) => {
state.is_focused = None;
state.is_dragging = false;