Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Add predefined layouts and use it for section groups
Erik Kundt committed 1 year ago
commit f12b1fb97d9680285493f312ccb83104adc1f056
parent dba75b31f5225b029bd1305ecf26b5ceeb6d5dee
2 files changed +52 -3
modified src/ui/widget.rs
@@ -6,6 +6,7 @@ pub mod utils;
pub mod window;

use std::any::Any;
+
use std::rc::Rc;

use tokio::sync::mpsc::UnboundedSender;

@@ -80,6 +81,42 @@ impl ViewState {
    }
}

+
#[derive(Clone, Default)]
+
pub enum PredefinedLayout {
+
    #[default]
+
    None,
+
    Expandable3,
+
}
+

+
impl PredefinedLayout {
+
    pub fn split(&self, area: Rect) -> Rc<[Rect]> {
+
        match self {
+
            Self::Expandable3 => {
+
                if area.width <= 140 {
+
                    let [left, right] = Layout::horizontal([
+
                        Constraint::Percentage(50),
+
                        Constraint::Percentage(50),
+
                    ])
+
                    .areas(area);
+
                    let [right_top, right_bottom] =
+
                        Layout::vertical([Constraint::Percentage(50), Constraint::Percentage(50)])
+
                            .areas(right);
+

+
                    [left, right_top, right_bottom].into()
+
                } else {
+
                    Layout::horizontal([
+
                        Constraint::Percentage(33),
+
                        Constraint::Percentage(33),
+
                        Constraint::Percentage(33),
+
                    ])
+
                    .split(area)
+
                }
+
            }
+
            _ => Layout::default().split(area),
+
        }
+
    }
+
}
+

/// General properties that specify how a `View` is rendered.
#[derive(Clone, Default)]
pub struct RenderProps {
modified src/ui/widget/container.rs
@@ -9,7 +9,7 @@ use ratatui::widgets::{Block, BorderType, Borders, Row};
use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock};
use crate::ui::theme::style;

-
use super::{RenderProps, View, ViewProps, Widget};
+
use super::{PredefinedLayout, RenderProps, View, ViewProps, Widget};

#[derive(Clone, Debug)]
pub struct Column<'a> {
@@ -389,6 +389,8 @@ pub struct SectionGroupState {
pub struct SectionGroupProps {
    /// If this pages' keys should be handled.
    handle_keys: bool,
+
    /// Section layout
+
    layout: PredefinedLayout,
}

impl SectionGroupProps {
@@ -396,6 +398,11 @@ impl SectionGroupProps {
        self.handle_keys = handle_keys;
        self
    }
+

+
    pub fn layout(mut self, layout: PredefinedLayout) -> Self {
+
        self.layout = layout;
+
        self
+
    }
}

pub struct SectionGroup<S, M> {
@@ -482,8 +489,13 @@ where
        }
    }

-
    fn render(&self, _props: Option<&ViewProps>, render: RenderProps, frame: &mut Frame) {
-
        let areas = render.layout.split(render.area);
+
    fn render(&self, props: Option<&ViewProps>, render: RenderProps, frame: &mut Frame) {
+
        let default = SectionGroupProps::default();
+
        let props = props
+
            .and_then(|props| props.inner_ref::<SectionGroupProps>())
+
            .unwrap_or(&default);
+

+
        let areas = props.layout.split(render.area);

        for (index, area) in areas.iter().enumerate() {
            if let Some(section) = self.sections.get(index) {