-
Notifications
You must be signed in to change notification settings - Fork 931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blur-Behind / Glassy Windows implementation #568
Changes from 8 commits
2cb2826
03e7eb2
d0e7a2f
a0e9484
e7686aa
ea070f0
916eec2
79d1f94
ea119ff
bc53f54
4eb989c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
extern crate winit; | ||
|
||
fn main() { | ||
let mut events_loop = winit::EventsLoop::new(); | ||
|
||
let window = winit::WindowBuilder::new() | ||
.with_title("A blurry window!") | ||
.with_blur(true) | ||
.build(&events_loop) | ||
.unwrap(); | ||
|
||
#[cfg(target_os = "macos")] | ||
{ | ||
// On macOS the blur material is 'light' by default. | ||
// Let's change it to a dark theme! | ||
use winit::os::macos::{BlurMaterial, WindowExt}; | ||
unsafe { window.set_blur_material(BlurMaterial::Dark) }; | ||
} | ||
|
||
events_loop.run_forever(|event| { | ||
match event { | ||
winit::Event::WindowEvent { | ||
event: winit::WindowEvent::CloseRequested, | ||
.. | ||
} => winit::ControlFlow::Break, | ||
winit::Event::WindowEvent { | ||
event: winit::WindowEvent::KeyboardInput { | ||
input: winit::KeyboardInput { | ||
virtual_keycode: Some(winit::VirtualKeyCode::Escape), | ||
.. | ||
}, | ||
.. | ||
}, | ||
.. | ||
} => winit::ControlFlow::Break, | ||
_ => winit::ControlFlow::Continue, | ||
} | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,13 @@ pub trait WindowExt { | |
/// | ||
/// The pointer will become invalid when the `Window` is destroyed. | ||
fn get_nsview(&self) -> *mut c_void; | ||
|
||
/// For windows created with the [blurred](WindowBuilder::with_blur) option, | ||
/// this controls the appearance of the blur effect. | ||
/// | ||
/// Marked as unsafe because depending on the version of macOS and the `BlurMaterial` variant passed, | ||
/// this might cause a crash. | ||
unsafe fn set_blur_material(&self, material: BlurMaterial); | ||
} | ||
|
||
impl WindowExt for Window { | ||
|
@@ -28,6 +35,11 @@ impl WindowExt for Window { | |
fn get_nsview(&self) -> *mut c_void { | ||
self.window.get_nsview() | ||
} | ||
|
||
#[inline] | ||
unsafe fn set_blur_material(&self, material: BlurMaterial) { | ||
self.window.set_blur_material(material); | ||
} | ||
} | ||
|
||
/// Corresponds to `NSApplicationActivationPolicy`. | ||
|
@@ -157,3 +169,53 @@ impl MonitorIdExt for MonitorId { | |
self.inner.get_nsscreen().map(|s| s as *mut c_void) | ||
} | ||
} | ||
|
||
/// Enumeration of all possible blur materials for macOS. Applies to macOS SDK 10.10+. | ||
/// | ||
/// Not all versions of macOS support all the variants listed here. | ||
/// Check [Apple's documentation](https://developer.apple.com/documentation/appkit/nsvisualeffectview/material) | ||
/// to find out what your target version supports. | ||
/// The behaviour for using a material which is not supported depends how it is implemented in cocoa, | ||
/// but will most likely cause a crash. | ||
#[repr(i64)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these actually supposed to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's an I have added some valuable documentation in my new commit. |
||
#[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
pub enum BlurMaterial { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this enum is problematic. I created it based on the documentation Apple updated a while ago, based on the upcoming SDK version for macOS Mojave. Most of these variants are not supported on High Sierra, and will crash the application when attempted to be used. I'm unsure how to go about this. Should we remove the enum entirely and make the |
||
/// A default material for the view’s effective appearance. | ||
AppearanceBased = 0, | ||
/// A material with a light effect. | ||
Light = 1, | ||
/// A material with a dark effect. | ||
Dark = 2, | ||
/// The material for a window’s titlebar. | ||
Titlebar = 3, | ||
/// The material used to indicate a selection. | ||
Selection = 4, | ||
/// The material for menus. | ||
Menu = 5, | ||
/// The material for the background of popover windows. | ||
Popover = 6, | ||
/// The material for the background of window sidebars. | ||
Sidebar = 7, | ||
/// A material with a medium-light effect. | ||
MediumLight = 8, | ||
/// A material with an ultra-dark effect. | ||
UltraDark = 9, | ||
/// The material for in-line header or footer views. | ||
HeaderView = 10, | ||
/// The material for the background of sheet windows. | ||
Sheet = 11, | ||
/// The material for the background of opaque windows. | ||
WindowBackground = 12, | ||
/// The material for the background of heads-up display (HUD) windows. | ||
HudWindow = 13, | ||
/// The material for the background of a full-screen modal interface. | ||
FullScreenUi = 15, | ||
/// The material for the background of a tool tip. | ||
ToolTip = 17, | ||
/// The material for the background of opaque content. | ||
ContentBackground = 18, | ||
/// The material for under a window's background. | ||
UnderWindowBackground = 21, | ||
/// The material for the area behind the pages of a document. | ||
UnderPageBackground = 22, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a lowest common denominator of supported enum variants? I don't think people will be comfortable using a method that can crash based on criteria that are only vaguely explained.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lowest common denominator appears to be
AppearanceBased
,Light
,Dark
,MediumLight
andUltraDark
(source), but those are now marked as deprecated. There are some marked as "modified", I think that means their underlying integer value has changed (validation needed), which would disqualify them. This is all quite problematic since this swift enum is our only source of truth and it appears Apple is comfortable changing the order of constants willy nilly - they only keep the swift code consistent, but apparently not the enum's binary representation.Honestly I don't know what to do here. This is why I was originally unsure about the
BlurMaterial
enum in the first place.The only alternative I see is exposing the raw
i64
and removing the enum, but that won't solve the crash / macOS-version dependency problem either.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How impractical would it be for us to translate the values depending on the version of macOS?
Also, shouldn't it be
isize
instead ofi64
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that would be too much work, definitely doable.
Yeah you could be right with
isize
overi64
. The right size is whatever the size of the swift enum is.isize
should be a good bet.