boing 0.7.0

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

//! [`Spinbox`].

use crate::prelude::*;

impl Ui {
    /// Creates a new [`Spinbox`].
    ///
    /// # Arguments
    ///
    /// `min` is the initial minimum value and `max` is the initial maximum value of the slider.
    ///
    /// # Panics
    ///
    /// Panics if `min` is greater than `max`.
    pub fn create_spinbox<'ui>(
        &'ui self,
        min: impl Into<NonNegativeInt>,
        max: impl Into<NonNegativeInt>,
    ) -> Result<&'ui mut Spinbox, crate::Error> {
        let min = min.into();
        let max = max.into();
        assert!(min <= max, "minimum must be less than or equal to maximum");

        unsafe {
            call_libui_new_fn!(
                ui: self,
                fn: uiNewSpinbox(min.to_libui(), max.to_libui()) -> Spinbox,
            )
        }
    }
}

#[subcontrol(handle = "uiSpinbox")]
pub struct Spinbox;

impl<'ui> Spinbox<'ui> {
    /// The current value of this spinbox.
    #[inline]
    pub fn value(&self) -> NonNegativeInt {
        unsafe { NonNegativeInt::from_libui(uiSpinboxValue(self.as_ptr())) }
    }

    /// Sets the value of this spinbox.
    #[inline]
    pub fn set_value(&self, value: impl Into<NonNegativeInt>) {
        // Note: *libui-ng* documentation states that out-of-range values are clamped to the nearest
        // in range, so we don't have to clamp it ourselves.
        unsafe { uiSpinboxSetValue(self.as_ptr(), value.into().to_libui()) };
    }

    /// Sets a callback for when this spinbox changes.
    ///
    /// This callback is unset by default. This is not activated when [`set_value`](Self::set_value)
    /// is called.
    #[bind_callback(fn = "uiSpinboxOnChanged")]
    pub fn on_changed(&self, f: fn()) {
        f();
    }
}