From 47dc81c8d949336fd25c13faab0965759b3122f6 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 12 Jun 2020 19:36:29 -0700 Subject: [PATCH] termwiz: separate CursorVisibility from CursorShape They are separate concepts and this simplifies some processing. closes: https://github.com/wez/wezterm/pull/212 --- src/frontend/gui/overlay/search.rs | 1 + src/frontend/gui/termwindow.rs | 110 ++++++++++++++++------------- src/frontend/gui/utilsprites.rs | 15 ++-- src/mux/renderable.rs | 2 + src/update.rs | 4 +- term/src/lib.rs | 1 + term/src/screen.rs | 1 + term/src/terminalstate.rs | 9 +-- term/src/test/mod.rs | 3 +- termwiz/src/render/terminfo.rs | 14 +--- termwiz/src/surface/mod.rs | 68 +++++++++--------- 11 files changed, 120 insertions(+), 108 deletions(-) diff --git a/src/frontend/gui/overlay/search.rs b/src/frontend/gui/overlay/search.rs index e2b2d5ce8..d5be082b2 100644 --- a/src/frontend/gui/overlay/search.rs +++ b/src/frontend/gui/overlay/search.rs @@ -423,6 +423,7 @@ impl Renderable for SearchRenderable { x: 8 + term::unicode_column_width(&self.pattern), y: self.compute_search_row(), shape: termwiz::surface::CursorShape::SteadyBlock, + visibility: termwiz::surface::CursorVisibility::Visible, } } diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index da0122d7c..f1950e59c 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -51,7 +51,7 @@ use term::input::LastMouseClick; use term::{Line, StableRowIndex, Underline}; use termwiz::color::RgbColor; use termwiz::hyperlink::Hyperlink; -use termwiz::surface::CursorShape; +use termwiz::surface::{CursorShape, CursorVisibility}; struct RenderScreenLineOpenGLParams<'a> { line_idx: usize, @@ -2631,7 +2631,7 @@ impl TermWindow { } } - if cursor_shape != CursorShape::Hidden { + if cursor_shape.is_some() { let software = self.render_state.software(); let sprite = software.util_sprites.cursor_sprite(cursor_shape); ctx.draw_image( @@ -2675,7 +2675,7 @@ impl TermWindow { ); ctx.clear_rect(cell_rect, bg_color); - if cursor_shape != CursorShape::Hidden { + if cursor_shape.is_some() { let software = self.render_state.software(); let sprite = software.util_sprites.cursor_sprite(cursor_shape); ctx.draw_image( @@ -2719,60 +2719,74 @@ impl TermWindow { fg_color: Color, bg_color: Color, palette: &ColorPalette, - ) -> (Color, Color, CursorShape) { + ) -> (Color, Color, Option) { let selected = selection.contains(&cell_idx); let is_cursor = stable_line_idx == Some(cursor.y) && cursor.x == cell_idx; - let cursor_shape = if is_cursor { - // This logic figures out whether the cursor is visible or not. - // If the cursor is explicitly hidden then it is obviously not - // visible. - // If the cursor is set to a blinking mode then we are visible - // depending on the current time. - let config = configuration(); - let shape = config.default_cursor_style.effective_shape(cursor.shape); - // Work out the blinking shape if its a blinking cursor and it hasn't been disabled - // and the window is focused. - let blinking = - shape.is_blinking() && config.cursor_blink_rate != 0 && self.focused.is_some(); - if blinking { - // Divide the time since we last moved by the blink rate. - // If the result is even then the cursor is "on", else it - // is "off" - let now = std::time::Instant::now(); - let milli_uptime = now - .duration_since(self.prev_cursor.last_cursor_movement()) - .as_millis(); - let ticks = milli_uptime / config.cursor_blink_rate as u128; - if (ticks & 1) == 0 { - shape + let (cursor_shape, visibility) = + if is_cursor && cursor.visibility == CursorVisibility::Visible { + // This logic figures out whether the cursor is visible or not. + // If the cursor is explicitly hidden then it is obviously not + // visible. + // If the cursor is set to a blinking mode then we are visible + // depending on the current time. + let config = configuration(); + let shape = config.default_cursor_style.effective_shape(cursor.shape); + // Work out the blinking shape if its a blinking cursor and it hasn't been disabled + // and the window is focused. + let blinking = + shape.is_blinking() && config.cursor_blink_rate != 0 && self.focused.is_some(); + if blinking { + // Divide the time since we last moved by the blink rate. + // If the result is even then the cursor is "on", else it + // is "off" + let now = std::time::Instant::now(); + let milli_uptime = now + .duration_since(self.prev_cursor.last_cursor_movement()) + .as_millis(); + let ticks = milli_uptime / config.cursor_blink_rate as u128; + ( + shape, + if (ticks & 1) == 0 { + CursorVisibility::Visible + } else { + CursorVisibility::Hidden + }, + ) } else { - CursorShape::Hidden + (shape, cursor.visibility) } } else { - shape - } - } else { - CursorShape::Hidden - }; + (cursor.shape, cursor.visibility) + }; - let (fg_color, bg_color) = match (selected, self.focused.is_some(), cursor_shape) { - // Selected text overrides colors - (true, _, CursorShape::Hidden) => ( - rgbcolor_to_window_color(palette.selection_fg), - rgbcolor_to_window_color(palette.selection_bg), - ), - // Cursor cell overrides colors - (_, true, CursorShape::BlinkingBlock) | (_, true, CursorShape::SteadyBlock) => ( - rgbcolor_to_window_color(palette.cursor_fg), - rgbcolor_to_window_color(palette.cursor_bg), - ), - // Normally, render the cell as configured (or if the window is unfocused) - _ => (fg_color, bg_color), - }; + let (fg_color, bg_color) = + match (selected, self.focused.is_some(), cursor_shape, visibility) { + // Selected text overrides colors + (true, _, _, CursorVisibility::Hidden) => ( + rgbcolor_to_window_color(palette.selection_fg), + rgbcolor_to_window_color(palette.selection_bg), + ), + // Cursor cell overrides colors + (_, true, CursorShape::BlinkingBlock, CursorVisibility::Visible) + | (_, true, CursorShape::SteadyBlock, CursorVisibility::Visible) => ( + rgbcolor_to_window_color(palette.cursor_fg), + rgbcolor_to_window_color(palette.cursor_bg), + ), + // Normally, render the cell as configured (or if the window is unfocused) + _ => (fg_color, bg_color), + }; - (fg_color, bg_color, cursor_shape) + ( + fg_color, + bg_color, + if visibility == CursorVisibility::Visible { + Some(cursor_shape) + } else { + None + }, + ) } pub fn tab_state(&self, tab_id: TabId) -> RefMut { diff --git a/src/frontend/gui/utilsprites.rs b/src/frontend/gui/utilsprites.rs index f41ab3500..40c1e39d2 100644 --- a/src/frontend/gui/utilsprites.rs +++ b/src/frontend/gui/utilsprites.rs @@ -293,12 +293,17 @@ impl UtilSprites { } } - pub fn cursor_sprite(&self, shape: CursorShape) -> &Sprite { + pub fn cursor_sprite(&self, shape: Option) -> &Sprite { match shape { - CursorShape::Default | CursorShape::Hidden => &self.white_space, - CursorShape::BlinkingBlock | CursorShape::SteadyBlock => &self.cursor_box, - CursorShape::BlinkingBar | CursorShape::SteadyBar => &self.cursor_i_beam, - CursorShape::BlinkingUnderline | CursorShape::SteadyUnderline => &self.cursor_underline, + None => &self.white_space, + Some(shape) => match shape { + CursorShape::Default => &self.white_space, + CursorShape::BlinkingBlock | CursorShape::SteadyBlock => &self.cursor_box, + CursorShape::BlinkingBar | CursorShape::SteadyBar => &self.cursor_i_beam, + CursorShape::BlinkingUnderline | CursorShape::SteadyUnderline => { + &self.cursor_underline + } + }, } } } diff --git a/src/mux/renderable.rs b/src/mux/renderable.rs index a0e09ba11..c3bd43cd0 100644 --- a/src/mux/renderable.rs +++ b/src/mux/renderable.rs @@ -11,6 +11,7 @@ pub struct StableCursorPosition { pub x: usize, pub y: StableRowIndex, pub shape: termwiz::surface::CursorShape, + pub visibility: termwiz::surface::CursorVisibility, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, Serialize)] @@ -75,6 +76,7 @@ impl Renderable for Terminal { x: pos.x, y: self.screen().visible_row_to_stable_row(pos.y), shape: pos.shape, + visibility: pos.visibility, } } diff --git a/src/update.rs b/src/update.rs index fe6f32168..e4dac64f1 100644 --- a/src/update.rs +++ b/src/update.rs @@ -12,7 +12,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use std::time::Duration; use termwiz::cell::{AttributeChange, Hyperlink, Underline}; -use termwiz::surface::{Change, CursorShape}; +use termwiz::surface::{Change, CursorVisibility}; #[derive(Debug, Deserialize, Clone)] pub struct Release { @@ -186,7 +186,7 @@ fn show_update_available(release: Release) { render.parse_str(&brief_blurb); let mut output = vec![ - Change::CursorShape(CursorShape::Hidden), + Change::CursorVisibility(CursorVisibility::Hidden), Change::Attribute(AttributeChange::Underline(Underline::Single)), Change::Attribute(AttributeChange::Hyperlink(Some(Arc::new(Hyperlink::new( install, diff --git a/term/src/lib.rs b/term/src/lib.rs index 8dec11cce..f0df17213 100644 --- a/term/src/lib.rs +++ b/term/src/lib.rs @@ -94,6 +94,7 @@ pub struct CursorPosition { pub x: usize, pub y: VisibleRowIndex, pub shape: termwiz::surface::CursorShape, + pub visibility: termwiz::surface::CursorVisibility, } pub mod color; diff --git a/term/src/screen.rs b/term/src/screen.rs index 2461032fe..b198ff880 100644 --- a/term/src/screen.rs +++ b/term/src/screen.rs @@ -218,6 +218,7 @@ impl Screen { x: cursor_x, y: vis_cursor_y, shape: cursor.shape, + visibility: cursor.visibility, } } diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index e1ef9dc49..478ff12f4 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -19,7 +19,7 @@ use termwiz::escape::{ Action, ControlCode, Esc, EscCode, OneBased, OperatingSystemCommand, Sixel, SixelData, CSI, }; use termwiz::image::{ImageCell, ImageData, TextureCoordinate}; -use termwiz::surface::CursorShape; +use termwiz::surface::{CursorShape, CursorVisibility}; use url::Url; struct TabStop { @@ -910,10 +910,11 @@ impl TerminalState { CursorPosition { x: self.cursor.x, y: self.cursor.y, - shape: if self.cursor_visible { - self.cursor.shape + shape: self.cursor.shape, + visibility: if self.cursor_visible { + CursorVisibility::Visible } else { - CursorShape::Hidden + CursorVisibility::Hidden }, } } diff --git a/term/src/test/mod.rs b/term/src/test/mod.rs index 346f3aa8f..7e18c0107 100644 --- a/term/src/test/mod.rs +++ b/term/src/test/mod.rs @@ -13,7 +13,7 @@ use std::cell::RefCell; use std::sync::Arc; use termwiz::escape::csi::{Edit, EraseInDisplay, EraseInLine}; use termwiz::escape::{OneBased, OperatingSystemCommand, CSI}; -use termwiz::surface::CursorShape; +use termwiz::surface::{CursorShape, CursorVisibility}; #[derive(Debug)] struct LocalClip { @@ -149,6 +149,7 @@ impl TestTerm { x, y, shape: CursorShape::Default, + visibility: CursorVisibility::Visible, }; assert_eq!( cursor, expect, diff --git a/termwiz/src/render/terminfo.rs b/termwiz/src/render/terminfo.rs index c6dc0d170..c3fb9d672 100644 --- a/termwiz/src/render/terminfo.rs +++ b/termwiz/src/render/terminfo.rs @@ -524,26 +524,14 @@ impl TerminfoRenderer { CursorShape::Default => { if let Some(normal) = self.get_capability::() { normal.expand().to(out.by_ref())?; - } else { - if let Some(show) = self.get_capability::() { - show.expand().to(out.by_ref())?; - } } if let Some(reset) = self.get_capability::() { reset.expand().to(out.by_ref())?; } } - CursorShape::Hidden => { - if let Some(hide) = self.get_capability::() { - hide.expand().to(out.by_ref())?; - } - } _ => { - if let Some(show) = self.get_capability::() { - show.expand().to(out.by_ref())?; - } let param = match shape { - CursorShape::Default | CursorShape::Hidden => unreachable!(), + CursorShape::Default => unreachable!(), CursorShape::BlinkingBlock => 1, CursorShape::SteadyBlock => 2, CursorShape::BlinkingUnderline => 3, diff --git a/termwiz/src/surface/mod.rs b/termwiz/src/surface/mod.rs index 466e7dc6d..f54c1c9c3 100644 --- a/termwiz/src/surface/mod.rs +++ b/termwiz/src/surface/mod.rs @@ -42,7 +42,6 @@ impl Default for CursorVisibility { #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] pub enum CursorShape { - Hidden, Default, BlinkingBlock, SteadyBlock, @@ -58,15 +57,6 @@ impl Default for CursorShape { } } -impl From for CursorShape { - fn from(visibility: CursorVisibility) -> CursorShape { - match visibility { - CursorVisibility::Hidden => CursorShape::Hidden, - CursorVisibility::Visible => CursorShape::Default, - } - } -} - impl CursorShape { pub fn is_blinking(self) -> bool { match self { @@ -114,7 +104,8 @@ pub struct Surface { ypos: usize, seqno: SequenceNo, changes: Vec, - cursor_shape: CursorShape, + cursor_shape: Option, + cursor_visibility: CursorVisibility, cursor_color: ColorAttribute, title: String, } @@ -210,10 +201,14 @@ impl Surface { (self.xpos, self.ypos) } - pub fn cursor_shape(&self) -> CursorShape { + pub fn cursor_shape(&self) -> Option { self.cursor_shape } + pub fn cursor_visibility(&self) -> CursorVisibility { + self.cursor_visibility + } + pub fn title(&self) -> &str { &self.title } @@ -288,8 +283,8 @@ impl Surface { Change::ClearToEndOfLine(color) => self.clear_eol(*color), Change::ClearToEndOfScreen(color) => self.clear_eos(*color), Change::CursorColor(color) => self.cursor_color = *color, - Change::CursorShape(shape) => self.cursor_shape = *shape, - Change::CursorVisibility(visibility) => self.cursor_shape = (*visibility).into(), + Change::CursorShape(shape) => self.cursor_shape = Some(*shape), + Change::CursorVisibility(visibility) => self.cursor_visibility = *visibility, Change::Image(image) => self.add_image(image), Change::Title(text) => self.title = text.to_owned(), Change::ScrollRegionUp { @@ -587,7 +582,7 @@ impl Surface { // Home the cursor and clear the screen to defaults. Hide the // cursor while we're repainting. - result.push(Change::CursorShape(CursorShape::Hidden)); + result.push(Change::CursorVisibility(CursorVisibility::Hidden)); result.push(Change::ClearScreen(Default::default())); if !self.title.is_empty() { @@ -720,8 +715,11 @@ impl Surface { // Set the intended cursor shape. We hid the cursor at the start // of the repaint, so no need to hide it again. - if self.cursor_shape != CursorShape::Hidden { - result.push(Change::CursorShape(self.cursor_shape)); + if self.cursor_visibility != CursorVisibility::Hidden { + result.push(Change::CursorVisibility(CursorVisibility::Visible)); + if let Some(shape) = self.cursor_shape { + result.push(Change::CursorShape(shape)); + } } result @@ -983,7 +981,7 @@ mod test { let (_seq, changes) = s.get_changes(0); assert_eq!( &[ - Change::CursorShape(CursorShape::Hidden), + Change::CursorVisibility(CursorVisibility::Hidden), Change::ClearScreen(Default::default()), Change::Text("hel".into()), Change::CursorPosition { @@ -995,7 +993,7 @@ mod test { x: Position::Absolute(1), y: Position::Absolute(1), }, - Change::CursorShape(CursorShape::Default), + Change::CursorVisibility(CursorVisibility::Visible), ], &*changes ); @@ -1017,7 +1015,7 @@ mod test { let (_seq, changes) = s.get_changes(0); assert_eq!( &[ - Change::CursorShape(CursorShape::Hidden), + Change::CursorVisibility(CursorVisibility::Hidden), Change::ClearScreen(Default::default()), Change::AllAttributes( CellAttributes::default() @@ -1035,7 +1033,7 @@ mod test { x: Position::Absolute(1), y: Position::Absolute(1), }, - Change::CursorShape(CursorShape::Default), + Change::CursorVisibility(CursorVisibility::Visible), ], &*changes ); @@ -1051,7 +1049,7 @@ mod test { let (_seq, changes) = s.get_changes(0); assert_eq!( &[ - Change::CursorShape(CursorShape::Hidden), + Change::CursorVisibility(CursorVisibility::Hidden), Change::ClearScreen(Default::default()), Change::AllAttributes( CellAttributes::default() @@ -1073,7 +1071,7 @@ mod test { x: Position::Absolute(3), y: Position::Absolute(2), }, - Change::CursorShape(CursorShape::Default), + Change::CursorVisibility(CursorVisibility::Visible), ], &*changes ); @@ -1089,13 +1087,13 @@ mod test { let (_seq, changes) = s.get_changes(0); assert_eq!( &[ - Change::CursorShape(CursorShape::Hidden), + Change::CursorVisibility(CursorVisibility::Hidden), Change::ClearScreen(Default::default()), Change::CursorPosition { x: Position::Absolute(3), y: Position::Absolute(2), }, - Change::CursorShape(CursorShape::Default), + Change::CursorVisibility(CursorVisibility::Visible), ], &*changes ); @@ -1168,9 +1166,9 @@ mod test { let s = Surface::new(4, 3); let empty = &[ - Change::CursorShape(CursorShape::Hidden), + Change::CursorVisibility(CursorVisibility::Hidden), Change::ClearScreen(Default::default()), - Change::CursorShape(CursorShape::Default), + Change::CursorVisibility(CursorVisibility::Visible), ]; let (seq, changes) = s.get_changes(0); @@ -1201,14 +1199,14 @@ mod test { s.resize(2, 2); let full = &[ - Change::CursorShape(CursorShape::Hidden), + Change::CursorVisibility(CursorVisibility::Hidden), Change::ClearScreen(Default::default()), Change::Text("a".to_string()), Change::CursorPosition { x: Position::Absolute(1), y: Position::Absolute(0), }, - Change::CursorShape(CursorShape::Default), + Change::CursorVisibility(CursorVisibility::Visible), ]; let (_seq, changes) = s.get_changes(seq); @@ -1226,7 +1224,7 @@ mod test { let (_seq, changes) = s.get_changes(0); assert_eq!( &[ - Change::CursorShape(CursorShape::Hidden), + Change::CursorVisibility(CursorVisibility::Hidden), Change::ClearScreen(Default::default()), Change::AllAttributes( CellAttributes::default() @@ -1238,7 +1236,7 @@ mod test { x: Position::Absolute(2), y: Position::Absolute(0), }, - Change::CursorShape(CursorShape::Default), + Change::CursorVisibility(CursorVisibility::Visible), ], &*changes ); @@ -1261,14 +1259,14 @@ mod test { assert_eq!(s.ypos, 1); let full = &[ - Change::CursorShape(CursorShape::Hidden), + Change::CursorVisibility(CursorVisibility::Hidden), Change::ClearScreen(Default::default()), Change::Text(" a".to_string()), Change::CursorPosition { x: Position::Absolute(1), y: Position::Absolute(1), }, - Change::CursorShape(CursorShape::Default), + Change::CursorVisibility(CursorVisibility::Visible), ]; let (_seq, changes) = s.get_changes(0); @@ -1285,14 +1283,14 @@ mod test { s.flush_changes_older_than(1); let initial = &[ - Change::CursorShape(CursorShape::Hidden), + Change::CursorVisibility(CursorVisibility::Hidden), Change::ClearScreen(Default::default()), Change::Text("a".to_string()), Change::CursorPosition { x: Position::Absolute(1), y: Position::Absolute(0), }, - Change::CursorShape(CursorShape::Default), + Change::CursorVisibility(CursorVisibility::Visible), ]; let seq_pos = {