Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Section groups expose their focus state
Erik Kundt committed 1 year ago
commit de16d5e41a5401ffee60070379bb1b4a1d4e727a
parent e4297b3f2ad489f753655b5f09504a76a9aff89f
1 file changed +25 -4
modified 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<usize>,
+
    pub focus: Option<usize>,
}

#[derive(Clone, Default)]
pub struct SectionGroupProps {
+
    /// Index of currently focused section. If set, it will override the widgets'
+
    /// internal state.
+
    focus: Option<usize>,
    /// 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<usize>) -> Self {
+
        self.focus = focus;
+
        self
+
    }
}

pub struct SectionGroup<S, M> {
@@ -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::<SectionGroupProps>())
+
            .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<super::ViewState> {
+
        Some(ViewState::SectionGroup(self.state.clone()))
+
    }
}