13508cbd9c
The creative inventory's search box is an append-only EditBox of 50 characters, but its editing was a plain String pop, so Ctrl+Backspace removed one character instead of the word before the caret, unlike every other text field and vanilla's EditBox.keyPressed. Move EditBox.getWordPosition and deleteWords into one shared helper in ui.rs (the UI and chat text fields had two copies of it), and route a Ctrl+Backspace in the creative search through it. A Select All selection is still what backspace deletes first, as in vanilla. Fixes #1764. Co-Authored-By: OpenCode (DeepSeek V4.1 Flash Free) <noreply@opencode.ai>
128 lines
5.0 KiB
Rust
128 lines
5.0 KiB
Rust
//! Copy and paste in the text fields: `KeyboardHandler`'s clipboard and the
|
|
//! shortcuts of `EditBox`, `MultiLineEditBox` and `TextFieldHelper`.
|
|
use super::*;
|
|
use horizon_client::clipboard::{self, EditShortcut};
|
|
use winit::event::KeyEvent;
|
|
|
|
impl Game<'_> {
|
|
/// `InputWithModifiers.isSelectAll`/`isCopy`/`isPaste`/`isCut` of a key
|
|
/// press (or repeat): the keyboard's modifier state, or a modifier key an
|
|
/// agent holds.
|
|
pub(super) fn edit_shortcut(&self, event: &KeyEvent) -> Option<EditShortcut> {
|
|
if event.state != ElementState::Pressed {
|
|
return None;
|
|
}
|
|
let m = self.modifiers;
|
|
let held = |keys: [KeyCode; 2]| keys.iter().any(|k| self.keys.contains(k));
|
|
// InputQuirks.EDIT_SHORTCUT_KEY_MODIFIER: Cmd on macOS, Ctrl elsewhere.
|
|
let edit = if cfg!(target_os = "macos") {
|
|
m.super_key() || held([KeyCode::SuperLeft, KeyCode::SuperRight])
|
|
} else {
|
|
m.control_key() || held([KeyCode::ControlLeft, KeyCode::ControlRight])
|
|
};
|
|
clipboard::edit_shortcut(
|
|
clipboard::shortcut_letter(&event.logical_key, event.physical_key),
|
|
edit,
|
|
m.shift_key() || held([KeyCode::ShiftLeft, KeyCode::ShiftRight]),
|
|
m.alt_key() || held([KeyCode::AltLeft, KeyCode::AltRight]),
|
|
)
|
|
}
|
|
/// A shortcut in an append-only field limited to `max_chars` characters
|
|
/// (an `EditBox` with `setMaxLength`). True when the value changed.
|
|
pub(super) fn append_shortcut(
|
|
selection: &mut clipboard::AppendSelection,
|
|
board: &mut clipboard::SystemClipboard,
|
|
id: clipboard::FieldId,
|
|
value: &mut String,
|
|
shortcut: EditShortcut,
|
|
max_chars: usize,
|
|
) -> bool {
|
|
selection.apply(id, value, shortcut, board, |v, t| {
|
|
clipboard::append_limited(v, t, max_chars)
|
|
})
|
|
}
|
|
/// Shortcuts on the title, world creation and server screens.
|
|
pub(super) fn title_shortcut(&mut self, shortcut: EditShortcut) {
|
|
let Some(title) = self.title.as_mut() else {
|
|
return;
|
|
};
|
|
if title.has_caret_field() {
|
|
return title.field_shortcut(shortcut, &mut self.clipboard);
|
|
}
|
|
if let Some((id, value, max)) = title.focused_field() {
|
|
let changed = Self::append_shortcut(
|
|
&mut self.text_selection,
|
|
&mut self.clipboard,
|
|
id,
|
|
value,
|
|
shortcut,
|
|
max,
|
|
);
|
|
if changed {
|
|
title.search_changed();
|
|
}
|
|
}
|
|
}
|
|
/// Typing or a backspace in a field of the title screens: in the server
|
|
/// screens' append-only fields Select All's selection is replaced first.
|
|
pub(super) fn title_edit(&mut self, typed: Option<&str>, backspace: bool) {
|
|
let Some(title) = self.title.as_mut() else {
|
|
return;
|
|
};
|
|
let cleared = !title.has_caret_field()
|
|
&& title
|
|
.focused_field()
|
|
.is_some_and(|(id, value, _)| self.text_selection.take(id, value));
|
|
if cleared {
|
|
title.search_changed();
|
|
}
|
|
if backspace {
|
|
if !cleared {
|
|
title.backspace();
|
|
}
|
|
} else if let Some(text) = typed {
|
|
title.type_text(text);
|
|
}
|
|
}
|
|
/// Shortcuts in the creative inventory's search box (an `EditBox` of 50).
|
|
pub(super) fn creative_search_shortcut(&mut self, shortcut: EditShortcut) {
|
|
if Self::append_shortcut(
|
|
&mut self.text_selection,
|
|
&mut self.clipboard,
|
|
CREATIVE_SEARCH,
|
|
&mut self.creative_screen.search,
|
|
shortcut,
|
|
CREATIVE_SEARCH_CHARS,
|
|
) {
|
|
self.creative_screen.scroll = 0.0;
|
|
}
|
|
}
|
|
/// A backspace in the creative search box: with Ctrl, the whole word
|
|
/// before the caret (`EditBox.deleteWords(-1)`), otherwise one character.
|
|
/// A Select All selection is what it deletes, as in `EditBox.keyPressed`.
|
|
pub(super) fn creative_search_backspace(&mut self, ctrl: bool) {
|
|
let search = &mut self.creative_screen.search;
|
|
if !self.text_selection.take(CREATIVE_SEARCH, search) {
|
|
if ctrl {
|
|
horizon_client::ui::delete_words(search, -1);
|
|
} else {
|
|
search.pop();
|
|
}
|
|
}
|
|
self.creative_screen.scroll = 0.0;
|
|
}
|
|
/// Typing in the creative search box.
|
|
pub(super) fn creative_search_edit(&mut self, typed: Option<&str>) {
|
|
let search = &mut self.creative_screen.search;
|
|
self.text_selection.take(CREATIVE_SEARCH, search);
|
|
if let Some(text) = typed {
|
|
clipboard::append_limited(search, text, CREATIVE_SEARCH_CHARS);
|
|
}
|
|
self.creative_screen.scroll = 0.0;
|
|
}
|
|
}
|
|
|
|
/// `CreativeModeInventoryScreen`'s search box: `setMaxLength(50)`.
|
|
pub(super) const CREATIVE_SEARCH_CHARS: usize = 50;
|
|
pub(super) const CREATIVE_SEARCH: horizon_client::clipboard::FieldId = ("creative.search", 0);
|