From 79b2417bc2f0ba86a565c93a0168cd29c4cf0544 Mon Sep 17 00:00:00 2001 From: Gkirito Date: Mon, 20 Feb 2023 00:31:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=8C=9F=20feat(cocoa/src/appkit):=20ad?= =?UTF-8?q?d=20NSVisualEffectView?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocoa/src/appkit.rs | 139 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/cocoa/src/appkit.rs b/cocoa/src/appkit.rs index aba678a..7770263 100644 --- a/cocoa/src/appkit.rs +++ b/cocoa/src/appkit.rs @@ -390,6 +390,145 @@ pub enum NSBezelStyle { NSRoundedDisclosureBezelStyle = 14, } +// https://developer.apple.com/documentation/appkit/nsvisualeffectview/blendingmode +#[allow(dead_code)] +#[repr(u64)] +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum NSVisualEffectBlendingMode { + BehindWindow = 0, + WithinWindow = 1 +} + +// https://developer.apple.com/documentation/appkit/nsvisualeffectview/state +#[allow(dead_code)] +#[repr(u64)] +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum NSVisualEffectState { + FollowsWindowActiveState = 0, + Active = 1, + Inactive = 2 +} + +/// +#[repr(u64)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum NSVisualEffectMaterial { + /// A default material for the view's effectiveAppearance. + #[deprecated = "Use a semantic material instead."] + AppearanceBased = 0, + #[deprecated = "Use a semantic material instead."] + Light = 1, + #[deprecated = "Use a semantic material instead."] + Dark = 2, + #[deprecated = "Use a semantic material instead."] + MediumLight = 8, + #[deprecated = "Use a semantic material instead."] + UltraDark = 9, + + // macOS 10.10+ + Titlebar = 3, + Selection = 4, + + // macOS 10.11+ + Menu = 5, + Popover = 6, + Sidebar = 7, + + // macOS 10.14+ + HeaderView = 10, + Sheet = 11, + WindowBackground = 12, + HudWindow = 13, + FullScreenUI = 15, + Tooltip = 17, + ContentBackground = 18, + UnderWindowBackground = 21, + UnderPageBackground = 22 +} + +// macOS 10.10+ - https://developer.apple.com/documentation/appkit/nsvisualeffectview +#[allow(non_snake_case)] +pub trait NSVisualEffectView: Sized { + unsafe fn alloc(_: Self) -> id { + msg_send![class!(NSVisualEffectView), alloc] + } + + unsafe fn init(self) -> id; + unsafe fn initWithFrame_(self, frameRect: NSRect) -> id; + unsafe fn bounds(self) -> NSRect; + unsafe fn frame(self) -> NSRect; + unsafe fn setFrameSize(self, frameSize: NSSize); + unsafe fn setFrameOrigin(self, frameOrigin: NSPoint); + + unsafe fn superview(self) -> id; + unsafe fn removeFromSuperview(self); + // API_AVAILABLE(macos(10.12)); + unsafe fn isEmphasized(self) -> BOOL; + // API_AVAILABLE(macos(10.12)); + unsafe fn setEmphasized_(self, emphasized: BOOL); + + unsafe fn setMaterial_(self, material: NSVisualEffectMaterial); + unsafe fn setState_(self, state: NSVisualEffectState); + unsafe fn setBlendingMode_(self, mode: NSVisualEffectBlendingMode); +} + +#[allow(non_snake_case)] +impl NSVisualEffectView for id { + unsafe fn init(self) -> id { + msg_send![self, init] + } + + unsafe fn initWithFrame_(self, frameRect: NSRect) -> id { + msg_send![self, initWithFrame: frameRect] + } + + unsafe fn bounds(self) -> NSRect { + msg_send![self, bounds] + } + + unsafe fn frame(self) -> NSRect { + msg_send![self, frame] + } + + unsafe fn setFrameSize(self, frameSize: NSSize) { + msg_send![self, setFrameSize: frameSize] + } + + unsafe fn setFrameOrigin(self, frameOrigin: NSPoint) { + msg_send![self, setFrameOrigin: frameOrigin] + } + + unsafe fn superview(self) -> id { + msg_send![self, superview] + } + + unsafe fn removeFromSuperview(self) { + msg_send![self, removeFromSuperview] + } + + // API_AVAILABLE(macos(10.12)); + unsafe fn isEmphasized(self) -> BOOL { + msg_send![self, isEmphasized] + } + + // API_AVAILABLE(macos(10.12)); + unsafe fn setEmphasized_(self, emphasized: BOOL) { + msg_send![self, setEmphasized: emphasized] + } + + unsafe fn setMaterial_(self, material: NSVisualEffectMaterial) { + msg_send![self, setMaterial: material] + } + + unsafe fn setState_(self, state: NSVisualEffectState) { + msg_send![self, setState: state] + } + + unsafe fn setBlendingMode_(self, mode: NSVisualEffectBlendingMode) { + msg_send![self, setBlendingMode: mode] + } +} + #[repr(u64)] #[derive(Clone, Copy, Debug, PartialEq)] pub enum NSRequestUserAttentionType { From 8bbdd5fd4ce925d3e0a7835df1fff67049e6d480 Mon Sep 17 00:00:00 2001 From: Gkirito Date: Mon, 20 Feb 2023 00:33:13 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A7=AA=20test(cocoa/examples/nsvisual?= =?UTF-8?q?effectview=5Fblur):=20add=20an=20example=20for=20NSVisualEffect?= =?UTF-8?q?View?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocoa/examples/nsvisualeffectview_blur.rs | 76 +++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 cocoa/examples/nsvisualeffectview_blur.rs diff --git a/cocoa/examples/nsvisualeffectview_blur.rs b/cocoa/examples/nsvisualeffectview_blur.rs new file mode 100644 index 0000000..0c6cc94 --- /dev/null +++ b/cocoa/examples/nsvisualeffectview_blur.rs @@ -0,0 +1,76 @@ +extern crate cocoa; +extern crate objc; + +use objc::*; +use cocoa::base::{selector, id, nil, NO}; + +use cocoa::foundation::{NSRect, NSPoint, NSSize, NSAutoreleasePool, NSProcessInfo, + NSString}; +use cocoa::appkit::{NSApp, NSColor, NSColorSpace, NSApplication, NSApplicationActivationPolicyRegular, NSMenu, NSMenuItem, NSWindowStyleMask, NSBackingStoreType, NSWindow, NSRunningApplication, NSApplicationActivateIgnoringOtherApps, NSView, NSVisualEffectMaterial, NSVisualEffectView, NSViewWidthSizable, NSViewHeightSizable, NSVisualEffectBlendingMode, NSVisualEffectState, NSWindowOrderingMode}; + + +fn main() { + unsafe { + // Create the app. + let _pool = NSAutoreleasePool::new(nil); + + let app = NSApp(); + app.setActivationPolicy_(NSApplicationActivationPolicyRegular); + + // create Menu Bar + let menubar = NSMenu::new(nil).autorelease(); + let app_menu_item = NSMenuItem::new(nil).autorelease(); + menubar.addItem_(app_menu_item); + app.setMainMenu_(menubar); + + // create Application menu + let app_menu = NSMenu::new(nil).autorelease(); + let quit_prefix = NSString::alloc(nil).init_str("Quit "); + let quit_title = + quit_prefix.stringByAppendingString_(NSProcessInfo::processInfo(nil).processName()); + let quit_action = selector("terminate:"); + let quit_key = NSString::alloc(nil).init_str("q"); + let quit_item = NSMenuItem::alloc(nil) + .initWithTitle_action_keyEquivalent_(quit_title, quit_action, quit_key) + .autorelease(); + app_menu.addItem_(quit_item); + app_menu_item.setSubmenu_(app_menu); + + + // Create some colors + let clear = NSColor::clearColor(nil); + + // Create windows with different color types. + let window = NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_( + NSRect::new(NSPoint::new(0., 0.), NSSize::new(200., 200.)), + NSWindowStyleMask::NSTitledWindowMask | + NSWindowStyleMask::NSClosableWindowMask | + NSWindowStyleMask::NSResizableWindowMask | + NSWindowStyleMask::NSMiniaturizableWindowMask | + NSWindowStyleMask::NSUnifiedTitleAndToolbarWindowMask, + NSBackingStoreType::NSBackingStoreBuffered, + NO + ).autorelease(); + + window.cascadeTopLeftFromPoint_(NSPoint::new(20., 20.)); + window.setTitle_(NSString::alloc(nil).init_str("NSVisualEffectView_blur")); + window.setBackgroundColor_(clear); + window.makeKeyAndOrderFront_(nil); + + //NSVisualEffectView blur + let ns_view = window.contentView(); + let bounds = NSView::bounds(ns_view); + let blurred_view = NSVisualEffectView::initWithFrame_(NSVisualEffectView::alloc(nil), bounds); + blurred_view.autorelease(); + + blurred_view.setMaterial_(NSVisualEffectMaterial::HudWindow); + blurred_view.setBlendingMode_(NSVisualEffectBlendingMode::BehindWindow); + blurred_view.setState_(NSVisualEffectState::FollowsWindowActiveState); + blurred_view.setAutoresizingMask_(NSViewWidthSizable | NSViewHeightSizable); + + let _: () = msg_send![ns_view, addSubview: blurred_view positioned: NSWindowOrderingMode::NSWindowBelow relativeTo: 0]; + + app.run(); + } +} +