Skip to content
Snippets Groups Projects
panel.rs 2.42 KiB
Newer Older
Louis's avatar
Louis committed
use bevy::prelude::*;
use kayak_ui::prelude::*;

use crate::parent_widget;
use crate::theme::tokens::THEME_NINEPATCH_PANEL_DEFAULT;
use crate::theme::ThemeProvider;
use crate::{px, value};

#[derive(Default, Copy, Clone, Eq, PartialEq)]
pub enum PanelVariant {
    #[default]
    Regular,
    Simple,
}

#[derive(Component, Default, Clone, PartialEq)]
pub struct PanelProps {
    pub background: Option<String>,
    pub inner_layout: StyleProp<LayoutType>,
}

impl Widget for PanelProps {}

parent_widget!(PanelProps => PanelWidget);

pub fn render_panel_widget(
    In(entity): In<Entity>,
    widget_context: Res<KayakWidgetContext>,
    mut commands: Commands,
    mut query: Query<(&PanelProps, &KChildren, &mut ComputedStyles, &KStyle)>,
    theme_provider: ThemeProvider,
) -> bool {
    if let Ok((props, children, mut computed, style)) = query.get_mut(entity) {
        let parent_id = Some(entity);
        let patch = theme_provider
            .get_patch(
                props
                    .background
                    .clone()
                    .unwrap_or_else(|| String::from(THEME_NINEPATCH_PANEL_DEFAULT)),
            )
            .unwrap();

        *computed = KStyle {
            render_command: value(RenderCommand::NinePatch {
                handle: patch.handle.clone_weak(),
                border: patch.border,
                scale: 1.0,
            }),
            min_height: px(patch.border.bottom + patch.border.top + 8.0),
            min_width: px(patch.border.left + patch.border.right + 8.0),
            ..Default::default()
        }
            .with_style(style)
            .with_style(KStyle {
                padding: value(Edge::new(
                    Units::Pixels(patch.border.top),
                    Units::Pixels(patch.border.right),
                    Units::Pixels(patch.border.bottom),
                    Units::Pixels(patch.border.left),
                )),
                ..Default::default()
            })
            .into();

        let inner_style = match &style.padding {
            StyleProp::Unset => KStyle {
                layout_type: props.inner_layout.clone(),
                ..Default::default()
            },
            pad => KStyle {
                layout_type: props.inner_layout.clone(),
                padding: pad.clone(),
                ..Default::default()
            },
        };

        children.process(&widget_context, &mut commands, Some(entity));
    }
    true
}