boing 0.7.0

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

//! A labeled container around a child control.

use crate::prelude::*;

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

        unsafe { call_libui_new_fn!(ui: self, fn: uiNewGroup(title.as_ptr()) -> Group) }
    }
}

/// A labeled container around a child control.
///
/// Groups enclose a single child control, providing a descriptive title of the contents. They
/// are traditionally rendered as a rectangular border with an inline label. A group [can
/// optionally be margined](Self::set_margined), in which case empty space is inserted outside
/// the border.
#[subcontrol(handle = "uiGroup")]
pub struct Group;

impl<'ui> Group<'ui> {
    /// The title of this group.
    #[bind_text(fn = "uiGroupTitle")]
    pub fn title(&self) -> _;

    /// Sets the title of this group.
    ///
    /// This overrides the title supplied to [`Ui::create_group`].
    pub fn set_title(&self, title: impl Into<Vec<u8>>) -> Result<(), crate::Error> {
        let title = self.ui.make_cstring(title)?;
        unsafe { uiGroupSetTitle(self.as_ptr(), title.as_ptr()) };

        Ok(())
    }

    /// Sets the child control of this group.
    ///
    /// This is unset by default.
    #[bind_set_child(fn = "uiGroupSetChild")]
    pub fn set_child(&self, ...) -> _;

    /// Clears the child control of this group if one is set.
    #[inline]
    pub fn clear(&self) {
        unsafe { uiGroupSetChild(self.as_ptr(), std::ptr::null_mut()) };
    }

    /// Determines if this group has margins.
    ///
    /// Groups are not margined by default.
    #[inline]
    pub fn is_margined(&self) -> bool {
        bool_from_libui(unsafe { uiGroupMargined(self.as_ptr()) })
    }

    /// Sets whether or not this group has margins.
    #[inline]
    pub fn set_margined(&self, value: bool) {
        unsafe { uiGroupSetMargined(self.as_ptr(), value.into()) };
    }
}