From 18df92e748695fc5d945e6a998cf2042977c63f5 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 29 Dec 2023 16:10:56 +0000 Subject: [PATCH] Notify ActivityState mutably when it gets resized. This should enable it to prepare anything it needs for the next redraw. --- src/tui.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/tui.rs b/src/tui.rs index c7869cd..6813058 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -271,10 +271,12 @@ impl Tui { fn main_loop(&mut self) -> Result<(), TuiError> { 'outer: loop { + let state = &mut self.state; + self.terminal.draw(|frame| { let area = frame.size(); let buf = frame.buffer_mut(); - if let Some((x, y)) = self.state.draw_frame(area, buf) { + if let Some((x, y)) = state.draw_frame(area, buf) { if let (Ok(x), Ok(y)) = (x.try_into(), y.try_into()) { frame.set_cursor(x, y); } @@ -288,7 +290,7 @@ impl Tui { Event::Key(key) => { if key.kind == KeyEventKind::Press { for ourkey in Self::translate_keypress(key) { - match self.state.handle_keypress( + match state.handle_keypress( ourkey, &mut self.client) { PhysicalAction::Beep => { Self::beep()? @@ -338,6 +340,7 @@ pub enum LogicalAction { } pub trait ActivityState { + fn resize(&mut self, _w: usize, _h: usize) {} fn draw(&self, w: usize, h: usize) -> (Vec, CursorPosition); fn handle_keypress(&mut self, key: OurKey, client: &mut Client) -> @@ -347,6 +350,7 @@ pub trait ActivityState { struct TuiLogicalState { activity_stack: ActivityStack, activity_state: Box, + last_area: Option, } fn new_activity_state(activity: Activity, client: &mut Client) -> @@ -382,11 +386,18 @@ impl TuiLogicalState { TuiLogicalState { activity_stack: activity_stack, activity_state: activity_state, + last_area: None, } } - fn draw_frame(&self, area: Rect, buf: &mut Buffer) + fn draw_frame(&mut self, area: Rect, buf: &mut Buffer) -> Option<(usize, usize)> { + if self.last_area != Some(area) { + self.last_area = Some(area); + self.activity_state.resize( + area.width as usize, area.height as usize); + } + let (lines, cursorpos) = self.activity_state.draw( area.width as usize, area.height as usize); buf.reset(); -- 2.30.2