From 1a4a2ec120232937ef6b83e71f26e63ba7097391 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 24 Oct 2026 20:27:37 +0200 Subject: [PATCH] Debug keys: check the title's focused field inline The helper's release check repeated the `pressed &&` guard below it, and its test only covered the helper. The comment now says why text screens take presses first: vanilla types through charTyped, which debug keys cannot swallow, while here the character comes with the key press. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/debug_keys.rs | 76 ++++---------------- 1 file changed, 13 insertions(+), 63 deletions(-) diff --git a/crates/horizon-client/src/game/debug_keys.rs b/crates/horizon-client/src/game/debug_keys.rs index 5a3dcbd01..e0ad706de 100644 --- a/crates/horizon-client/src/game/debug_keys.rs +++ b/crates/horizon-client/src/game/debug_keys.rs @@ -20,15 +20,6 @@ use winit::keyboard::KeyCode; const YELLOW: u32 = 0xFF_FF55; const RED: u32 = 0xFF_5555; -/// Title text fields receive presses before debug shortcuts, but releases must -/// still clear a debug modifier held before the field gained focus. -fn title_takes_text_key( - title: Option<&mut horizon_client::title::Title>, - action: KeyAction, -) -> bool { - action != KeyAction::Release && title.is_some_and(|title| title.focused_field().is_some()) -} - /// The game as `KeyboardHandler` sees it while it runs one key event. struct Host<'g, 'a> { game: &'g mut Game<'a>, @@ -91,18 +82,22 @@ impl Game<'_> { } return true; } - // Screens with a text box take every key press first - // (EditBox.keyPressed); releases still reach KeyboardHandler. - let typing = title_takes_text_key(self.title.as_mut(), action) - || matches!( - self.screen, - Screen::Chat | Screen::Options | Screen::WorldOptions | Screen::CommandBlock - ) - || self.sign_edit.is_some() + // Vanilla types into a focused EditBox through charTyped, which no + // debug key can swallow. Here the character comes with the key press, + // so a screen with a focused text box takes every press first; + // releases still reach KeyboardHandler. + let typing = matches!( + self.screen, + Screen::Chat | Screen::Options | Screen::WorldOptions | Screen::CommandBlock + ) || self.sign_edit.is_some() || self.book.is_some() || (self.screen == Screen::Creative && self.creative_screen.tab().search) || self.recipe_search_focused() - || self.station_typing(); + || self.station_typing() + || self + .title + .as_mut() + .is_some_and(|t| t.focused_field().is_some()); // Container screens use Escape, the inventory key and the hotbar and // drop keys first; any other screen closes on Escape. let screen_key = match self.screen { @@ -824,48 +819,3 @@ impl Game<'_> { g } } - -#[cfg(test)] -mod tests { - use super::*; - use horizon_client::{ - multiplayer::Screen as ServerScreen, - title::{Mode, Title}, - }; - - #[test] - fn server_fields_take_debug_bound_text_keys_but_not_releases() { - let root = std::env::temp_dir().join(format!("horizon-debug-title-{}", std::process::id())); - let mut title = Title::new(root.join("saves"), &root); - for screen in [ - ServerScreen::Direct { - address: String::new(), - }, - ServerScreen::Edit { - index: None, - name: String::new(), - address: String::new(), - focus: 0, - }, - ServerScreen::Edit { - index: Some(0), - name: String::new(), - address: String::new(), - focus: 1, - }, - ServerScreen::friend(), - ] { - title.mode = Mode::Multiplayer(screen); - for action in [KeyAction::Press, KeyAction::Repeat] { - assert!(title_takes_text_key(Some(&mut title), action)); - } - assert!(!title_takes_text_key(Some(&mut title), KeyAction::Release)); - // A period bound to the debug modifier must reach the field. - title.type_text("mc.example.org"); - assert_eq!(title.focused_field().unwrap().1, "mc.example.org"); - } - title.mode = Mode::Main; - assert!(!title_takes_text_key(Some(&mut title), KeyAction::Press)); - assert!(!title_takes_text_key(None, KeyAction::Press)); - } -}