Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/bindings/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,10 +1030,10 @@ unsafe extern "C" {
unsafe extern "C" fn(
elementId: Clay_ElementId,
pointerData: Clay_PointerData,
userData: isize,
userData: *mut ::core::ffi::c_void,
),
>,
userData: isize,
userData: *mut ::core::ffi::c_void,
);
}
unsafe extern "C" {
Expand Down
50 changes: 50 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,34 @@ impl<'render, 'clay: 'render, ImageElementData: 'render, CustomElementData: 'ren
unsafe { Clay_Hovered() }
}

pub fn on_hover<F, T>(&self, callback: F, user_data: T)
where
F: Fn(Id, Clay_PointerData, &mut T) + 'static
{
let boxed = Box::new((callback, user_data));
let user_data_ptr = Box::into_raw(boxed) as *mut core::ffi::c_void;

unsafe extern "C" fn trampoline<F, T>(
element_id: Clay_ElementId,
pointer_data: Clay_PointerData,
user_data: *mut core::ffi::c_void,
)
where
F: Fn(Id, Clay_PointerData, &mut T) + 'static,
{
let (callback, data) = &mut *(user_data as *mut (F, T));
let id = Id { id: element_id };
callback(id, pointer_data, data);
}

unsafe {
Clay_OnHover(
Some(trampoline::<F, T>),
user_data_ptr,
);
}
}

pub fn scroll_offset(&self) -> Vector2 {
unsafe { Clay_GetScrollOffset().into() }
}
Expand Down Expand Up @@ -365,6 +393,16 @@ impl Clay {
unsafe { Clay_PointerOver(cfg.id) }
}

#[cfg(feature = "std")]
/// Z-sorted list of element IDs that the cursor is currently over
pub fn pointer_over_ids(&self) -> Vec<Id> {
unsafe {
let array = Clay_GetPointerOverIds();
let slice = core::slice::from_raw_parts(array.internalArray, array.length as _);
slice.iter().map(|&id| Id { id }).collect()
}
}

#[cfg(not(feature = "std"))]
pub unsafe fn new_with_memory(dimensions: Dimensions, memory: *mut core::ffi::c_void) -> Self {
let memory_size = Self::required_memory_size();
Expand Down Expand Up @@ -477,6 +515,18 @@ impl Clay {
}
}

/// Returns if debug mode is enabled
pub fn is_debug_mode(&self) -> bool {
unsafe { Clay_IsDebugModeEnabled() }
}

/// Enables or disables culling
pub fn set_culling(&self, enable: bool) {
unsafe {
Clay_SetCullingEnabled(enable);
}
}

/// Sets the dimensions of the global layout, use if, for example the window size you render to
/// changed
pub fn set_layout_dimensions(&self, dimensions: Dimensions) {
Expand Down