Skip to content

Commit

Permalink
Fix/cache bug (#57)
Browse files Browse the repository at this point in the history
* rerender on x/y change as well

* don't panic on options widget when too small

* update CHANGELOG
  • Loading branch information
tarkah authored Feb 9, 2021
1 parent 1f85921 commit 6c48b99
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
39 changes: 29 additions & 10 deletions src/draw.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -148,7 +148,7 @@ fn draw_main<B: Backend>(frame: &mut Frame<B>, 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())
Expand All @@ -157,10 +157,32 @@ fn draw_main<B: Backend>(frame: &mut Frame<B>, 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],
);
}
}
}
}
Expand Down Expand Up @@ -240,12 +262,9 @@ fn draw_summary<B: Backend>(frame: &mut Frame<B>, 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);
Expand Down
2 changes: 1 addition & 1 deletion src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait CachableWidget<T: Hash>: StatefulWidget<State = T> + Sized {
} = <Self as CachableWidget<T>>::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;
Expand Down
6 changes: 2 additions & 4 deletions src/widget/stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,8 @@ impl CachableWidget<StockState> 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())];

Expand Down

0 comments on commit 6c48b99

Please sign in to comment.