boing 0.7.0

A safe wrapper over libui-ng-sys
Documentation
// SPDX-License-Identifier: MPL-2.0

//! A togglable button with adjacent customizable text.

use crate::prelude::*;

impl Ui {
    /// Creates a new [`Checkbox`] with the given adjacent text.
    pub fn create_checkbox<'ui>(
        &'ui self,
        text: impl Into<Vec<u8>>,
    ) -> Result<&'ui mut Checkbox, crate::Error> {
        let text = self.make_cstring(text)?;

        unsafe { call_libui_new_fn!(ui: self, fn: uiNewCheckbox(text.as_ptr()) -> Checkbox) }
    }
}

/// A togglable button with adjacent customizable text.
///
/// Checkboxes are used to communicate boolean information such as true or false and yes or no. They
/// are conventionally rendered as a square that contains a checkmark when encoding a true state and
/// which is empty when false.
#[subcontrol(handle = "uiCheckbox")]
pub struct Checkbox;

impl<'ui> Checkbox<'ui> {
    /// The text displayed adjacent to this checkbox.
    #[bind_text(fn = "uiCheckboxText")]
    pub fn text(&self) -> _;

    /// Sets the text displayed adjacent to this checkbox.
    ///
    /// This overrides the text supplied to [`Ui::create_checkbox`].
    pub fn set_text(&self, text: impl Into<Vec<u8>>) -> Result<(), crate::Error> {
        let text = self.ui.make_cstring(text)?;
        unsafe { uiCheckboxSetText(self.as_ptr(), text.as_ptr()) };

        Ok(())
    }

    /// Determines if this checkbox is checked.
    ///
    /// Checkboxes are unchecked by default.
    pub fn is_checked(&self) -> bool {
        bool_from_libui(unsafe { uiCheckboxChecked(self.as_ptr()) })
    }

    /// Sets whether or not this checkbox is checked.
    ///
    /// This overrides the state set by the user.
    pub fn set_checked(&self, value: bool) {
        unsafe { uiCheckboxSetChecked(self.as_ptr(), value.into()) };
    }

    /// Sets a callback for when the user toggles this checkbox.
    ///
    /// This callback is unset by default. This is not activated when
    /// [`set_checked`](Self::set_checked) is called.
    #[bind_callback(fn = "uiCheckboxOnToggled")]
    pub fn on_toggled(&self, f: fn()) {
        f();
    }
}