### Complete NSVisualEffectViewTagged Usage Example Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/ns-visual-effect-view.md Demonstrates the setup of an NSVisualEffectViewTagged for macOS, including material, blending mode, corner radius, and state configuration. Ensure this code runs on the main thread and that the parent view is correctly identified. ```rust #[cfg(target_os = "macos")] unsafe fn setup_effect_view(parent_view: &NSView) -> Result<(), Box> { use objc2::MainThreadMarker; use objc2_app_kit::{NSAutoresizingMaskOptions, NSVisualEffectBlendingMode, NSWindowOrderingMode}; use window_vibrancy::{NSVisualEffectMaterial, NSVisualEffectState, NSVisualEffectViewTagged, NS_VIEW_TAG_BLUR_VIEW}; let mtm = MainThreadMarker::new().ok_or("Not on main thread")?; // Get parent frame let bounds = parent_view.bounds(); // Create and initialize let view = NSVisualEffectViewTagged::initWithFrame( mtm.alloc(), bounds, NS_VIEW_TAG_BLUR_VIEW, ); // Configure view.setMaterial(NSVisualEffectMaterial::HudWindow); view.setBlendingMode(NSVisualEffectBlendingMode::BehindWindow); view.setCornerRadius(12.0); view.setState(NSVisualEffectState::Active); view.setAutoresizingMask( NSAutoresizingMaskOptions::ViewWidthSizable | NSAutoresizingMaskOptions::ViewHeightSizable ); // Add to view hierarchy parent_view.addSubview_positioned_relativeTo(&view, NSWindowOrderingMode::Below, None); Ok(()) } ``` -------------------------------- ### Apply Clear Liquid Glass Example Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/types.md Example of applying a clear liquid glass effect to a window with a specified radius. Requires the 'window-vibrancy' crate and macOS 26.0+. ```rust use window_vibrancy::{apply_liquid_glass, NSGlassEffectViewStyle}; #[cfg(target_os = "macos")] apply_liquid_glass( &window, NSGlassEffectViewStyle::Clear, None, Some(12.0), )?; ``` -------------------------------- ### Apply Acrylic/Vibrancy with Tao Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md Integrates window vibrancy with the Tao event loop. This example shows how to set up a transparent, undecorated window and apply acrylic effects on Windows or vibrancy effects on macOS. ```rust use tao:: event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, }; #[cfg(target_os = "windows")] use window_vibrancy::apply_acrylic; #[cfg(target_os = "macos")] use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial}; fn main() { let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_transparent(true) .with_decorations(false) .build(&event_loop) .unwrap(); #[cfg(target_os = "windows")] apply_acrylic(&window, None).expect("Failed to apply acrylic"); #[cfg(target_os = "macos")] apply_vibrancy( &window, NSVisualEffectMaterial::HudWindow, None, None, ).expect("Failed to apply vibrancy"); event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; // Handle events... }); } ``` -------------------------------- ### Apply Active Vibrancy Example Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/types.md Example of applying an active vibrancy effect to a window using NSVisualEffectMaterial::Sidebar and NSVisualEffectState::Active. Requires the 'window-vibrancy' crate and macOS. ```rust use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial, NSVisualEffectState}; #[cfg(target_os = "macos")] apply_vibrancy( &window, NSVisualEffectMaterial::Sidebar, Some(NSVisualEffectState::Active), None, )?; ``` -------------------------------- ### Handling Window Vibrancy Errors in Rust Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/types.md Demonstrates how to match and handle different error types returned by the `apply_blur` function. This example specifically targets Windows and shows how to react to unsupported platforms, version issues, and Win32 API errors. ```rust use window_vibrancy::{apply_blur, Error}; #[cfg(target_os = "windows")] match apply_blur(&window, None) { Ok(()) => println!("Blur applied"), Err(Error::UnsupportedPlatform(msg)) => eprintln!("Not supported: {}", msg), Err(Error::UnsupportedPlatformVersion(msg)) => eprintln!("Version too old: {}", msg), Err(Error::Win32Error { api, result }) => eprintln!("{} failed: 0x{:x}", api, result), Err(e) => eprintln!("Error: {}", e), } ``` -------------------------------- ### Runtime Platform Detection with Conditional Compilation Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/platform-support.md Use conditional compilation to execute platform-specific code. This example shows how to set up effects differently for Windows and macOS, with a fallback for other systems. ```rust #[cfg(target_os = "windows")] fn setup_windows() { apply_blur(&window, None).ok(); } #[cfg(target_os = "macos")] fn setup_macos() { apply_vibrancy( &window, NSVisualEffectMaterial::HudWindow, None, None, ).ok(); } #[cfg(not(any(target_os = "windows", target_os = "macos")))] fn setup_other() { // Linux or other platforms eprintln!("Vibrancy effects not supported"); } ``` -------------------------------- ### Mitigate UnsupportedPlatformVersion Error with Runtime Version Detection Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/errors.md Implement a fallback strategy using runtime version detection to apply the best available visual effect based on the operating system version. This example demonstrates trying `apply_mica`, then `apply_acrylic`, and finally `apply_blur` on Windows. ```rust fn apply_best_effect(window: &Window) -> Result<(), Box> { #[cfg(target_os = "windows")] { use window_vibrancy::{apply_mica, apply_acrylic, apply_blur}; if let Ok(()) = apply_mica(window, None) { return Ok(()); } if let Ok(()) = apply_acrylic(window, None) { return Ok(()); } apply_blur(window, None)?; } Ok(()) } ``` -------------------------------- ### Apply Vibrancy with NSVisualEffectMaterial Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/types.md Example of applying a HUD window visual effect to a Tauri window on macOS. Ensure the `window-vibrancy` crate is imported and the target OS is macOS. ```rust use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial}; #[cfg(target_os = "macos")] apply_vibrancy( &window, NSVisualEffectMaterial::HudWindow, None, None, )?; ``` -------------------------------- ### Cross-Platform Window Effect Setup Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md This Rust code sets up window vibrancy effects. It attempts to apply platform-specific effects like Mica, Acrylic, or Blur on Windows, and Liquid Glass or Vibrancy on macOS. Includes fallbacks for older OS versions and a message for unsupported platforms. ```rust use raw_window_handle::HasWindowHandle; #[cfg(target_os = "windows")] use window_vibrancy::{apply_mica, apply_acrylic, apply_blur, Error}; #[cfg(target_os = "macos")] use window_vibrancy::{apply_liquid_glass, apply_vibrancy, NSGlassEffectViewStyle, NSVisualEffectMaterial, Error}; fn setup_window_effect(window: &impl HasWindowHandle) -> Result<(), Box> { #[cfg(target_os = "windows")] { // Windows: try mica → acrylic → blur match apply_mica(window, None) { Ok(()) => println!("Applied Windows 11 mica effect"), Err(Error::UnsupportedPlatformVersion(_)) => { match apply_acrylic(window, None) { Ok(()) => println!("Applied Windows 10 acrylic effect"), Err(Error::UnsupportedPlatformVersion(_)) => { apply_blur(window, None)?; println!("Applied Windows 7+ blur effect"); } Err(e) => return Err(e.into()), } } Err(e) => return Err(e.into()), } } #[cfg(target_os = "macos")] { // macOS: try liquid glass → vibrancy match apply_liquid_glass( window, NSGlassEffectViewStyle::Regular, Some((255, 255, 255, 50)), Some(12.0), ) { Ok(()) => println!("Applied macOS 26.0+ liquid glass effect"), Err(Error::UnsupportedPlatformVersion(_)) => { apply_vibrancy( window, NSVisualEffectMaterial::HudWindow, None, Some(12.0), )?; println!("Applied macOS 10.10+ vibrancy effect"); } Err(e) => return Err(e.into()), } } #[cfg(not(any(target_os = "windows", target_os = "macos")))] { eprintln!("Window vibrancy not supported on this platform"); } Ok(()) } // Usage fn main() { // Assuming `window` is obtained from your windowing library if let Err(e) = setup_window_effect(&window) { eprintln!("Failed to setup window effect: {}", e); } } ``` -------------------------------- ### Apply Acrylic Effect on Windows Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md Applies an acrylic background effect to a window on Windows using specified colors. This example shows applying a semi-transparent white and then a semi-transparent blue. ```rust #[cfg(target_os = "windows")] fn use_colors(window: &impl raw_window_handle::HasWindowHandle) -> Result<(), Box> { use window_vibrancy::apply_acrylic; let white_50 = rgb_to_color(255, 255, 255, 128); let blue_75 = hex_to_color(0x1084D7, 192); apply_acrylic(window, Some(white_50))?; // Later... apply_acrylic(window, Some(blue_75))?; Ok(()) } ``` -------------------------------- ### RGBA Color Format Example Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/README.md Illustrates the RGBA color format used for window effects. Colors are represented as tuples of four `u8` values (0-255) for Red, Green, Blue, and Alpha (opacity). ```rust (255, 255, 255, 128) // White with 50% opacity (0, 0, 0, 255) // Opaque black (100, 150, 255, 192) // Blue-tinted, 75% opacity ``` -------------------------------- ### Get Tag of NSVisualEffectViewTagged Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/ns-visual-effect-view.md Retrieves the tag identifier of an NSVisualEffectViewTagged instance. Use this to verify or identify a specific vibrancy effect view. ```rust let tag_id = view.tag(); if tag_id == 91376254 { println!("Found vibrancy effect view"); } ``` -------------------------------- ### Winit Application with Window Vibrancy Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md This Rust code demonstrates how to set up a Winit window with transparency and apply blur on Windows or vibrancy on macOS using the `window-vibrancy` crate. ```rust use winit:: event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, }; #[cfg(target_os = "windows")] use window_vibrancy::apply_blur; #[cfg(target_os = "macos")] use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial}; fn main() { let event_loop = EventLoop::new(); let window = WindowBuilder::new() .with_transparent(true) .with_decorations(false) .build(&event_loop) .unwrap(); #[cfg(target_os = "windows")] apply_blur(&window, None).expect("Failed to apply blur"); #[cfg(target_os = "macos")] apply_vibrancy( &window, NSVisualEffectMaterial::HudWindow, None, None, ).expect("Failed to apply vibrancy"); event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; // Handle events... }); } ``` -------------------------------- ### Winit Window with Transparency and Blur Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/README.md Creates a Winit window with transparency enabled and applies a blur effect. Requires `apply_blur` function. ```rust let window = WindowBuilder::new() .with_transparent(true) .build(&event_loop)?; apply_blur(&window, None)?; ``` -------------------------------- ### Import Pattern for Window Vibrancy Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/README.md Demonstrates the import pattern for all functions and types provided by the window_vibrancy crate. ```rust use window_vibrancy::{ // Functions apply_blur, clear_blur, apply_acrylic, clear_acrylic, apply_mica, clear_mica, apply_tabbed, clear_tabbed, apply_vibrancy, clear_vibrancy, apply_liquid_glass, clear_liquid_glass, // Types Color, NSVisualEffectMaterial, NSVisualEffectState, NSGlassEffectViewStyle, Error, }; ``` -------------------------------- ### Project File Structure Overview Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/README.md This snippet shows the directory structure of the window-vibrancy project. It helps in understanding the organization of different modules and documentation files. ```tree output/ ├── README.md (This file) ├── types.md (Type definitions) ├── errors.md (Error reference) ├── platform-support.md (Version matrix & detection) ├── usage-guide.md (Code examples & patterns) ├── api-reference/ │ ├── core-functions.md (Public API functions) │ ├── windows-implementation.md (Internal Windows details) │ ├── macos-implementation.md (Internal macOS details) │ └── ns-visual-effect-view.md (NSVisualEffectViewTagged class) ``` -------------------------------- ### Integration Test Pattern for Windows and macOS Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md Provides a template for integration tests, demonstrating how to set up separate test modules for Windows and macOS to apply vibrancy or blur effects. ```rust #[cfg(all(test, target_os = "windows"))] mod windows_tests { use window_vibrancy::apply_blur; // Integration test with real window... } ``` ```rust #[cfg(all(test, target_os = "macos"))] mod macos_tests { use window_vibrancy::apply_vibrancy; // Integration test with real window... } ``` -------------------------------- ### Tao Window with Transparency and Acrylic Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/README.md Creates a Tao window with transparency enabled and applies an acrylic effect. Requires `apply_acrylic` function. ```rust let window = WindowBuilder::new() .with_transparent(true) .build(&event_loop)?; apply_acrylic(&window, None)?; ``` -------------------------------- ### Create NSVisualEffectView Instance Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/ns-visual-effect-view.md Allocates and initializes a new NSVisualEffectView instance with a specified frame and tag. This is the first step in using the visual effect view. ```rust let view = NSVisualEffectViewTagged::initWithFrame( mtm.alloc(), frame_rect, tag, ); ``` -------------------------------- ### Enable Transparent Window in Tauri Config Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/platform-support.md Configure `tauri.conf.json` to enable transparent windows. This requires setting `transparent` to `true` within the `windows` array. Additionally, `macOSPrivateApi` should be set to `true` for macOS. ```json { "tauri": { "windows": [ { "transparent": true } ], "macOS": { "macOSPrivateApi": true } } } ``` -------------------------------- ### Vibrancy Materials (macOS 10.14+) Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/platform-support.md Lists recommended semantic vibrancy materials for macOS Mojave and later, along with earlier materials that remain available. ```rust // Recommended semantic materials (10.14+) NSVisualEffectMaterial::HeaderView NSVisualEffectMaterial::Sheet NSVisualEffectMaterial::WindowBackground NSVisualEffectMaterial::HudWindow NSVisualEffectMaterial::FullScreenUI NSVisualEffectMaterial::Tooltip NSVisualEffectMaterial::ContentBackground NSVisualEffectMaterial::UnderWindowBackground NSVisualEffectMaterial::UnderPageBackground // Earlier materials still available NSVisualEffectMaterial::Titlebar // 10.10+ NSVisualEffectMaterial::Selection // 10.10+ NSVisualEffectMaterial::Menu // 10.11+ NSVisualEffectMaterial::Popover // 10.11+ NSVisualEffectMaterial::Sidebar // 10.11+ ``` -------------------------------- ### Import Vibrancy Functions Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md Import the necessary functions and enums from the window-vibrancy crate for applying effects. ```rust use window_vibrancy::{apply_vibrancy, apply_blur, NSVisualEffectMaterial}; ``` -------------------------------- ### Add Dependencies to Cargo.toml Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md Include the window-vibrancy and raw-window-handle crates in your project's Cargo.toml file to enable vibrancy features. ```toml [dependencies] window-vibrancy = "0.7" raw-window-handle = "0.6" ``` -------------------------------- ### Tauri Configuration for Transparent Windows Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md This JSON configuration is for `tauri.conf.json` and enables transparent windows with disabled decorations, which is necessary for applying vibrancy effects. ```json { "tauri": { "windows": [ { "transparent": true, "decorations": false } ], "macOS": { "macOSPrivateApi": true } } } ``` -------------------------------- ### NSVisualEffectView Creation Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/ns-visual-effect-view.md Allocates and initializes a new NSVisualEffectView instance. This is the first step before any configuration or insertion into the view hierarchy. ```APIDOC ## NSVisualEffectView::initWithFrame ### Description Allocates a new NSVisualEffectView instance and initializes its variables with the provided tag. It also calls the parent initWithFrame method. ### Method Rust function call (simulating Objective-C FFI) ### Parameters - **mtm** (MainThreadMarker) - Used for allocation on the main thread. - **frame_rect** (CGRect) - The initial frame for the view. - **tag** (isize) - A tag to identify the view. ### Code Example ```rust let view = NSVisualEffectViewTagged::initWithFrame( mtm.alloc(), frame_rect, tag, ); ``` ``` -------------------------------- ### Tauri Configuration for Transparency Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/README.md JSON configuration for Tauri to enable window transparency and private APIs on macOS. ```json { "windows": [{ "transparent": true }], "macOS": { "macOSPrivateApi": true } } ``` -------------------------------- ### Unit Test Pattern for Windows Blur Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md Illustrates a unit test structure for the blur effect on Windows. Note that this test requires a real window handle and is better suited for integration tests. ```rust #[cfg(test)] mod tests { #[test] #[cfg(target_os = "windows")] fn test_blur_returns_ok() { // Note: This requires a real window handle, difficult in unit tests // Integration tests are recommended instead } } ``` -------------------------------- ### Enable Simple Blur on Windows Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md Applies a basic blur effect to the window. This function is only available on Windows. ```rust use window_vibrancy::apply_blur; #[cfg(target_os = "windows")] fn enable_blur(window: &impl raw_window_handle::HasWindowHandle) -> Result<(), Box> { apply_blur(window, None)?; Ok(()) } ``` -------------------------------- ### Public Function Exports Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/windows-implementation.md These are the public functions accessible from the main `window_vibrancy` crate for applying and clearing various window effects on Windows. ```APIDOC ## Public Function Exports These functions provide direct access to window effect manipulation on Windows. ### `apply_blur(hwnd, color)` **Description:** Apply blur effect to a window. **Parameters:** - `hwnd` (HWND): The handle to the window. - `color` (Color): The color to apply with the blur. ### `clear_blur(hwnd)` **Description:** Clear the blur effect from a window. **Parameters:** - `hwnd` (HWND): The handle to the window. ### `apply_acrylic(hwnd, color)` **Description:** Apply acrylic (translucent) effect to a window. **Parameters:** - `hwnd` (HWND): The handle to the window. - `color` (Color): The color to apply with the acrylic effect. ### `clear_acrylic(hwnd)` **Description:** Clear the acrylic effect from a window. **Parameters:** - `hwnd` (HWND): The handle to the window. ### `apply_mica(hwnd, dark)` **Description:** Apply the Mica effect to a window, with an option for dark mode. **Parameters:** - `hwnd` (HWND): The handle to the window. - `dark` (bool): Enable dark mode for the Mica effect. ### `clear_mica(hwnd)` **Description:** Clear the Mica effect from a window. **Parameters:** - `hwnd` (HWND): The handle to the window. ### `apply_tabbed(hwnd, dark)` **Description:** Apply the tabbed Mica effect to a window, with an option for dark mode. **Parameters:** - `hwnd` (HWND): The handle to the window. - `dark` (bool): Enable dark mode for the tabbed Mica effect. ### `clear_tabbed(hwnd)` **Description:** Clear the tabbed Mica effect from a window. **Parameters:** - `hwnd` (HWND): The handle to the window. **General Notes:** - All functions accept `HWND` (internal use from lib.rs). - All functions return `Result<(), Error>`. - Implement version detection and fallback logic. ``` -------------------------------- ### Import objc2 and AppKit for FFI Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/ns-visual-effect-view.md Imports necessary types from the objc2 and objc2_app_kit crates for Objective-C Foreign Function Interface (FFI) interoperability. ```rust use objc2::{define_class, msg_send, rc::{Allocated, Retained}}; use objc2_app_kit::NSVisualEffectView; ``` -------------------------------- ### Initialize NSVisualEffectViewTagged with Frame and Tag Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/ns-visual-effect-view.md Initializes a new NSVisualEffectViewTagged instance with a specified frame and tag. This method is used to create and configure the vibrancy effect view. ```rust #[cfg(target_os = "macos")] unsafe fn create_effect_view() { use objc2::MainThreadMarker; use objc2_foundation::NSRect; let mtm = MainThreadMarker::new().unwrap(); let frame = NSRect { origin: objc2_foundation::NSPoint { x: 0.0, y: 0.0 }, size: objc2_foundation::NSSize { width: 400.0, height: 300.0 }, }; let view = NSVisualEffectViewTagged::initWithFrame( mtm.alloc(), frame, 91376254, // NS_VIEW_TAG_BLUR_VIEW ); } ``` -------------------------------- ### Liquid Glass Styles (macOS 26.0+) Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/platform-support.md Lists officially supported and private API styles for the liquid glass effect. Use Regular or Clear for stable compatibility; other styles use private APIs and may change. ```rust // Officially supported: NSGlassEffectViewStyle::Regular // Standard glass NSGlassEffectViewStyle::Clear // Clear glass // Private API styles (may change): NSGlassEffectViewStyle::Dock NSGlassEffectViewStyle::AppIcons NSGlassEffectViewStyle::Widgets NSGlassEffectViewStyle::Text NSGlassEffectViewStyle::AvPlayer NSGlassEffectViewStyle::FaceTime NSGlassEffectViewStyle::ControlCenter NSGlassEffectViewStyle::NotificationCenter NSGlassEffectViewStyle::Monogram NSGlassEffectViewStyle::Bubbles NSGlassEffectViewStyle::Identity NSGlassEffectViewStyle::FocusBorder NSGlassEffectViewStyle::FocusPlatter NSGlassEffectViewStyle::Keyboard NSGlassEffectViewStyle::Sidebar NSGlassEffectViewStyle::AbuttedSidebar NSGlassEffectViewStyle::Inspector NSGlassEffectViewStyle::Control NSGlassEffectViewStyle::Loupe NSGlassEffectViewStyle::Slider NSGlassEffectViewStyle::Camera NSGlassEffectViewStyle::CartouchePopover ``` -------------------------------- ### Basic Window Effect Error Handling Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/errors.md Applies a blur effect to a window on Windows. Handles potential errors during blur application. ```rust #[cfg(target_os = "windows")] use window_vibrancy::{apply_blur, Error}; fn setup_window_effect(window: &Window) -> Result<(), Box> { apply_blur(window, Some((18, 18, 18, 125)))?; Ok(()) } ``` -------------------------------- ### Dynamic Material Selection for macOS Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md Selects and applies different NSVisualEffectMaterials to a window based on whether the theme is dark or light. Clears previous vibrancy before applying the new material. ```rust #[cfg(target_os = "macos")] use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial}; #[cfg(target_os = "macos")] fn select_material_for_theme(dark: bool) -> NSVisualEffectMaterial { if dark { NSVisualEffectMaterial::HudWindow } else { NSVisualEffectMaterial::WindowBackground } } #[cfg(target_os = "macos")] fn update_theme(window: &impl raw_window_handle::HasWindowHandle, dark: bool) -> Result<(), Box> { use window_vibrancy::clear_vibrancy; // Clear old vibrancy clear_vibrancy(window).ok(); // Apply new material apply_vibrancy( window, select_material_for_theme(dark), None, None, )?; Ok(()) } ``` -------------------------------- ### Private Initialization: initWithFrame Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/macos-implementation.md Provides the implementation for the `initWithFrame` method, which initializes the NSVisualEffectViewTagged instance with a frame and a tag. ```APIDOC ## initWithFrame ### Description Initializes the NSVisualEffectViewTagged instance with a frame and a tag. ### Method `initWithFrame` ### Signature `(Allocated, NSRect, NSInteger) -> Retained` ### Implementation ```rust pub unsafe fn initWithFrame( this: Allocated, frame_rect: NSRect, tag: NSInteger, ) -> Retained { let state = NSVisualEffectViewTaggedIvars { tag }; let this = this.set_ivars(state); msg_send![super(this), initWithFrame: frame_rect] } ``` ### Notes Sets up instance variables before calling parent's initWithFrame. ``` -------------------------------- ### Log Apply Blur Errors Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/usage-guide.md Demonstrates logging errors that occur when attempting to apply blur. This method uses an `if let` to check for errors and prints them to `stderr`. ```rust #[cfg(target_os = "windows")] fn setup(window: &impl raw_window_handle::HasWindowHandle) { if let Err(e) = apply_blur(window, None) { eprintln!("Failed to apply blur: {}", e); } } ``` -------------------------------- ### Detect Minimum Windows Build Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/windows-implementation.md Checks if the current Windows build number is at least the specified value. Used to determine support for various DWM features. ```rust fn is_at_least_build(build: u32) -> bool ``` ```rust // Check if Windows 11 21H2 or newer if is_at_least_build(22000) { println!("Windows 11 or newer"); } ``` -------------------------------- ### SetWindowCompositionAttribute Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/windows-implementation.md A low-level wrapper for the Windows SetWindowCompositionAttribute API, used to apply visual effects like blur and acrylic to windows. It dynamically loads the necessary DLL and constructs the required data structures for the API call. ```APIDOC ## SetWindowCompositionAttribute ### Description Low-level wrapper for Windows SetWindowCompositionAttribute API. This function dynamically loads `SetWindowCompositionAttribute` from user32.dll at runtime, constructs the `ACCENT_POLICY` with the provided state and color, packs it into `WINDOWCOMPOSITIONATTRIBDATA`, and calls the function if available. It is used by various functions to apply blur and acrylic effects. ### Function Signature ```rust unsafe fn SetWindowCompositionAttribute( hwnd: HWND, accent_state: ACCENT_STATE, color: Option, ) ``` ### Parameters #### Path Parameters - **hwnd** (HWND) - Required - Window handle - **accent_state** (ACCENT_STATE) - Required - Accent state (DISABLED, BLUR, or ACRYLIC) - **color** (Option) - Optional - RGBA color for effect. Color components are packed into a 32-bit integer in `RRGGBBAA` format (reversed order). For acrylic, a minimum alpha of 1 is enforced. The default color is (0, 0, 0, 0) if not provided. ### Behavior 1. Dynamically loads `SetWindowCompositionAttribute` from user32.dll at runtime. 2. Constructs `ACCENT_POLICY` with provided state and color. 3. Packs into `WINDOWCOMPOSITIONATTRIBDATA`. 4. Calls the function if available. ### Thread Safety Safe to call from any thread but may be non-atomic with respect to window operations. ### Used by - `apply_blur()` on Windows 10 v1809+ - `clear_blur()` on Windows 10 v1809+ - `apply_acrylic()` on Windows 10 v1809 (older builds) - `clear_acrylic()` on Windows 10 v1809 (older builds) ``` -------------------------------- ### NSVisualEffectViewTagged Methods Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/types.md Provides methods for initializing and configuring NSVisualEffectViewTagged, including setting material, blending mode, state, resizing behavior, and corner radius. Note that setCornerRadius uses a private API. ```rust #[cfg(target_os = "macos")] impl NSVisualEffectViewTagged { unsafe fn initWithFrame(this: Allocated, frame_rect: NSRect, tag: NSInteger) -> Retained { // implementation omitted } unsafe fn setMaterial(&self, material: NSVisualEffectMaterial) { // implementation omitted } unsafe fn setBlendingMode(&self, mode: NSVisualEffectBlendingMode) { // implementation omitted } unsafe fn setState(&self, state: NSVisualEffectState) { // implementation omitted } unsafe fn setAutoresizingMask(&self, mask: NSAutoresizingMaskOptions) { // implementation omitted } unsafe fn setCornerRadius(&self, radius: CGFloat) { // implementation omitted } } ``` -------------------------------- ### NSVisualEffectView Configuration Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/ns-visual-effect-view.md Configures the visual properties and behavior of the NSVisualEffectView after creation. These methods should be called in the specified order. ```APIDOC ## NSVisualEffectView Configuration Methods ### Description These methods configure the appearance and behavior of an NSVisualEffectView. They should be called in the following order after the view has been created: 1. `setMaterial()`: Selects the visual effect material (e.g., vibrancy, blur). 2. `setBlendingMode()`: Sets the blending mode, typically to `BehindWindow`. 3. `setCornerRadius()`: Defines the radius for rounded corners. 4. `setState()`: Controls the state behavior of the effect. 5. `setAutoresizingMask()`: Specifies how the view should resize automatically. ### Methods - `setMaterial(material: NSVisualEffectMaterial)` - `setBlendingMode(blendingMode: NSVisualEffectBlendingMode)` - `setCornerRadius(radius: CGFloat)` - `setState(state: NSVisualEffectState)` - `setAutoresizingMask(mask: NSAutoresizingMaskOptions)` ``` -------------------------------- ### is_swca_supported() Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/windows-implementation.md Checks if the SetWindowCompositionAttribute (SWCA) API is supported on the current Windows version. This is a fallback for older Windows 10 and Windows 11 versions. ```APIDOC ## is_swca_supported() ### Description Detects if SetWindowCompositionAttribute is supported. ### Signature ```rust fn is_swca_supported() -> bool ``` ### Returns `bool` - True if build >= 17763 (Windows 10 v1809), false otherwise. ### Used By - `apply_blur()` - `clear_blur()` - `apply_acrylic()` - `clear_acrylic()` ``` -------------------------------- ### NSGlassEffectViewTagged Methods Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/types.md Provides a method for initializing NSGlassEffectViewTagged with a frame and tag. This is used internally for applying liquid glass effects. ```rust #[cfg(target_os = "macos")] impl NSGlassEffectViewTagged { unsafe fn initWithFrame(this: Allocated, frame_rect: NSRect, tag: NSInteger) -> Retained { // implementation omitted } } ``` -------------------------------- ### Vibrancy Materials (macOS 10.10-10.13) Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/platform-support.md Lists available vibrancy materials for macOS Yosemite through High Sierra. Use semantic materials when possible; deprecated materials may behave differently. ```rust NSVisualEffectMaterial::Titlebar // 10.10+ NSVisualEffectMaterial::Selection // 10.10+ NSVisualEffectMaterial::Menu // 10.11+ NSVisualEffectMaterial::Popover // 10.11+ NSVisualEffectMaterial::Sidebar // 10.11+ // Deprecated (but work): NSVisualEffectMaterial::AppearanceBased NSVisualEffectMaterial::Light NSVisualEffectMaterial::Dark NSVisualEffectMaterial::MediumLight NSVisualEffectMaterial::UltraDark ``` -------------------------------- ### Try Latest Visual Effects on macOS Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/README.md Attempts to apply the latest 'liquid glass' effect, falling back to standard vibrancy if the former is not supported or fails. Includes a specific alpha value for the effect. ```rust // Try liquid glass (macOS 26.0+), fall back to vibrancy if apply_liquid_glass(window, NSGlassEffectViewStyle::Clear, None, Some(12.0)).is_err() { let _ = apply_vibrancy(window, NSVisualEffectMaterial::HudWindow, None, Some(12.0)); } ``` -------------------------------- ### Platform-Specific Window Effect Implementation Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/README.md Applies blur on Windows, vibrancy on macOS, and prints a message for unsupported platforms. This snippet demonstrates conditional compilation for cross-platform compatibility. ```rust #[cfg(target_os = "windows")] apply_blur(&window, Some((18, 18, 18, 125)))?; #[cfg(target_os = "macos")] apply_vibrancy(&window, NSVisualEffectMaterial::HudWindow, None, None)?; #[cfg(not(any(target_os = "windows", target_os = "macos")))] eprintln!("Not supported on this platform"); ``` -------------------------------- ### Windows DwmSetWindowAttribute API Wrapper Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/windows-implementation.md This unsafe generic function wraps the Windows `DwmSetWindowAttribute` API. It allows setting various window attributes by providing a window handle, an attribute kind constant, and a reference to the attribute value. The function returns a `Result` indicating success or a `Win32Error` if the underlying Windows API call fails. ```rust unsafe fn dwm_set_window_attribute( hwnd: HWND, kind: u32, object: &T, ) -> Result<(), Error> ``` -------------------------------- ### Advanced Window Effect with Fallback Error Handling Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/errors.md Attempts to apply visual effects (Mica, Acrylic, Blur) in order of preference on Windows, falling back if an effect is unsupported or fails. Returns an error if no effects can be applied. ```rust #[cfg(target_os = "windows")] use window_vibrancy::{apply_mica, apply_acrylic, apply_blur, Error}; fn setup_window_with_fallback(window: &Window) -> Result<(), String> { // Try in order of preference for effect in [ ("Mica", || apply_mica(window, None)), ("Acrylic", || apply_acrylic(window, None)), ("Blur", || apply_blur(window, None)), ] { match effect.1() { Ok(()) => { println!("Applied {}", effect.0); return Ok(()) } Err(Error::UnsupportedPlatformVersion(_)) => { continue; // Try next effect } Err(e) => { return Err(format!("{} failed: {}", effect.0, e)); } } } Err("No visual effects supported on this platform".to_string()) } ``` -------------------------------- ### is_at_least_build(build: u32) Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/windows-implementation.md Detects if the current Windows build number is at least the specified build. This is crucial for determining support for newer Windows features. ```APIDOC ## is_at_least_build(build: u32) ### Description Detects if Windows build is at least the specified build number. ### Signature ```rust fn is_at_least_build(build: u32) -> bool ``` ### Parameters #### Path Parameters - **build** (u32) - Required - Minimum Windows build number to check against. ### Returns `bool` - True if current build >= specified build, false otherwise. ### Implementation Details Checks Windows version via `windows_version::OsVersion::current()` comparing build numbers. ### Example ```rust // Check if Windows 11 21H2 or newer if is_at_least_build(22000) { println!("Windows 11 or newer"); } ``` ### Used By - `is_swca_supported()` - `is_undocumented_mica_supported()` - `is_backdroptype_supported()` ``` -------------------------------- ### Apply Liquid Glass Effect (macOS) Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/macos-implementation.md Applies an NSGlassEffectView-based liquid glass effect to a window. Requires macOS 26.0+. Ensure the function is called from the main thread. ```rust pub unsafe fn apply_liquid_glass( ns_view: NonNull, style: super::NSGlassEffectViewStyle, tint_color: Option, radius: Option, ) -> Result<(), Error> ``` ```rust let (r, g, b, a) = color; NSColor::colorWithRed_green_blue_alpha( r as f64 / 255.0, // 0.0-1.0 g as f64 / 255.0, // 0.0-1.0 b as f64 / 255.0, // 0.0-1.0 a as f64 / 255.0, // 0.0-1.0 ) ``` ```rust use window_vibrancy::{apply_liquid_glass, NSGlassEffectViewStyle}; #[cfg(target_os = "macos")] fn setup_glass(window: &Window) -> Result<(), Box> { use raw_window_handle::HasWindowHandle; let handle = window.window_handle()?; if let raw_window_handle::RawWindowHandle::AppKit(appkit) = handle.as_raw() { unsafe { apply_liquid_glass( appkit.ns_view, NSGlassEffectViewStyle::Regular, Some((255, 255, 255, 128)), Some(12.0), )?; } } Ok(()) } ``` -------------------------------- ### Applying Window Vibrancy Effects with Fallbacks Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/errors.md This function attempts to apply Mica, Acrylic, or Blur effects sequentially, handling Win32Error and UnsupportedPlatformVersion. It's useful for ensuring a visual effect is applied even on older Windows versions or when specific APIs fail. ```rust fn apply_effect(window: &Window) -> Result<(), Box> { use window_vibrancy::{apply_mica, apply_acrylic, apply_blur, Error}; match apply_mica(window, None) { Ok(()) => Ok(()), Err(Error::Win32Error { .. }) | Err(Error::UnsupportedPlatformVersion(_)) => { // Mica not available, try acrylic match apply_acrylic(window, None) { Ok(()) => Ok(()), Err(_) => { // Acrylic not available, try blur apply_blur(window, None) } } } Err(e) => Err(e.into()), } } ``` -------------------------------- ### Windows SetWindowCompositionAttribute API Wrapper Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/windows-implementation.md This unsafe function wraps the Windows `SetWindowCompositionAttribute` API. It dynamically loads the function from user32.dll and constructs the necessary data structures to apply accent states like blur or acrylic. Color components are expected in `RRGGBBAA` format, and acrylic effects enforce a minimum alpha of 1. It is safe to call from any thread but may not be atomic with respect to window operations. ```rust unsafe fn SetWindowCompositionAttribute( hwnd: HWND, accent_state: ACCENT_STATE, color: Option, ) ``` -------------------------------- ### Catching Win32Error in Rust Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/errors.md Demonstrates how to catch a Win32Error when applying Mica effect. It prints the API name and error code. Common error codes are commented for reference. ```rust use window_vibrancy::{apply_mica, Error}; #[cfg(target_os = "windows")] match apply_mica(&window, None) { Ok(()) => println!("Success"), Err(Error::Win32Error { api, result }) => { eprintln!("Win32 API {} failed with code 0x{:08x}", api, result); // Common codes: // 0x80070001 = E_NOTIMPL (not implemented) // 0x80070057 = E_INVALIDARG (invalid argument) } Err(e) => eprintln!("Error: {}", e), } ``` -------------------------------- ### macOS Runtime Effect Fallback with Conditional Compilation Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/platform-support.md On macOS, attempt to apply effects in a preferred order: Liquid Glass (macOS 26.0+) or standard Vibrancy. This function checks for success and prints the result. ```rust #[cfg(target_os = "macos")] fn setup_macos_with_fallback(window: &Window) -> Result<(), Box> { use window_vibrancy::{apply_liquid_glass, apply_vibrancy, NSGlassEffectViewStyle, NSVisualEffectMaterial}; // Try liquid glass first (macOS 26.0+) if apply_liquid_glass(window, NSGlassEffectViewStyle::Clear, None, Some(12.0)).is_ok() { println!("Using liquid glass"); } else if apply_vibrancy(window, NSVisualEffectMaterial::HudWindow, None, Some(12.0)).is_ok() { println!("Using vibrancy"); } else { println!("No effects available"); } Ok(()) } ``` -------------------------------- ### Catch NoWindowHandle Error Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/errors.md Demonstrates how to catch the `NoWindowHandle` error, which occurs when the window handle cannot be extracted from the provided window object. This is often due to an uninitialized or invalid window. ```rust use window_vibrancy::{apply_blur, Error}; use raw_window_handle::HandleError; match apply_blur(&window, None) { Ok(()) => println!("Success"), Err(Error::NoWindowHandle(handle_err)) => { eprintln!("Failed to get window handle: {}", handle_err); } Err(e) => eprintln!("Error: {}", e), } ``` -------------------------------- ### Mitigate NotMainThread Error with winit Channel Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/errors.md Illustrates dispatching window vibrancy calls to the main thread using a channel, suitable for applications using winit. This pattern ensures macOS-specific functions are executed on the correct thread. ```rust // Using winit with a channel let (tx, rx) = std::sync::mpsc::channel(); // In event loop (main thread) if let Ok(window) = rx.recv() { apply_vibrancy(&window, effect, None, None)?; } // In worker thread tx.send(window.clone()).ok(); ``` -------------------------------- ### Windows Runtime Effect Fallback with Conditional Compilation Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/platform-support.md On Windows, attempt to apply effects in a preferred order: Mica, Acrylic, or Blur. This function checks for success at each step and prints the result. ```rust #[cfg(target_os = "windows")] fn setup_windows_with_fallback(window: &Window) -> Result<(), Box> { use window_vibrancy::{apply_mica, apply_acrylic, apply_blur}; // Try in order of preference if apply_mica(window, None).is_ok() { println!("Using mica"); } else if apply_acrylic(window, None).is_ok() { println!("Using acrylic"); } else if apply_blur(window, None).is_ok() { println!("Using blur"); } else { println!("No effects available"); } Ok(()) } ``` -------------------------------- ### NSVisualEffectViewTagged::initWithFrame Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/api-reference/ns-visual-effect-view.md Initializes a new NSVisualEffectViewTagged instance with a specified frame and tag. This is the primary method for creating a vibrancy effect view. ```APIDOC ## initWithFrame ### Description Initializes a new NSVisualEffectViewTagged instance with a specified frame and tag. This is the primary method for creating a vibrancy effect view. ### Method `initWithFrame` ### Parameters #### Path Parameters - **this** (Allocated) - Required - Newly allocated instance - **frame_rect** (NSRect) - Required - Frame rectangle for the view (x, y, width, height) - **tag** (NSInteger) - Required - Tag identifier (typically 91376254) ### Returns `Retained` — Properly initialized and retained instance ### Safety Marked `unsafe` because it calls Objective-C methods. ### Implementation Details 1. Creates instance variables with provided tag 2. Calls parent NSVisualEffectView's initWithFrame 3. Returns retained instance ### Example ```rust #[cfg(target_os = "macos")] unsafe fn create_effect_view() { use objc2::MainThreadMarker; use objc2_foundation::NSRect; let mtm = MainThreadMarker::new().unwrap(); let frame = NSRect { origin: objc2_foundation::NSPoint { x: 0.0, y: 0.0 }, size: objc2_foundation::NSSize { width: 400.0, height: 300.0 }, }; let view = NSVisualEffectViewTagged::initWithFrame( mtm.alloc(), frame, 91376254, // NS_VIEW_TAG_BLUR_VIEW ); } ``` ``` -------------------------------- ### NSGlassEffectViewTagged Methods Source: https://github.com/tauri-apps/window-vibrancy/blob/dev/_autodocs/types.md Provides methods for initializing NSGlassEffectViewTagged, a custom NSGlassEffectView subclass with tag support for internal use on macOS. ```APIDOC ## NSGlassEffectViewTagged ### Description Custom NSGlassEffectView subclass with tag support for internal use. ### Methods #### `initWithFrame(frame_rect: NSRect, tag: NSInteger)` - **Safety**: unsafe - **Parameters**: - `frame_rect` (NSRect) - The frame rectangle for the view. - `tag` (NSInteger) - The tag to associate with the view. - **Returns**: `Retained` - **Description**: Initialize with frame and tag. ### Platform - macOS 26.0+ ```