Skip to content

Commit

Permalink
Handle HiDPI factor change event
Browse files Browse the repository at this point in the history
The Display's resize channel is now a command channel, for both resize and factor change commands
  • Loading branch information
valpackett committed Apr 2, 2018
1 parent 23b8568 commit 3443033
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
4 changes: 4 additions & 0 deletions font/src/darwin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ impl ::Rasterize for Rasterizer {
Err(Error::MissingGlyph(glyph.c))
})
}

fn set_device_pixel_ratio(&mut self, dpr: f32) {
self.device_pixel_ratio = dpr;
}
}

impl Rasterizer {
Expand Down
3 changes: 3 additions & 0 deletions font/src/ft/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ impl ::Rasterize for FreeTypeRasterizer {
self.get_rendered_glyph(glyph_key)
}

fn set_device_pixel_ratio(&mut self, dpr: f32) {
self.device_pixel_ratio = dpr;
}
}

pub trait IntoFontconfigType {
Expand Down
3 changes: 3 additions & 0 deletions font/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,7 @@ pub trait Rasterize {

/// Rasterize the glyph described by `GlyphKey`.
fn get_glyph(&mut self, &GlyphKey) -> Result<RasterizedGlyph, Self::Err>;

/// Update stored device pixel ratio
fn set_device_pixel_ratio(&mut self, f32);
}
4 changes: 2 additions & 2 deletions src/bin/alacritty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn run(mut config: Config, options: &cli::Options) -> Result<(), Box<Error>> {
// Need the Rc<RefCell<_>> here since a ref is shared in the resize callback
let mut processor = event::Processor::new(
event_loop::Notifier(event_loop.channel()),
display.resize_channel(),
display.command_channel(),
options,
&config,
options.ref_test,
Expand Down Expand Up @@ -209,7 +209,7 @@ fn run(mut config: Config, options: &cli::Options) -> Result<(), Box<Error>> {
// Try to update the position of the input method editor
let (x, y) = display.current_xim_spot(&terminal);
window.send_xim_spot(x, y);
// Handle pending resize events
// Handle pending resize (and HiDPI factor change) events
//
// The second argument is a list of types that want to be notified
// of display size changes.
Expand Down
27 changes: 18 additions & 9 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,18 @@ impl From<renderer::Error> for Error {
}
}

pub enum DisplayCommand {
NewSize(u32, u32),
NewHiDPIFactor(f32),
}

/// The display wraps a window, font rasterizer, and GPU renderer
pub struct Display {
renderer: QuadRenderer,
glyph_cache: GlyphCache,
render_timer: bool,
rx: mpsc::Receiver<(u32, u32)>,
tx: mpsc::Sender<(u32, u32)>,
rx: mpsc::Receiver<DisplayCommand>,
tx: mpsc::Sender<DisplayCommand>,
meter: Meter,
font_size: font::Size,
size_info: SizeInfo,
Expand Down Expand Up @@ -220,11 +225,11 @@ impl Display {
Ok((glyph_cache, cell_width.floor(), cell_height.floor()))
}

pub fn update_glyph_cache(&mut self, config: &Config) {
pub fn update_glyph_cache(&mut self, config: &Config, new_dpr: Option<f32>) {
let cache = &mut self.glyph_cache;
let size = self.font_size;
self.renderer.with_loader(|mut api| {
let _ = cache.update_font_size(config.font(), size, &mut api);
let _ = cache.update_font_size(config.font(), size, new_dpr, &mut api);
});

let metrics = cache.font_metrics();
Expand All @@ -233,11 +238,11 @@ impl Display {
}

#[inline]
pub fn resize_channel(&self) -> mpsc::Sender<(u32, u32)> {
pub fn command_channel(&self) -> mpsc::Sender<DisplayCommand> {
self.tx.clone()
}

/// Process pending resize events
/// Process pending resize (and HiDPI factor) events
pub fn handle_resize(
&mut self,
terminal: &mut MutexGuard<Term>,
Expand All @@ -248,16 +253,20 @@ impl Display {
// iterator. This has the effect of coalescing multiple resize
// events into one.
let mut new_size = None;
let mut new_dpr = None;

// Take most recent resize event, if any
while let Ok(sz) = self.rx.try_recv() {
new_size = Some(sz);
match sz {
DisplayCommand::NewSize(w, h) => new_size = Some((w, h)),
DisplayCommand::NewHiDPIFactor(dpr) => new_dpr = Some(dpr)
}
}

// Font size modification detected
if terminal.font_size != self.font_size {
if terminal.font_size != self.font_size || new_dpr.is_some() {
self.font_size = terminal.font_size;
self.update_glyph_cache(config);
self.update_glyph_cache(config, new_dpr);

if new_size == None {
// Force a resize to refresh things
Expand Down
20 changes: 12 additions & 8 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use copypasta::{Clipboard, Load, Store};

use config::{self, Config};
use cli::Options;
use display::OnResize;
use display::{OnResize, DisplayCommand};
use index::{Line, Column, Side, Point};
use input::{self, MouseBinding, KeyBinding};
use selection::Selection;
Expand Down Expand Up @@ -190,7 +190,7 @@ pub struct Processor<N> {
wait_for_event: bool,
notifier: N,
mouse: Mouse,
resize_tx: mpsc::Sender<(u32, u32)>,
display_tx: mpsc::Sender<DisplayCommand>,
ref_test: bool,
size_info: SizeInfo,
pub selection: Option<Selection>,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<N: Notify> Processor<N> {
/// pty.
pub fn new(
notifier: N,
resize_tx: mpsc::Sender<(u32, u32)>,
display_tx: mpsc::Sender<DisplayCommand>,
options: &Options,
config: &Config,
ref_test: bool,
Expand All @@ -232,7 +232,7 @@ impl<N: Notify> Processor<N> {
print_events: options.print_events,
wait_for_event: true,
notifier,
resize_tx,
display_tx,
ref_test,
mouse: Default::default(),
selection: None,
Expand All @@ -253,7 +253,7 @@ impl<N: Notify> Processor<N> {
processor: &mut input::Processor<'a, ActionContext<'a, N>>,
event: Event,
ref_test: bool,
resize_tx: &mpsc::Sender<(u32, u32)>,
display_tx: &mpsc::Sender<DisplayCommand>,
hide_cursor: &mut bool,
window_is_focused: &mut bool,
) {
Expand Down Expand Up @@ -287,9 +287,13 @@ impl<N: Notify> Processor<N> {
::std::process::exit(0);
},
Resized(w, h) => {
resize_tx.send((w, h)).expect("send new size");
display_tx.send(DisplayCommand::NewSize(w, h)).expect("send new size");
processor.ctx.terminal.dirty = true;
},
HiDPIFactorChanged(dpr) => {
display_tx.send(DisplayCommand::NewHiDPIFactor(dpr)).expect("send new size");
processor.ctx.terminal.dirty = true;
}
KeyboardInput { input, .. } => {
let glutin::KeyboardInput { state, virtual_keycode, modifiers, .. } = input;
processor.process_key(state, virtual_keycode, &modifiers);
Expand Down Expand Up @@ -370,7 +374,7 @@ impl<N: Notify> Processor<N> {
let print_events = self.print_events;

let ref_test = self.ref_test;
let resize_tx = &self.resize_tx;
let display_tx = &self.display_tx;

if self.wait_for_event {
// A Vec is used here since wait_events can potentially yield
Expand Down Expand Up @@ -418,7 +422,7 @@ impl<N: Notify> Processor<N> {
&mut processor,
event,
ref_test,
resize_tx,
display_tx,
hide_cursor,
&mut window_is_focused,
);
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ impl GlyphCache {
&mut self,
font: &config::Font,
size: font::Size,
new_dpr: Option<f32>,
loader: &mut L
) -> Result<(), font::Error> {
// Clear currently cached data in both GL and the registry
Expand All @@ -306,6 +307,9 @@ impl GlyphCache {
let font = font.to_owned().with_size(size);
info!("Font size changed: {:?}", font.size);
let (regular, bold, italic) = Self::compute_font_keys(&font, &mut self.rasterizer)?;
if let Some(dpr) = new_dpr {
self.rasterizer.set_device_pixel_ratio(dpr);
}
self.rasterizer.get_glyph(&GlyphKey { font_key: regular, c: 'm', size: font.size() })?;
let metrics = self.rasterizer.metrics(regular)?;

Expand Down

0 comments on commit 3443033

Please sign in to comment.