Browse Source

feat(core): add dark/light mica option (#7384)

* feat(core): add dark/light mica option

* Update .changes/dark-light-mica-effect.md

* fix macos build
Amr Bashir 2 năm trước cách đây
mục cha
commit
74b1f4fc66

+ 5 - 0
.changes/dark-light-mica-effect.md

@@ -0,0 +1,5 @@
+---
+'tauri-utils': 'patch:feat'
+---
+
+Add `WindowEffect::MicaDark` and `WindowEffect::MicaLight`

+ 15 - 1
core/tauri-config-schema/schema.json

@@ -771,12 +771,26 @@
           ]
         },
         {
-          "description": "*Windows 11 Only**",
+          "description": "Mica effect that matches the system dark perefence **Windows 11 Only**",
           "type": "string",
           "enum": [
             "mica"
           ]
         },
+        {
+          "description": "Mica effect with dark mode but only if dark mode is enabled on the system **Windows 11 Only**",
+          "type": "string",
+          "enum": [
+            "micaDark"
+          ]
+        },
+        {
+          "description": "Mica effect with light mode **Windows 11 Only**",
+          "type": "string",
+          "enum": [
+            "micaLight"
+          ]
+        },
         {
           "description": "**Windows 7/10/11(22H1) Only**\n\n## Notes\n\nThis effect has bad performance when resizing/dragging the window on Windows 11 build 22621.",
           "type": "string",

+ 2 - 0
core/tauri-utils/src/config.rs

@@ -2111,6 +2111,8 @@ mod build {
         WindowEffect::UnderWindowBackground => quote! { #prefix::UnderWindowBackground},
         WindowEffect::UnderPageBackground => quote! { #prefix::UnderPageBackground},
         WindowEffect::Mica => quote! { #prefix::Mica},
+        WindowEffect::MicaDark => quote! { #prefix::MicaDark},
+        WindowEffect::MicaLight => quote! { #prefix::MicaLight},
         WindowEffect::Blur => quote! { #prefix::Blur},
         WindowEffect::Acrylic => quote! { #prefix::Acrylic},
       })

+ 5 - 1
core/tauri-utils/src/lib.rs

@@ -118,8 +118,12 @@ mod window_effects {
     UnderWindowBackground,
     /// **macOS 10.14+**
     UnderPageBackground,
-    /// **Windows 11 Only**
+    /// Mica effect that matches the system dark perefence **Windows 11 Only**
     Mica,
+    /// Mica effect with dark mode but only if dark mode is enabled on the system **Windows 11 Only**
+    MicaDark,
+    /// Mica effect with light mode **Windows 11 Only**
+    MicaLight,
     /// **Windows 7/10/11(22H1) Only**
     ///
     /// ## Notes

+ 1 - 1
core/tauri/src/vibrancy/macos.rs

@@ -283,7 +283,7 @@ impl From<crate::window::Effect> for NSVisualEffectMaterial {
       Effect::ContentBackground => NSVisualEffectMaterial::ContentBackground,
       Effect::UnderWindowBackground => NSVisualEffectMaterial::UnderWindowBackground,
       Effect::UnderPageBackground => NSVisualEffectMaterial::UnderPageBackground,
-      Effect::Mica | Effect::Blur | Effect::Acrylic => unreachable!(),
+      _ => unreachable!(),
     }
   }
 }

+ 24 - 7
core/tauri/src/vibrancy/windows.rs

@@ -12,7 +12,9 @@ use std::ffi::c_void;
 use crate::utils::config::WindowEffectsConfig;
 use crate::window::{Color, Effect};
 use tauri_utils::platform::{get_function_impl, is_windows_7, windows_version};
-use windows::Win32::Graphics::Dwm::{DwmSetWindowAttribute, DWMWINDOWATTRIBUTE};
+use windows::Win32::Graphics::Dwm::{
+  DwmSetWindowAttribute, DWMWA_USE_IMMERSIVE_DARK_MODE, DWMWINDOWATTRIBUTE,
+};
 use windows::Win32::{
   Foundation::{BOOL, HWND},
   Graphics::{
@@ -23,10 +25,12 @@ use windows::Win32::{
 
 pub fn apply_effects(window: HWND, effects: WindowEffectsConfig) {
   let WindowEffectsConfig { effects, color, .. } = effects;
-  let effect = if let Some(effect) = effects
-    .iter()
-    .find(|e| matches!(e, Effect::Mica | Effect::Acrylic | Effect::Blur))
-  {
+  let effect = if let Some(effect) = effects.iter().find(|e| {
+    matches!(
+      e,
+      Effect::Mica | Effect::MicaDark | Effect::MicaLight | Effect::Acrylic | Effect::Blur
+    )
+  }) {
     effect
   } else {
     return;
@@ -35,7 +39,9 @@ pub fn apply_effects(window: HWND, effects: WindowEffectsConfig) {
   match effect {
     Effect::Blur => apply_blur(window, color),
     Effect::Acrylic => apply_acrylic(window, color),
-    Effect::Mica => apply_mica(window),
+    Effect::Mica => apply_mica(window, None),
+    Effect::MicaDark => apply_mica(window, Some(true)),
+    Effect::MicaLight => apply_mica(window, Some(false)),
     _ => unreachable!(),
   }
 }
@@ -114,7 +120,18 @@ pub fn clear_acrylic(hwnd: HWND) {
   }
 }
 
-pub fn apply_mica(hwnd: HWND) {
+pub fn apply_mica(hwnd: HWND, dark: Option<bool>) {
+  if let Some(dark) = dark {
+    unsafe {
+      DwmSetWindowAttribute(
+        hwnd,
+        DWMWA_USE_IMMERSIVE_DARK_MODE,
+        &(dark as u32) as *const _ as _,
+        4,
+      );
+    }
+  }
+
   if is_backdroptype_supported() {
     unsafe {
       let _ = DwmSetWindowAttribute(

+ 15 - 1
tooling/cli/schema.json

@@ -771,12 +771,26 @@
           ]
         },
         {
-          "description": "*Windows 11 Only**",
+          "description": "Mica effect that matches the system dark perefence **Windows 11 Only**",
           "type": "string",
           "enum": [
             "mica"
           ]
         },
+        {
+          "description": "Mica effect with dark mode but only if dark mode is enabled on the system **Windows 11 Only**",
+          "type": "string",
+          "enum": [
+            "micaDark"
+          ]
+        },
+        {
+          "description": "Mica effect with light mode **Windows 11 Only**",
+          "type": "string",
+          "enum": [
+            "micaLight"
+          ]
+        },
         {
           "description": "**Windows 7/10/11(22H1) Only**\n\n## Notes\n\nThis effect has bad performance when resizing/dragging the window on Windows 11 build 22621.",
           "type": "string",