macOS: Fix NSWindowLevel values (#2545)

* Fix NSWindowLevel values

* Fix formatting
This commit is contained in:
Mads Marquart 2022-11-22 11:08:56 +01:00 committed by GitHub
parent 05484c5888
commit bdcbd7d1f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 40 deletions

View file

@ -240,53 +240,69 @@ unsafe impl Encode for NSWindowButton {
const ENCODING: Encoding = NSUInteger::ENCODING; const ENCODING: Encoding = NSUInteger::ENCODING;
} }
// CGWindowLevel.h
//
// Note: There are two different things at play in this header:
// `CGWindowLevel` and `CGWindowLevelKey`.
//
// It seems like there was a push towards using "key" values instead of the
// raw window level values, and then you were supposed to use
// `CGWindowLevelForKey` to get the actual level.
//
// But the values that `NSWindowLevel` has are compiled in, and as such has
// to remain ABI compatible, so they're safe for us to hardcode as well.
#[allow(dead_code)] #[allow(dead_code)]
mod window_level_key { mod window_level {
use objc2::foundation::NSInteger; const kCGNumReservedWindowLevels: i32 = 16;
pub const kCGBaseWindowLevelKey: NSInteger = 0; const kCGNumReservedBaseWindowLevels: i32 = 5;
pub const kCGMinimumWindowLevelKey: NSInteger = 1;
pub const kCGDesktopWindowLevelKey: NSInteger = 2; pub const kCGBaseWindowLevel: i32 = i32::MIN;
pub const kCGBackstopMenuLevelKey: NSInteger = 3; pub const kCGMinimumWindowLevel: i32 = kCGBaseWindowLevel + kCGNumReservedBaseWindowLevels;
pub const kCGNormalWindowLevelKey: NSInteger = 4; pub const kCGMaximumWindowLevel: i32 = i32::MAX - kCGNumReservedWindowLevels;
pub const kCGFloatingWindowLevelKey: NSInteger = 5;
pub const kCGTornOffMenuWindowLevelKey: NSInteger = 6; pub const kCGDesktopWindowLevel: i32 = kCGMinimumWindowLevel + 20;
pub const kCGDockWindowLevelKey: NSInteger = 7; pub const kCGDesktopIconWindowLevel: i32 = kCGDesktopWindowLevel + 20;
pub const kCGMainMenuWindowLevelKey: NSInteger = 8; pub const kCGBackstopMenuLevel: i32 = -20;
pub const kCGStatusWindowLevelKey: NSInteger = 9; pub const kCGNormalWindowLevel: i32 = 0;
pub const kCGModalPanelWindowLevelKey: NSInteger = 10; pub const kCGFloatingWindowLevel: i32 = 3;
pub const kCGPopUpMenuWindowLevelKey: NSInteger = 11; pub const kCGTornOffMenuWindowLevel: i32 = 3;
pub const kCGDraggingWindowLevelKey: NSInteger = 12; pub const kCGModalPanelWindowLevel: i32 = 8;
pub const kCGScreenSaverWindowLevelKey: NSInteger = 13; pub const kCGUtilityWindowLevel: i32 = 19;
pub const kCGMaximumWindowLevelKey: NSInteger = 14; pub const kCGDockWindowLevel: i32 = 20;
pub const kCGOverlayWindowLevelKey: NSInteger = 15; pub const kCGMainMenuWindowLevel: i32 = 24;
pub const kCGHelpWindowLevelKey: NSInteger = 16; pub const kCGStatusWindowLevel: i32 = 25;
pub const kCGUtilityWindowLevelKey: NSInteger = 17; pub const kCGPopUpMenuWindowLevel: i32 = 101;
pub const kCGDesktopIconWindowLevelKey: NSInteger = 18; pub const kCGOverlayWindowLevel: i32 = 102;
pub const kCGCursorWindowLevelKey: NSInteger = 19; pub const kCGHelpWindowLevel: i32 = 200;
pub const kCGNumberOfWindowLevelKeys: NSInteger = 20; pub const kCGDraggingWindowLevel: i32 = 500;
pub const kCGScreenSaverWindowLevel: i32 = 1000;
pub const kCGAssistiveTechHighWindowLevel: i32 = 1500;
pub const kCGCursorWindowLevel: i32 = kCGMaximumWindowLevel - 1;
} }
use window_level_key::*; use window_level::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct NSWindowLevel(pub NSInteger);
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] impl NSWindowLevel {
#[repr(isize)]
pub enum NSWindowLevel {
#[doc(alias = "NSNormalWindowLevel")] #[doc(alias = "NSNormalWindowLevel")]
Normal = kCGBaseWindowLevelKey, pub const Normal: Self = Self(kCGNormalWindowLevel as _);
#[doc(alias = "NSFloatingWindowLevel")] #[doc(alias = "NSFloatingWindowLevel")]
Floating = kCGFloatingWindowLevelKey, pub const Floating: Self = Self(kCGFloatingWindowLevel as _);
#[doc(alias = "NSTornOffMenuWindowLevel")] #[doc(alias = "NSTornOffMenuWindowLevel")]
TornOffMenu = kCGTornOffMenuWindowLevelKey, pub const TornOffMenu: Self = Self(kCGTornOffMenuWindowLevel as _);
#[doc(alias = "NSModalPanelWindowLevel")] #[doc(alias = "NSModalPanelWindowLevel")]
ModalPanel = kCGModalPanelWindowLevelKey, pub const ModalPanel: Self = Self(kCGModalPanelWindowLevel as _);
#[doc(alias = "NSMainMenuWindowLevel")] #[doc(alias = "NSMainMenuWindowLevel")]
MainMenu = kCGMainMenuWindowLevelKey, pub const MainMenu: Self = Self(kCGMainMenuWindowLevel as _);
#[doc(alias = "NSStatusWindowLevel")] #[doc(alias = "NSStatusWindowLevel")]
Status = kCGStatusWindowLevelKey, pub const Status: Self = Self(kCGStatusWindowLevel as _);
#[doc(alias = "NSPopUpMenuWindowLevel")] #[doc(alias = "NSPopUpMenuWindowLevel")]
PopUpMenu = kCGPopUpMenuWindowLevelKey, pub const PopUpMenu: Self = Self(kCGPopUpMenuWindowLevel as _);
#[doc(alias = "NSScreenSaverWindowLevel")] #[doc(alias = "NSScreenSaverWindowLevel")]
ScreenSaver = kCGScreenSaverWindowLevelKey, pub const ScreenSaver: Self = Self(kCGScreenSaverWindowLevel as _);
} }
unsafe impl Encode for NSWindowLevel { unsafe impl Encode for NSWindowLevel {

View file

@ -36,7 +36,8 @@ use crate::{
use core_graphics::display::{CGDisplay, CGPoint}; use core_graphics::display::{CGDisplay, CGPoint};
use objc2::declare::{Ivar, IvarDrop}; use objc2::declare::{Ivar, IvarDrop};
use objc2::foundation::{ use objc2::foundation::{
is_main_thread, CGFloat, NSArray, NSCopying, NSObject, NSPoint, NSRect, NSSize, NSString, is_main_thread, CGFloat, NSArray, NSCopying, NSInteger, NSObject, NSPoint, NSRect, NSSize,
NSString,
}; };
use objc2::rc::{autoreleasepool, Id, Owned, Shared}; use objc2::rc::{autoreleasepool, Id, Owned, Shared};
use objc2::{declare_class, msg_send, msg_send_id, sel, ClassType}; use objc2::{declare_class, msg_send, msg_send_id, sel, ClassType};
@ -944,10 +945,9 @@ impl WinitWindow {
| NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar; | NSApplicationPresentationOptions::NSApplicationPresentationHideMenuBar;
app.setPresentationOptions(presentation_options); app.setPresentationOptions(presentation_options);
#[allow(clippy::let_unit_value)] let window_level =
unsafe { NSWindowLevel(unsafe { ffi::CGShieldingWindowLevel() } as NSInteger + 1);
let _: () = msg_send![self, setLevel: ffi::CGShieldingWindowLevel() + 1]; self.setLevel(window_level);
}
} }
(&Some(Fullscreen::Exclusive(ref video_mode)), &Some(Fullscreen::Borderless(_))) => { (&Some(Fullscreen::Exclusive(ref video_mode)), &Some(Fullscreen::Borderless(_))) => {
let presentation_options = let presentation_options =