godoru 0.1.0

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

pub struct TextInputBuilder<'a, A = NoAction> {
    ui: &'a mut Ui<A>,
    textInput: Option<TextInput<A>>,
}

impl<'a, A: Clone> TextInputBuilder<'a, A> {
    pub(crate) fn new(ui: &'a mut Ui<A>, value: impl Into<String>) -> Self {
        Self {
            ui,
            textInput: Some(TextInput::new(value)),
        }
    }

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

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

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

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

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

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

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

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

    pub fn onFocus(mut self, action: A) -> Self {
        if let Some(textInput) = self.textInput.as_mut() {
            textInput.interactions.focus = Some(action);
        }
        self
    }

    pub fn onBlur(mut self, action: A) -> Self {
        if let Some(textInput) = self.textInput.as_mut() {
            textInput.interactions.blur = Some(action);
        }
        self
    }
}

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