From f17020b6af66420b14c675b5a65fd5dec158b189 Mon Sep 17 00:00:00 2001 From: Jan Sosulski Date: Wed, 3 Nov 2021 17:32:12 +0100 Subject: [PATCH] added setting to hide mouse cursor when typing --- src/window/settings.rs | 2 ++ src/window/window_wrapper/keyboard_manager.rs | 9 +++++++-- src/window/window_wrapper/mod.rs | 2 +- src/window/window_wrapper/mouse_manager.rs | 17 ++++++++++------- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/window/settings.rs b/src/window/settings.rs index 87748fd..cb799fa 100644 --- a/src/window/settings.rs +++ b/src/window/settings.rs @@ -8,6 +8,7 @@ pub struct WindowSettings { pub fullscreen: bool, pub iso_layout: bool, pub remember_window_size: bool, + pub hide_mouse_when_typing: bool, } impl Default for WindowSettings { @@ -19,6 +20,7 @@ impl Default for WindowSettings { refresh_rate: 60, no_idle: SETTINGS.get::().no_idle, remember_window_size: false, + hide_mouse_when_typing: false, } } } diff --git a/src/window/window_wrapper/keyboard_manager.rs b/src/window/window_wrapper/keyboard_manager.rs index 01803f7..f72e756 100644 --- a/src/window/window_wrapper/keyboard_manager.rs +++ b/src/window/window_wrapper/keyboard_manager.rs @@ -1,3 +1,4 @@ +use glutin::{PossiblyCurrent, WindowedContext}; use glutin::event::{ElementState, Event, KeyEvent, WindowEvent}; use glutin::keyboard::Key; @@ -6,7 +7,7 @@ use winit::platform::modifier_supplement::KeyEventExtModifierSupplement; use crate::bridge::{SerialCommand, UiCommand}; use crate::channel_utils::LoggingTx; use crate::settings::SETTINGS; -use crate::window::KeyboardSettings; +use crate::window::{KeyboardSettings, WindowSettings}; pub struct KeyboardManager { command_sender: LoggingTx, @@ -31,7 +32,7 @@ impl KeyboardManager { } } - pub fn handle_event(&mut self, event: &Event<()>) { + pub fn handle_event(&mut self, event: &Event<()>, windowed_context: &WindowedContext) { match event { Event::WindowEvent { event: WindowEvent::Focused(_focused), @@ -66,12 +67,16 @@ impl KeyboardManager { Event::MainEventsCleared => { // And the window wasn't just focused. let settings = SETTINGS.get::(); + let window_settings = SETTINGS.get::(); if !self.should_ignore_input(&settings) { // If we have a keyboard event this frame for key_event in self.queued_key_events.iter() { // And a key was pressed if key_event.state == ElementState::Pressed { + if window_settings.hide_mouse_when_typing { + windowed_context.window().set_cursor_visible(false); + } if let Some(keybinding) = self.maybe_get_keybinding(key_event) { self.command_sender .send(SerialCommand::Keyboard(keybinding).into()) diff --git a/src/window/window_wrapper/mod.rs b/src/window/window_wrapper/mod.rs index fe8a416..810b65f 100644 --- a/src/window/window_wrapper/mod.rs +++ b/src/window/window_wrapper/mod.rs @@ -120,7 +120,7 @@ impl GlutinWindowWrapper { } pub fn handle_event(&mut self, event: Event<()>) { - self.keyboard_manager.handle_event(&event); + self.keyboard_manager.handle_event(&event, &self.windowed_context); self.mouse_manager.handle_event( &event, &self.keyboard_manager, diff --git a/src/window/window_wrapper/mouse_manager.rs b/src/window/window_wrapper/mouse_manager.rs index f261b73..602a2b2 100644 --- a/src/window/window_wrapper/mouse_manager.rs +++ b/src/window/window_wrapper/mouse_manager.rs @@ -316,13 +316,16 @@ impl MouseManager { Event::WindowEvent { event: WindowEvent::CursorMoved { position, .. }, .. - } => self.handle_pointer_motion( - position.x as i32, - position.y as i32, - keyboard_manager, - renderer, - windowed_context, - ), + } => { + self.handle_pointer_motion( + position.x as i32, + position.y as i32, + keyboard_manager, + renderer, + windowed_context, + ); + windowed_context.window().set_cursor_visible(true); + } Event::WindowEvent { event: WindowEvent::MouseWheel {