boing 0.7.0

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

//! A box in which a single line of text is displayed.

use crate::prelude::*;

macro_rules! impl_text_entry {
    ($self_fn:ident $libui_fn:ident) => {
        impl Ui {
            pub fn $self_fn<'ui>(&'ui self) -> Result<&'ui mut TextEntry, crate::Error> {
                unsafe { call_libui_new_fn!(ui: self, fn: $libui_fn() -> TextEntry) }
            }
        }
    };
}

impl_text_entry!(create_text_entry uiNewEntry);
impl_text_entry!(create_password_text_entry uiNewPasswordEntry);
impl_text_entry!(create_search_text_entry uiNewSearchEntry);

/// A box in which a single line of text is displayed.
#[subcontrol(handle = "uiEntry")]
pub struct TextEntry;

impl<'ui> TextEntry<'ui> {
    /// The text displayed in this entry.
    #[bind_text(fn = "uiEntryText")]
    pub fn text(&self) -> _;

    /// Sets the text displayed in this entry.
    pub fn set_text(&self, text: impl Into<Vec<u8>>) -> Result<(), crate::Error> {
        let text = self.ui.make_cstring(text)?;
        unsafe { uiEntrySetText(self.as_ptr(), text.as_ptr()) };

        Ok(())
    }

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

    /// Determines if this entry is read-only.
    #[inline]
    pub fn is_read_only(&self) -> bool {
        bool_from_libui(unsafe { uiEntryReadOnly(self.as_ptr()) })
    }

    /// Sets whether or not this entry is read-only.
    #[inline]
    pub fn set_read_only(&self, value: bool) {
        unsafe { uiEntrySetReadOnly(self.as_ptr(), value.into()) };
    }
}