floem_window_vibrancy/
lib.rs1#![allow(clippy::deprecated_semver)]
25
26mod macos;
27mod windows;
28
29pub use macos::{NSVisualEffectMaterial, NSVisualEffectState};
30
31pub type Color = (u8, u8, u8, u8);
33
34pub fn apply_blur(
48 window: impl raw_window_handle::HasRawWindowHandle,
49 #[allow(unused)] color: Option<Color>,
50) -> Result<(), Error> {
51 match window.raw_window_handle() {
52 #[cfg(target_os = "windows")]
53 raw_window_handle::RawWindowHandle::Win32(handle) => {
54 windows::apply_blur(handle.hwnd as _, color)
55 }
56 _ => Err(Error::UnsupportedPlatform(
57 "\"apply_blur()\" is only supported on Windows.",
58 )),
59 }
60}
61
62pub fn clear_blur(window: impl raw_window_handle::HasRawWindowHandle) -> Result<(), Error> {
68 match window.raw_window_handle() {
69 #[cfg(target_os = "windows")]
70 raw_window_handle::RawWindowHandle::Win32(handle) => windows::clear_blur(handle.hwnd as _),
71 _ => Err(Error::UnsupportedPlatform(
72 "\"clear_blur()\" is only supported on Windows.",
73 )),
74 }
75}
76
77pub fn apply_acrylic(
91 window: impl raw_window_handle::HasRawWindowHandle,
92 #[allow(unused)] color: Option<Color>,
93) -> Result<(), Error> {
94 match window.raw_window_handle() {
95 #[cfg(target_os = "windows")]
96 raw_window_handle::RawWindowHandle::Win32(handle) => {
97 windows::apply_acrylic(handle.hwnd as _, color)
98 }
99 _ => Err(Error::UnsupportedPlatform(
100 "\"apply_acrylic()\" is only supported on Windows.",
101 )),
102 }
103}
104
105pub fn clear_acrylic(window: impl raw_window_handle::HasRawWindowHandle) -> Result<(), Error> {
111 match window.raw_window_handle() {
112 #[cfg(target_os = "windows")]
113 raw_window_handle::RawWindowHandle::Win32(handle) => {
114 windows::clear_acrylic(handle.hwnd as _)
115 }
116 _ => Err(Error::UnsupportedPlatform(
117 "\"clear_acrylic()\" is only supported on Windows.",
118 )),
119 }
120}
121
122pub fn apply_mica(
132 window: impl raw_window_handle::HasRawWindowHandle,
133 dark: Option<bool>,
134) -> Result<(), Error> {
135 #[cfg(not(target_os = "windows"))]
136 let _ = dark;
137 match window.raw_window_handle() {
138 #[cfg(target_os = "windows")]
139 raw_window_handle::RawWindowHandle::Win32(handle) => {
140 windows::apply_mica(handle.hwnd as _, dark)
141 }
142 _ => Err(Error::UnsupportedPlatform(
143 "\"apply_mica()\" is only supported on Windows.",
144 )),
145 }
146}
147
148pub fn clear_mica(window: impl raw_window_handle::HasRawWindowHandle) -> Result<(), Error> {
154 match window.raw_window_handle() {
155 #[cfg(target_os = "windows")]
156 raw_window_handle::RawWindowHandle::Win32(handle) => windows::clear_mica(handle.hwnd as _),
157 _ => Err(Error::UnsupportedPlatform(
158 "\"clear_mica()\" is only supported on Windows.",
159 )),
160 }
161}
162
163pub fn apply_tabbed(
173 window: impl raw_window_handle::HasRawWindowHandle,
174 dark: Option<bool>,
175) -> Result<(), Error> {
176 #[cfg(not(target_os = "windows"))]
177 let _ = dark;
178 match window.raw_window_handle() {
179 #[cfg(target_os = "windows")]
180 raw_window_handle::RawWindowHandle::Win32(handle) => {
181 windows::apply_tabbed(handle.hwnd as _, dark)
182 }
183 _ => Err(Error::UnsupportedPlatform(
184 "\"apply_tabbed()\" is only supported on Windows.",
185 )),
186 }
187}
188
189pub fn clear_tabbed(window: impl raw_window_handle::HasRawWindowHandle) -> Result<(), Error> {
195 match window.raw_window_handle() {
196 #[cfg(target_os = "windows")]
197 raw_window_handle::RawWindowHandle::Win32(handle) => {
198 windows::clear_tabbed(handle.hwnd as _)
199 }
200 _ => Err(Error::UnsupportedPlatform(
201 "\"clear_tabbed()\" is only supported on Windows.",
202 )),
203 }
204}
205
206pub fn apply_vibrancy(
212 window: impl raw_window_handle::HasRawWindowHandle,
213 #[allow(unused)] effect: NSVisualEffectMaterial,
214 #[allow(unused)] state: Option<NSVisualEffectState>,
215 #[allow(unused)] radius: Option<f64>,
216) -> Result<(), Error> {
217 match window.raw_window_handle() {
218 #[cfg(target_os = "macos")]
219 raw_window_handle::RawWindowHandle::AppKit(handle) => {
220 macos::apply_vibrancy(handle.ns_window as _, effect, state, radius)
221 }
222 _ => Err(Error::UnsupportedPlatform(
223 "\"apply_vibrancy()\" is only supported on macOS.",
224 )),
225 }
226}
227
228#[derive(Debug)]
229pub enum Error {
230 UnsupportedPlatform(&'static str),
231 UnsupportedPlatformVersion(&'static str),
232 NotMainThread(&'static str),
233}
234
235impl std::fmt::Display for Error {
236 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
237 match self {
238 Error::UnsupportedPlatform(e)
239 | Error::UnsupportedPlatformVersion(e)
240 | Error::NotMainThread(e) => {
241 write!(f, "{}", e)
242 }
243 }
244 }
245}
246
247impl std::error::Error for Error {}