boing 0.7.0

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

//! [`MultilineTextEntry`].

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 MultilineTextEntry, crate::Error> {
                unsafe { call_libui_new_fn!(ui: self, fn: $libui_fn() -> MultilineTextEntry) }
            }
        }
    };
}

impl_text_entry!(create_wrapping_multiline_text_entry uiNewMultilineEntry);
impl_text_entry!(create_non_wrapping_multiline_text_entry uiNewNonWrappingMultilineEntry);

/// A box in which multiple lines of text are displayed.
#[subcontrol(handle = "uiMultilineEntry")]
pub struct MultilineTextEntry;

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

    /// Replaces 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 { uiMultilineEntrySetText(self.as_ptr(), text.as_ptr()) };

        Ok(())
    }

    /// Appends the given text to this entry.
    pub fn push_text(&self, text: impl Into<Vec<u8>>) -> Result<(), crate::Error> {
        let text = self.ui.make_cstring(text)?;
        unsafe { uiMultilineEntryAppend(self.as_ptr(), text.as_ptr()) };

        Ok(())
    }

    /// Sets a callback for when the text within this entry changes.
    #[bind_callback(fn = "uiMultilineEntryOnChanged")]
    pub fn on_changed(&self, f: fn()) {
        f();
    }

    /// Determines if this entry is read-only.
    ///
    /// Multiline text entries are read-write by default.
    #[inline]
    pub fn is_read_only(&self) -> bool {
        bool_from_libui(unsafe { uiMultilineEntryReadOnly(self.as_ptr()) })
    }

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