boing 0.7.0

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

//! Colors and color pickers.

use std::ptr::addr_of_mut;

use crate::prelude::*;

impl Ui {
    /// Creates a new [color picker button](Picker).
    pub fn create_color_picker<'ui>(&'ui self) -> Result<&'ui mut Picker, crate::Error> {
        unsafe { call_libui_new_fn!(ui: self, fn: uiNewColorButton() -> Picker) }
    }
}

/// A button that opens a color picker dialog.
#[subcontrol(handle = "uiColorButton")]
pub struct Picker;

/// An RGBA color.
///
/// All components must be normalized to the `[0.0, 1.0]` range.
#[derive(Clone, Copy, Default)]
pub struct Color {
    /// The red component.
    pub red: f64,
    /// The green component.
    pub green: f64,
    /// The blue component.
    pub blue: f64,
    /// The alpha, or transparency, component.
    pub alpha: f64,
}

impl<'ui> Picker<'ui> {
    /// The color currently selected by this picker.
    pub fn selected_color(&self) -> Color {
        // SAFETY: the *libui-ng* documentation does not describe the behavior when this function is
        // called and a color has not yet been selected, and I would rather not guess and invoke UB,
        // so we will default-initialize `color`.
        let mut color = Color::default();
        unsafe {
            uiColorButtonColor(
                self.as_ptr(),
                addr_of_mut!(color.red),
                addr_of_mut!(color.green),
                addr_of_mut!(color.blue),
                addr_of_mut!(color.alpha),
            )
        };

        color
    }

    /// Selects the given color.
    ///
    /// # Panics
    ///
    /// Panics if any components of `color` are not normalized to the `[0.0, 1.0]` range. See
    /// [`select_color_unchecked`] for an unchecked version of this function.
    pub fn select_color(&self, color: Color) {
        let check_component = |comp: f64| {
            assert!((comp >= 0.) && (comp <= 1.), "component outside [0, 1] range");
        };
        check_component(color.red);
        check_component(color.green);
        check_component(color.blue);
        check_component(color.alpha);

        unsafe { self.select_color_unchecked(color) };
    }

    /// Selects the given color without first checking if it is valid.
    ///
    /// # Safety
    ///
    /// All components of `color` must be normalized to the `[0.0, 1.0]` range.
    pub unsafe fn select_color_unchecked(&self, color: Color) {
        uiColorButtonSetColor(
            self.as_ptr(),
            color.red,
            color.green,
            color.blue,
            color.alpha,
        );
    }

    /// Sets a callback for when the user selects a new color.
    ///
    /// This callback is unset by default. This is not activated when
    /// [`select_color`](Self::select_color) or
    /// [`select_color_unchecked`](Self::select_color_unchecked) is called.
    #[bind_callback(fn = "uiColorButtonOnChanged")]
    pub fn on_selected(&self, f: fn()) {
        f();
    }
}