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