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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// The use of NSVisualEffectView comes from https://github.com/joboet/winit/tree/macos_blurred_background
// with a bit of rewrite by @youngsing to make it more like cocoa::appkit style.

/// <https://developer.apple.com/documentation/appkit/nsvisualeffectview/material>
#[repr(u64)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum NSVisualEffectMaterial {
  #[deprecated(
    since = "macOS 10.14",
    note = "A default material appropriate for the view's effectiveAppearance.  You should instead choose an appropriate semantic material."
  )]
  AppearanceBased = 0, // Default, Deprecated macOS 10.10–10.14
  #[deprecated(since = "macOS 10.14", note = "Use a semantic material instead.")]
  Light = 1, // Deprecated macOS 10.10–10.14
  #[deprecated(since = "macOS 10.14", note = "Use a semantic material instead.")]
  Dark = 2, // Deprecated macOS 10.10–10.14
  #[deprecated(since = "macOS 10.14", note = "Use a semantic material instead.")]
  MediumLight = 8, // Deprecated macOS 10.11–10.14
  #[deprecated(since = "macOS 10.14", note = "Use a semantic material instead.")]
  UltraDark = 9, // Deprecated macOS 10.11–10.14

  Titlebar = 3,  // macOS 10.10+
  Selection = 4, // macOS 10.10+

  Menu = 5,    // macOS 10.11+
  Popover = 6, // macOS 10.11+
  Sidebar = 7, // macOS 10.11+

  HeaderView = 10,            // macOS 10.14+
  Sheet = 11,                 // macOS 10.14+
  WindowBackground = 12,      // macOS 10.14+
  HudWindow = 13,             // macOS 10.14+
  FullScreenUI = 15,          // macOS 10.14+
  Tooltip = 17,               // macOS 10.14+
  ContentBackground = 18,     // macOS 10.14+
  UnderWindowBackground = 21, // macOS 10.14+
  UnderPageBackground = 22,   // macOS 10.14+
}

#[cfg(target_os = "macos")]
pub use internal::apply_vibrancy;

#[cfg(target_os = "macos")]
mod internal {
  use super::NSVisualEffectMaterial;

  use cocoa::{
    appkit::{
      NSAppKitVersionNumber, NSAppKitVersionNumber10_10, NSAppKitVersionNumber10_11,
      NSAutoresizingMaskOptions, NSView, NSViewHeightSizable, NSViewWidthSizable, NSWindow,
      NSWindowOrderingMode,
    },
    base::{id, nil, BOOL},
    foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize},
  };
  use objc::{class, msg_send, sel, sel_impl};

  use crate::Error;

  #[allow(deprecated)]
  pub fn apply_vibrancy(window: id, appearance: NSVisualEffectMaterial) -> Result<(), Error> {
    unsafe {
      if NSAppKitVersionNumber < NSAppKitVersionNumber10_10 {
        eprintln!("\"NSVisualEffectView\" is only available on macOS 10.10 or newer");
        return Err(Error::UnsupportedPlatformVersion(
          "\"apply_vibrancy()\" is only available on macOS 10.0 or newer.",
        ));
      }

      if !msg_send![class!(NSThread), isMainThread] {
        return Err(Error::NotMainThread(
          "\"apply_vibrancy()\" can only be used on the main thread.",
        ));
      }

      let mut m = appearance;
      if appearance as u32 > 9 && NSAppKitVersionNumber < NSAppKitVersionNumber10_14 {
        m = NSVisualEffectMaterial::AppearanceBased;
      } else if appearance as u32 > 4 && NSAppKitVersionNumber < NSAppKitVersionNumber10_11 {
        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();

      blurred_view.setMaterial_(m);
      blurred_view.setBlendingMode_(NSVisualEffectBlendingMode::BehindWindow);
      blurred_view.setState_(NSVisualEffectState::FollowsWindowActiveState);
      NSVisualEffectView::setAutoresizingMask_(
        blurred_view,
        NSViewWidthSizable | NSViewHeightSizable,
      );

      let _: () = msg_send![ns_view, addSubview: blurred_view positioned: NSWindowOrderingMode::NSWindowBelow relativeTo: 0];
    }
    Ok(())
  }

  #[allow(non_upper_case_globals)]
  const NSAppKitVersionNumber10_14: f64 = 1671.0;

  // https://developer.apple.com/documentation/appkit/nsvisualeffectview/blendingmode
  #[allow(dead_code)]
  #[repr(u64)]
  #[derive(Clone, Copy, Debug, PartialEq)]
  enum NSVisualEffectBlendingMode {
    BehindWindow = 0,
    WithinWindow = 1,
  }

  // https://developer.apple.com/documentation/appkit/nsvisualeffectview/state
  #[allow(dead_code)]
  #[repr(u64)]
  #[derive(Clone, Copy, Debug, PartialEq)]
  enum NSVisualEffectState {
    FollowsWindowActiveState = 0,
    Active = 1,
    Inactive = 2,
  }

  // macos 10.10+
  // https://developer.apple.com/documentation/appkit/nsvisualeffectview
  #[allow(non_snake_case)]
  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);
    unsafe fn setAutoresizingMask_(self, autoresizingMask: NSAutoresizingMaskOptions);

    // 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]
    }

    unsafe fn setAutoresizingMask_(self, autoresizingMask: NSAutoresizingMaskOptions) {
      msg_send![self, setAutoresizingMask: autoresizingMask]
    }

    // 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]
    }
  }
}