From b3709af150870e6b35263d93d95a3f454d491852 Mon Sep 17 00:00:00 2001 From: Takeshi Tsukamoto Date: Thu, 25 Apr 2024 00:37:24 +0900 Subject: [PATCH] Manage state in component --- src/components/devices.rs | 14 +++++--------- src/components/inspector.rs | 19 +++++++------------ src/components/logs.rs | 5 +++-- src/components/network.rs | 5 +++-- src/components/performance.rs | 5 +++-- src/components/pubspec.rs | 6 ++++-- src/widgets/tree.rs | 6 ++++-- 7 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/components/devices.rs b/src/components/devices.rs index 4eb16a1..8fb381f 100644 --- a/src/components/devices.rs +++ b/src/components/devices.rs @@ -20,19 +20,15 @@ use daemon::flutter::FlutterDaemon; use super::Component; +#[derive(Default)] pub struct DevicesComponent { action_tx: Option>, -} - -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<()> { @@ -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") @@ -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 { diff --git a/src/components/inspector.rs b/src/components/inspector.rs index c6a653c..021cce3 100644 --- a/src/components/inspector.rs +++ b/src/components/inspector.rs @@ -19,19 +19,15 @@ use daemon::flutter::FlutterDaemon; use super::Component; +#[derive(Default)] pub struct InspectorComponent { action_tx: Option>, -} - -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 { @@ -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); } } diff --git a/src/components/logs.rs b/src/components/logs.rs index a9c5087..5fd7967 100644 --- a/src/components/logs.rs +++ b/src/components/logs.rs @@ -24,6 +24,7 @@ use super::Component; pub struct LogsComponent { action_tx: Option>, wrapped_logs: HashMap>, + state: ListState, } impl LogsComponent { @@ -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; @@ -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 { diff --git a/src/components/network.rs b/src/components/network.rs index b7e34ad..464ce53 100644 --- a/src/components/network.rs +++ b/src/components/network.rs @@ -19,6 +19,7 @@ use super::Component; #[derive(Default)] pub struct NetworkComponent { action_tx: Option>, + state: TableState, } impl NetworkComponent { @@ -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), @@ -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 { diff --git a/src/components/performance.rs b/src/components/performance.rs index c429b53..2603276 100644 --- a/src/components/performance.rs +++ b/src/components/performance.rs @@ -19,6 +19,7 @@ use super::Component; #[derive(Default)] pub struct PerformanceComponent { action_tx: Option>, + state: ListState, } impl PerformanceComponent { @@ -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; @@ -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 { diff --git a/src/components/pubspec.rs b/src/components/pubspec.rs index 4bde1fb..13f7e58 100644 --- a/src/components/pubspec.rs +++ b/src/components/pubspec.rs @@ -23,6 +23,7 @@ use super::Component; pub struct PubspecComponent { pub project_root: PathBuf, pub lines: Vec>, + state: ListState, scroll_poition: usize, } @@ -32,6 +33,7 @@ impl PubspecComponent { project_root, lines: vec![], scroll_poition: 0, + state: ListState::default(), } } } @@ -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() @@ -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 { diff --git a/src/widgets/tree.rs b/src/widgets/tree.rs index 03e9744..7380402 100644 --- a/src/widgets/tree.rs +++ b/src/widgets/tree.rs @@ -6,6 +6,7 @@ use ratatui::widgets::{Block, List, ListItem, ListState, StatefulWidgetRef, Widg pub struct TreeState { pub selected: Option, pub opened: HashSet, + list_state: ListState, } impl Default for TreeState { @@ -19,6 +20,7 @@ impl TreeState { TreeState { selected: None, opened: HashSet::new(), + list_state: ListState::default(), } } @@ -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); } }