godoru 0.1.0

UI Framework for Rust using Godot
use super::super::{Checkbox, NoAction, Style, Ui, UiNode};

pub struct CheckboxBuilder<'a, A = NoAction> {
    ui: &'a mut Ui<A>,
    checkbox: Option<Checkbox<A>>,
}

impl<'a, A: Clone> CheckboxBuilder<'a, A> {
    pub(crate) fn new(ui: &'a mut Ui<A>, checked: bool) -> Self {
        Self {
            ui,
            checkbox: Some(Checkbox::new(checked)),
        }
    }

    pub fn id(mut self, id: impl Into<String>) -> Self {
        if let Some(checkbox) = self.checkbox.as_mut() {
            checkbox.id = Some(id.into());
        }
        self
    }

    pub fn key(mut self, key: impl Into<String>) -> Self {
        if let Some(checkbox) = self.checkbox.as_mut() {
            checkbox.key = Some(key.into());
        }
        self
    }

    pub fn class(mut self, class: impl Into<String>) -> Self {
        if let Some(checkbox) = self.checkbox.as_mut() {
            checkbox.class = Some(class.into());
        }
        self
    }

    pub fn text(mut self, text: impl Into<String>) -> Self {
        if let Some(checkbox) = self.checkbox.as_mut() {
            checkbox.text = Some(text.into());
        }
        self
    }

    pub fn customMinimumSize(mut self, width: u32, height: u32) -> Self {
        if let Some(checkbox) = self.checkbox.as_mut() {
            checkbox.customMinimumSize = Some((width, height));
        }
        self
    }

    pub fn style(mut self, configure: impl FnOnce(&mut Style)) -> Self {
        if let Some(checkbox) = self.checkbox.as_mut() {
            configure(&mut checkbox.style);
        }
        self
    }

    pub fn onToggle(mut self, action: impl Fn(bool) -> A + 'static) -> Self {
        if let Some(checkbox) = self.checkbox.as_mut() {
            checkbox.onToggle = Some(std::sync::Arc::new(action));
        }
        self
    }
}

impl<A> Drop for CheckboxBuilder<'_, A> {
    fn drop(&mut self) {
        if let Some(checkbox) = self.checkbox.take() {
            self.ui.nodes.push(UiNode::Checkbox(checkbox));
        }
    }
}