Skip to content

Commit

Permalink
non-mutable widget state
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thiel committed Jun 4, 2019
1 parent 7ce062f commit 971e235
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
13 changes: 6 additions & 7 deletions src/interactive/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ impl From<WalkOptions> for DisplayOptions {
pub struct AppState {
pub root: TreeIndex,
pub selected: Option<TreeIndex>,
pub entries_list_start: usize,
pub sorting: SortMode,
pub message: Option<String>,
}
Expand All @@ -58,6 +57,7 @@ pub struct TerminalApp {
pub traversal: Traversal,
pub display: DisplayOptions,
pub state: AppState,
pub widgets: WidgetState,
}

enum CursorDirection {
Expand All @@ -76,16 +76,16 @@ impl TerminalApp {
traversal,
display,
state,
widgets,
} = self;

terminal.draw(|mut f| {
let full_screen = f.size();
let mut state_mut = WidgetState;
MainWindow {
traversal,
display: *display,
state: &state,
state_mut: &mut state_mut,
widgets: &widgets,
}
.render(&mut f, full_screen)
})?;
Expand Down Expand Up @@ -197,14 +197,13 @@ impl TerminalApp {
sorting: Default::default(),
message: Some("-> scanning <-".into()),
selected: None,
entries_list_start: 0,
};
let mut state_mut = WidgetState;
let state_mut = WidgetState;
MainWindow {
traversal,
display: display_options,
state: &state,
state_mut: &mut state_mut,
widgets: &state_mut,
}
.render(&mut f, full_screen)
})?;
Expand All @@ -222,10 +221,10 @@ impl TerminalApp {
sorting,
message: None,
selected,
entries_list_start: 0,
},
display: display_options,
traversal,
widgets: WidgetState,
})
}
}
6 changes: 2 additions & 4 deletions src/interactive/widgets/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub struct Entries<'a, 'b> {
pub display: DisplayOptions,
pub sorting: SortMode,
pub selected: Option<TreeIndex>,
pub list_start: usize,
pub state_mut: &'b mut WidgetState,
pub state: &'b WidgetState,
}

impl<'a, 'b> Widget for Entries<'a, 'b> {
Expand All @@ -30,8 +29,7 @@ impl<'a, 'b> Widget for Entries<'a, 'b> {
display,
sorting,
selected,
list_start: _,
state_mut,
state: _,
} = self;
let is_top = |node_idx| {
tree.neighbors_directed(node_idx, petgraph::Incoming)
Expand Down
8 changes: 3 additions & 5 deletions src/interactive/widgets/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ pub struct MainWindow<'a, 'b, 'c> {
pub traversal: &'a Traversal,
pub display: DisplayOptions,
pub state: &'b AppState,
/// State that can change during drawing, for convenience
pub state_mut: &'c mut WidgetState,
pub widgets: &'c WidgetState,
}

impl<'a, 'b, 'c> Widget for MainWindow<'a, 'b, 'c> {
Expand All @@ -33,7 +32,7 @@ impl<'a, 'b, 'c> Widget for MainWindow<'a, 'b, 'c> {
},
display,
state,
state_mut,
widgets,
} = self;
let regions = Layout::default()
.direction(Direction::Vertical)
Expand All @@ -46,8 +45,7 @@ impl<'a, 'b, 'c> Widget for MainWindow<'a, 'b, 'c> {
display: *display,
sorting: state.sorting,
selected: state.selected,
list_start: state.entries_list_start,
state_mut,
state: widgets,
}
.draw(entries, buf);

Expand Down

0 comments on commit 971e235

Please sign in to comment.