rcus-core 0.1.2

Core types for rcus: retained view tree, style, input, and the host bridge protocol
Documentation
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Size {
    pub width: f32,
    pub height: f32,
}

impl Size {
    /// Builds a size. A zero component means "not set on this axis", which is
    /// what makes a partial size work from JSON: `{"height": 32}` keeps the
    /// width flexible.
    pub const fn new(width: f32, height: f32) -> Self {
        Self { width, height }
    }

    pub fn width(self, value: f32) -> Self {
        Self {
            width: value,
            ..self
        }
    }

    pub fn height(self, value: f32) -> Self {
        Self {
            width: self.width,
            height: value,
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Rect {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl Rect {
    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    pub fn right(self) -> f32 {
        self.x + self.width
    }

    pub fn bottom(self) -> f32 {
        self.y + self.height
    }

    pub fn contains(self, x: f32, y: f32) -> bool {
        x >= self.x && x < self.right() && y >= self.y && y < self.bottom()
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Insets {
    pub left: f32,
    pub top: f32,
    pub right: f32,
    pub bottom: f32,
}

impl Insets {
    pub const fn all(value: f32) -> Self {
        Self {
            left: value,
            top: value,
            right: value,
            bottom: value,
        }
    }

    pub const fn symmetric(horizontal: f32, vertical: f32) -> Self {
        Self {
            left: horizontal,
            top: vertical,
            right: horizontal,
            bottom: vertical,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Color {
    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
        Self { r, g, b, a }
    }

    pub fn hex(value: u32) -> Self {
        Self::rgba(
            ((value >> 16) & 0xff) as f32 / 255.0,
            ((value >> 8) & 0xff) as f32 / 255.0,
            (value & 0xff) as f32 / 255.0,
            1.0,
        )
    }

    pub const TRANSPARENT: Self = Self {
        r: 0.0,
        g: 0.0,
        b: 0.0,
        a: 0.0,
    };
}

/// Opaque black, the default text color for a style that names none.
impl Default for Color {
    fn default() -> Self {
        Self::rgba(0.0, 0.0, 0.0, 1.0)
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Axis {
    #[default]
    Column,
    Row,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Align {
    #[default]
    Stretch,
    Start,
    Center,
    End,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Justify {
    #[default]
    Start,
    Center,
    End,
    SpaceBetween,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Style {
    pub size: Option<Size>,
    pub min_size: Option<Size>,
    pub max_size: Option<Size>,
    pub padding: Insets,
    pub gap: f32,
    pub flex_grow: f32,
    pub flex_shrink: f32,
    pub align: Align,
    pub justify: Justify,
    pub background: Option<Color>,
    pub clip: bool,
    /// Takes the whole content box on the parent's main axis, which is what a
    /// scrollback panel or a virtual list wants.
    pub fill: bool,
    pub row_height: f32,
    /// Overrides the line box height. `None` derives it from `font_size` and
    /// the real font metrics, which is what a view spec wants.
    pub line_height: Option<f32>,
    /// Font size in pixels, used by text renderers for shaping and metrics.
    pub font_size: f32,
    pub color: Option<Color>,
    pub weight: f32,
}

impl Default for Style {
    fn default() -> Self {
        Self {
            size: None,
            min_size: None,
            max_size: None,
            padding: Insets::default(),
            gap: 0.0,
            flex_grow: 0.0,
            flex_shrink: 1.0,
            align: Align::Stretch,
            justify: Justify::Start,
            background: None,
            clip: false,
            fill: false,
            row_height: 24.0,
            line_height: None,
            font_size: 14.0,
            color: None,
            weight: 400.0,
        }
    }
}

impl Style {
    pub fn size(mut self, size: Size) -> Self {
        self.size = Some(size);
        self
    }

    pub fn width(mut self, value: f32) -> Self {
        let size = self.size.unwrap_or_default();
        self.size = Some(size.width(value));
        self
    }

    pub fn height(mut self, value: f32) -> Self {
        let size = self.size.unwrap_or_default();
        self.size = Some(size.height(value));
        self
    }

    pub fn min_size(mut self, size: Size) -> Self {
        self.min_size = Some(size);
        self
    }

    pub fn max_size(mut self, size: Size) -> Self {
        self.max_size = Some(size);
        self
    }

    pub fn padding(mut self, value: Insets) -> Self {
        self.padding = value;
        self
    }

    pub fn gap(mut self, value: f32) -> Self {
        self.gap = value;
        self
    }

    pub fn flex_grow(mut self, value: f32) -> Self {
        self.flex_grow = value.max(0.0);
        self
    }

    pub fn align(mut self, value: Align) -> Self {
        self.align = value;
        self
    }

    pub fn justify(mut self, value: Justify) -> Self {
        self.justify = value;
        self
    }

    pub fn background(mut self, value: Color) -> Self {
        self.background = Some(value);
        self
    }

    pub fn clip(mut self, value: bool) -> Self {
        self.clip = value;
        self
    }

    pub fn fill(mut self, value: bool) -> Self {
        self.fill = value;
        self
    }

    pub fn row_height(mut self, value: f32) -> Self {
        self.row_height = value.max(1.0);
        self
    }

    /// Pins the line box height instead of deriving it from the font.
    pub fn line_height(mut self, value: f32) -> Self {
        self.line_height = Some(value.max(1.0));
        self
    }

    pub fn font_size(mut self, value: f32) -> Self {
        self.font_size = value.max(1.0);
        self
    }

    /// Resolved line box height for a style, given the font's own line height.
    pub fn resolved_line_height(&self, font_line_height: f32) -> f32 {
        self.line_height
            .unwrap_or_else(|| font_line_height.max(1.0))
    }

    pub fn color(mut self, value: Color) -> Self {
        self.color = Some(value);
        self
    }

    pub fn weight(mut self, value: f32) -> Self {
        self.weight = value;
        self
    }

    /// Resolved size along the given axis, if the style pins a non-zero one.
    pub fn main_size(&self, axis: Axis) -> Option<f32> {
        self.size.and_then(|size| {
            let value = match axis {
                Axis::Column => size.height,
                Axis::Row => size.width,
            };
            (value > 0.0).then_some(value)
        })
    }

    /// Resolved size across the given axis, if the style pins a non-zero one.
    pub fn cross_size(&self, axis: Axis) -> Option<f32> {
        self.size.and_then(|size| {
            let value = match axis {
                Axis::Column => size.width,
                Axis::Row => size.height,
            };
            (value > 0.0).then_some(value)
        })
    }
}