diff options
author | Joe Wilm <joe@jwilm.com> | 2016-07-04 14:46:25 -0700 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2016-07-04 14:46:25 -0700 |
commit | 14ec851c6947eb4a65f20b3b41fadd6d4f19cb2d (patch) | |
tree | 55afac31d52b82d20a4fb67bd21313a77bb4d8e6 /src | |
parent | 4426e57320d441a31c7c142c9694176f62e050b5 (diff) | |
download | alacritty-14ec851c6947eb4a65f20b3b41fadd6d4f19cb2d.tar.gz alacritty-14ec851c6947eb4a65f20b3b41fadd6d4f19cb2d.zip |
Use "invert" cursor instead of drawing block
The cursor now matches perfectly cell widths and actually shows the
character underneath.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 13 | ||||
-rw-r--r-- | src/renderer/mod.rs | 16 | ||||
-rw-r--r-- | src/term.rs | 51 |
3 files changed, 54 insertions, 26 deletions
diff --git a/src/main.rs b/src/main.rs index 5c52dcda..be45ed11 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,6 +110,7 @@ fn main() { .with_vsync() .with_title("Alacritty") .build().unwrap(); + window.set_window_resize_callback(Some(window_resize_handler as fn(u32, u32))); gl::load_with(|symbol| window.get_proc_address(symbol) as *const _); @@ -271,18 +272,14 @@ fn main() { } { - // Draw grid + cursor + // Draw grid { let _sampler = meter.sampler(); - renderer.with_api(terminal.size_info(), |mut api| { + let size_info = terminal.size_info().clone(); + renderer.with_api(&size_info, |mut api| { // Draw the grid - api.render_grid(terminal.grid(), &mut glyph_cache); - - // Also draw the cursor - if terminal.mode().contains(term::mode::SHOW_CURSOR) { - api.render_cursor(terminal.cursor(), &mut glyph_cache); - } + api.render_grid(&terminal.render_grid(), &mut glyph_cache); }) } diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 4cd26771..d5946c76 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -577,22 +577,6 @@ impl<'a> RenderApi<'a> { } } - pub fn render_cursor(&mut self, cursor: &index::Cursor, glyph_cache: &mut GlyphCache) { - if let Some(glyph) = glyph_cache.get(term::CURSOR_SHAPE, self) { - let cell = Cell { - c: term::CURSOR_SHAPE, - fg: term::DEFAULT_FG, - bg: term::DEFAULT_BG, - flags: cell::Flags::empty(), - }; - - let y: usize = *cursor.line; - let x: usize = *cursor.col; - - self.add_render_item(y as f32, x as f32, &cell, glyph); - } - } - pub fn render_grid(&mut self, grid: &Grid<Cell>, glyph_cache: &mut GlyphCache) { for (i, line) in grid.lines().enumerate() { for (j, cell) in line.cells().enumerate() { diff --git a/src/term.rs b/src/term.rs index 22e72810..0471fd28 100644 --- a/src/term.rs +++ b/src/term.rs @@ -13,8 +13,9 @@ // limitations under the License. // //! Exports the `Term` type which is a high-level API for the Grid -use std::ops::Range; use std::fmt; +use std::mem; +use std::ops::{Deref, Range}; use ansi::{self, Attr}; use grid::{Grid, ClearRegion}; @@ -31,6 +32,48 @@ macro_rules! debug_println { } } +/// RAII type which manages grid state for render +/// +/// This manages the cursor during a render. The cursor location is inverted to +/// draw it, and reverted after drawing to maintain state. +pub struct RenderGrid<'a> { + inner: &'a mut Grid<Cell>, + cursor: &'a Cursor, + mode: TermMode, +} + +impl<'a> RenderGrid<'a> { + fn new<'b>(grid: &'b mut Grid<Cell>, cursor: &'b Cursor, mode: TermMode) -> RenderGrid<'b> { + if mode.contains(mode::SHOW_CURSOR) { + let cell = &mut grid[cursor]; + mem::swap(&mut cell.fg, &mut cell.bg); + } + + RenderGrid { + inner: grid, + cursor: cursor, + mode: mode, + } + } +} + +impl<'a> Drop for RenderGrid<'a> { + fn drop(&mut self) { + if self.mode.contains(mode::SHOW_CURSOR) { + let cell = &mut self.inner[self.cursor]; + mem::swap(&mut cell.fg, &mut cell.bg); + } + } +} + +impl<'a> Deref for RenderGrid<'a> { + type Target = Grid<Cell>; + + fn deref(&self) -> &Self::Target { + self.inner + } +} + /// coerce val to be between min and max fn limit<T: PartialOrd>(val: T, min: T, max: T) -> T { if val < min { @@ -175,7 +218,7 @@ pub struct Term { } /// Terminal size info -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub struct SizeInfo { /// Terminal window width pub width: f32, @@ -245,6 +288,10 @@ impl Term { } } + pub fn render_grid<'a>(&'a mut self) -> RenderGrid<'a> { + RenderGrid::new(&mut self.grid, &self.cursor, self.mode) + } + /// Resize terminal to new dimensions pub fn resize(&mut self, width: f32, height: f32) { let size = SizeInfo { |