1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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();
}
}