forked from tauri-apps/window-vibrancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal.rs
79 lines (66 loc) · 2.7 KB
/
internal.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use objc2_app_kit::{
NSAppKitVersionNumber, NSAppKitVersionNumber10_10, NSAppKitVersionNumber10_11,
NSAppKitVersionNumber10_14, NSAutoresizingMaskOptions, NSView, NSVisualEffectBlendingMode,
NSVisualEffectMaterial, NSVisualEffectState, NSWindowOrderingMode,
};
use objc2_foundation::{MainThreadMarker, NSInteger};
use std::{ffi::c_void, ptr::NonNull};
use crate::macos::NSVisualEffectViewTagged;
use crate::Error;
/// NSView::tag for NSVisualEffectViewTagged, just a random number
pub const NS_VIEW_TAG_BLUR_VIEW: NSInteger = 91376254;
#[allow(deprecated)]
pub unsafe fn apply_vibrancy(
ns_view: NonNull<c_void>,
appearance: super::NSVisualEffectMaterial,
state: Option<super::NSVisualEffectState>,
radius: Option<f64>,
) -> Result<(), Error> {
let mtm = MainThreadMarker::new().ok_or(Error::NotMainThread(
"\"apply_vibrancy()\" can only be used on the main thread.",
))?;
unsafe {
let view: &NSView = ns_view.cast().as_ref();
if NSAppKitVersionNumber < NSAppKitVersionNumber10_10 {
return Err(Error::UnsupportedPlatformVersion(
"\"apply_vibrancy()\" is only available on macOS 10.0 or newer.",
));
}
let mut m = NSVisualEffectMaterial(appearance as isize);
if (appearance as u32 > 9 && NSAppKitVersionNumber < NSAppKitVersionNumber10_14)
|| (appearance as u32 > 4 && NSAppKitVersionNumber < NSAppKitVersionNumber10_11)
{
m = NSVisualEffectMaterial::AppearanceBased;
}
let bounds = view.bounds();
let blurred_view =
NSVisualEffectViewTagged::initWithFrame(mtm.alloc(), bounds, NS_VIEW_TAG_BLUR_VIEW);
blurred_view.setMaterial(m);
blurred_view.setCornerRadius(radius.unwrap_or(0.0));
blurred_view.setBlendingMode(NSVisualEffectBlendingMode::BehindWindow);
blurred_view.setState(
state
.map(|state| NSVisualEffectState(state as isize))
.unwrap_or(NSVisualEffectState::FollowsWindowActiveState),
);
blurred_view.setAutoresizingMask(
NSAutoresizingMaskOptions::NSViewWidthSizable
| NSAutoresizingMaskOptions::NSViewHeightSizable,
);
view.addSubview_positioned_relativeTo(
&blurred_view,
NSWindowOrderingMode::NSWindowBelow,
None,
);
}
Ok(())
}
pub unsafe fn clear_vibrancy(ns_view: NonNull<c_void>) -> Result<bool, Error> {
let view: &NSView = ns_view.cast().as_ref();
let blurred_view = view.viewWithTag(NS_VIEW_TAG_BLUR_VIEW);
if let Some(blurred_view) = blurred_view {
blurred_view.removeFromSuperview();
return Ok(true);
}
Ok(false)
}