fui-rs 0.2.3

Retained-mode Rust UI SDK for EffinDOM
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
use crate::ffi;
use crate::logger::warn;
use crate::signal::{Callback, Signal, SubscriptionGuard};
use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use std::thread::LocalKey;

const FIRST_DYNAMIC_SVG_ID: u32 = 0x1000_0000;
const FIRST_DYNAMIC_TEXTURE_ID: u32 = 0x2000_0000;
const MAX_DYNAMIC_ASSET_ID: u32 = 0xffff_ffff;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AssetLoadState {
    Idle = 0,
    Loading = 1,
    Ready = 2,
    Failed = 3,
}

#[derive(Clone)]
pub struct AssetStateSignal {
    inner: Rc<RefCell<Signal<AssetLoadState>>>,
}

impl AssetStateSignal {
    fn new(initial: AssetLoadState) -> Self {
        Self {
            inner: Rc::new(RefCell::new(Signal::new(initial))),
        }
    }

    pub fn get(&self) -> AssetLoadState {
        self.inner.borrow().get()
    }

    pub fn subscribe(&self, callback: Callback) -> SubscriptionGuard {
        self.inner.borrow_mut().subscribe(callback)
    }

    fn set(&self, next: AssetLoadState) {
        let callbacks = self.inner.borrow_mut().set(next);
        if let Some(callbacks) = callbacks {
            for callback in callbacks {
                callback();
            }
        }
    }
}

#[derive(Clone)]
struct AssetRecord {
    state: AssetStateSignal,
    error: String,
    width: f32,
    height: f32,
    url: String,
}

impl Default for AssetRecord {
    fn default() -> Self {
        Self {
            state: AssetStateSignal::new(AssetLoadState::Idle),
            error: String::new(),
            width: 0.0,
            height: 0.0,
            url: String::new(),
        }
    }
}

thread_local! {
    static SVG_ASSETS: RefCell<HashMap<u32, Rc<RefCell<AssetRecord>>>> = RefCell::new(HashMap::new());
    static TEXTURE_ASSETS: RefCell<HashMap<u32, Rc<RefCell<AssetRecord>>>> = RefCell::new(HashMap::new());
    static LOADED_FONT_IDS: RefCell<HashSet<u32>> = RefCell::new(default_loaded_font_ids());
    static SVG_IDS_BY_URL: RefCell<HashMap<String, u32>> = RefCell::new(HashMap::new());
    static TEXTURE_IDS_BY_URL: RefCell<HashMap<String, u32>> = RefCell::new(HashMap::new());
    static SVG_REF_COUNTS: RefCell<HashMap<u32, i32>> = RefCell::new(HashMap::new());
    static TEXTURE_REF_COUNTS: RefCell<HashMap<u32, i32>> = RefCell::new(HashMap::new());
    static PINNED_SVG_IDS: RefCell<HashSet<u32>> = RefCell::new(HashSet::new());
    static PINNED_TEXTURE_IDS: RefCell<HashSet<u32>> = RefCell::new(HashSet::new());
    static NEXT_DYNAMIC_SVG_ID: Cell<u32> = const { Cell::new(FIRST_DYNAMIC_SVG_ID) };
    static NEXT_DYNAMIC_TEXTURE_ID: Cell<u32> = const { Cell::new(FIRST_DYNAMIC_TEXTURE_ID) };
}

fn default_loaded_font_ids() -> HashSet<u32> {
    HashSet::from([1, 2, 7])
}

fn with_utf8(value: &str, callback: impl FnOnce(usize, u32)) {
    let bytes = value.as_bytes();
    callback(
        if bytes.is_empty() {
            0
        } else {
            bytes.as_ptr() as usize
        },
        bytes.len() as u32,
    );
}

fn next_dynamic_svg_id() -> u32 {
    NEXT_DYNAMIC_SVG_ID.with(|slot| {
        let next = slot.get();
        assert!(
            next != MAX_DYNAMIC_ASSET_ID,
            "Dynamic SVG asset id space exhausted."
        );
        slot.set(next + 1);
        next
    })
}

fn next_dynamic_texture_id() -> u32 {
    NEXT_DYNAMIC_TEXTURE_ID.with(|slot| {
        let next = slot.get();
        assert!(
            next != MAX_DYNAMIC_ASSET_ID,
            "Dynamic texture asset id space exhausted."
        );
        slot.set(next + 1);
        next
    })
}

fn get_svg_record(svg_id: u32) -> Rc<RefCell<AssetRecord>> {
    SVG_ASSETS.with(|records| {
        let mut records = records.borrow_mut();
        records
            .entry(svg_id)
            .or_insert_with(|| Rc::new(RefCell::new(AssetRecord::default())))
            .clone()
    })
}

fn get_texture_record(texture_id: u32) -> Rc<RefCell<AssetRecord>> {
    TEXTURE_ASSETS.with(|records| {
        let mut records = records.borrow_mut();
        records
            .entry(texture_id)
            .or_insert_with(|| Rc::new(RefCell::new(AssetRecord::default())))
            .clone()
    })
}

fn begin_load(record: &Rc<RefCell<AssetRecord>>) {
    let state = {
        let mut record = record.borrow_mut();
        record.error.clear();
        record.width = 0.0;
        record.height = 0.0;
        record.state.clone()
    };
    state.set(AssetLoadState::Loading);
}

fn mark_loaded(record: &Rc<RefCell<AssetRecord>>, width: f32, height: f32) {
    let state = {
        let mut record = record.borrow_mut();
        record.error.clear();
        record.width = width;
        record.height = height;
        record.state.clone()
    };
    state.set(AssetLoadState::Ready);
}

fn mark_failed(record: &Rc<RefCell<AssetRecord>>, error: String) {
    let state = {
        let mut record = record.borrow_mut();
        record.error = error;
        record.width = 0.0;
        record.height = 0.0;
        record.state.clone()
    };
    state.set(AssetLoadState::Failed);
}

fn increment_ref_count(ref_counts: &RefCell<HashMap<u32, i32>>, asset_id: u32) {
    let mut ref_counts = ref_counts.borrow_mut();
    let next = ref_counts.get(&asset_id).copied().unwrap_or(0) + 1;
    ref_counts.insert(asset_id, next);
}

fn decrement_ref_count(ref_counts: &RefCell<HashMap<u32, i32>>, asset_id: u32) -> i32 {
    let mut ref_counts = ref_counts.borrow_mut();
    let Some(current) = ref_counts.get(&asset_id).copied() else {
        return -1;
    };
    let next = current - 1;
    if next <= 0 {
        ref_counts.remove(&asset_id);
        0
    } else {
        ref_counts.insert(asset_id, next);
        next
    }
}

fn remove_url_binding(
    url_to_id: &'static LocalKey<RefCell<HashMap<String, u32>>>,
    url: &str,
    asset_id: u32,
) {
    if url.is_empty() {
        return;
    }
    url_to_id.with(|url_to_id| {
        let mut url_to_id = url_to_id.borrow_mut();
        if url_to_id.get(url).copied() == Some(asset_id) {
            url_to_id.remove(url);
        }
    });
}

fn load_svg_internal(svg_id: u32, url: &str, pinned: bool) {
    let record = get_svg_record(svg_id);
    {
        let mut record_mut = record.borrow_mut();
        if !record_mut.url.is_empty() && record_mut.url != url {
            remove_url_binding(&SVG_IDS_BY_URL, &record_mut.url, svg_id);
        }
        record_mut.url = url.to_string();
    }
    SVG_IDS_BY_URL.with(|map| {
        map.borrow_mut().insert(url.to_string(), svg_id);
    });
    PINNED_SVG_IDS.with(|ids| {
        let mut ids = ids.borrow_mut();
        if pinned {
            ids.insert(svg_id);
        } else {
            ids.remove(&svg_id);
        }
    });
    begin_load(&record);
    with_utf8(url, |ptr, len| unsafe {
        ffi::fui_load_svg(svg_id, ptr, len);
    });
}

fn load_texture_internal(texture_id: u32, url: &str, pinned: bool) {
    let record = get_texture_record(texture_id);
    {
        let mut record_mut = record.borrow_mut();
        if !record_mut.url.is_empty() && record_mut.url != url {
            remove_url_binding(&TEXTURE_IDS_BY_URL, &record_mut.url, texture_id);
        }
        record_mut.url = url.to_string();
    }
    TEXTURE_IDS_BY_URL.with(|map| {
        map.borrow_mut().insert(url.to_string(), texture_id);
    });
    PINNED_TEXTURE_IDS.with(|ids| {
        let mut ids = ids.borrow_mut();
        if pinned {
            ids.insert(texture_id);
        } else {
            ids.remove(&texture_id);
        }
    });
    begin_load(&record);
    with_utf8(url, |ptr, len| unsafe {
        ffi::fui_load_texture(texture_id, ptr, len);
    });
}

pub(crate) fn load_font(font_id: u32, url: &str) {
    LOADED_FONT_IDS.with(|ids| {
        ids.borrow_mut().remove(&font_id);
    });
    with_utf8(url, |ptr, len| unsafe {
        ffi::fui_load_font(font_id, ptr, len);
    });
}

pub fn allocate_dynamic_texture_id() -> u32 {
    next_dynamic_texture_id()
}

pub(crate) fn is_font_loaded(font_id: u32) -> bool {
    LOADED_FONT_IDS.with(|ids| ids.borrow().contains(&font_id))
}

pub(crate) fn on_font_loaded(font_id: u32) {
    LOADED_FONT_IDS.with(|ids| {
        ids.borrow_mut().insert(font_id);
    });
}

pub fn load_svg(svg_id: u32, url: &str) {
    load_svg_internal(svg_id, url, true);
}

pub fn load_texture(texture_id: u32, url: &str) {
    load_texture_internal(texture_id, url, true);
}

pub fn acquire_svg_asset(url: &str) -> u32 {
    if url.is_empty() {
        return 0;
    }
    let svg_id = SVG_IDS_BY_URL
        .with(|map| map.borrow().get(url).copied())
        .unwrap_or_else(|| {
            let svg_id = next_dynamic_svg_id();
            load_svg_internal(svg_id, url, false);
            svg_id
        });
    SVG_REF_COUNTS.with(|ref_counts| increment_ref_count(ref_counts, svg_id));
    svg_id
}

pub fn acquire_texture_asset(url: &str) -> u32 {
    if url.is_empty() {
        return 0;
    }
    let texture_id = TEXTURE_IDS_BY_URL
        .with(|map| map.borrow().get(url).copied())
        .unwrap_or_else(|| {
            let texture_id = next_dynamic_texture_id();
            load_texture_internal(texture_id, url, false);
            texture_id
        });
    TEXTURE_REF_COUNTS.with(|ref_counts| increment_ref_count(ref_counts, texture_id));
    texture_id
}

pub fn release_svg_asset(svg_id: u32) {
    if svg_id == 0 {
        return;
    }
    let remaining = SVG_REF_COUNTS.with(|ref_counts| decrement_ref_count(ref_counts, svg_id));
    let pinned = PINNED_SVG_IDS.with(|ids| ids.borrow().contains(&svg_id));
    if remaining != 0 || pinned {
        return;
    }
    let url = get_svg_record(svg_id).borrow().url.clone();
    remove_url_binding(&SVG_IDS_BY_URL, &url, svg_id);
    SVG_ASSETS.with(|records| {
        records.borrow_mut().remove(&svg_id);
    });
    unsafe {
        ffi::fui_release_svg(svg_id);
    }
}

pub fn release_texture_asset(texture_id: u32) {
    if texture_id == 0 {
        return;
    }
    let remaining =
        TEXTURE_REF_COUNTS.with(|ref_counts| decrement_ref_count(ref_counts, texture_id));
    let pinned = PINNED_TEXTURE_IDS.with(|ids| ids.borrow().contains(&texture_id));
    if remaining != 0 || pinned {
        return;
    }
    let url = get_texture_record(texture_id).borrow().url.clone();
    remove_url_binding(&TEXTURE_IDS_BY_URL, &url, texture_id);
    TEXTURE_ASSETS.with(|records| {
        records.borrow_mut().remove(&texture_id);
    });
    unsafe {
        ffi::fui_release_texture(texture_id);
    }
}

pub fn ensure_svg_asset(url: &str) -> u32 {
    acquire_svg_asset(url)
}

pub fn ensure_texture_asset(url: &str) -> u32 {
    acquire_texture_asset(url)
}

pub fn get_svg_asset_state(svg_id: u32) -> AssetStateSignal {
    get_svg_record(svg_id).borrow().state.clone()
}

pub fn get_texture_asset_state(texture_id: u32) -> AssetStateSignal {
    get_texture_record(texture_id).borrow().state.clone()
}

pub fn get_svg_asset_state_value(svg_id: u32) -> AssetLoadState {
    get_svg_record(svg_id).borrow().state.get()
}

pub fn get_texture_asset_state_value(texture_id: u32) -> AssetLoadState {
    get_texture_record(texture_id).borrow().state.get()
}

pub fn get_svg_asset_error(svg_id: u32) -> String {
    get_svg_record(svg_id).borrow().error.clone()
}

pub fn get_svg_asset_url(svg_id: u32) -> String {
    get_svg_record(svg_id).borrow().url.clone()
}

pub fn get_svg_asset_width(svg_id: u32) -> f32 {
    get_svg_record(svg_id).borrow().width
}

pub fn get_svg_asset_height(svg_id: u32) -> f32 {
    get_svg_record(svg_id).borrow().height
}

pub fn get_texture_asset_error(texture_id: u32) -> String {
    get_texture_record(texture_id).borrow().error.clone()
}

pub fn get_texture_asset_url(texture_id: u32) -> String {
    get_texture_record(texture_id).borrow().url.clone()
}

pub fn get_texture_asset_width(texture_id: u32) -> f32 {
    get_texture_record(texture_id).borrow().width
}

pub fn get_texture_asset_height(texture_id: u32) -> f32 {
    get_texture_record(texture_id).borrow().height
}

pub fn mark_texture_asset_ready(texture_id: u32, width: f32, height: f32) {
    mark_loaded(&get_texture_record(texture_id), width, height);
}

pub fn on_svg_loaded(svg_id: u32, width: f32, height: f32) {
    mark_loaded(&get_svg_record(svg_id), width, height);
}

pub fn on_svg_failed(svg_id: u32, error: String) {
    let record = get_svg_record(svg_id);
    let url = record.borrow().url.clone();
    let message = if error.is_empty() {
        "unknown error".to_string()
    } else {
        error.clone()
    };
    mark_failed(&record, error);
    warn(
        "Assets",
        &format!("SVG load failed for \"{url}\": {message}"),
    );
}

pub fn on_texture_loaded(texture_id: u32, width: f32, height: f32) {
    mark_loaded(&get_texture_record(texture_id), width, height);
}

pub fn on_texture_failed(texture_id: u32, error: String) {
    let record = get_texture_record(texture_id);
    let url = record.borrow().url.clone();
    let message = if error.is_empty() {
        "unknown error".to_string()
    } else {
        error.clone()
    };
    mark_failed(&record, error);
    warn(
        "Assets",
        &format!("Texture load failed for \"{url}\": {message}"),
    );
}

#[cfg(test)]
pub(crate) fn test_reset() {
    SVG_ASSETS.with(|slot| slot.borrow_mut().clear());
    TEXTURE_ASSETS.with(|slot| slot.borrow_mut().clear());
    SVG_IDS_BY_URL.with(|slot| slot.borrow_mut().clear());
    TEXTURE_IDS_BY_URL.with(|slot| slot.borrow_mut().clear());
    SVG_REF_COUNTS.with(|slot| slot.borrow_mut().clear());
    TEXTURE_REF_COUNTS.with(|slot| slot.borrow_mut().clear());
    PINNED_SVG_IDS.with(|slot| slot.borrow_mut().clear());
    PINNED_TEXTURE_IDS.with(|slot| slot.borrow_mut().clear());
    LOADED_FONT_IDS.with(|ids| {
        *ids.borrow_mut() = default_loaded_font_ids();
    });
    NEXT_DYNAMIC_SVG_ID.with(|slot| slot.set(FIRST_DYNAMIC_SVG_ID));
    NEXT_DYNAMIC_TEXTURE_ID.with(|slot| slot.set(FIRST_DYNAMIC_TEXTURE_ID));
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ffi::{self, Call};
    use std::cell::Cell;
    use std::rc::Rc;

    #[test]
    fn asset_loaders_emit_host_calls() {
        test_reset();
        ffi::test::reset();

        load_font(1, "/v2/fonts/NotoSans-Regular.ttf");
        load_svg(2, "data:image/svg+xml,%3Csvg/%3E");
        load_texture(3, "/v2/fui-as/demo/demo-texture.png");

        let calls = ffi::test::take_calls();
        assert!(calls.iter().any(|call| matches!(call, Call::LoadFont { font_id: 1, url } if url == "/v2/fonts/NotoSans-Regular.ttf")));
        assert!(calls.iter().any(|call| matches!(call, Call::LoadSvg { svg_id: 2, url } if url == "data:image/svg+xml,%3Csvg/%3E")));
        assert!(calls.iter().any(|call| matches!(call, Call::LoadTexture { texture_id: 3, url } if url == "/v2/fui-as/demo/demo-texture.png")));
    }

    #[test]
    fn acquired_assets_share_url_and_release_when_last_ref_drops() {
        test_reset();
        ffi::test::reset();

        let first = acquire_texture_asset("/img/a.png");
        let second = acquire_texture_asset("/img/a.png");
        assert_eq!(first, second);
        let calls = ffi::test::take_calls();
        let load_calls = calls
            .iter()
            .filter(|call| matches!(call, Call::LoadTexture { .. }))
            .count();
        assert_eq!(load_calls, 1);

        release_texture_asset(first);
        let calls = ffi::test::take_calls();
        assert!(!calls.iter().any(
            |call| matches!(call, Call::ReleaseTexture { texture_id } if *texture_id == first)
        ));

        release_texture_asset(second);
        let calls = ffi::test::take_calls();
        assert!(calls.iter().any(
            |call| matches!(call, Call::ReleaseTexture { texture_id } if *texture_id == first)
        ));
    }

    #[test]
    fn pinned_assets_are_not_released_by_ref_count_drop() {
        test_reset();
        ffi::test::reset();

        load_svg(55, "/img/pinned.svg");
        let acquired = acquire_svg_asset("/img/pinned.svg");
        assert_eq!(acquired, 55);
        ffi::test::take_calls();

        release_svg_asset(acquired);
        let calls = ffi::test::take_calls();
        assert!(!calls
            .iter()
            .any(|call| matches!(call, Call::ReleaseSvg { svg_id } if *svg_id == acquired)));
    }

    #[test]
    fn asset_state_signal_notifies_and_tracks_ready_and_failed_payloads() {
        test_reset();
        let texture_id = acquire_texture_asset("/img/ready.png");
        let state = get_texture_asset_state(texture_id);
        assert_eq!(state.get(), AssetLoadState::Loading);

        let hits = Rc::new(Cell::new(0));
        let hits_clone = hits.clone();
        let callback: Callback = Rc::new(move || hits_clone.set(hits_clone.get() + 1));
        let _guard = state.subscribe(callback);

        on_texture_loaded(texture_id, 64.0, 32.0);
        assert_eq!(state.get(), AssetLoadState::Ready);
        assert_eq!(get_texture_asset_width(texture_id), 64.0);
        assert_eq!(get_texture_asset_height(texture_id), 32.0);

        on_texture_failed(texture_id, "broken".to_string());
        assert_eq!(state.get(), AssetLoadState::Failed);
        assert_eq!(get_texture_asset_error(texture_id), "broken");
        assert_eq!(hits.get(), 2);
    }
}