blit-widget 0.1.0

built-in widgets for blit
Documentation
use blit::{
    Axis, LayoutCx, Scalar, Scope, Size, Ui,
    state::{NoLayout, Open},
};

use crate::{Context, interact};

blit::builder! {
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    pub struct ResizeState<T> {
        new(),
        #[into]
        size: Option<Size<T>> = None,
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ResizeEdge {
    Right,
    Bottom,
    Corner,
}

/// what a grip builder needs to draw itself
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ResizeGrip {
    pub edge: ResizeEdge,
    pub hovered: bool,
    /// pressed or being dragged
    pub active: bool,
}

blit::builder! {
    pub struct Resize<'a, T: Scalar> {
        new(state: &'a mut ResizeState<T>),
        grip_size: Size<T> = Size::uniform(T::from_f32(1.0)),
    }
}

impl<T: Scalar> Resize<'_, T> {
    pub fn begin<'a, C, S>(self, mut ui: Ui<'a, C, S>) -> ResizeContent<'a, C>
    where
        C: Context<Scalar = T>,
        S: NoLayout,
    {
        let state = self.state;
        let previous = ui.area();
        let scope = ui.scope();
        let mut grip_input = |key: u8, edge| {
            let id = ui.resolve(scope, key);
            let mut part = ui.node(id);
            let (press, drag) = (part.interact(interact::press()), part.interact(interact::drag()));
            let grip = ResizeGrip {
                edge,
                hovered: press.hovered,
                active: press.active || drag.active,
            };
            (grip, drag.delta)
        };
        let (right, right_delta) = grip_input(1, ResizeEdge::Right);
        let (bottom, bottom_delta) = grip_input(2, ResizeEdge::Bottom);
        let (corner, corner_delta) = grip_input(3, ResizeEdge::Corner);
        let delta = Size::new(right_delta.x + corner_delta.x, bottom_delta.y + corner_delta.y);
        if delta != Size::ZERO {
            let base = previous.map(|area| area.size()).or(state.size).unwrap_or(Size::ZERO);
            let next = base + delta;
            state.size = Some(Size::new(next.width.max(T::ZERO), next.height.max(T::ZERO)));
        }
        let shell = ui.layout(Layout {
            size: state.size,
            grip_size: self.grip_size,
        });
        ResizeContent(ResizeGrips {
            shell,
            scope,
            grips: [right, bottom, corner],
        })
    }
}

#[must_use]
pub struct ResizeContent<'a, C: Context>(ResizeGrips<'a, C>);

impl<'a, C: Context> ResizeContent<'a, C> {
    pub fn content(mut self, content: impl FnOnce(Ui<'_, C>)) -> ResizeGrips<'a, C> {
        self.0.shell.child().item(Item::Content).build(content);
        self.0
    }
}

#[must_use]
pub struct ResizeGrips<'a, C: Context> {
    shell: Ui<'a, C, Open<Layout<C::Scalar>>>,
    scope: Scope,
    grips: [ResizeGrip; 3],
}

impl<C: Context> ResizeGrips<'_, C> {
    pub fn grip(mut self, mut grip: impl FnMut(Ui<'_, C>, ResizeGrip)) {
        for (item, grip_id, handle) in [
            (Item::Right, 1, self.grips[0]),
            (Item::Bottom, 2, self.grips[1]),
            (Item::Corner, 3, self.grips[2]),
        ] {
            self.shell
                .child_in(self.scope, grip_id)
                .item(item)
                .build(|ui: Ui<'_, C>| grip(ui, handle));
        }
    }
}

#[derive(Clone, Copy, Default)]
enum Item {
    #[default]
    Content,
    Right,
    Bottom,
    Corner,
}

#[derive(Clone, Copy)]
struct Layout<T> {
    size: Option<Size<T>>,
    grip_size: Size<T>,
}

impl<C: Context<Scalar = T>, T: Scalar> blit::Layout<C> for Layout<T> {
    type Item = Item;

    fn fit(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis) -> blit::Fit<T> {
        if let Some(size) = self.size {
            return blit::Fit::exact(axis.extent(size).max(T::ZERO));
        }
        cx.children()
            .find(|&child| matches!(*cx.item(child), Item::Content))
            .map_or(blit::Fit::ZERO, |child| cx.fit(child, axis))
    }

    fn distribute(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis, extent: T) {
        let grip = axis.extent(self.grip_size).max(T::ZERO).min(extent);
        for child in cx.children() {
            let (assigned, offset, z_index) = match (*cx.item(child), axis) {
                (Item::Content, _) => (extent, T::ZERO, 0),
                (Item::Right, Axis::Horizontal) | (Item::Bottom, Axis::Vertical) => (grip, extent - grip, 1),
                (Item::Right, Axis::Vertical) | (Item::Bottom, Axis::Horizontal) => (extent, T::ZERO, 1),
                (Item::Corner, _) => (grip, extent - grip, 2),
            };
            cx.set_extent(child, assigned);
            cx.set_offset(child, offset);
            cx.set_child_z_index(child, z_index);
        }
    }
}