chiark / gitweb /
Notify ActivityState mutably when it gets resized.
authorSimon Tatham <anakin@pobox.com>
Fri, 29 Dec 2023 16:10:56 +0000 (16:10 +0000)
committerSimon Tatham <anakin@pobox.com>
Fri, 29 Dec 2023 18:17:34 +0000 (18:17 +0000)
This should enable it to prepare anything it needs for the next
redraw.

src/tui.rs

index c7869cd5ba88a8b1bc8ee67996e31d47da9b0742..681305810138472609d33c4e8ecfa669bb23db15 100644 (file)
@@ -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<ColouredString>, 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<dyn ActivityState>,
+    last_area: Option<Rect>,
 }
 
 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();