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);
}
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()?
}
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) ->
struct TuiLogicalState {
activity_stack: ActivityStack,
activity_state: Box<dyn ActivityState>,
+ last_area: Option<Rect>,
}
fn new_activity_state(activity: Activity, client: &mut Client) ->
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();