Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mica Tabbed backdrop type support #101

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/tabbed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"window-vibrancy": "patch"
---

On Windows, add `apply_tabbed` and `clear_tabbed`
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,49 @@ pub fn clear_mica(window: impl raw_window_handle::HasRawWindowHandle) -> Result<
}
}

/// Applies mica tabbed effect to window. Works only on Windows 11.
///
/// ## Arguments
///
/// - `dark`: If `None` is provide, it will match the system preference
///
/// ## Platform-specific
///
/// - **Linux / macOS**: Unsupported.
pub fn apply_tabbed(
window: impl raw_window_handle::HasRawWindowHandle,
dark: Option<bool>,
) -> Result<(), Error> {
#[cfg(not(target_os = "windows"))]
let _ = dark;
match window.raw_window_handle() {
#[cfg(target_os = "windows")]
raw_window_handle::RawWindowHandle::Win32(handle) => {
windows::apply_tabbed(handle.hwnd as _, dark)
}
_ => Err(Error::UnsupportedPlatform(
"\"apply_tabbed()\" is only supported on Windows.",
)),
}
}

/// Clears mica tabbed effect applied to window. Works only on Windows 11.
///
/// ## Platform-specific
///
/// - **Linux / macOS**: Unsupported.
pub fn clear_tabbed(window: impl raw_window_handle::HasRawWindowHandle) -> Result<(), Error> {
match window.raw_window_handle() {
#[cfg(target_os = "windows")]
raw_window_handle::RawWindowHandle::Win32(handle) => {
windows::clear_tabbed(handle.hwnd as _)
}
_ => Err(Error::UnsupportedPlatform(
"\"clear_tabbed()\" is only supported on Windows.",
)),
}
}

/// Applies macos vibrancy effect to window. Works only on macOS 10.10 or newer.
///
/// ## Platform-specific
Expand Down
47 changes: 47 additions & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,53 @@ pub fn clear_mica(hwnd: HWND) -> Result<(), Error> {
Ok(())
}

pub fn apply_tabbed(hwnd: HWND, dark: Option<bool>) -> Result<(), Error> {
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 {
DwmSetWindowAttribute(
hwnd,
DWMWA_SYSTEMBACKDROP_TYPE,
&DWM_SYSTEMBACKDROP_TYPE::DWMSBT_TABBEDWINDOW as *const _ as _,
4,
);
}
} else {
return Err(Error::UnsupportedPlatformVersion(
"\"apply_tabbed()\" is only available on Windows 11.",
));
}
Ok(())
}

pub fn clear_tabbed(hwnd: HWND) -> Result<(), Error> {
if is_backdroptype_supported() {
unsafe {
DwmSetWindowAttribute(
hwnd,
DWMWA_SYSTEMBACKDROP_TYPE,
&DWM_SYSTEMBACKDROP_TYPE::DWMSBT_DISABLE as *const _ as _,
4,
);
}
} else {
return Err(Error::UnsupportedPlatformVersion(
"\"clear_tabbed()\" is only available on Windows 11.",
));
}
Ok(())
}

fn get_function_impl(library: &str, function: &str) -> Option<FARPROC> {
assert_eq!(library.chars().last(), Some('\0'));
assert_eq!(function.chars().last(), Some('\0'));
Expand Down