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
pub mod gpu_driver;
pub mod javascript;
pub mod platform;
pub mod renderer;
pub mod sys;

pub use javascript::*;
pub use platform::*;
pub use renderer::*;
use sys::{ulConfigSetAnimationTimerDelay, ulConfigSetCachePath, ulViewConfigSetIsTransparent};

use crate::sys::{
    ulConfigSetResourcePathPrefix, ulCreateConfig, ulCreateString, ulCreateViewConfig,
    ulDestroyConfig, ulDestroyString, ulDestroyViewConfig, ulViewConfigSetInitialDeviceScale,
    ulViewConfigSetIsAccelerated, ULConfig, ULViewConfig,
};
use std::ffi::CString;

pub struct Config {
    inner: ULConfig,
}

impl Config {
    pub fn set_resource_path_prefix(&mut self, path: String) {
        let path = CString::new(path).unwrap();
        unsafe {
            let path = ulCreateString(path.as_ptr());
            ulConfigSetResourcePathPrefix(self.inner, path);
            ulConfigSetAnimationTimerDelay(self.inner, 0.0);
            ulDestroyString(path);
        }
    }

    pub fn set_cache_path(&mut self, path: String) {
        let path = CString::new(path).unwrap();
        unsafe {
            let path = ulCreateString(path.as_ptr());
            ulConfigSetCachePath(self.inner, path);
            ulDestroyString(path);
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            inner: unsafe { ulCreateConfig() },
        }
    }
}

impl Drop for Config {
    fn drop(&mut self) {
        unsafe {
            ulDestroyConfig(self.inner);
        }
    }
}

impl From<&Config> for ULConfig {
    fn from(value: &Config) -> Self {
        value.inner
    }
}

pub struct ViewConfig {
    inner: ULViewConfig,
}

impl Default for ViewConfig {
    fn default() -> Self {
        let inner = unsafe { ulCreateViewConfig() };
        unsafe {
            ulViewConfigSetInitialDeviceScale(inner, 1.0);
            ulViewConfigSetIsAccelerated(inner, false);
            ulViewConfigSetIsTransparent(inner, true);
        }

        Self { inner }
    }
}

impl ViewConfig {
    pub fn set_gpu_accelerated(&mut self) {
        unsafe {
            ulViewConfigSetIsAccelerated(self.inner, true);
        }
    }
}

impl Drop for ViewConfig {
    fn drop(&mut self) {
        unsafe {
            ulDestroyViewConfig(self.inner);
        }
    }
}

impl From<&ViewConfig> for ULViewConfig {
    fn from(value: &ViewConfig) -> Self {
        value.inner
    }
}