Skip to content

Commit

Permalink
Help pane is now a component :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 5, 2019
1 parent 3f3fe77 commit c243521
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
26 changes: 13 additions & 13 deletions src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::interactive::{
react::Terminal,
sorted_entries,
widgets::{HelpPaneState, ReactMainWindow},
widgets::{ReactHelpPane, ReactMainWindow},
ByteVisualization, DisplayOptions, EntryDataBundle, SortMode,
};
use dua::{
Expand Down Expand Up @@ -35,7 +35,6 @@ pub struct AppState {
pub entries: Vec<EntryDataBundle>,
pub sorting: SortMode,
pub message: Option<String>,
pub help_pane: Option<HelpPaneState>,
pub focussed: FocussedPane,
}

Expand Down Expand Up @@ -89,7 +88,7 @@ impl TerminalApp {
Main => break,
Help => {
self.state.focussed = Main;
self.state.help_pane = None
self.window.help_pane = None
}
},
_ => {}
Expand Down Expand Up @@ -125,7 +124,7 @@ impl TerminalApp {

fn cycle_focus(&mut self) {
use FocussedPane::*;
self.state.focussed = match (self.state.focussed, self.state.help_pane) {
self.state.focussed = match (self.state.focussed, &self.window.help_pane) {
(Main, Some(_)) => Help,
(Help, _) => Main,
_ => Main,
Expand All @@ -136,11 +135,11 @@ impl TerminalApp {
use FocussedPane::*;
self.state.focussed = match self.state.focussed {
Main => {
self.state.help_pane = Some(HelpPaneState::default());
self.window.help_pane = Some(ReactHelpPane::default());
Help
}
Help => {
self.state.help_pane = None;
self.window.help_pane = None;
Main
}
}
Expand Down Expand Up @@ -191,13 +190,14 @@ impl TerminalApp {

fn scroll_help(&mut self, direction: CursorDirection) {
use CursorDirection::*;
let scroll = self.window.draw_state.help_scroll; // TODO: don't do this - make it private when ready
self.window.draw_state.help_scroll = match direction {
Down => scroll.saturating_add(1),
Up => scroll.saturating_sub(1),
PageDown => scroll.saturating_add(10),
PageUp => scroll.saturating_sub(10),
};
if let Some(ref mut pane) = self.window.help_pane {
pane.scroll = match direction {
Down => pane.scroll.saturating_add(1),
Up => pane.scroll.saturating_sub(1),
PageDown => pane.scroll.saturating_add(10),
PageUp => pane.scroll.saturating_sub(10),
};
}
}

fn change_entry_selection(&mut self, direction: CursorDirection) {
Expand Down
22 changes: 14 additions & 8 deletions src/interactive/widgets/help.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::interactive::react::Component;
use std::borrow::Borrow;
use std::cell::{Cell, RefCell};
use tui::style::Color;
use tui::{
Expand All @@ -7,17 +9,19 @@ use tui::{
widgets::{Block, Borders, Paragraph, Text, Widget},
};

#[derive(Default, Copy, Clone)]
pub struct HelpPaneState;

pub struct HelpPane {
pub state: HelpPaneState,
#[derive(Default, Clone)]
pub struct ReactHelpPane {
pub scroll: u16,
}

pub struct ReactHelpPaneProps {
pub border_style: Style,
}

impl Widget for HelpPane {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
impl Component for ReactHelpPane {
type Props = ReactHelpPaneProps;

fn render(&mut self, props: impl Borrow<Self::Props>, area: Rect, buf: &mut Buffer) {
let (texts, num_lines) = {
let num_lines = Cell::new(0u16);
let count = |n| num_lines.set(num_lines.get() + n);
Expand Down Expand Up @@ -91,9 +95,11 @@ impl Widget for HelpPane {
(lines.into_inner(), num_lines.get())
};

let ReactHelpPaneProps { border_style } = props.borrow();

let mut block = Block::default()
.title("Help")
.border_style(self.border_style)
.border_style(*border_style)
.borders(Borders::ALL);
block.draw(area, buf);

Expand Down
19 changes: 8 additions & 11 deletions src/interactive/widgets/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::interactive::{
react::Component,
widgets::{Entries, Footer, Header, HelpPane, ListState},
widgets::{Entries, Footer, Header, ListState, ReactHelpPane, ReactHelpPaneProps},
FocussedPane, TerminalApp,
};
use dua::traverse::Traversal;
Expand All @@ -18,12 +18,12 @@ use tui::{
#[derive(Default, Clone)] // TODO: remove Clone derive
pub struct DrawState {
entries_list: ListState,
pub help_scroll: u16,
}

#[derive(Default, Clone)] // TODO: remove clone derive
pub struct ReactMainWindow {
pub draw_state: DrawState,
pub help_pane: Option<ReactHelpPane>,
}

impl<'a, 'b> Component for ReactMainWindow {
Expand Down Expand Up @@ -56,13 +56,13 @@ impl<'a, 'b> Component for ReactMainWindow {
)
.split(area);
let (header_area, entries_area, footer_area) = (regions[0], regions[1], regions[2]);
let (entries_area, help_area_state) = match state.help_pane {
Some(state) => {
let (entries_area, help_pane) = match self.help_pane {
Some(ref mut pane) => {
let regions = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(entries_area);
(regions[0], Some((regions[1], state)))
(regions[0], Some((regions[1], pane)))
}
None => (entries_area, None),
};
Expand Down Expand Up @@ -97,14 +97,11 @@ impl<'a, 'b> Component for ReactMainWindow {
}
.draw(entries_area, buf);

if let Some((help_area, state)) = help_area_state {
let mut pane = HelpPane {
scroll: draw_state.help_scroll,
state,
if let Some((help_area, pane)) = help_pane {
let props = ReactHelpPaneProps {
border_style: help_style,
};
pane.draw(help_area, buf);
draw_state.help_scroll = pane.scroll;
pane.render(props, help_area, buf);
}

Footer {
Expand Down

0 comments on commit c243521

Please sign in to comment.