Skip to content

Commit

Permalink
Finally, everything was properly ported to tui-react
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 5, 2019
1 parent ae679ed commit 7549e82
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 27 deletions.
34 changes: 24 additions & 10 deletions src/interactive/app/eventloop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::interactive::{
sorted_entries,
widgets::{ReactHelpPane, ReactMainWindow},
widgets::{ReactHelpPane, ReactMainWindow, ReactMainWindowProps},
ByteVisualization, DisplayOptions, EntryDataBundle, SortMode,
};
use dua::{
Expand Down Expand Up @@ -54,15 +54,30 @@ enum CursorDirection {
}

impl TerminalApp {
fn draw<B>(&mut self, terminal: &mut Terminal<B>) -> Result<(), Error>
fn draw_window<B>(
window: &mut ReactMainWindow,
props: ReactMainWindowProps,
terminal: &mut Terminal<B>,
) -> Result<(), Error>
where
B: Backend,
{
let mut window = self.window.clone(); // TODO: fix this - we shouldn't have to pass ourselves as props!
terminal.render(&mut window, &*self)?;
self.window = window;
let area = terminal.pre_render()?;
window.render(props, area, terminal.current_buffer_mut());
terminal.post_render()?;
Ok(())
}
fn draw<B>(&mut self, terminal: &mut Terminal<B>) -> Result<(), Error>
where
B: Backend,
{
let props = ReactMainWindowProps {
traversal: &self.traversal,
display: self.display,
state: &self.state,
};
Self::draw_window(&mut self.window, props, terminal)
}
pub fn process_events<B, R>(
&mut self,
terminal: &mut Terminal<B>,
Expand Down Expand Up @@ -242,13 +257,12 @@ impl TerminalApp {
message: Some("-> scanning <-".into()),
..Default::default()
};
let app = TerminalApp {
traversal: traversal.clone(), // TODO absolutely fix this! We should not rely on this anymore when done
let props = ReactMainWindowProps {
traversal,
display: display_options,
state,
window: Default::default(),
state: &state,
};
terminal.render(&mut window, &app).map_err(Into::into)
Self::draw_window(&mut window, props, terminal)
})?;

let sorting = Default::default();
Expand Down
2 changes: 1 addition & 1 deletion src/interactive/widgets/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct ReactEntriesProps<'a> {
pub is_focussed: bool,
}

#[derive(Default, Clone)]
#[derive(Default)]
pub struct ReactEntries {
pub list: ReactList,
}
Expand Down
24 changes: 16 additions & 8 deletions src/interactive/widgets/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::interactive::{
Header, ReactEntries, ReactEntriesProps, ReactFooter, ReactFooterProps, ReactHelpPane,
ReactHelpPaneProps,
},
FocussedPane, TerminalApp,
AppState, DisplayOptions, FocussedPane,
};
use dua::traverse::Traversal;
use std::borrow::Borrow;
Expand All @@ -16,17 +16,26 @@ use tui::{
};
use tui_react::ToplevelComponent;

#[derive(Default, Clone)] // TODO: remove clone derive
pub struct ReactMainWindowProps<'a> {
pub traversal: &'a Traversal,
pub display: DisplayOptions,
pub state: &'a AppState,
}

#[derive(Default)]
pub struct ReactMainWindow {
pub help_pane: Option<ReactHelpPane>,
pub entries_pane: ReactEntries,
}

impl<'a, 'b> ToplevelComponent for ReactMainWindow {
type Props = TerminalApp;

fn render(&mut self, props: impl Borrow<TerminalApp>, area: Rect, buf: &mut Buffer) {
let TerminalApp {
impl ReactMainWindow {
pub fn render<'a>(
&mut self,
props: impl Borrow<ReactMainWindowProps<'a>>,
area: Rect,
buf: &mut Buffer,
) {
let ReactMainWindowProps {
traversal:
Traversal {
tree,
Expand All @@ -36,7 +45,6 @@ impl<'a, 'b> ToplevelComponent for ReactMainWindow {
},
display,
state,
..
} = props.borrow();
let regions = Layout::default()
.direction(Direction::Vertical)
Expand Down
2 changes: 1 addition & 1 deletion src/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct EntryData {
const REFRESH_RATE: Duration = Duration::from_millis(100);

/// The result of the previous filesystem traversal
#[derive(Default, Debug, Clone)] // TODO remove clone bound
#[derive(Default, Debug)]
pub struct Traversal {
/// A tree representing the entire filestem traversal
pub tree: Tree,
Expand Down
2 changes: 1 addition & 1 deletion tui-react/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn fill_background_to_right(mut s: String, entire_width: u16) -> String {
}
}

#[derive(Default, Clone)] // TODO: remove Clone derive
#[derive(Default)]
pub struct ReactList {
/// The index at which the list last started. Used for scrolling
offset: usize,
Expand Down
20 changes: 14 additions & 6 deletions tui-react/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,15 @@ where
Ok(())
}

pub fn render<C>(&mut self, component: &mut C, props: impl Borrow<C::Props>) -> io::Result<()>
where
C: ToplevelComponent,
{
/// Get ready for rendering and return the maximum display size as `Rect`
pub fn pre_render(&mut self) -> io::Result<Rect> {
// Autoresize - otherwise we get glitches if shrinking or potential desync between widgets
// and the terminal (if growing), which may OOB.
self.autoresize()?;
Ok(self.known_size.clone())
}

component.render(props, self.known_size, self.current_buffer_mut());

pub fn post_render(&mut self) -> io::Result<()> {
self.reconcile_and_flush()?;

self.buffers[1 - self.current].reset();
Expand All @@ -101,6 +100,15 @@ where
Ok(())
}

pub fn render<C>(&mut self, component: &mut C, props: impl Borrow<C::Props>) -> io::Result<()>
where
C: ToplevelComponent,
{
self.pre_render()?;
component.render(props, self.known_size, self.current_buffer_mut());
self.post_render()
}

pub fn hide_cursor(&mut self) -> io::Result<()> {
self.backend.hide_cursor()?;
self.hidden_cursor = true;
Expand Down

0 comments on commit 7549e82

Please sign in to comment.