Skip to main content

euv_engine/config/
impl.rs

1use super::*;
2
3/// Implements `Default` for `RenderConfig` with sensible default values.
4impl Default for RenderConfig {
5    fn default() -> RenderConfig {
6        RenderConfig {
7            backend: RenderBackendType::default(),
8            canvas_selector: CONFIG_DEFAULT_CANVAS_SELECTOR.to_string(),
9            width: CONFIG_DEFAULT_CANVAS_WIDTH,
10            height: CONFIG_DEFAULT_CANVAS_HEIGHT,
11            quality: RenderQuality::default(),
12            antialias: CONFIG_DEFAULT_ANTIALIAS,
13            power_preference: GpuPowerPreference::default(),
14            ssaa_scale_factor: CONFIG_DEFAULT_SSAA_SCALE_FACTOR,
15        }
16    }
17}
18
19/// Implements `Default` for `EngineConfig` with default render and scheduler configs.
20impl Default for EngineConfig {
21    fn default() -> EngineConfig {
22        EngineConfig {
23            render: RenderConfig::default(),
24            scheduler: SchedulerConfig::default(),
25        }
26    }
27}
28
29/// Implements construction helpers for `RenderConfig`.
30impl RenderConfig {
31    /// Creates a rendering configuration for the Canvas 2D backend.
32    ///
33    /// # Arguments
34    ///
35    /// - `S: AsRef<str>` - The CSS selector for the canvas element.
36    /// - `f64` - The viewport width in CSS pixels.
37    /// - `f64` - The viewport height in CSS pixels.
38    ///
39    /// # Returns
40    ///
41    /// - `RenderConfig` - The Canvas 2D rendering configuration.
42    pub fn canvas2d<S>(canvas_selector: S, width: f64, height: f64) -> RenderConfig
43    where
44        S: AsRef<str>,
45    {
46        RenderConfig {
47            backend: RenderBackendType::Canvas2D,
48            canvas_selector: canvas_selector.as_ref().to_string(),
49            width,
50            height,
51            ..RenderConfig::default()
52        }
53    }
54
55    /// Creates a rendering configuration for the WebGPU backend.
56    ///
57    /// # Arguments
58    ///
59    /// - `S: AsRef<str>` - The CSS selector for the canvas element.
60    /// - `f64` - The viewport width in CSS pixels.
61    /// - `f64` - The viewport height in CSS pixels.
62    ///
63    /// # Returns
64    ///
65    /// - `RenderConfig` - The WebGPU rendering configuration.
66    pub fn webgpu<S>(canvas_selector: S, width: f64, height: f64) -> RenderConfig
67    where
68        S: AsRef<str>,
69    {
70        RenderConfig {
71            backend: RenderBackendType::WebGpu,
72            canvas_selector: canvas_selector.as_ref().to_string(),
73            width,
74            height,
75            ..RenderConfig::default()
76        }
77    }
78}
79
80/// Implements construction helpers for `EngineConfig`.
81impl EngineConfig {
82    /// Creates an engine configuration with the given render config and default scheduler.
83    ///
84    /// # Arguments
85    ///
86    /// - `RenderConfig` - The rendering configuration.
87    ///
88    /// # Returns
89    ///
90    /// - `EngineConfig` - The engine configuration.
91    pub fn create(render: RenderConfig) -> EngineConfig {
92        EngineConfig {
93            render,
94            scheduler: SchedulerConfig::default(),
95        }
96    }
97
98    /// Sets the scheduler configuration and returns the updated config.
99    ///
100    /// # Arguments
101    ///
102    /// - `SchedulerConfig` - The scheduler configuration.
103    ///
104    /// # Returns
105    ///
106    /// - `EngineConfig` - The updated engine configuration.
107    pub fn with_scheduler(mut self, scheduler: SchedulerConfig) -> EngineConfig {
108        self.set_scheduler(scheduler);
109        self
110    }
111}
112
113/// Implements WebGPU power preference conversion for `GpuPowerPreference`.
114impl GpuPowerPreference {
115    /// Converts this power preference to the WebGPU string value.
116    ///
117    /// Used to set the `powerPreference` field on `GpuRequestAdapterOptions`
118    /// via `Reflect::set`, avoiding a direct dependency on the
119    /// `web_sys::GpuPowerPreference` type.
120    ///
121    /// # Returns
122    ///
123    /// - `&'static str` - The WebGPU power preference string (e.g., `"low-power"`).
124    pub fn to_web_sys_string(&self) -> &'static str {
125        match self {
126            GpuPowerPreference::LowPower => GPU_POWER_PREFERENCE_LOW_POWER,
127            GpuPowerPreference::HighPerformance => GPU_POWER_PREFERENCE_HIGH_PERFORMANCE,
128        }
129    }
130}