1use std::sync::{Mutex, OnceLock};
14
15use crate::ComponentId;
16
17#[derive(Clone, Debug, Default, PartialEq, Eq)]
18pub enum GpuChoice {
19 #[default]
21 Auto,
22 HighPerformance,
24 LowPower,
26 FollowMonitor,
29 Named(String),
31}
32
33impl GpuChoice {
34 pub fn parse(text: &str) -> GpuChoice {
36 match text.trim().to_lowercase().as_str() {
37 "" | "auto" | "automatica" | "automática" => GpuChoice::Auto,
38 "alto-desempenho" | "high-performance" | "alto" | "high" => GpuChoice::HighPerformance,
39 "economia" | "low-power" | "baixo" | "low" => GpuChoice::LowPower,
40 "monitor" | "seguir-monitor" | "follow-monitor" => GpuChoice::FollowMonitor,
41 _ => GpuChoice::Named(text.trim().to_string()),
42 }
43 }
44
45 pub fn to_text(&self) -> String {
47 match self {
48 GpuChoice::Auto => String::new(),
49 GpuChoice::HighPerformance => "alto-desempenho".into(),
50 GpuChoice::LowPower => "economia".into(),
51 GpuChoice::FollowMonitor => "monitor".into(),
52 GpuChoice::Named(name) => name.clone(),
53 }
54 }
55
56 pub fn matches_name(&self, adapter: &str) -> bool {
58 match self {
59 GpuChoice::Named(name) => adapter.to_lowercase().contains(&name.to_lowercase()),
60 _ => false,
61 }
62 }
63}
64
65#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
68pub enum PresentMode {
69 #[default]
72 Window,
73 Composition,
78 Composed,
84 Transparent,
88}
89
90impl PresentMode {
91 pub fn parse(text: &str) -> PresentMode {
93 match text.trim().to_lowercase().as_str() {
94 "composition" | "composicao" | "composição" => PresentMode::Composition,
95 "composed" | "composto" => PresentMode::Composed,
96 "transparent" | "transparente" => PresentMode::Transparent,
97 _ => PresentMode::Window,
98 }
99 }
100
101 pub fn name(self) -> &'static str {
103 match self {
104 PresentMode::Window => "",
105 PresentMode::Composition => "composition",
106 PresentMode::Composed => "composed",
107 PresentMode::Transparent => "transparent",
108 }
109 }
110}
111
112static PRESENT: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
113
114pub fn set_present(mode: PresentMode) {
116 let code = match mode {
117 PresentMode::Window => 0,
118 PresentMode::Composition => 1,
119 PresentMode::Composed => 2,
120 PresentMode::Transparent => 3,
121 };
122 PRESENT.store(code, std::sync::atomic::Ordering::Relaxed);
123}
124
125pub fn present() -> PresentMode {
127 match PRESENT.load(std::sync::atomic::Ordering::Relaxed) {
128 1 => PresentMode::Composition,
129 2 => PresentMode::Composed,
130 3 => PresentMode::Transparent,
131 _ => PresentMode::Window,
132 }
133}
134
135pub fn present_for(form_text: &str) -> PresentMode {
137 if form_text.trim().is_empty() || form_text.trim() == "project" { present() } else { PresentMode::parse(form_text) }
138}
139
140#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
143pub enum FormPresent {
144 #[default]
145 Project,
146 Window,
147 Composition,
148 Composed,
149}
150
151impl crate::PropEnum for FormPresent {
152 const NAMES: &'static [&'static str] = &["project", "window", "composition", "composed"];
153 fn from_index(index: usize) -> Option<Self> {
154 [Self::Project, Self::Window, Self::Composition, Self::Composed].get(index).copied()
155 }
156 fn index(self) -> usize {
157 self as usize
158 }
159 fn from_name(name: &str) -> Option<Self> {
161 if name.trim().is_empty() {
162 return Some(Self::Project);
163 }
164 Self::NAMES.iter().position(|n| n.eq_ignore_ascii_case(name.trim())).and_then(Self::from_index)
165 }
166}
167
168#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
175pub enum BlendCompositor {
176 #[default]
178 Direct2d,
179 Direct3d,
182}
183
184impl crate::PropEnum for BlendCompositor {
185 const NAMES: &'static [&'static str] = &["direct2d", "direct3d"];
186 fn from_index(index: usize) -> Option<Self> {
187 [Self::Direct2d, Self::Direct3d].get(index).copied()
188 }
189 fn index(self) -> usize {
190 self as usize
191 }
192}
193
194static BLEND_COMPOSITOR: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
195
196pub fn set_blend_compositor(compositor: BlendCompositor) {
198 BLEND_COMPOSITOR.store(compositor as u8, std::sync::atomic::Ordering::Relaxed);
199}
200
201pub fn blend_compositor() -> BlendCompositor {
203 if BLEND_COMPOSITOR.load(std::sync::atomic::Ordering::Relaxed) == 1 { BlendCompositor::Direct3d } else { BlendCompositor::Direct2d }
204}
205
206#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
208pub enum FormBlendCompositor {
209 #[default]
210 Project,
211 Direct2d,
212 Direct3d,
213}
214
215impl FormBlendCompositor {
216 pub fn own(self) -> Option<BlendCompositor> {
218 match self {
219 Self::Project => None,
220 Self::Direct2d => Some(BlendCompositor::Direct2d),
221 Self::Direct3d => Some(BlendCompositor::Direct3d),
222 }
223 }
224}
225
226impl crate::PropEnum for FormBlendCompositor {
227 const NAMES: &'static [&'static str] = &["project", "direct2d", "direct3d"];
228 fn from_index(index: usize) -> Option<Self> {
229 [Self::Project, Self::Direct2d, Self::Direct3d].get(index).copied()
230 }
231 fn index(self) -> usize {
232 self as usize
233 }
234}
235
236static CHOICE: Mutex<GpuChoice> = Mutex::new(GpuChoice::Auto);
237
238pub fn set_choice(choice: GpuChoice) {
240 if let Ok(mut c) = CHOICE.lock() {
241 *c = choice;
242 }
243}
244
245pub fn choice() -> GpuChoice {
247 CHOICE.lock().map(|c| c.clone()).unwrap_or_default()
248}
249
250pub fn choice_for(form_text: &str) -> GpuChoice {
252 if form_text.trim().is_empty() { choice() } else { GpuChoice::parse(form_text) }
253}
254
255static ADAPTER_LIST: OnceLock<fn() -> Vec<String>> = OnceLock::new();
258static ADAPTERS: OnceLock<Vec<String>> = OnceLock::new();
259
260pub fn set_adapter_list(list: fn() -> Vec<String>) {
262 let _ = ADAPTER_LIST.set(list);
263}
264
265pub fn adapters() -> &'static [String] {
267 ADAPTERS.get_or_init(|| ADAPTER_LIST.get().map(|list| list()).unwrap_or_default())
268}
269
270impl crate::Ui {
271
272 pub fn form_gpu(&self, form: ComponentId) -> Option<&str> {
274 self.form_gpus.get(&self.root_of(form)).map(String::as_str)
275 }
276
277 pub fn set_form_gpu(&mut self, form: ComponentId, adapter: Option<String>) {
280 let old = match adapter.clone() {
281 Some(a) => self.form_gpus.insert(form, a),
282 None => self.form_gpus.remove(&form),
283 };
284 if let (Some(old), Some(new)) = (old, adapter)
285 && old != new
286 {
287 self.fire(form, "on_gpu_change", &crate::EventArgs::Text(new));
288 }
289 }
290}
291
292thread_local! {
293 static BEFORE_FLUSH: std::cell::RefCell<Vec<Box<dyn FnOnce()>>> = const { std::cell::RefCell::new(Vec::new()) };
294}
295
296pub fn before_flush(f: impl FnOnce() + 'static) {
301 let _ = BEFORE_FLUSH.try_with(|list| list.borrow_mut().push(Box::new(f)));
302}
303
304pub fn run_before_flush() -> bool {
307 let pending = BEFORE_FLUSH.try_with(|list| std::mem::take(&mut *list.borrow_mut())).unwrap_or_default();
308 let ran = !pending.is_empty();
309 for f in pending {
310 f();
311 }
312 ran
313}