diff options
author | Dan Aloni <alonid@gmail.com> | 2017-10-14 20:35:56 +0300 |
---|---|---|
committer | Joe Wilm <jwilm@users.noreply.github.com> | 2017-10-14 10:35:56 -0700 |
commit | 8a0b1d9c3f5a9ddb98469a340bbcd06374d4a24e (patch) | |
tree | 80768003f9a2f3c99b0238dc68f4c9b119cc77d6 /src/display.rs | |
parent | fd410f9ec8bc53e35d46cc213244c978ef7c5816 (diff) | |
download | alacritty-8a0b1d9c3f5a9ddb98469a340bbcd06374d4a24e.tar.gz alacritty-8a0b1d9c3f5a9ddb98469a340bbcd06374d4a24e.zip |
Implement user actions for font resize (#625)
Adds support for font resizing at run-time. Three new actions are
introduced:
* IncreaseFontSize - Increases current font size by 1.0
* DecreaseFontSize - Decreases current font size by 1.0
* ResetFontSize - Resets font size to that specified in the
configuration.
The stock config files have example configuration for each which should
match gnome-terminal. For convenience, the config entries are:
- { key: Key0, mods: Control, action: ResetFontSize }
- { key: Equals, mods: Control, action: IncreaseFontSize }
- { key: Subtract, mods: Control, action: DecreaseFontSize }
Diffstat (limited to 'src/display.rs')
-rw-r--r-- | src/display.rs | 98 |
1 files changed, 69 insertions, 29 deletions
diff --git a/src/display.rs b/src/display.rs index cb5cf8c9..25318f0e 100644 --- a/src/display.rs +++ b/src/display.rs @@ -96,6 +96,7 @@ pub struct Display { rx: mpsc::Receiver<(u32, u32)>, tx: mpsc::Sender<(u32, u32)>, meter: Meter, + font_size_modifier: i8, size_info: SizeInfo, last_background_color: Rgb, } @@ -133,7 +134,6 @@ impl Display { options: &cli::Options, ) -> Result<Display, Error> { // Extract some properties from config - let font = config.font(); let render_timer = config.render_timer(); // Create the window where Alacritty will be displayed @@ -146,39 +146,17 @@ impl Display { info!("device_pixel_ratio: {}", dpr); - let rasterizer = font::Rasterizer::new(dpr, config.use_thin_strokes())?; - // Create renderer let mut renderer = QuadRenderer::new(&config, size)?; - // Initialize glyph cache - let glyph_cache = { - info!("Initializing glyph cache"); - let init_start = ::std::time::Instant::now(); - - let cache = renderer.with_loader(|mut api| { - GlyphCache::new(rasterizer, config, &mut api) - })?; - - let stop = init_start.elapsed(); - let stop_f = stop.as_secs() as f64 + stop.subsec_nanos() as f64 / 1_000_000_000f64; - info!("Finished initializing glyph cache in {}", stop_f); - - cache - }; - - // Need font metrics to resize the window properly. This suggests to me the - // font metrics should be computed before creating the window in the first - // place so that a resize is not needed. - let metrics = glyph_cache.font_metrics(); - let cell_width = (metrics.average_advance + font.offset().x as f64) as u32; - let cell_height = (metrics.line_height + font.offset().y as f64) as u32; + let (glyph_cache, cell_width, cell_height) = + Self::new_glyph_cache(&window, &mut renderer, config, 0)?; // Resize window to specified dimensions let dimensions = options.dimensions() .unwrap_or_else(|| config.dimensions()); - let width = cell_width * dimensions.columns_u32(); - let height = cell_height * dimensions.lines_u32(); + let width = cell_width as u32 * dimensions.columns_u32(); + let height = cell_height as u32 * dimensions.lines_u32(); let size = Size { width: Pixels(width), height: Pixels(height) }; info!("set_inner_size: {}", size); @@ -222,11 +200,56 @@ impl Display { tx: tx, rx: rx, meter: Meter::new(), + font_size_modifier: 0, size_info: size_info, last_background_color: background_color, }) } + fn new_glyph_cache(window : &Window, renderer : &mut QuadRenderer, + config: &Config, font_size_delta: i8) + -> Result<(GlyphCache, f32, f32), Error> + { + let font = config.font().clone().with_size_delta(font_size_delta as f32); + let dpr = window.hidpi_factor(); + let rasterizer = font::Rasterizer::new(dpr, config.use_thin_strokes())?; + + // Initialize glyph cache + let glyph_cache = { + info!("Initializing glyph cache"); + let init_start = ::std::time::Instant::now(); + + let cache = renderer.with_loader(|mut api| { + GlyphCache::new(rasterizer, &font, &mut api) + })?; + + let stop = init_start.elapsed(); + let stop_f = stop.as_secs() as f64 + stop.subsec_nanos() as f64 / 1_000_000_000f64; + info!("Finished initializing glyph cache in {}", stop_f); + + cache + }; + + // Need font metrics to resize the window properly. This suggests to me the + // font metrics should be computed before creating the window in the first + // place so that a resize is not needed. + let metrics = glyph_cache.font_metrics(); + let cell_width = (metrics.average_advance + font.offset().x as f64) as u32; + let cell_height = (metrics.line_height + font.offset().y as f64) as u32; + + return Ok((glyph_cache, cell_width as f32, cell_height as f32)); + } + + pub fn update_glyph_cache(&mut self, config: &Config, font_size_delta: i8) + { + let (glyph_cache, cell_width, cell_height) = + Self::new_glyph_cache(&self.window, + &mut self.renderer, config, font_size_delta).unwrap(); + self.glyph_cache = glyph_cache; + self.size_info.cell_width = cell_width; + self.size_info.cell_height = cell_height; + } + #[inline] pub fn resize_channel(&self) -> mpsc::Sender<(u32, u32)> { self.tx.clone() @@ -240,6 +263,7 @@ impl Display { pub fn handle_resize( &mut self, terminal: &mut MutexGuard<Term>, + config: &Config, items: &mut [&mut OnResize] ) { // Resize events new_size and are handled outside the poll_events @@ -252,11 +276,27 @@ impl Display { new_size = Some(sz); } + if terminal.font_size_modifier != self.font_size_modifier { + // Font size modification detected + + self.font_size_modifier = terminal.font_size_modifier; + self.update_glyph_cache(config, terminal.font_size_modifier); + + if new_size == None { + // Force a resize to refresh things + new_size = Some((self.size_info.width as u32, + self.size_info.height as u32)); + } + } + // Receive any resize events; only call gl::Viewport on last // available if let Some((w, h)) = new_size.take() { - terminal.resize(w as f32, h as f32); - let size = terminal.size_info(); + self.size_info.width = w as f32; + self.size_info.height = h as f32; + + let size = &self.size_info; + terminal.resize(size); for item in items { item.on_resize(size) |