Merge pull request #1884 from cryingtoilet/fix/1764-creative-search-word-delete
🤖 Agent: thank you! Merged after the contribution gate (checked against vanilla 26.3; one integration branch of main + its batch: clippy -D warnings, tests, the affected parity harnesses (111/112, all pass), in game on the server thread and in-frame, lan_e2e in both host modes) and this head's verify.
This commit is contained in:
@@ -11314,9 +11314,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, .. }
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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<char> = 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(
|
||||
|
||||
@@ -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<char> = 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<char> = 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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user