diff --git a/CHANGELOG.md b/CHANGELOG.md index 724e652..68264f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,12 @@ and `Removed`. ## [Unreleased] +### Fixed + +- Options pane now re-renders correctly when resizing terminal window ([#57]) +- Prevent application from crashing when terminal was too small with options pane + open ([#57]) + ## [0.10.0] - 2021-02-08 ### Fixed @@ -55,3 +61,4 @@ and `Removed`. [#53]: https://github.com/tarkah/tickrs/pull/53 [#54]: https://github.com/tarkah/tickrs/pull/54 [#55]: https://github.com/tarkah/tickrs/pull/55 +[#57]: https://github.com/tarkah/tickrs/pull/57 \ No newline at end of file diff --git a/src/draw.rs b/src/draw.rs index 08f5f20..d7c1291 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -1,7 +1,7 @@ use tui::backend::Backend; use tui::layout::{Alignment, Constraint, Direction, Layout, Rect}; use tui::style::{Color, Style}; -use tui::widgets::{Block, Borders, Paragraph, Tabs, Text}; +use tui::widgets::{Block, Borders, Clear, Paragraph, Tabs, Text}; use tui::{Frame, Terminal}; use crate::app::{App, Mode}; @@ -148,7 +148,7 @@ fn draw_main(frame: &mut Frame, app: &mut App, area: Rect) { if let Some(stock) = app.stocks.get_mut(app.current_tab) { // main_chunks[0] - Stock widget // main_chunks[1] - Options widget (optional) - let main_chunks = if app.mode == Mode::DisplayOptions { + let mut main_chunks = if app.mode == Mode::DisplayOptions { Layout::default() .direction(Direction::Horizontal) .constraints([Constraint::Min(0), Constraint::Length(44)].as_ref()) @@ -157,10 +157,32 @@ fn draw_main(frame: &mut Frame, app: &mut App, area: Rect) { vec![layout[1]] }; - frame.render_stateful_widget(StockWidget {}, main_chunks[0], stock); + // If width is too small, don't render stock widget and use entire space + // for options widget + if main_chunks[0].width >= 19 { + frame.render_stateful_widget(StockWidget {}, main_chunks[0], stock); + } else { + main_chunks[1] = layout[1]; + } if let Some(options) = stock.options.as_mut() { - frame.render_stateful_widget(OptionsWidget {}, main_chunks[1], options); + if main_chunks[1].width >= 44 && main_chunks[1].height >= 14 { + frame.render_stateful_widget(OptionsWidget {}, main_chunks[1], options); + } else { + main_chunks[1] = add_padding(main_chunks[1], 1, PaddingDirection::Left); + main_chunks[1] = add_padding(main_chunks[1], 1, PaddingDirection::Top); + + frame.render_widget( + Paragraph::new( + [Text::styled( + "Increase screen size to display options", + Style::default(), + )] + .iter(), + ), + main_chunks[1], + ); + } } } } @@ -240,12 +262,9 @@ fn draw_summary(frame: &mut Frame, app: &mut App, area: Rect) { layout[2] = add_padding(layout[2], 2, PaddingDirection::Right); // Clear out empty area - frame.render_widget( - Block::default() - .borders(Borders::TOP) - .border_style(Style::default().bg(Color::Black).fg(Color::Black)), - layout[2], - ); + layout[2].height -= 1; + frame.render_widget(Clear, layout[2]); + layout[2].height += 1; let offset = layout[2].height - 3; layout[2] = add_padding(layout[2], offset, PaddingDirection::Top); diff --git a/src/widget.rs b/src/widget.rs index d42b8c2..aad01cb 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -42,7 +42,7 @@ pub trait CachableWidget: StatefulWidget + Sized { } = >::cache_state_mut(state).clone(); // If current hash and layout matches previous, use cached buffer instead of re-rendering - if hash == prev_hash && prev_area.width == area.width && prev_area.height == area.height { + if hash == prev_hash && prev_area == area { for (idx, cell) in buf.content.iter_mut().enumerate() { let x = idx as u16 % buf.area.width; let y = idx as u16 / buf.area.width; diff --git a/src/widget/stock.rs b/src/widget/stock.rs index 263e6cd..834add2 100644 --- a/src/widget/stock.rs +++ b/src/widget/stock.rs @@ -665,10 +665,8 @@ impl CachableWidget for StockWidget { if !*HIDE_TOGGLE { let toggle_block = block::new(" Toggle ", None); toggle_block.render(info_chunks[1], buf); - info_chunks[1].x += 2; - info_chunks[1].width -= 2; - info_chunks[1].y += 1; - info_chunks[1].height -= 1; + info_chunks[1] = add_padding(info_chunks[1], 2, PaddingDirection::Left); + info_chunks[1] = add_padding(info_chunks[1], 1, PaddingDirection::Top); let mut toggle_info = vec![Text::styled("Summary 's'", Style::default())];