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,
Composed,
Transparent,
}
impl PresentMode {
pub fn parse(text: &str) -> PresentMode {
match text.trim().to_lowercase().as_str() {
"composition" | "composicao" | "composição" => PresentMode::Composition,
"composed" | "composto" => PresentMode::Composed,
"transparent" | "transparente" => PresentMode::Transparent,
_ => PresentMode::Window,
}
}
pub fn name(self) -> &'static str {
match self {
PresentMode::Window => "",
PresentMode::Composition => "composition",
PresentMode::Composed => "composed",
PresentMode::Transparent => "transparent",
}
}
}
static PRESENT: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
pub fn set_present(mode: PresentMode) {
let code = match mode {
PresentMode::Window => 0,
PresentMode::Composition => 1,
PresentMode::Composed => 2,
PresentMode::Transparent => 3,
};
PRESENT.store(code, std::sync::atomic::Ordering::Relaxed);
}
pub fn present() -> PresentMode {
match PRESENT.load(std::sync::atomic::Ordering::Relaxed) {
1 => PresentMode::Composition,
2 => PresentMode::Composed,
3 => PresentMode::Transparent,
_ => PresentMode::Window,
}
}
pub fn present_for(form_text: &str) -> PresentMode {
if form_text.trim().is_empty() || form_text.trim() == "project" { present() } else { PresentMode::parse(form_text) }
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FormPresent {
#[default]
Project,
Window,
Composition,
Composed,
}
impl crate::PropEnum for FormPresent {
const NAMES: &'static [&'static str] = &["project", "window", "composition", "composed"];
fn from_index(index: usize) -> Option<Self> {
[Self::Project, Self::Window, Self::Composition, Self::Composed].get(index).copied()
}
fn index(self) -> usize {
self as usize
}
fn from_name(name: &str) -> Option<Self> {
if name.trim().is_empty() {
return Some(Self::Project);
}
Self::NAMES.iter().position(|n| n.eq_ignore_ascii_case(name.trim())).and_then(Self::from_index)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BlendCompositor {
#[default]
Direct2d,
Direct3d,
}
impl crate::PropEnum for BlendCompositor {
const NAMES: &'static [&'static str] = &["direct2d", "direct3d"];
fn from_index(index: usize) -> Option<Self> {
[Self::Direct2d, Self::Direct3d].get(index).copied()
}
fn index(self) -> usize {
self as usize
}
}
static BLEND_COMPOSITOR: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
pub fn set_blend_compositor(compositor: BlendCompositor) {
BLEND_COMPOSITOR.store(compositor as u8, std::sync::atomic::Ordering::Relaxed);
}
pub fn blend_compositor() -> BlendCompositor {
if BLEND_COMPOSITOR.load(std::sync::atomic::Ordering::Relaxed) == 1 { BlendCompositor::Direct3d } else { BlendCompositor::Direct2d }
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FormBlendCompositor {
#[default]
Project,
Direct2d,
Direct3d,
}
impl FormBlendCompositor {
pub fn own(self) -> Option<BlendCompositor> {
match self {
Self::Project => None,
Self::Direct2d => Some(BlendCompositor::Direct2d),
Self::Direct3d => Some(BlendCompositor::Direct3d),
}
}
}
impl crate::PropEnum for FormBlendCompositor {
const NAMES: &'static [&'static str] = &["project", "direct2d", "direct3d"];
fn from_index(index: usize) -> Option<Self> {
[Self::Project, Self::Direct2d, Self::Direct3d].get(index).copied()
}
fn index(self) -> usize {
self as usize
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ColorOutput {
#[default]
Standard,
Deep,
Hdr,
}
impl ColorOutput {
pub fn bits(self) -> u32 {
match self {
Self::Standard => 8,
Self::Deep => 10,
Self::Hdr => 16,
}
}
}
impl crate::PropEnum for ColorOutput {
const NAMES: &'static [&'static str] = &["standard", "deep", "hdr"];
fn from_index(index: usize) -> Option<Self> {
[Self::Standard, Self::Deep, Self::Hdr].get(index).copied()
}
fn index(self) -> usize {
self as usize
}
}
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
}