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` - The CSS selector for the canvas element.
46    /// - `f64` - The viewport width in CSS pixels.
47    ///
48    /// # Returns
49    ///
50    /// - `RenderConfig` - The Canvas 2D rendering configuration.
51    pub fn canvas2d<S>(canvas_selector: S, width: f64, height: f64) -> RenderConfig
52    where
53        S: AsRef<str>,
54    {
55        RenderConfig {
56            backend: RenderBackendType::Canvas2D,
57            canvas_selector: canvas_selector.as_ref().to_string(),
58            width,
59            height,
60            ..RenderConfig::default()
61        }
62    }
63
64    /// Creates a rendering configuration for the WebGPU backend.
65    ///
66    /// # Arguments
67    ///
68    /// - `S` - The CSS selector for the canvas element.
69    /// - `f64` - The viewport width in CSS pixels.
70    ///
71    /// # Returns
72    ///
73    /// - `RenderConfig` - The WebGPU rendering configuration.
74    pub fn webgpu<S>(canvas_selector: S, width: f64, height: f64) -> RenderConfig
75    where
76        S: AsRef<str>,
77    {
78        RenderConfig {
79            backend: RenderBackendType::WebGpu,
80            canvas_selector: canvas_selector.as_ref().to_string(),
81            width,
82            height,
83            ..RenderConfig::default()
84        }
85    }
86
87    /// Creates a rendering configuration for the WebGL 2 backend.
88    ///
89    /// # Arguments
90    ///
91    /// - `S` - The CSS selector for the canvas element.
92    /// - `f64` - The viewport width in CSS pixels.
93    ///
94    /// # Returns
95    ///
96    /// - `RenderConfig` - The WebGL 2 rendering configuration.
97    pub fn webgl<S>(canvas_selector: S, width: f64, height: f64) -> RenderConfig
98    where
99        S: AsRef<str>,
100    {
101        RenderConfig {
102            backend: RenderBackendType::WebGl,
103            canvas_selector: canvas_selector.as_ref().to_string(),
104            width,
105            height,
106            ..RenderConfig::default()
107        }
108    }
109}
110
111/// Implements construction helpers for `EngineConfig`.
112impl EngineConfig {
113    /// Creates an engine configuration with the given render config and default scheduler.
114    ///
115    /// # Arguments
116    ///
117    /// - `RenderConfig` - The rendering configuration.
118    ///
119    /// # Returns
120    ///
121    /// - `EngineConfig` - The engine configuration.
122    pub fn create(render: RenderConfig) -> EngineConfig {
123        EngineConfig {
124            render,
125            scheduler: SchedulerConfig::default(),
126        }
127    }
128
129    /// Sets the scheduler configuration and returns the updated config.
130    ///
131    /// # Arguments
132    ///
133    /// - `SchedulerConfig` - The scheduler configuration.
134    ///
135    /// # Returns
136    ///
137    /// - `EngineConfig` - The updated engine configuration.
138    pub fn with_scheduler(mut self, scheduler: SchedulerConfig) -> EngineConfig {
139        self.set_scheduler(scheduler);
140        self
141    }
142}
143
144/// Implements WebGPU power preference conversion for `GpuPowerPreference`.
145impl GpuPowerPreference {
146    /// Converts this power preference to the WebGPU string value.
147    ///
148    /// Used to set the `powerPreference` field on `GpuRequestAdapterOptions`
149    /// via `Reflect::set`, avoiding a direct dependency on the
150    /// `GpuPowerPreference` type.
151    ///
152    /// # Returns
153    ///
154    /// - `&'static str` - The WebGPU power preference string (e.g., `"low-power"`).
155    pub fn to_web_sys_string(&self) -> &'static str {
156        match self {
157            GpuPowerPreference::LowPower => GPU_POWER_PREFERENCE_LOW_POWER,
158            GpuPowerPreference::HighPerformance => GPU_POWER_PREFERENCE_HIGH_PERFORMANCE,
159        }
160    }
161}