diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index 8a990f3a..afc4912e 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -10,7 +10,7 @@ use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock}; use crate::ui::theme::style; use crate::ui::{RENDER_WIDTH_LARGE, RENDER_WIDTH_MEDIUM, RENDER_WIDTH_SMALL}; -use super::{PredefinedLayout, RenderProps, View, ViewProps, Widget}; +use super::{PredefinedLayout, RenderProps, View, ViewProps, ViewState, Widget}; #[derive(Clone, Debug, Default)] pub struct ColumnView { @@ -581,14 +581,17 @@ where } } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct SectionGroupState { /// Index of currently focused section. - focus: Option, + pub focus: Option, } #[derive(Clone, Default)] pub struct SectionGroupProps { + /// Index of currently focused section. If set, it will override the widgets' + /// internal state. + focus: Option, /// If this pages' keys should be handled. handle_keys: bool, /// Section layout @@ -605,6 +608,11 @@ impl SectionGroupProps { self.layout = layout; self } + + pub fn focus(mut self, focus: Option) -> Self { + self.focus = focus; + self + } } pub struct SectionGroup { @@ -685,10 +693,19 @@ where None } - fn update(&mut self, _props: Option<&ViewProps>, state: &Self::State) { + fn update(&mut self, props: Option<&ViewProps>, state: &Self::State) { + let default = SectionGroupProps::default(); + let props = props + .and_then(|props| props.inner_ref::()) + .unwrap_or(&default); + for section in &mut self.sections { section.update(state); } + + if props.focus.is_some() && props.focus != self.state.focus { + self.state.focus = props.focus; + } } fn render(&mut self, props: Option<&ViewProps>, render: RenderProps, frame: &mut Frame) { @@ -711,4 +728,8 @@ where } } } + + fn view_state(&self) -> Option { + Some(ViewState::SectionGroup(self.state.clone())) + } }