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
This commit is contained in:
Amr Bashir 2023-07-10 14:31:29 +03:00 committed by GitHub
parent fd5dc788d1
commit 74b1f4fc66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 11 deletions

View File

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

View File

@ -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",

View File

@ -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},
})

View File

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

View File

@ -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!(),
}
}
}

View File

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

View File

@ -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",