Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c243521

Browse files
author
Sebastian Thiel
committed
Help pane is now a component :)
1 parent 3f3fe77 commit c243521

3 files changed

Lines changed: 35 additions & 32 deletions

File tree

src/interactive/app/eventloop.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::interactive::{
22
react::Terminal,
33
sorted_entries,
4-
widgets::{HelpPaneState, ReactMainWindow},
4+
widgets::{ReactHelpPane, ReactMainWindow},
55
ByteVisualization, DisplayOptions, EntryDataBundle, SortMode,
66
};
77
use dua::{
@@ -35,7 +35,6 @@ pub struct AppState {
3535
pub entries: Vec<EntryDataBundle>,
3636
pub sorting: SortMode,
3737
pub message: Option<String>,
38-
pub help_pane: Option<HelpPaneState>,
3938
pub focussed: FocussedPane,
4039
}
4140

@@ -89,7 +88,7 @@ impl TerminalApp {
8988
Main => break,
9089
Help => {
9190
self.state.focussed = Main;
92-
self.state.help_pane = None
91+
self.window.help_pane = None
9392
}
9493
},
9594
_ => {}
@@ -125,7 +124,7 @@ impl TerminalApp {
125124

126125
fn cycle_focus(&mut self) {
127126
use FocussedPane::*;
128-
self.state.focussed = match (self.state.focussed, self.state.help_pane) {
127+
self.state.focussed = match (self.state.focussed, &self.window.help_pane) {
129128
(Main, Some(_)) => Help,
130129
(Help, _) => Main,
131130
_ => Main,
@@ -136,11 +135,11 @@ impl TerminalApp {
136135
use FocussedPane::*;
137136
self.state.focussed = match self.state.focussed {
138137
Main => {
139-
self.state.help_pane = Some(HelpPaneState::default());
138+
self.window.help_pane = Some(ReactHelpPane::default());
140139
Help
141140
}
142141
Help => {
143-
self.state.help_pane = None;
142+
self.window.help_pane = None;
144143
Main
145144
}
146145
}
@@ -191,13 +190,14 @@ impl TerminalApp {
191190

192191
fn scroll_help(&mut self, direction: CursorDirection) {
193192
use CursorDirection::*;
194-
let scroll = self.window.draw_state.help_scroll; // TODO: don't do this - make it private when ready
195-
self.window.draw_state.help_scroll = match direction {
196-
Down => scroll.saturating_add(1),
197-
Up => scroll.saturating_sub(1),
198-
PageDown => scroll.saturating_add(10),
199-
PageUp => scroll.saturating_sub(10),
200-
};
193+
if let Some(ref mut pane) = self.window.help_pane {
194+
pane.scroll = match direction {
195+
Down => pane.scroll.saturating_add(1),
196+
Up => pane.scroll.saturating_sub(1),
197+
PageDown => pane.scroll.saturating_add(10),
198+
PageUp => pane.scroll.saturating_sub(10),
199+
};
200+
}
201201
}
202202

203203
fn change_entry_selection(&mut self, direction: CursorDirection) {

src/interactive/widgets/help.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::interactive::react::Component;
2+
use std::borrow::Borrow;
13
use std::cell::{Cell, RefCell};
24
use tui::style::Color;
35
use tui::{
@@ -7,17 +9,19 @@ use tui::{
79
widgets::{Block, Borders, Paragraph, Text, Widget},
810
};
911

10-
#[derive(Default, Copy, Clone)]
11-
pub struct HelpPaneState;
12-
13-
pub struct HelpPane {
14-
pub state: HelpPaneState,
12+
#[derive(Default, Clone)]
13+
pub struct ReactHelpPane {
1514
pub scroll: u16,
15+
}
16+
17+
pub struct ReactHelpPaneProps {
1618
pub border_style: Style,
1719
}
1820

19-
impl Widget for HelpPane {
20-
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
21+
impl Component for ReactHelpPane {
22+
type Props = ReactHelpPaneProps;
23+
24+
fn render(&mut self, props: impl Borrow<Self::Props>, area: Rect, buf: &mut Buffer) {
2125
let (texts, num_lines) = {
2226
let num_lines = Cell::new(0u16);
2327
let count = |n| num_lines.set(num_lines.get() + n);
@@ -91,9 +95,11 @@ impl Widget for HelpPane {
9195
(lines.into_inner(), num_lines.get())
9296
};
9397

98+
let ReactHelpPaneProps { border_style } = props.borrow();
99+
94100
let mut block = Block::default()
95101
.title("Help")
96-
.border_style(self.border_style)
102+
.border_style(*border_style)
97103
.borders(Borders::ALL);
98104
block.draw(area, buf);
99105

src/interactive/widgets/main.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::interactive::{
22
react::Component,
3-
widgets::{Entries, Footer, Header, HelpPane, ListState},
3+
widgets::{Entries, Footer, Header, ListState, ReactHelpPane, ReactHelpPaneProps},
44
FocussedPane, TerminalApp,
55
};
66
use dua::traverse::Traversal;
@@ -18,12 +18,12 @@ use tui::{
1818
#[derive(Default, Clone)] // TODO: remove Clone derive
1919
pub struct DrawState {
2020
entries_list: ListState,
21-
pub help_scroll: u16,
2221
}
2322

2423
#[derive(Default, Clone)] // TODO: remove clone derive
2524
pub struct ReactMainWindow {
2625
pub draw_state: DrawState,
26+
pub help_pane: Option<ReactHelpPane>,
2727
}
2828

2929
impl<'a, 'b> Component for ReactMainWindow {
@@ -56,13 +56,13 @@ impl<'a, 'b> Component for ReactMainWindow {
5656
)
5757
.split(area);
5858
let (header_area, entries_area, footer_area) = (regions[0], regions[1], regions[2]);
59-
let (entries_area, help_area_state) = match state.help_pane {
60-
Some(state) => {
59+
let (entries_area, help_pane) = match self.help_pane {
60+
Some(ref mut pane) => {
6161
let regions = Layout::default()
6262
.direction(Direction::Horizontal)
6363
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
6464
.split(entries_area);
65-
(regions[0], Some((regions[1], state)))
65+
(regions[0], Some((regions[1], pane)))
6666
}
6767
None => (entries_area, None),
6868
};
@@ -97,14 +97,11 @@ impl<'a, 'b> Component for ReactMainWindow {
9797
}
9898
.draw(entries_area, buf);
9999

100-
if let Some((help_area, state)) = help_area_state {
101-
let mut pane = HelpPane {
102-
scroll: draw_state.help_scroll,
103-
state,
100+
if let Some((help_area, pane)) = help_pane {
101+
let props = ReactHelpPaneProps {
104102
border_style: help_style,
105103
};
106-
pane.draw(help_area, buf);
107-
draw_state.help_scroll = pane.scroll;
104+
pane.render(props, help_area, buf);
108105
}
109106

110107
Footer {

0 commit comments

Comments
 (0)