Skip to content

Commit

Permalink
feat(core): add dark/light mica option (#7384)
Browse files Browse the repository at this point in the history
* feat(core): add dark/light mica option

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

* fix macos build
  • Loading branch information
amrbashir authored Jul 10, 2023
1 parent fd5dc78 commit 74b1f4f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/dark-light-mica-effect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-utils': 'patch:feat'
---

Add `WindowEffect::MicaDark` and `WindowEffect::MicaLight`
16 changes: 15 additions & 1 deletion core/tauri-config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
})
Expand Down
6 changes: 5 additions & 1 deletion core/tauri-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/vibrancy/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
}
}
}
Expand Down
31 changes: 24 additions & 7 deletions core/tauri/src/vibrancy/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;
Expand All @@ -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!(),
}
}
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 15 additions & 1 deletion tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 74b1f4f

Please sign in to comment.