From 13508cbd9c6ee7122694dc3f1482a10683f408ca Mon Sep 17 00:00:00 2001 From: Jan Pres Date: Sun, 25 Oct 2026 17:41:07 +0000 Subject: [PATCH] Creative: Ctrl+Backspace deletes a word in the search 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) --- crates/horizon-client/src/game.rs | 5 +- crates/horizon-client/src/game/clipboard.rs | 22 ++++-- crates/horizon-client/src/game/control.rs | 2 +- crates/horizon-client/src/game/pad_menu.rs | 6 +- crates/horizon-client/src/screen_draw.rs | 21 +---- crates/horizon-client/src/ui.rs | 86 ++++++++++++++++----- 6 files changed, 92 insertions(+), 50 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7fcb3be89..6df6e531c 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -11272,9 +11272,10 @@ impl ApplicationHandler for Game<'_> { // The hovered slot took the key (`ignoreTextInput`). } else if let Some(shortcut) = self.edit_shortcut(&event) { self.creative_search_shortcut(shortcut); + } else if event.physical_key == PhysicalKey::Code(KeyCode::Backspace) { + self.creative_search_backspace(self.has_control_down()); } else { - let backspace = event.physical_key == PhysicalKey::Code(KeyCode::Backspace); - self.creative_search_edit(event.text.as_deref(), backspace); + self.creative_search_edit(event.text.as_deref()); } } WindowEvent::KeyboardInput { event, .. } diff --git a/crates/horizon-client/src/game/clipboard.rs b/crates/horizon-client/src/game/clipboard.rs index b9558ddfd..c87aa3681 100644 --- a/crates/horizon-client/src/game/clipboard.rs +++ b/crates/horizon-client/src/game/clipboard.rs @@ -97,15 +97,25 @@ impl Game<'_> { self.creative_screen.scroll = 0.0; } } - /// Typing or a backspace in the creative search box. - pub(super) fn creative_search_edit(&mut self, typed: Option<&str>, backspace: bool) { + /// 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; - let cleared = self.text_selection.take(CREATIVE_SEARCH, search); - if backspace { - if !cleared { + if !self.text_selection.take(CREATIVE_SEARCH, search) { + if ctrl { + horizon_client::ui::delete_words(search, -1); + } else { search.pop(); } - } else if let Some(text) = typed { + } + 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; diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index a0d8987d8..987937066 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -569,7 +569,7 @@ impl Game<'_> { } Screen::Creative if self.creative_screen.tab().search => { // As typed: replaces a selected query, keeps the length limit. - self.creative_search_edit(Some(text), false) + self.creative_search_edit(Some(text)) } _ => { return Err( diff --git a/crates/horizon-client/src/game/pad_menu.rs b/crates/horizon-client/src/game/pad_menu.rs index 32acdd256..30f2e7337 100644 --- a/crates/horizon-client/src/game/pad_menu.rs +++ b/crates/horizon-client/src/game/pad_menu.rs @@ -921,8 +921,10 @@ impl Game<'_> { } else if self.book.is_some() { self.book_input(key, text); } else if self.screen == Screen::Creative && self.creative_screen.tab().search { - if key != Some(KeyCode::Enter) { - self.creative_search_edit(text, backspace); + if backspace { + self.creative_search_backspace(false); + } else if key != Some(KeyCode::Enter) { + self.creative_search_edit(text); } } else if self.recipe_search_focused() { if key != Some(KeyCode::Enter) { diff --git a/crates/horizon-client/src/screen_draw.rs b/crates/horizon-client/src/screen_draw.rs index 572ee202f..03f979a68 100644 --- a/crates/horizon-client/src/screen_draw.rs +++ b/crates/horizon-client/src/screen_draw.rs @@ -619,26 +619,7 @@ impl TextField { /// `getWordPosition(dir, from)`: past the next space run, or back to the /// start of the word. pub fn word_position(&self, dir: isize, from: usize) -> usize { - let v: Vec = self.value.chars().collect(); - let mut at = from; - if dir > 0 { - at = v - .iter() - .skip(at) - .position(|c| *c == ' ') - .map_or(v.len(), |p| p + at); - while at < v.len() && v[at] == ' ' { - at += 1; - } - } else { - while at > 0 && v[at - 1] == ' ' { - at -= 1; - } - while at > 0 && v[at - 1] != ' ' { - at -= 1; - } - } - at + crate::ui::word_position(&self.value, dir, from) } /// `keyPressed` of a focused, editable box: `Some(changed)` when handled. pub fn key( diff --git a/crates/horizon-client/src/ui.rs b/crates/horizon-client/src/ui.rs index 1c6e93067..5f8f7b9f9 100644 --- a/crates/horizon-client/src/ui.rs +++ b/crates/horizon-client/src/ui.rs @@ -821,6 +821,40 @@ fn byte_index(s: &str, chars: usize) -> usize { pub fn char_len(s: &str) -> usize { s.chars().count() } +/// `EditBox.getWordPosition(dir, from)`: the character index `dir` words away +/// from `from`. Forward, past the rest of the word and its spaces; backward, +/// over any spaces to the start of the word. +pub fn word_position(value: &str, dir: isize, from: usize) -> usize { + let chars: Vec = value.chars().collect(); + let mut pos = from.min(chars.len()); + if dir < 0 { + while pos > 0 && chars[pos - 1] == ' ' { + pos -= 1; + } + while pos > 0 && chars[pos - 1] != ' ' { + pos -= 1; + } + } else { + while pos < chars.len() && chars[pos] != ' ' { + pos += 1; + } + while pos < chars.len() && chars[pos] == ' ' { + pos += 1; + } + } + pos +} +/// `EditBox.deleteWords(dir)` for an append-only field, whose caret is at the +/// end: removes the word in that direction. True when the value changed. +pub fn delete_words(value: &mut String, dir: isize) -> bool { + let len = char_len(value); + let to = word_position(value, dir, len); + if to == len { + return false; + } + value.truncate(byte_index(value, to)); + true +} impl crate::clipboard::Editable for EditBox { fn select_all(&mut self) { self.move_cursor_to_end(false); @@ -919,25 +953,7 @@ impl EditBox { } /// EditBox.getWordPosition(dir, from). fn word_position_from(&self, dir: isize, from: usize) -> usize { - let chars: Vec = self.value.chars().collect(); - let mut pos = from.min(chars.len()); - if dir < 0 { - while pos > 0 && chars[pos - 1] == ' ' { - pos -= 1; - } - while pos > 0 && chars[pos - 1] != ' ' { - pos -= 1; - } - } else { - let len = chars.len(); - while pos < len && chars[pos] != ' ' { - pos += 1; - } - while pos < len && chars[pos] == ' ' { - pos += 1; - } - } - pos + word_position(&self.value, dir, from) } pub fn set_cursor(&mut self, pos: usize, select: bool) { self.cursor = pos.min(char_len(&self.value)); @@ -1451,4 +1467,36 @@ mod tests { assert_eq!(e.value, "éx worl"); assert_eq!(e.cursor, 2); } + #[test] + fn word_position_steps_over_a_word_and_its_spaces() { + assert_eq!(word_position("hello world", -1, 11), 6); + assert_eq!(word_position("hello world", -1, 6), 0); + assert_eq!(word_position("hello world", -1, 13), 8); + assert_eq!(word_position("hello world", 1, 5), 8); + // From inside a word: back to its start, forward past it and its spaces. + assert_eq!(word_position("hello world", -1, 8), 6); + assert_eq!(word_position("hello world", 1, 2), 6); + // The ends, and a position past the end. + assert_eq!(word_position("hello", -1, 0), 0); + assert_eq!(word_position("hello", 1, 5), 5); + assert_eq!(word_position("hi", -1, 99), 0); + assert_eq!(word_position("hi", 1, 99), 2); + } + #[test] + fn delete_words_removes_the_word_before_the_end() { + let mut v = "stone bricks".to_string(); + assert!(delete_words(&mut v, -1)); + assert_eq!(v, "stone "); + assert!(delete_words(&mut v, -1)); + assert_eq!(v, ""); + assert!(!delete_words(&mut v, -1)); + // Characters, not bytes. + let mut v = "a é§x".to_string(); + assert!(delete_words(&mut v, -1)); + assert_eq!(v, "a "); + // Nothing after the end to delete. + let mut v = "stone".to_string(); + assert!(!delete_words(&mut v, 1)); + assert_eq!(v, "stone"); + } }