1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 23:21:08 +03:00

window: introduce interface for determining if dark mode is active

This is a baby step: it introduces the Appearance concept
and provides an accessor at the Connection level.

Only macos implements this at this time, and nothing else
makes use of it.

refs: https://github.com/wez/wezterm/issues/894
refs: https://github.com/wez/wezterm/issues/806
This commit is contained in:
Wez Furlong 2021-07-16 21:51:42 -07:00
parent 75eaaab6a1
commit daaa3299d6
3 changed files with 52 additions and 1 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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();