From 071559746ef04d154468025c0bb8e6d25fcbf2a0 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 3 Dec 2024 09:32:34 +0900 Subject: [PATCH] feat: add text methods for checkbox --- libui/src/controls/checkbox.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/libui/src/controls/checkbox.rs b/libui/src/controls/checkbox.rs index 548285f..d0f6053 100644 --- a/libui/src/controls/checkbox.rs +++ b/libui/src/controls/checkbox.rs @@ -1,3 +1,4 @@ +use std::ffi::{CStr, CString}; use super::Control; use callback_helpers::{from_void_ptr, to_heap_ptr}; use std::i32; @@ -48,4 +49,26 @@ impl Checkbox { ); } } + + /// Get a copy of the existing text on the checkbox. + pub fn text(&self) -> String { + unsafe { + CStr::from_ptr(libui_ffi::uiCheckboxText(self.uiCheckbox)) + .to_string_lossy() + .into_owned() + } + } + + /// Get a reference to the existing text on the checkbox. + pub fn text_ref(&self) -> &CStr { + unsafe { CStr::from_ptr(libui_ffi::uiCheckboxText(self.uiCheckbox)) } + } + + /// Set the text label on the checkbox + pub fn set_text(&mut self, text: &str) { + unsafe { + let c_string = CString::new(text.as_bytes().to_vec()).unwrap(); + libui_ffi::uiCheckboxSetText(self.uiCheckbox, c_string.as_ptr()) + } + } }