Skip to content

Commit

Permalink
Manage state in component
Browse files Browse the repository at this point in the history
  • Loading branch information
itome committed Apr 24, 2024
1 parent eac49ad commit b3709af
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 31 deletions.
14 changes: 5 additions & 9 deletions src/components/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ use daemon::flutter::FlutterDaemon;

use super::Component;

#[derive(Default)]
pub struct DevicesComponent {
action_tx: Option<UnboundedSender<ActionOrThunk>>,
}

impl Default for DevicesComponent {
fn default() -> Self {
Self::new()
}
state: ListState,
}

impl DevicesComponent {
pub fn new() -> Self {
Self { action_tx: None }
Self::default()
}

fn next(&self) -> Result<()> {
Expand Down Expand Up @@ -100,7 +96,7 @@ impl Component for DevicesComponent {
} else {
None
};
let mut list_state = ListState::default().with_selected(selected_index);
self.state.select(selected_index);

let block = Block::default()
.title("Devices")
Expand Down Expand Up @@ -132,7 +128,7 @@ impl Component for DevicesComponent {
.highlight_spacing(HighlightSpacing::Never)
.block(block);

f.render_stateful_widget(list, area, &mut list_state);
f.render_stateful_widget(list, area, &mut self.state);
f.render_stateful_widget(
scrollbar,
area.inner(&Margin {
Expand Down
19 changes: 7 additions & 12 deletions src/components/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@ use daemon::flutter::FlutterDaemon;

use super::Component;

#[derive(Default)]
pub struct InspectorComponent {
action_tx: Option<UnboundedSender<ActionOrThunk>>,
}

impl Default for InspectorComponent {
fn default() -> Self {
Self::new()
}
state: TreeState,
}

impl InspectorComponent {
pub fn new() -> Self {
Self { action_tx: None }
Self::default()
}

fn item_builder(item: &DiagnosticNode) -> Node {
Expand Down Expand Up @@ -215,13 +211,12 @@ impl Component for InspectorComponent {
},
);

let mut state = TreeState::new()
.with_opened(session.opened_widget_value_ids.clone())
.with_selected(session.selected_widget_value_id.clone());
self.state.opened = session.opened_widget_value_ids.clone();
self.state.selected = session.selected_widget_value_id.clone();
if let Some(selected_widget_value_id) = session.selected_widget_value_id.as_ref() {
*state.selected_mut() = Some(selected_widget_value_id.clone());
*self.state.selected_mut() = Some(selected_widget_value_id.clone());
}

f.render_stateful_widget(tree, area, &mut state);
f.render_stateful_widget(tree, area, &mut self.state);
}
}
5 changes: 3 additions & 2 deletions src/components/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use super::Component;
pub struct LogsComponent {
action_tx: Option<UnboundedSender<ActionOrThunk>>,
wrapped_logs: HashMap<String, Vec<String>>,
state: ListState,
}

impl LogsComponent {
Expand Down Expand Up @@ -95,7 +96,7 @@ impl Component for LogsComponent {
return;
};
let selected_index = session.selected_log_index.unwrap_or(0) as usize;
let mut list_state = ListState::default().with_selected(Some(selected_index));
self.state.select(Some(selected_index));
let should_wrap_text = state.focus == Focus::DevTools(DevTools::App);
let log_width = area.width as usize - 4;

Expand Down Expand Up @@ -173,7 +174,7 @@ impl Component for LogsComponent {
})
.highlight_spacing(HighlightSpacing::Never);

f.render_stateful_widget(list, area, &mut list_state);
f.render_stateful_widget(list, area, &mut self.state);
f.render_stateful_widget(
scrollbar,
area.inner(&Margin {
Expand Down
5 changes: 3 additions & 2 deletions src/components/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::Component;
#[derive(Default)]
pub struct NetworkComponent {
action_tx: Option<UnboundedSender<ActionOrThunk>>,
state: TableState,
}

impl NetworkComponent {
Expand Down Expand Up @@ -109,7 +110,7 @@ impl Component for NetworkComponent {
} else {
None
};
let mut table_state = TableState::default().with_selected(selected_index);
self.state.select(selected_index);

let widths = [
Constraint::Length(7),
Expand Down Expand Up @@ -192,7 +193,7 @@ impl Component for NetworkComponent {
)
.highlight_spacing(HighlightSpacing::Never);

f.render_stateful_widget(table, area, &mut table_state);
f.render_stateful_widget(table, area, &mut self.state);
f.render_stateful_widget(
scrollbar,
area.inner(&Margin {
Expand Down
5 changes: 3 additions & 2 deletions src/components/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::Component;
#[derive(Default)]
pub struct PerformanceComponent {
action_tx: Option<UnboundedSender<ActionOrThunk>>,
state: ListState,
}

impl PerformanceComponent {
Expand Down Expand Up @@ -95,7 +96,7 @@ impl Component for PerformanceComponent {
.frames
.iter()
.position(|f| Some(f.number) == session.selected_frame_number);
let mut list_state = ListState::default().with_selected(selected_index);
self.state.select(selected_index);

let lines = session.frames.iter().map(|frame| {
let target_ms_per_frame = 1000 / session.display_refresh_rate as u128;
Expand All @@ -122,7 +123,7 @@ impl Component for PerformanceComponent {
})
.highlight_spacing(HighlightSpacing::Never);

f.render_stateful_widget(text, area, &mut list_state);
f.render_stateful_widget(text, area, &mut self.state);
f.render_stateful_widget(
scrollbar,
area.inner(&Margin {
Expand Down
6 changes: 4 additions & 2 deletions src/components/pubspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use super::Component;
pub struct PubspecComponent {
pub project_root: PathBuf,
pub lines: Vec<Vec<(String, Style)>>,
state: ListState,
scroll_poition: usize,
}

Expand All @@ -32,6 +33,7 @@ impl PubspecComponent {
project_root,
lines: vec![],
scroll_poition: 0,
state: ListState::default(),
}
}
}
Expand Down Expand Up @@ -100,7 +102,7 @@ impl Component for PubspecComponent {
);
});

let mut list_state = ListState::default().with_selected(Some(self.scroll_poition));
self.state.select(Some(self.scroll_poition));
let mut scrollbar_state = ScrollbarState::new(lines.len()).position(self.scroll_poition);
let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight);
let block = Block::default()
Expand All @@ -114,7 +116,7 @@ impl Component for PubspecComponent {
.highlight_style(Style::default().bg(Color::DarkGray))
.highlight_spacing(HighlightSpacing::Never);

f.render_stateful_widget(text, area, &mut list_state);
f.render_stateful_widget(text, area, &mut self.state);
f.render_stateful_widget(
scrollbar,
area.inner(&Margin {
Expand Down
6 changes: 4 additions & 2 deletions src/widgets/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ratatui::widgets::{Block, List, ListItem, ListState, StatefulWidgetRef, Widg
pub struct TreeState {
pub selected: Option<String>,
pub opened: HashSet<String>,
list_state: ListState,
}

impl Default for TreeState {
Expand All @@ -19,6 +20,7 @@ impl TreeState {
TreeState {
selected: None,
opened: HashSet::new(),
list_state: ListState::default(),
}
}

Expand Down Expand Up @@ -178,12 +180,12 @@ impl<'a> StatefulWidgetRef for Tree<'a> {
if let Some(block) = &self.block {
list = list.block(block.clone());
}
let mut list_state = ListState::default().with_selected(
state.list_state.select(
lines
.iter()
.position(|line| state.selected == Some(line.0.clone())),
);
StatefulWidgetRef::render_ref(&list, area, buf, &mut list_state);
StatefulWidgetRef::render_ref(&list, area, buf, &mut state.list_state);
}
}

Expand Down

0 comments on commit b3709af

Please sign in to comment.