boing 0.7.0

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

//! A clickable button containing customizable text.

use crate::prelude::*;

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

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

/// A clickable button containing customizable text.
#[subcontrol(handle = "uiButton")]
pub struct Pushbutton;

impl<'ui> Pushbutton<'ui> {
    /// The text displayed within this pushbutton.
    #[bind_text(fn = "uiButtonText")]
    pub fn text(&self) -> _;

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

        Ok(())
    }

    /// Sets a callback for when the user clicks this pushbutton.
    ///
    /// This callback is unset by default.
    #[bind_callback(fn = "uiButtonOnClicked")]
    pub fn on_clicked(&self, f: fn()) {
        f();
    }
}