Skip to content

Commit

Permalink
feat(deps): update raw-window-handle to v0.6 (#121)
Browse files Browse the repository at this point in the history
* feat(deps): update raw-window-handle to v0.6

Signed-off-by: rhysd <lin90162@yahoo.co.jp>

* Update Cargo.toml

* add a note about tauri@v1

---------

Signed-off-by: rhysd <lin90162@yahoo.co.jp>
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
  • Loading branch information
rhysd and amrbashir authored Feb 7, 2024
1 parent e110b9a commit 19cdde3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changes/rwh06.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"window-vibrancy": minor
---

**Breaking change** Update `raw-window-handle` crate to 0.6. Now APIs require `HasWindowHandle` trait boundary instead of `HasRawWindowHandle`.
12 changes: 3 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@ default-target = "x86_64-pc-windows-msvc"
targets = [ "x86_64-apple-darwin", "x86_64-pc-windows-msvc" ]

[dependencies]
raw-window-handle = "0.5"
raw-window-handle = "0.6"

[dev-dependencies]
tao = { version = "0.25", features = ["rwh_05"] }
winit = { version = "0.29", default-features = false, features = [
"x11",
"wayland",
"wayland-dlopen",
"wayland-csd-adwaita",
"rwh_05"
] }
tao = "0.25"
winit = "0.29"

[target."cfg(target_os = \"windows\")".dependencies]
windows-version = "0.1"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

Make your windows vibrant.

> [!Tip]
> If you're using `tauri@v1`, you need to use version `0.4` of this crate.
## Platform-specific

- **Linux**: Unsupported, Blur and any vibrancy effects are controlled by the compositor installed on the end-user system.
Expand Down
70 changes: 42 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! ```no_run
//! use window_vibrancy::{apply_vibrancy, apply_blur, NSVisualEffectMaterial};
//!
//! # let window: &dyn raw_window_handle::HasRawWindowHandle = unsafe { std::mem::zeroed() };
//! # let window: &dyn raw_window_handle::HasWindowHandle = unsafe { std::mem::zeroed() };
//! #[cfg(target_os = "macos")]
//! apply_vibrancy(&window, NSVisualEffectMaterial::AppearanceBased, None, None).expect("Unsupported platform! 'apply_vibrancy' is only supported on macOS");
//!
Expand Down Expand Up @@ -45,13 +45,13 @@ pub type Color = (u8, u8, u8, u8);
/// - **Windows**: *`color`* is ignored on Windows 7 and has no effect.
/// - **Linux / macOS**: Unsupported.
pub fn apply_blur(
window: impl raw_window_handle::HasRawWindowHandle,
window: impl raw_window_handle::HasWindowHandle,
#[allow(unused)] color: Option<Color>,
) -> Result<(), Error> {
match window.raw_window_handle() {
match window.window_handle()?.as_raw() {
#[cfg(target_os = "windows")]
raw_window_handle::RawWindowHandle::Win32(handle) => {
windows::apply_blur(handle.hwnd as _, color)
windows::apply_blur(handle.hwnd.get() as _, color)
}
_ => Err(Error::UnsupportedPlatform(
"\"apply_blur()\" is only supported on Windows.",
Expand All @@ -64,10 +64,12 @@ pub fn apply_blur(
/// ## Platform-specific
///
/// - **Linux / macOS**: Unsupported.
pub fn clear_blur(window: impl raw_window_handle::HasRawWindowHandle) -> Result<(), Error> {
match window.raw_window_handle() {
pub fn clear_blur(window: impl raw_window_handle::HasWindowHandle) -> Result<(), Error> {
match window.window_handle()?.as_raw() {
#[cfg(target_os = "windows")]
raw_window_handle::RawWindowHandle::Win32(handle) => windows::clear_blur(handle.hwnd as _),
raw_window_handle::RawWindowHandle::Win32(handle) => {
windows::clear_blur(handle.hwnd.get() as _)
}
_ => Err(Error::UnsupportedPlatform(
"\"clear_blur()\" is only supported on Windows.",
)),
Expand All @@ -88,13 +90,13 @@ pub fn clear_blur(window: impl raw_window_handle::HasRawWindowHandle) -> Result<
/// - **Windows**: *`color`* is ignored on Windows 7 and has no effect.
/// - **Linux / macOS**: Unsupported.
pub fn apply_acrylic(
window: impl raw_window_handle::HasRawWindowHandle,
window: impl raw_window_handle::HasWindowHandle,
#[allow(unused)] color: Option<Color>,
) -> Result<(), Error> {
match window.raw_window_handle() {
match window.window_handle()?.as_raw() {
#[cfg(target_os = "windows")]
raw_window_handle::RawWindowHandle::Win32(handle) => {
windows::apply_acrylic(handle.hwnd as _, color)
windows::apply_acrylic(handle.hwnd.get() as _, color)
}
_ => Err(Error::UnsupportedPlatform(
"\"apply_acrylic()\" is only supported on Windows.",
Expand All @@ -107,11 +109,11 @@ pub fn apply_acrylic(
/// ## Platform-specific
///
/// - **Linux / macOS**: Unsupported.
pub fn clear_acrylic(window: impl raw_window_handle::HasRawWindowHandle) -> Result<(), Error> {
match window.raw_window_handle() {
pub fn clear_acrylic(window: impl raw_window_handle::HasWindowHandle) -> Result<(), Error> {
match window.window_handle()?.as_raw() {
#[cfg(target_os = "windows")]
raw_window_handle::RawWindowHandle::Win32(handle) => {
windows::clear_acrylic(handle.hwnd as _)
windows::clear_acrylic(handle.hwnd.get() as _)
}
_ => Err(Error::UnsupportedPlatform(
"\"clear_acrylic()\" is only supported on Windows.",
Expand All @@ -129,15 +131,15 @@ pub fn clear_acrylic(window: impl raw_window_handle::HasRawWindowHandle) -> Resu
///
/// - **Linux / macOS**: Unsupported.
pub fn apply_mica(
window: impl raw_window_handle::HasRawWindowHandle,
window: impl raw_window_handle::HasWindowHandle,
dark: Option<bool>,
) -> Result<(), Error> {
#[cfg(not(target_os = "windows"))]
let _ = dark;
match window.raw_window_handle() {
match window.window_handle()?.as_raw() {
#[cfg(target_os = "windows")]
raw_window_handle::RawWindowHandle::Win32(handle) => {
windows::apply_mica(handle.hwnd as _, dark)
windows::apply_mica(handle.hwnd.get() as _, dark)
}
_ => Err(Error::UnsupportedPlatform(
"\"apply_mica()\" is only supported on Windows.",
Expand All @@ -150,10 +152,12 @@ pub fn apply_mica(
/// ## Platform-specific
///
/// - **Linux / macOS**: Unsupported.
pub fn clear_mica(window: impl raw_window_handle::HasRawWindowHandle) -> Result<(), Error> {
match window.raw_window_handle() {
pub fn clear_mica(window: impl raw_window_handle::HasWindowHandle) -> Result<(), Error> {
match window.window_handle()?.as_raw() {
#[cfg(target_os = "windows")]
raw_window_handle::RawWindowHandle::Win32(handle) => windows::clear_mica(handle.hwnd as _),
raw_window_handle::RawWindowHandle::Win32(handle) => {
windows::clear_mica(handle.hwnd.get() as _)
}
_ => Err(Error::UnsupportedPlatform(
"\"clear_mica()\" is only supported on Windows.",
)),
Expand All @@ -170,15 +174,15 @@ pub fn clear_mica(window: impl raw_window_handle::HasRawWindowHandle) -> Result<
///
/// - **Linux / macOS**: Unsupported.
pub fn apply_tabbed(
window: impl raw_window_handle::HasRawWindowHandle,
window: impl raw_window_handle::HasWindowHandle,
dark: Option<bool>,
) -> Result<(), Error> {
#[cfg(not(target_os = "windows"))]
let _ = dark;
match window.raw_window_handle() {
match window.window_handle()?.as_raw() {
#[cfg(target_os = "windows")]
raw_window_handle::RawWindowHandle::Win32(handle) => {
windows::apply_tabbed(handle.hwnd as _, dark)
windows::apply_tabbed(handle.hwnd.get() as _, dark)
}
_ => Err(Error::UnsupportedPlatform(
"\"apply_tabbed()\" is only supported on Windows.",
Expand All @@ -191,11 +195,11 @@ pub fn apply_tabbed(
/// ## Platform-specific
///
/// - **Linux / macOS**: Unsupported.
pub fn clear_tabbed(window: impl raw_window_handle::HasRawWindowHandle) -> Result<(), Error> {
match window.raw_window_handle() {
pub fn clear_tabbed(window: impl raw_window_handle::HasWindowHandle) -> Result<(), Error> {
match window.window_handle()?.as_raw() {
#[cfg(target_os = "windows")]
raw_window_handle::RawWindowHandle::Win32(handle) => {
windows::clear_tabbed(handle.hwnd as _)
windows::clear_tabbed(handle.hwnd.get() as _)
}
_ => Err(Error::UnsupportedPlatform(
"\"clear_tabbed()\" is only supported on Windows.",
Expand All @@ -209,15 +213,15 @@ pub fn clear_tabbed(window: impl raw_window_handle::HasRawWindowHandle) -> Resul
///
/// - **Linux / Windows**: Unsupported.
pub fn apply_vibrancy(
window: impl raw_window_handle::HasRawWindowHandle,
window: impl raw_window_handle::HasWindowHandle,
#[allow(unused)] effect: NSVisualEffectMaterial,
#[allow(unused)] state: Option<NSVisualEffectState>,
#[allow(unused)] radius: Option<f64>,
) -> Result<(), Error> {
match window.raw_window_handle() {
match window.window_handle()?.as_raw() {
#[cfg(target_os = "macos")]
raw_window_handle::RawWindowHandle::AppKit(handle) => {
macos::apply_vibrancy(handle.ns_window as _, effect, state, radius)
macos::apply_vibrancy(handle.ns_view.as_ptr() as _, effect, state, radius)
}
_ => Err(Error::UnsupportedPlatform(
"\"apply_vibrancy()\" is only supported on macOS.",
Expand All @@ -230,6 +234,7 @@ pub enum Error {
UnsupportedPlatform(&'static str),
UnsupportedPlatformVersion(&'static str),
NotMainThread(&'static str),
NoWindowHandle(raw_window_handle::HandleError),
}

impl std::fmt::Display for Error {
Expand All @@ -240,8 +245,17 @@ impl std::fmt::Display for Error {
| Error::NotMainThread(e) => {
write!(f, "{}", e)
}
Error::NoWindowHandle(e) => {
write!(f, "{}", e)
}
}
}
}

impl std::error::Error for Error {}

impl From<raw_window_handle::HandleError> for Error {
fn from(err: raw_window_handle::HandleError) -> Self {
Error::NoWindowHandle(err)
}
}
6 changes: 2 additions & 4 deletions src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mod internal {
use cocoa::{
appkit::{
NSAppKitVersionNumber, NSAppKitVersionNumber10_10, NSAppKitVersionNumber10_11,
NSAutoresizingMaskOptions, NSView, NSViewHeightSizable, NSViewWidthSizable, NSWindow,
NSAutoresizingMaskOptions, NSView, NSViewHeightSizable, NSViewWidthSizable,
NSWindowOrderingMode,
},
base::{id, nil, BOOL},
Expand All @@ -91,7 +91,7 @@ mod internal {

#[allow(deprecated)]
pub fn apply_vibrancy(
window: id,
ns_view: id,
appearance: NSVisualEffectMaterial,
state: Option<NSVisualEffectState>,
radius: Option<f64>,
Expand All @@ -117,9 +117,7 @@ mod internal {
m = NSVisualEffectMaterial::AppearanceBased;
}

let ns_view: id = window.contentView();
let bounds = NSView::bounds(ns_view);

let blurred_view =
NSVisualEffectView::initWithFrame_(NSVisualEffectView::alloc(nil), bounds);
blurred_view.autorelease();
Expand Down
2 changes: 1 addition & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::ffi::c_void;
pub use windows_sys::Win32::{
Foundation::*,
Graphics::{Dwm::*, Gdi::*},
System::{LibraryLoader::*, SystemInformation::*},
System::LibraryLoader::*,
};

use crate::{Color, Error};
Expand Down

0 comments on commit 19cdde3

Please sign in to comment.