From 7bb49fabfa0d2686e4e74c1c11362d6b6aade1ca Mon Sep 17 00:00:00 2001 From: Tuomas Siipola Date: Wed, 22 Feb 2017 21:52:37 +0200 Subject: [PATCH] Add hide cursor when typing option --- alacritty.yml | 2 ++ alacritty_macos.yml | 2 ++ src/config.rs | 11 +++++++++++ src/event.rs | 14 ++++++++++++++ src/window.rs | 7 +++++++ 5 files changed, 36 insertions(+) diff --git a/alacritty.yml b/alacritty.yml index ba44cf11..15ea4fa8 100644 --- a/alacritty.yml +++ b/alacritty.yml @@ -233,6 +233,8 @@ mouse: selection: semantic_escape_chars: ",│`|:\"' ()[]{}<>" +hide_cursor_when_typing: false + # Shell # # You can set shell.program to the path of your favorite shell, e.g. /bin/fish. diff --git a/alacritty_macos.yml b/alacritty_macos.yml index 98a0ee63..8f024dbc 100644 --- a/alacritty_macos.yml +++ b/alacritty_macos.yml @@ -232,6 +232,8 @@ mouse: selection: semantic_escape_chars: ",│`|:\"' ()[]{}<>" +hide_cursor_when_typing: false + # Shell # # You can set shell.program to the path of your favorite shell, e.g. /bin/fish. diff --git a/src/config.rs b/src/config.rs index 3be8e4c8..bab2cc20 100644 --- a/src/config.rs +++ b/src/config.rs @@ -227,6 +227,10 @@ pub struct Config { /// Visual bell configuration #[serde(default)] visual_bell: VisualBellConfig, + + /// Hide cursor when typing + #[serde(default)] + hide_cursor_when_typing: bool, } #[cfg(not(target_os="macos"))] @@ -273,6 +277,7 @@ impl Default for Config { config_path: None, visual_bell: Default::default(), env: Default::default(), + hide_cursor_when_typing: Default::default(), } } } @@ -1008,6 +1013,12 @@ impl Config { &self.env } + /// Should hide cursor when typing + #[inline] + pub fn hide_cursor_when_typing(&self) -> bool { + self.hide_cursor_when_typing + } + fn load_from>(path: P) -> Result { let path = path.into(); let raw = Config::read_file(path.as_path())?; diff --git a/src/event.rs b/src/event.rs index 9a7d2e8e..d6b0796a 100644 --- a/src/event.rs +++ b/src/event.rs @@ -142,6 +142,8 @@ pub struct Processor { ref_test: bool, size_info: SizeInfo, pub selection: Selection, + hide_cursor_when_typing: bool, + hide_cursor: bool, } /// Notify that the terminal was resized @@ -178,6 +180,8 @@ impl Processor { mouse: Default::default(), selection: Default::default(), size_info: size_info, + hide_cursor_when_typing: config.hide_cursor_when_typing(), + hide_cursor: false, } } @@ -189,6 +193,7 @@ impl Processor { event: glutin::Event, ref_test: bool, resize_tx: &mpsc::Sender<(u32, u32)>, + hide_cursor: &mut bool, ) { match event { glutin::Event::Closed => { @@ -219,9 +224,11 @@ impl Processor { processor.ctx.terminal.dirty = true; }, glutin::Event::KeyboardInput(state, _code, key, mods, string) => { + *hide_cursor = true; processor.process_key(state, key, mods, string); }, glutin::Event::MouseInput(state, button) => { + *hide_cursor = false; processor.mouse_input(state, button); processor.ctx.terminal.dirty = true; }, @@ -229,6 +236,7 @@ impl Processor { let x = limit(x, 0, processor.ctx.size_info.width as i32); let y = limit(y, 0, processor.ctx.size_info.height as i32); + *hide_cursor = false; processor.mouse_moved(x as u32, y as u32); if !processor.ctx.selection.is_empty() { @@ -236,6 +244,7 @@ impl Processor { } }, glutin::Event::MouseWheel(scroll_delta, touch_phase) => { + *hide_cursor = false; processor.on_mouse_wheel(scroll_delta, touch_phase); }, glutin::Event::Focused(true) | @@ -275,6 +284,7 @@ impl Processor { $event, self.ref_test, &self.resize_tx, + &mut self.hide_cursor, ) } } @@ -309,6 +319,10 @@ impl Processor { for event in window.poll_events() { process!(event); } + + if self.hide_cursor_when_typing { + window.set_cursor_visible(!self.hide_cursor); + } } self.wait_for_event = !terminal.dirty; diff --git a/src/window.rs b/src/window.rs index a5f6e2c6..b90c242d 100644 --- a/src/window.rs +++ b/src/window.rs @@ -288,6 +288,13 @@ impl Window { pub fn set_title(&self, title: &str) { self.glutin_window.set_title(title); } + + /// Set cursor visible + #[inline] + pub fn set_cursor_visible(&self, show: bool) { + self.glutin_window.set_cursor(if show { glutin::MouseCursor::Default } + else { glutin::MouseCursor::NoneCursor }); + } } impl Proxy {