use std::sync::{Mutex, OnceLock};
use crate::ComponentId;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum GpuChoice {
#[default]
Auto,
HighPerformance,
LowPower,
FollowMonitor,
Named(String),
}
impl GpuChoice {
pub fn parse(text: &str) -> GpuChoice {
match text.trim().to_lowercase().as_str() {
"" | "auto" | "automatica" | "automática" => GpuChoice::Auto,
"alto-desempenho" | "high-performance" | "alto" | "high" => GpuChoice::HighPerformance,
"economia" | "low-power" | "baixo" | "low" => GpuChoice::LowPower,
"monitor" | "seguir-monitor" | "follow-monitor" => GpuChoice::FollowMonitor,
_ => GpuChoice::Named(text.trim().to_string()),
}
}
pub fn to_text(&self) -> String {
match self {
GpuChoice::Auto => String::new(),
GpuChoice::HighPerformance => "alto-desempenho".into(),
GpuChoice::LowPower => "economia".into(),
GpuChoice::FollowMonitor => "monitor".into(),
GpuChoice::Named(name) => name.clone(),
}
}
pub fn matches_name(&self, adapter: &str) -> bool {
match self {
GpuChoice::Named(name) => adapter.to_lowercase().contains(&name.to_lowercase()),
_ => false,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PresentMode {
#[default]
Window,
Composition,
}
impl PresentMode {
pub fn parse(text: &str) -> PresentMode {
match text.trim().to_lowercase().as_str() {
"composition" | "composicao" | "composição" => PresentMode::Composition,
_ => PresentMode::Window,
}
}
pub fn name(self) -> &'static str {
match self {
PresentMode::Window => "",
PresentMode::Composition => "composition",
}
}
}
static PRESENT: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
pub fn set_present(mode: PresentMode) {
PRESENT.store(mode == PresentMode::Composition, std::sync::atomic::Ordering::Relaxed);
}
pub fn present() -> PresentMode {
if PRESENT.load(std::sync::atomic::Ordering::Relaxed) { PresentMode::Composition } else { PresentMode::Window }
}
static CHOICE: Mutex<GpuChoice> = Mutex::new(GpuChoice::Auto);
pub fn set_choice(choice: GpuChoice) {
if let Ok(mut c) = CHOICE.lock() {
*c = choice;
}
}
pub fn choice() -> GpuChoice {
CHOICE.lock().map(|c| c.clone()).unwrap_or_default()
}
pub fn choice_for(form_text: &str) -> GpuChoice {
if form_text.trim().is_empty() { choice() } else { GpuChoice::parse(form_text) }
}
static ADAPTER_LIST: OnceLock<fn() -> Vec<String>> = OnceLock::new();
static ADAPTERS: OnceLock<Vec<String>> = OnceLock::new();
pub fn set_adapter_list(list: fn() -> Vec<String>) {
let _ = ADAPTER_LIST.set(list);
}
pub fn adapters() -> &'static [String] {
ADAPTERS.get_or_init(|| ADAPTER_LIST.get().map(|list| list()).unwrap_or_default())
}
impl crate::Ui {
pub fn form_gpu(&self, form: ComponentId) -> Option<&str> {
self.form_gpus.get(&self.root_of(form)).map(String::as_str)
}
pub fn set_form_gpu(&mut self, form: ComponentId, adapter: Option<String>) {
let old = match adapter.clone() {
Some(a) => self.form_gpus.insert(form, a),
None => self.form_gpus.remove(&form),
};
if let (Some(old), Some(new)) = (old, adapter)
&& old != new
{
self.fire(form, "on_gpu_change", &crate::EventArgs::Text(new));
}
}
}
thread_local! {
static BEFORE_FLUSH: std::cell::RefCell<Vec<Box<dyn FnOnce()>>> = const { std::cell::RefCell::new(Vec::new()) };
}
pub fn before_flush(f: impl FnOnce() + 'static) {
let _ = BEFORE_FLUSH.try_with(|list| list.borrow_mut().push(Box::new(f)));
}
pub fn run_before_flush() -> bool {
let pending = BEFORE_FLUSH.try_with(|list| std::mem::take(&mut *list.borrow_mut())).unwrap_or_default();
let ran = !pending.is_empty();
for f in pending {
f();
}
ran
}