Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ pub struct Id {
impl Id {
/// Creates a clay id using the `label`
#[inline]
pub(crate) fn new(label: &str) -> Id {
pub(crate) fn new(label: &'static str) -> Id {
Self::new_index(label, 0)
}

/// Creates a clay id using the `label` and the `index`
#[inline]
pub(crate) fn new_index(label: &str, index: u32) -> Id {
pub(crate) fn new_index(label: &'static str, index: u32) -> Id {
Self::new_index_internal(label, index)
}

#[inline]
pub(crate) fn new_index_internal(label: &str, index: u32) -> Id {
pub(crate) fn new_index_internal(label: &'static str, index: u32) -> Id {
let id = unsafe { Clay__HashString(label.into(), index, 0) };
Id { id }
}

#[inline]
pub(crate) fn new_index_local(label: &str, index: u32) -> Id {
pub(crate) fn new_index_local(label: &'static str, index: u32) -> Id {
let id = unsafe { Clay__HashString(label.into(), index, Clay__GetParentElementId()) };
Id { id }
}
Expand Down
68 changes: 59 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ pub struct ClayLayoutScope<'clay, 'render, ImageElementData, CustomElementData>
clay: &'clay mut Clay,
_phantom: core::marker::PhantomData<(&'render ImageElementData, &'render CustomElementData)>,
dropped: bool,
#[cfg(feature = "std")]
owned_strings: core::cell::RefCell<std::vec::Vec<std::string::String>>,
}

impl<'render, 'clay: 'render, ImageElementData: 'render, CustomElementData: 'render>
Expand Down Expand Up @@ -246,11 +248,48 @@ impl<'render, 'clay: 'render, ImageElementData: 'render, CustomElementData: 'ren
.map(|command| unsafe { RenderCommand::from_clay_render_command(*command) })
}

/// Adds a text element to the current open element or to the root layout
/// Adds a text element to the current open element or to the root layout.
/// The string data is copied and stored.
/// For string literals, use `text_literal()` for better performance (avoids copying).
/// For dynamic strings, use `text_string()`.
#[cfg(feature = "std")]
pub fn text(&self, text: &str, config: TextElementConfig) {
let owned = std::string::String::from(text);
self.text_string(owned, config);
}

/// Adds a text element from a string that must live until fully used.
/// Only available in no_std - you must ensure the string lives long enough.
#[cfg(not(feature = "std"))]
pub fn text(&self, text: &'render str, config: TextElementConfig) {
unsafe { Clay__OpenTextElement(text.into(), config.into()) };
}

/// Adds a text element from a static string literal without copying.
pub fn text_literal(&self, text: &'static str, config: TextElementConfig) {
let clay_string = Clay_String {
isStaticallyAllocated: true,
length: text.len() as _,
chars: text.as_ptr() as _,
};
unsafe { Clay__OpenTextElement(clay_string, config.into()) };
}

/// Adds a text element from an owned string that will be stored.
#[cfg(feature = "std")]
pub fn text_string(&self, text: std::string::String, config: TextElementConfig) {
let mut owned_strings = self.owned_strings.borrow_mut();
owned_strings.push(text);
let text_ref = owned_strings.last().unwrap();

let clay_string = Clay_String {
isStaticallyAllocated: false,
length: text_ref.len() as _,
chars: text_ref.as_ptr() as _,
};
unsafe { Clay__OpenTextElement(clay_string, config.into()) };
}

pub fn hovered(&self) -> bool {
unsafe { Clay_Hovered() }
}
Expand Down Expand Up @@ -299,6 +338,8 @@ impl Clay {
clay: self,
_phantom: core::marker::PhantomData,
dropped: false,
#[cfg(feature = "std")]
owned_strings: core::cell::RefCell::new(std::vec::Vec::new()),
}
}

Expand Down Expand Up @@ -333,31 +374,31 @@ impl Clay {
///
/// This ID is global and must be unique across the entire scope.
#[inline]
pub fn id(&self, label: &str) -> id::Id {
pub fn id(&self, label: &'static str) -> id::Id {
id::Id::new(label)
}

/// Generates a unique indexed ID based on the given `label` and `index`.
///
/// This is useful when multiple elements share the same label but need distinct IDs.
#[inline]
pub fn id_index(&self, label: &str, index: u32) -> id::Id {
pub fn id_index(&self, label: &'static str, index: u32) -> id::Id {
id::Id::new_index(label, index)
}

/// Generates a locally unique ID based on the given `label`.
///
/// The ID is unique within a specific local scope but not globally.
#[inline]
pub fn id_local(&self, label: &str) -> id::Id {
pub fn id_local(&self, label: &'static str) -> id::Id {
id::Id::new_index_local(label, 0)
}

/// Generates a locally unique indexed ID based on the given `label` and `index`.
///
/// This is useful for differentiating elements within a local scope while keeping their labels consistent.
#[inline]
pub fn id_index_local(&self, label: &str, index: u32) -> id::Id {
pub fn id_index_local(&self, label: &'static str, index: u32) -> id::Id {
id::Id::new_index_local(label, index)
}

Expand Down Expand Up @@ -550,8 +591,7 @@ impl Drop for Clay {
impl From<&str> for Clay_String {
fn from(value: &str) -> Self {
Self {
// TODO: Can we support &'static str here?
isStaticallyAllocated: false,
isStaticallyAllocated: true,
length: value.len() as _,
chars: value.as_ptr() as _,
}
Expand Down Expand Up @@ -630,7 +670,7 @@ mod tests {
.end()
.background_color(Color::rgb(255., 255., 255.)), |clay|
{
clay.text("test", TextConfig::new()
clay.text_literal("test", TextConfig::new()
.color(Color::rgb(255., 255., 255.))
.font_size(24)
.end());
Expand Down Expand Up @@ -690,7 +730,17 @@ mod tests {
.end()
.background_color(Color::rgb(255., 255., 255.)), |clay|
{
clay.text("test", TextConfig::new()
clay.text_literal("test", TextConfig::new()
.color(Color::rgb(255., 255., 255.))
.font_size(24)
.end());

clay.text(&format!("dynamic str {}", 1234), TextConfig::new()
.color(Color::rgb(255., 255., 255.))
.font_size(24)
.end());

clay.text_string(format!("String {}", 1234), TextConfig::new()
.color(Color::rgb(255., 255., 255.))
.font_size(24)
.end());
Expand Down