diff --git a/window/src/connection.rs b/window/src/connection.rs index c37ffe6f0..d36845662 100644 --- a/window/src/connection.rs +++ b/window/src/connection.rs @@ -1,4 +1,4 @@ -use crate::Connection; +use crate::{Appearance, Connection}; use anyhow::Result as Fallible; use std::cell::RefCell; use std::rc::Rc; @@ -36,6 +36,11 @@ pub trait ConnectionOps { fn terminate_message_loop(&self); fn run_message_loop(&self) -> Fallible<()>; + /// Retrieve the current appearance for the application. + fn get_appearance(&self) -> Appearance { + Appearance::Light + } + /// Hide the application. /// This actions hides all of the windows of the application and switches /// focus away from it. diff --git a/window/src/lib.rs b/window/src/lib.rs index dea63c900..2a5632712 100644 --- a/window/src/lib.rs +++ b/window/src/lib.rs @@ -61,6 +61,34 @@ pub enum MouseCursor { SizeLeftRight, } +/// Represents the preferred appearance of the windowing +/// environment. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Appearance { + /// Standard dark-text-on-light-background presentation + Light, + /// Dark mode, with predominantly dark or muted colors + Dark, + /// dark-text-on-light-background, but in a higher contrast + /// more accesible palette + LightHighContrast, + /// darker background but with higher contrast than regular + /// dark mode + DarkHighContrast, +} + +impl std::string::ToString for Appearance { + fn to_string(&self) -> String { + match self { + Self::Light => "Light", + Self::Dark => "Dark", + Self::LightHighContrast => "LightHighContrast", + Self::DarkHighContrast => "DarkHighContrast", + } + .to_string() + } +} + #[derive(Debug)] pub enum WindowEvent { /// Called when the window close button is clicked. diff --git a/window/src/os/macos/connection.rs b/window/src/os/macos/connection.rs index 741218077..f7dd1a142 100644 --- a/window/src/os/macos/connection.rs +++ b/window/src/os/macos/connection.rs @@ -1,9 +1,11 @@ // let () = msg_send! is a common pattern for objc #![allow(clippy::let_unit_value)] +use super::nsstring_to_str; use super::window::WindowInner; use crate::connection::ConnectionOps; use crate::spawn::*; +use crate::Appearance; use cocoa::appkit::{NSApp, NSApplication, NSApplicationActivationPolicyRegular}; use cocoa::base::{id, nil}; use objc::*; @@ -81,6 +83,22 @@ impl ConnectionOps for Connection { } } + fn get_appearance(&self) -> Appearance { + let name = unsafe { + let appearance: id = msg_send![self.ns_app, effectiveAppearance]; + nsstring_to_str(msg_send![appearance, name]) + }; + match name { + "NSAppearanceNameVibrantDark" | "NSAppearanceNameDarkAqua" => Appearance::Dark, + "NSAppearanceNameVibrantLight" | "NSAppearanceNameAqua" => Appearance::Light, + "NSAppearanceNameAccessibilityHighContrastVibrantLight" + | "NSAppearanceNameAccessibilityHighContrastAqua" => Appearance::LightHighContrast, + "NSAppearanceNameAccessibilityHighContrastVibrantDark" + | "NSAppearanceNameAccessibilityHighContrastDarkAqua" => Appearance::DarkHighContrast, + _ => Appearance::Light, + } + } + fn run_message_loop(&self) -> anyhow::Result<()> { unsafe { self.ns_app.run();