termwiz: separate CursorVisibility from CursorShape

They are separate concepts and this simplifies some processing.

closes: https://github.com/wez/wezterm/pull/212
This commit is contained in:
Wez Furlong committed 2020-06-12 19:44:54 -07:00
1 parent fc352d00f9
commit 47dc81c8d9
11 files changed
+120 -108

No files matched your search

+1
View File
@@ -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,
}
}
+62 -48
View File
@@ -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<CursorShape>) {
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<TabState> {
+10 -5
View File
@@ -293,12 +293,17 @@ impl<T: Texture2d> UtilSprites<T> {
}
}
pub fn cursor_sprite(&self, shape: CursorShape) -> &Sprite<T> {
pub fn cursor_sprite(&self, shape: Option<CursorShape>) -> &Sprite<T> {
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
}
},
}
}
}
+2
View File
@@ -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,
}
}
+2 -2
View File
@@ -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,
+1
View File
@@ -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;
+1
View File
@@ -218,6 +218,7 @@ impl Screen {
x: cursor_x,
y: vis_cursor_y,
shape: cursor.shape,
visibility: cursor.visibility,
}
}
+5 -4
View File
@@ -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
},
}
}
+2 -1
View File
@@ -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,
+1 -13
View File
@@ -524,26 +524,14 @@ impl TerminfoRenderer {
CursorShape::Default => {
if let Some(normal) = self.get_capability::<cap::CursorNormal>() {
normal.expand().to(out.by_ref())?;
} else {
if let Some(show) = self.get_capability::<cap::CursorVisible>() {
show.expand().to(out.by_ref())?;
}
}
if let Some(reset) = self.get_capability::<cap::ResetCursorStyle>() {
reset.expand().to(out.by_ref())?;
}
}
CursorShape::Hidden => {
if let Some(hide) = self.get_capability::<cap::CursorInvisible>() {
hide.expand().to(out.by_ref())?;
}
}
_ => {
if let Some(show) = self.get_capability::<cap::CursorVisible>() {
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,
+33 -35
View File
@@ -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<CursorVisibility> 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<Change>,
cursor_shape: CursorShape,
cursor_shape: Option<CursorShape>,
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<CursorShape> {
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 = {