diff options
author | Joe Wilm <joe@jwilm.com> | 2016-12-12 09:31:48 -0800 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2016-12-12 09:31:48 -0800 |
commit | 1a1b740c38cfbadff4cd985ee925ac024627d2b9 (patch) | |
tree | 98f51ebedcc1abff0101da7747403b0d8aee698f /src/display.rs | |
parent | 4e9a307bed50fc02f101eb6cbfa4d39283dd6b8c (diff) | |
download | alacritty-1a1b740c38cfbadff4cd985ee925ac024627d2b9.tar.gz alacritty-1a1b740c38cfbadff4cd985ee925ac024627d2b9.zip |
Remove need for Rc<RefCell<_>> usage
This adds a trait OnResize and a separate method handle_resize to the
display. Instead of having a callback to receive resize events, a list
of &mut OnResize are passed to this new method. Doing this allowed the
only RefCell usage in the codebase to be removed :).
Diffstat (limited to 'src/display.rs')
-rw-r--r-- | src/display.rs | 63 |
1 files changed, 33 insertions, 30 deletions
diff --git a/src/display.rs b/src/display.rs index cfe684c0..b16bf95a 100644 --- a/src/display.rs +++ b/src/display.rs @@ -30,7 +30,7 @@ use term::{Term, SizeInfo}; use window::{self, Size, Pixels, Window, SetInnerSize}; /// The display wraps a window, font rasterizer, and GPU renderer -pub struct Display<F> { +pub struct Display { window: Window, renderer: QuadRenderer, glyph_cache: GlyphCache, @@ -38,22 +38,24 @@ pub struct Display<F> { rx: mpsc::Receiver<(u32, u32)>, tx: mpsc::Sender<(u32, u32)>, meter: Meter, - resize_callback: Option<F>, size_info: SizeInfo, } /// Can wakeup the render loop from other threads pub struct Notifier(window::Proxy); +/// Types that are interested in when the display is resized +pub trait OnResize { + fn on_resize(&mut self, size: &SizeInfo); +} + impl Notifier { pub fn notify(&self) { self.0.wakeup_event_loop(); } } -impl<F> Display<F> - where F: Fn(&SizeInfo) -{ +impl Display { pub fn notifier(&self) -> Notifier { Notifier(self.window.create_window_proxy()) } @@ -63,11 +65,6 @@ impl<F> Display<F> self.render_timer = config.render_timer(); } - /// Provide a callback to be invoked then the display changes size. - pub fn set_resize_callback(&mut self, callback: F) { - self.resize_callback = Some(callback); - } - /// Get size info about the display pub fn size(&self) -> &SizeInfo { &self.size_info @@ -76,7 +73,7 @@ impl<F> Display<F> pub fn new( config: &Config, options: &cli::Options, - ) -> Result<Display<F>, window::Error> { + ) -> Result<Display, window::Error> { // Extract some properties from config let font = config.font(); let dpi = config.dpi(); @@ -156,7 +153,6 @@ impl<F> Display<F> tx: tx, rx: rx, meter: Meter::new(), - resize_callback: None, size_info: size_info, }; @@ -179,27 +175,14 @@ impl<F> Display<F> &self.window } - /// Draw the screen - /// - /// A reference to Term whose state is being drawn must be provided. - /// - /// This call may block if vsync is enabled - pub fn draw(&mut self, mut terminal: MutexGuard<Term>, config: &Config) { - // This is a hack since sometimes we get stuck waiting for events - // in the main loop otherwise. - // - // TODO figure out why this is necessary - self.window.clear_wakeup_flag(); - - // Clear dirty flag - terminal.dirty = false; - + /// Process pending resize events + pub fn handle_resize(&mut self, terminal: &mut MutexGuard<Term>, items: &mut [&mut OnResize]) { // Resize events new_size and are handled outside the poll_events // iterator. This has the effect of coalescing multiple resize // events into one. let mut new_size = None; - // Check for any out-of-band resize events (mac only) + // Take most recent resize event, if any while let Ok(sz) = self.rx.try_recv() { new_size = Some(sz); } @@ -209,11 +192,31 @@ impl<F> Display<F> if let Some((w, h)) = new_size.take() { terminal.resize(w as f32, h as f32); let size = terminal.size_info(); - self.resize_callback.as_ref() - .map(|func| func(&size)); + + for mut item in items { + item.on_resize(size) + } + self.renderer.resize(w as i32, h as i32); } + } + + /// Draw the screen + /// + /// A reference to Term whose state is being drawn must be provided. + /// + /// This call may block if vsync is enabled + pub fn draw(&mut self, mut terminal: MutexGuard<Term>, config: &Config) { + // This is a hack since sometimes we get stuck waiting for events + // in the main loop otherwise. + // + // TODO figure out why this is necessary + self.window.clear_wakeup_flag(); + + // Clear dirty flag + terminal.dirty = false; + { let glyph_cache = &mut self.glyph_cache; // Draw grid |