From e755afce916c11969454ab09d2079b966524b001 Mon Sep 17 00:00:00 2001 From: Imbris Date: Sun, 14 Apr 2019 20:45:54 -0400 Subject: [PATCH] simplify chat keybaord capture Former-commit-id: 3c5c5c241fe0c0d97059c7b87c0c08104eb5c5fe --- voxygen/src/hud/mod.rs | 38 ++++++++++++++++---------------------- voxygen/src/ui/mod.rs | 34 ++++++++++------------------------ 2 files changed, 26 insertions(+), 46 deletions(-) diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 2793dfdbe8..645fad5ec4 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -402,7 +402,6 @@ pub struct Hud { ids: Ids, imgs: Imgs, chat: chat::Chat, - typing: bool, cursor_grabbed: bool, font_metamorph: FontId, font_opensans: FontId, @@ -456,7 +455,6 @@ impl Hud { imgs, ids, chat, - typing: false, cursor_grabbed: true, settings_tab: SettingsTab::Interface, show_help: true, @@ -1443,14 +1441,6 @@ impl Hud { }; } - // update whether keyboard is captured - self.typing = - if let Some(widget_id) = ui_widgets.global_input().current.widget_capturing_keyboard { - widget_id == self.chat.input_box_id() - } else { - false - }; - events } @@ -1530,10 +1520,17 @@ impl Hud { self.cursor_grabbed = cursor_grabbed; } + fn typing(&self) -> bool { + match self.ui.widget_capturing_keyboard() { + Some(id) if id == self.chat.input_box_id() => true, + _ => false, + } + } + pub fn handle_event(&mut self, event: WinEvent) -> bool { match event { WinEvent::Ui(event) => { - if (self.typing && event.is_keyboard()) + if (self.typing() && event.is_keyboard()) || !(self.cursor_grabbed && event.is_keyboard_or_mouse()) { self.ui.handle_event(event); @@ -1542,18 +1539,15 @@ impl Hud { } WinEvent::Zoom(_) => !self.ui.no_widget_capturing_mouse(), WinEvent::KeyDown(Key::Enter) => { - if self.typing { - self.ui.focus_widget(None); - self.typing = false; + self.ui.focus_widget(if self.typing() { + None } else { - self.ui.focus_widget(Some(self.chat.input_box_id())); - self.typing = true; - }; + Some(self.chat.input_box_id()) + }); true } WinEvent::KeyDown(Key::Escape) => { - if self.typing { - self.typing = false; + if self.typing() { self.ui.focus_widget(None); } else { // Close windows on esc @@ -1561,7 +1555,7 @@ impl Hud { } true } - WinEvent::KeyDown(key) if !self.typing => match key { + WinEvent::KeyDown(key) if !self.typing() => match key { Key::Map => { self.toggle_map(); true @@ -1602,9 +1596,9 @@ impl Hud { }, WinEvent::KeyDown(key) | WinEvent::KeyUp(key) => match key { Key::ToggleCursor => false, - _ => self.typing, + _ => self.typing(), }, - WinEvent::Char(_) => self.typing, + WinEvent::Char(_) => self.typing(), _ => false, } } diff --git a/voxygen/src/ui/mod.rs b/voxygen/src/ui/mod.rs index 290f3bc52a..4cd1b89da4 100644 --- a/voxygen/src/ui/mod.rs +++ b/voxygen/src/ui/mod.rs @@ -246,31 +246,17 @@ impl Ui { self.ui.set_widgets() } - // Workaround because conrod currently gives us no way to programmatically set which widget is capturing key input - // Note the widget must be visible and not covered by other widgets at its center for this to work - // Accepts option so widget can be "unfocused" - // TODO: try https://docs.rs/conrod_core/0.63.0/conrod_core/struct.Ui.html#method.keyboard_capture + // Accepts option so widget can be unfocused pub fn focus_widget(&mut self, id: Option) { - let (x, y) = match id { - // get position of widget - Some(id) => if let Some([x, y]) = self.ui.xy_of(id) { - (x, y) - } else { - return; - }, - // outside window (origin is center) - None => (self.ui.win_h, self.ui.win_w) - }; - // things to consider: does cursor need to be moved back?, should we check if the mouse if already pressed and then trigger a release? - self.ui.handle_event( - Input::Motion(Motion::MouseCursor { x, y }) - ); - self.ui.handle_event( - Input::Press(Button::Mouse(MouseButton::Left)) - ); - self.ui.handle_event( - Input::Release(Button::Mouse(MouseButton::Left)) - ); + self.ui.keyboard_capture(match id { + Some(id) => id, + None => self.ui.window, + }); + } + + // Get id of current widget capturing keyboard + pub fn widget_capturing_keyboard(&self) -> Option { + self.ui.global_input().current.widget_capturing_keyboard } // Get whether the a widget besides the window is capturing the mouse