clay-layout 0.4.0

Rust bindings for Clay, a UI layout library written in C.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#![cfg_attr(not(feature = "std"), no_std)]

pub mod bindings;
pub mod color;
pub mod elements;
pub mod errors;
pub mod id;
pub mod layout;
pub mod math;
pub mod render_commands;
pub mod text;

mod mem;
pub mod renderers;

use core::marker::PhantomData;

pub use crate::bindings::*;
use errors::Error;
use id::Id;
use math::{BoundingBox, Dimensions, Vector2};
use render_commands::RenderCommand;

pub use color::Color;

#[cfg(feature = "std")]
use text::TextConfig;

use text::TextElementConfig;
#[derive(Copy, Clone)]
pub struct Declaration<'render, ImageElementData: 'render, CustomElementData: 'render> {
    inner: Clay_ElementDeclaration,
    _phantom: PhantomData<(&'render CustomElementData, &'render ImageElementData)>,
}

impl<'render, ImageElementData: 'render, CustomElementData: 'render>
    Declaration<'render, ImageElementData, CustomElementData>
{
    #[inline]
    pub fn new() -> Self {
        crate::mem::zeroed_init()
    }

    #[inline]
    pub fn background_color(&mut self, color: Color) -> &mut Self {
        self.inner.backgroundColor = color.into();
        self
    }

    /// Sets aspect ratio for image elements.
    #[inline]
    pub fn aspect_ratio(&mut self, aspect_ratio: f32) -> &mut Self {
        self.inner.aspectRatio.aspectRatio = aspect_ratio;
        self
    }

    #[inline]
    pub fn clip(&mut self, horizontal: bool, vertical: bool, child_offset: Vector2) -> &mut Self {
        self.inner.clip.horizontal = horizontal;
        self.inner.clip.vertical = vertical;
        self.inner.clip.childOffset = child_offset.into();
        self
    }

    #[inline]
    pub fn id(&mut self, id: Id) -> &mut Self {
        self.inner.id = id.id;
        self
    }

    #[inline]
    pub fn custom_element(&mut self, data: &'render CustomElementData) -> &mut Self {
        self.inner.custom.customData = data as *const CustomElementData as _;
        self
    }

    #[inline]
    pub fn layout(
        &mut self,
    ) -> layout::LayoutBuilder<'_, 'render, ImageElementData, CustomElementData> {
        layout::LayoutBuilder::new(self)
    }

    #[inline]
    pub fn image(
        &mut self,
    ) -> elements::ImageBuilder<'_, 'render, ImageElementData, CustomElementData> {
        elements::ImageBuilder::new(self)
    }

    #[inline]
    pub fn floating(
        &mut self,
    ) -> elements::FloatingBuilder<'_, 'render, ImageElementData, CustomElementData> {
        elements::FloatingBuilder::new(self)
    }

    #[inline]
    pub fn border(
        &mut self,
    ) -> elements::BorderBuilder<'_, 'render, ImageElementData, CustomElementData> {
        elements::BorderBuilder::new(self)
    }

    #[inline]
    pub fn corner_radius(
        &mut self,
    ) -> elements::CornerRadiusBuilder<'_, 'render, ImageElementData, CustomElementData> {
        elements::CornerRadiusBuilder::new(self)
    }
}

impl<ImageElementData, CustomElementData> Default
    for Declaration<'_, ImageElementData, CustomElementData>
{
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "std")]
unsafe extern "C" fn measure_text_trampoline_user_data<'a, F, T>(
    text_slice: Clay_StringSlice,
    config: *mut Clay_TextElementConfig,
    user_data: *mut core::ffi::c_void,
) -> Clay_Dimensions
where
    F: Fn(&str, &TextConfig, &'a mut T) -> Dimensions + 'a,
    T: 'a,
{
    let text = core::str::from_utf8_unchecked(core::slice::from_raw_parts(
        text_slice.chars as *const u8,
        text_slice.length as _,
    ));

    let closure_and_data: &mut (F, T) = &mut *(user_data as *mut (F, T));
    let text_config = TextConfig::from(*config);
    let (callback, data) = closure_and_data;
    callback(text, &text_config, data).into()
}

#[cfg(feature = "std")]
unsafe extern "C" fn measure_text_trampoline<'a, F>(
    text_slice: Clay_StringSlice,
    config: *mut Clay_TextElementConfig,
    user_data: *mut core::ffi::c_void,
) -> Clay_Dimensions
where
    F: Fn(&str, &TextConfig) -> Dimensions + 'a,
{
    let text = core::str::from_utf8_unchecked(core::slice::from_raw_parts(
        text_slice.chars as *const u8,
        text_slice.length as _,
    ));

    let tuple = &*(user_data as *const (F, usize));
    let text_config = TextConfig::from(*config);
    (tuple.0)(text, &text_config).into()
}

unsafe extern "C" fn error_handler(error_data: Clay_ErrorData) {
    let error: Error = error_data.into();
    panic!("Clay Error: (type: {:?}) {}", error.type_, error.text);
}

#[allow(dead_code)]
pub struct Clay {
    /// Memory used internally by clay
    #[cfg(feature = "std")]
    _memory: Vec<u8>,
    context: *mut Clay_Context,
    /// Memory used internally by clay. The caller is responsible for managing this memory in
    /// no_std case.
    #[cfg(not(feature = "std"))]
    _memory: *const core::ffi::c_void,
    /// Stores the raw pointer to the callback data for later cleanup
    text_measure_callback: Option<*const core::ffi::c_void>,
}

pub struct ClayLayoutScope<'clay, 'render, ImageElementData, CustomElementData> {
    clay: &'clay mut Clay,
    _phantom: core::marker::PhantomData<(&'render ImageElementData, &'render CustomElementData)>,
    dropped: bool,
}

impl<'render, 'clay: 'render, ImageElementData: 'render, CustomElementData: 'render>
    ClayLayoutScope<'clay, 'render, ImageElementData, CustomElementData>
{
    /// Create an element, passing its config and a function to add childrens
    pub fn with<
        F: FnOnce(&mut ClayLayoutScope<'clay, 'render, ImageElementData, CustomElementData>),
    >(
        &mut self,
        declaration: &Declaration<'render, ImageElementData, CustomElementData>,
        f: F,
    ) {
        unsafe {
            Clay_SetCurrentContext(self.clay.context);
            Clay__OpenElement();
            Clay__ConfigureOpenElement(declaration.inner);
        }

        f(self);

        unsafe {
            Clay__CloseElement();
        }
    }

    pub fn with_styling<
        G: FnOnce(
            &ClayLayoutScope<'clay, 'render, ImageElementData, CustomElementData>,
        ) -> Declaration<'render, ImageElementData, CustomElementData>,
        F: FnOnce(&ClayLayoutScope<'clay, 'render, ImageElementData, CustomElementData>),
    >(
        &self,
        g: G,
        f: F,
    ) {
        unsafe {
            Clay_SetCurrentContext(self.clay.context);
            Clay__OpenElement();
        }

        let declaration = g(self);

        unsafe {
            Clay__ConfigureOpenElement(declaration.inner);
        }

        f(self);

        unsafe {
            Clay__CloseElement();
        }
    }

    pub fn end(
        &mut self,
    ) -> impl Iterator<Item = RenderCommand<'render, ImageElementData, CustomElementData>> {
        let array = unsafe { Clay_EndLayout() };
        self.dropped = true;
        let slice = unsafe { core::slice::from_raw_parts(array.internalArray, array.length as _) };
        slice
            .iter()
            .map(|command| unsafe { RenderCommand::from_clay_render_command(*command) })
    }

    /// Generates a unique ID based on the given `label`.
    ///
    /// This ID is global and must be unique across the entire scope.
    #[inline]
    pub fn id(&self, label: &'render 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: &'render 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: &'render 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: &'render str, index: u32) -> id::Id {
        id::Id::new_index_local(label, index)
    }

    /// Adds a text element to the current open element or to the root layout
    pub fn text(&self, text: &'render str, config: TextElementConfig) {
        unsafe { Clay__OpenTextElement(text.into(), config.into()) };
    }

    pub fn hovered(&self) -> bool {
        unsafe { Clay_Hovered() }
    }

    pub fn pointer_over(&self, cfg: Id) -> bool {
        unsafe { Clay_PointerOver(cfg.id) }
    }

    pub fn scroll_container_data(&self, id: Id) -> Option<Clay_ScrollContainerData> {
        self.clay.scroll_container_data(id)
    }
    pub fn bounding_box(&self, id: Id) -> Option<BoundingBox> {
        self.clay.bounding_box(id)
    }

    pub fn scroll_offset(&self) -> Vector2 {
        unsafe { Clay_GetScrollOffset().into() }
    }
}

impl<ImageElementData, CustomElementData> Drop
    for ClayLayoutScope<'_, '_, ImageElementData, CustomElementData>
{
    fn drop(&mut self) {
        if !self.dropped {
            unsafe {
                Clay_EndLayout();
            }
        }
    }
}

impl Clay {
    pub fn begin<'render, ImageElementData: 'render, CustomElementData: 'render>(
        &mut self,
    ) -> ClayLayoutScope<'_, 'render, ImageElementData, CustomElementData> {
        unsafe { Clay_BeginLayout() };
        ClayLayoutScope {
            clay: self,
            _phantom: core::marker::PhantomData,
            dropped: false,
        }
    }

    #[cfg(feature = "std")]
    pub fn new(dimensions: Dimensions) -> Self {
        let memory_size = Self::required_memory_size();
        let memory = vec![0; memory_size];
        let context;

        unsafe {
            let arena =
                Clay_CreateArenaWithCapacityAndMemory(memory_size as _, memory.as_ptr() as _);

            context = Clay_Initialize(
                arena,
                dimensions.into(),
                Clay_ErrorHandler {
                    errorHandlerFunction: Some(error_handler),
                    userData: std::ptr::null_mut(),
                },
            );
        }

        Self {
            _memory: memory,
            context,
            text_measure_callback: None,
        }
    }

    #[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();
        let arena = Clay_CreateArenaWithCapacityAndMemory(memory_size as _, memory);

        let context = Clay_Initialize(
            arena,
            dimensions.into(),
            Clay_ErrorHandler {
                errorHandlerFunction: Some(error_handler),
                userData: core::ptr::null_mut(),
            },
        );

        Self {
            _memory: memory,
            context,
            text_measure_callback: None,
        }
    }

    /// Wrapper for `Clay_MinMemorySize`, returns the minimum required memory by clay
    pub fn required_memory_size() -> usize {
        unsafe { Clay_MinMemorySize() as usize }
    }

    /// Set the callback for text measurement with user data
    #[cfg(feature = "std")]
    pub fn set_measure_text_function_user_data<'clay, F, T>(
        &'clay mut self,
        userdata: T,
        callback: F,
    ) where
        F: Fn(&str, &TextConfig, &'clay mut T) -> Dimensions + 'static,
        T: 'clay,
    {
        // Box the callback and userdata together
        let boxed = Box::new((callback, userdata));

        // Get a raw pointer to the boxed data
        let user_data_ptr = Box::into_raw(boxed) as _;

        // Register the callback with the external C function
        unsafe {
            Self::set_measure_text_function_unsafe(
                measure_text_trampoline_user_data::<F, T>,
                user_data_ptr,
            );
        }

        // Store the raw pointer for later cleanup
        self.text_measure_callback = Some(user_data_ptr as *const core::ffi::c_void);
    }

    /// Set the callback for text measurement
    #[cfg(feature = "std")]
    pub fn set_measure_text_function<F>(&mut self, callback: F)
    where
        F: Fn(&str, &TextConfig) -> Dimensions + 'static,
    {
        // Box the callback and userdata together
        // Tuple here is to prevent Rust ZST optimization from breaking getting a raw pointer
        let boxed = Box::new((callback, 0usize));

        // Get a raw pointer to the boxed data
        let user_data_ptr = Box::into_raw(boxed) as *mut core::ffi::c_void;

        // Register the callback with the external C function
        unsafe {
            Self::set_measure_text_function_unsafe(measure_text_trampoline::<F>, user_data_ptr);
        }

        // Store the raw pointer for later cleanup
        self.text_measure_callback = Some(user_data_ptr as *const core::ffi::c_void);
    }

    /// Set the callback for text measurement with user data.
    /// # Safety
    /// This function is unsafe because it sets a callback function without any error checking
    pub unsafe fn set_measure_text_function_unsafe(
        callback: unsafe extern "C" fn(
            Clay_StringSlice,
            *mut Clay_TextElementConfig,
            *mut core::ffi::c_void,
        ) -> Clay_Dimensions,
        user_data: *mut core::ffi::c_void,
    ) {
        Clay_SetMeasureTextFunction(Some(callback), user_data);
    }

    /// Sets the maximum number of element that clay supports
    /// **Use only if you know what you are doing or your getting errors from clay**
    pub fn max_element_count(&mut self, max_element_count: u32) {
        unsafe {
            Clay_SetMaxElementCount(max_element_count as _);
        }
    }
    /// Sets the capacity of the cache used for text in the measure text function
    /// **Use only if you know what you are doing or your getting errors from clay**
    pub fn max_measure_text_cache_word_count(&self, count: u32) {
        unsafe {
            Clay_SetMaxElementCount(count as _);
        }
    }

    /// Enables or disables the debug mode of clay
    pub fn set_debug_mode(&self, enable: bool) {
        unsafe {
            Clay_SetDebugModeEnabled(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) {
        unsafe {
            Clay_SetLayoutDimensions(dimensions.into());
        }
    }
    /// Updates the state of the pointer for clay. Used to update scroll containers and for
    /// interactions functions
    pub fn pointer_state(&self, position: Vector2, is_down: bool) {
        unsafe {
            Clay_SetPointerState(position.into(), is_down);
        }
    }
    pub fn update_scroll_containers(
        &self,
        drag_scrolling_enabled: bool,
        scroll_delta: Vector2,
        delta_time: f32,
    ) {
        unsafe {
            Clay_UpdateScrollContainers(drag_scrolling_enabled, scroll_delta.into(), delta_time);
        }
    }

    /// Returns if the current element you are creating is hovered
    pub fn hovered(&self) -> bool {
        unsafe { Clay_Hovered() }
    }

    pub fn pointer_over(&self, cfg: Id) -> bool {
        unsafe { Clay_PointerOver(cfg.id) }
    }

    fn element_data(id: Id) -> Clay_ElementData {
        unsafe { Clay_GetElementData(id.id) }
    }

    pub fn bounding_box(&self, id: Id) -> Option<BoundingBox> {
        let element_data = Self::element_data(id);

        if element_data.found {
            Some(element_data.boundingBox.into())
        } else {
            None
        }
    }
    pub fn scroll_container_data(&self, id: Id) -> Option<Clay_ScrollContainerData> {
        unsafe {
            Clay_SetCurrentContext(self.context);
            let scroll_container_data = Clay_GetScrollContainerData(id.id);

            if scroll_container_data.found {
                Some(scroll_container_data)
            } else {
                None
            }
        }
    }
}

#[cfg(feature = "std")]
impl Drop for Clay {
    fn drop(&mut self) {
        unsafe {
            if let Some(ptr) = self.text_measure_callback {
                let _ = Box::from_raw(ptr as *mut (usize, usize));
            }

            Clay_SetCurrentContext(core::ptr::null_mut() as _);
        }
    }
}

impl From<&str> for Clay_String {
    fn from(value: &str) -> Self {
        Self {
            // TODO: Can we support &'static str here?
            isStaticallyAllocated: false,
            length: value.len() as _,
            chars: value.as_ptr() as _,
        }
    }
}

impl From<Clay_String> for &str {
    fn from(value: Clay_String) -> Self {
        unsafe {
            core::str::from_utf8_unchecked(core::slice::from_raw_parts(
                value.chars as *const u8,
                value.length as _,
            ))
        }
    }
}

impl From<Clay_StringSlice> for &str {
    fn from(value: Clay_StringSlice) -> Self {
        unsafe {
            core::str::from_utf8_unchecked(core::slice::from_raw_parts(
                value.chars as *const u8,
                value.length as _,
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use color::Color;
    use layout::{Padding, Sizing};

    #[rustfmt::skip]
    #[test]
    fn test_begin() {
        let mut callback_data = 0u32;

        let mut clay = Clay::new(Dimensions::new(800.0, 600.0));

        clay.set_measure_text_function_user_data(&mut callback_data, |text, _config, data| {
            println!(
                "set_measure_text_function_user_data {:?} count {:?}",
                text, data
            );
            **data += 1;
            Dimensions::default()
        });

        let mut clay = clay.begin::<(), ()>();

        clay.with(&Declaration::new()
            .id(clay.id("parent_rect"))
            .layout()
                .width(Sizing::Fixed(100.0))
                .height(Sizing::Fixed(100.0))
                .padding(Padding::all(10))
                .end()
            .background_color(Color::rgb(255., 255., 255.)), |clay|
        {
            clay.with(&Declaration::new()
                .layout()
                    .width(Sizing::Fixed(100.0))
                    .height(Sizing::Fixed(100.0))
                    .padding(Padding::all(10))
                    .end()
                .background_color(Color::rgb(255., 255., 255.)), |clay| 
            {
                clay.with(&Declaration::new()
                    .id(clay.id("rect_under_rect"))
                    .layout()
                        .width(Sizing::Fixed(100.0))
                        .height(Sizing::Fixed(100.0))
                        .padding(Padding::all(10))
                        .end()
                    .background_color(Color::rgb(255., 255., 255.)), |clay| 
                    {
                        clay.text("test", TextConfig::new()
                            .color(Color::rgb(255., 255., 255.))
                            .font_size(24)
                            .end());
                    },
                );
            });
        });

        clay.with(&Declaration::new()
            .id(clay.id_index("border_container", 1))
            .layout()
                .padding(Padding::all(16))
                .end()
            .border()
                .color(Color::rgb(255., 255., 0.))
                .all_directions(2)
                .end()
            .corner_radius().all(10.0).end(), |clay|
        {
            clay.with(&Declaration::new()
                .id(clay.id("rect_under_border"))
                .layout()
                    .width(Sizing::Fixed(50.0))
                    .height(Sizing::Fixed(50.0))
                    .end()
                .background_color(Color::rgb(0., 255., 255.)), |_clay| {},
            );
        });

        let items = clay.end();

        for item in items {
            println!(
                "id: {}\nbbox: {:?}\nconfig: {:?}",
                item.id, item.bounding_box, item.config,
            );
        }
    }

    #[rustfmt::skip]
    #[test]
    fn test_simple_text_measure() {
        let mut clay = Clay::new(Dimensions::new(800.0, 600.0));

        clay.set_measure_text_function(|_text, _config| {
            Dimensions::default()
        });

        let mut clay = clay.begin::<(), ()>();

        clay.with(&Declaration::new()
            .id(clay.id("parent_rect"))
            .layout()
                .width(Sizing::Fixed(100.0))
                .height(Sizing::Fixed(100.0))
                .padding(Padding::all(10))
                .end()
            .background_color(Color::rgb(255., 255., 255.)), |clay|
        {
            clay.text("test", TextConfig::new()
                .color(Color::rgb(255., 255., 255.))
                .font_size(24)
                .end());
        });

        let _items = clay.end();
    }
}