use std::ffi::CStr;
use wxdragon_sys as ffi;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub enum Appearance {
Light,
Dark,
System,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub enum AppearanceResult {
Ok,
Failure,
CannotChange,
}
impl From<Appearance> for ffi::wxd_Appearance {
fn from(appearance: Appearance) -> Self {
match appearance {
Appearance::Light => ffi::wxd_Appearance_WXD_APPEARANCE_LIGHT,
Appearance::Dark => ffi::wxd_Appearance_WXD_APPEARANCE_DARK,
Appearance::System => ffi::wxd_Appearance_WXD_APPEARANCE_SYSTEM,
}
}
}
impl From<ffi::wxd_Appearance> for Appearance {
fn from(appearance: ffi::wxd_Appearance) -> Self {
match appearance {
ffi::wxd_Appearance_WXD_APPEARANCE_LIGHT => Appearance::Light,
ffi::wxd_Appearance_WXD_APPEARANCE_DARK => Appearance::Dark,
ffi::wxd_Appearance_WXD_APPEARANCE_SYSTEM => Appearance::System,
_ => Appearance::System, }
}
}
impl From<ffi::wxd_AppearanceResult> for AppearanceResult {
fn from(result: ffi::wxd_AppearanceResult) -> Self {
match result {
ffi::wxd_AppearanceResult_WXD_APPEARANCE_RESULT_OK => AppearanceResult::Ok,
ffi::wxd_AppearanceResult_WXD_APPEARANCE_RESULT_FAILURE => AppearanceResult::Failure,
ffi::wxd_AppearanceResult_WXD_APPEARANCE_RESULT_CANNOT_CHANGE => AppearanceResult::CannotChange,
_ => AppearanceResult::Failure, }
}
}
pub struct SystemAppearance {
ptr: *mut ffi::wxd_SystemAppearance_t,
}
impl SystemAppearance {
pub(crate) unsafe fn from_ptr(ptr: *mut ffi::wxd_SystemAppearance_t) -> Option<Self> {
if ptr.is_null() { None } else { Some(SystemAppearance { ptr }) }
}
pub fn is_dark(&self) -> bool {
unsafe { ffi::wxd_SystemAppearance_IsDark(self.ptr) }
}
pub fn is_using_dark_background(&self) -> bool {
unsafe { ffi::wxd_SystemAppearance_IsUsingDarkBackground(self.ptr) }
}
pub fn get_name(&self) -> String {
let len = unsafe { ffi::wxd_SystemAppearance_GetName(self.ptr, std::ptr::null_mut(), 0) };
if len <= 0 {
return String::new();
}
let mut b = vec![0; len as usize + 1];
unsafe { ffi::wxd_SystemAppearance_GetName(self.ptr, b.as_mut_ptr(), b.len()) };
unsafe { CStr::from_ptr(b.as_ptr()).to_string_lossy().to_string() }
}
}
impl Drop for SystemAppearance {
fn drop(&mut self) {
unsafe {
ffi::wxd_SystemAppearance_Destroy(self.ptr);
}
}
}
unsafe impl Send for SystemAppearance {}
unsafe impl Sync for SystemAppearance {}
pub fn get_system_appearance() -> Option<SystemAppearance> {
unsafe {
let ptr = ffi::wxd_SystemSettings_GetAppearance();
SystemAppearance::from_ptr(ptr)
}
}
pub fn is_system_dark_mode() -> bool {
get_system_appearance()
.map(|appearance| appearance.is_dark())
.unwrap_or(false)
}
pub trait AppAppearance {
fn set_appearance(&self, appearance: Appearance) -> AppearanceResult;
}
pub struct App {
ptr: *mut ffi::wxd_App_t,
}
impl App {
pub(crate) unsafe fn from_ptr(ptr: *mut ffi::wxd_App_t) -> Option<Self> {
if ptr.is_null() { None } else { Some(App { ptr }) }
}
}
impl AppAppearance for App {
fn set_appearance(&self, appearance: Appearance) -> AppearanceResult {
unsafe {
let result = ffi::wxd_App_SetAppearance(self.ptr, appearance.into());
result.into()
}
}
}
pub fn get_app() -> Option<App> {
unsafe {
let ptr = ffi::wxd_GetApp();
App::from_ptr(ptr)
}
}