Add some missing Window getters

Implemented properly when it was easy for me, but a lot of places are
stubbed out for now.
This commit is contained in:
Mads Marquart
2025-09-05 18:47:51 +02:00
parent e99d59feaf
commit b23e38efcb
12 changed files with 562 additions and 16 deletions

View File

@@ -3,7 +3,7 @@
use std::sync::Arc;
use dispatch2::MainThreadBound;
use dpi::{Position, Size};
use dpi::{PhysicalSize, Position, Size};
use objc2::rc::{autoreleasepool, Retained};
use objc2::{define_class, MainThreadMarker, Message};
use objc2_app_kit::{NSPanel, NSResponder, NSWindow};
@@ -141,10 +141,18 @@ impl CoreWindow for Window {
self.maybe_wait_on_main(|delegate| delegate.safe_area())
}
fn min_surface_size(&self) -> Option<PhysicalSize<u32>> {
self.maybe_wait_on_main(|delegate| delegate.min_surface_size())
}
fn set_min_surface_size(&self, min_size: Option<Size>) {
self.maybe_wait_on_main(|delegate| delegate.set_min_surface_size(min_size))
}
fn max_surface_size(&self) -> Option<PhysicalSize<u32>> {
self.maybe_wait_on_main(|delegate| delegate.max_surface_size())
}
fn set_max_surface_size(&self, max_size: Option<Size>) {
self.maybe_wait_on_main(|delegate| delegate.set_max_surface_size(max_size));
}
@@ -161,10 +169,18 @@ impl CoreWindow for Window {
self.maybe_wait_on_main(|delegate| delegate.set_title(title));
}
fn is_transparent(&self) -> bool {
self.maybe_wait_on_main(|delegate| delegate.is_transparent())
}
fn set_transparent(&self, transparent: bool) {
self.maybe_wait_on_main(|delegate| delegate.set_transparent(transparent));
}
fn is_blurred(&self) -> bool {
self.maybe_wait_on_main(|delegate| delegate.is_blurred())
}
fn set_blur(&self, blur: bool) {
self.maybe_wait_on_main(|delegate| delegate.set_blur(blur));
}
@@ -225,10 +241,18 @@ impl CoreWindow for Window {
self.maybe_wait_on_main(|delegate| delegate.is_decorated())
}
fn window_level(&self) -> WindowLevel {
self.maybe_wait_on_main(|delegate| delegate.window_level())
}
fn set_window_level(&self, level: WindowLevel) {
self.maybe_wait_on_main(|delegate| delegate.set_window_level(level));
}
fn window_icon(&self) -> Option<Icon> {
self.maybe_wait_on_main(|delegate| delegate.window_icon())
}
fn set_window_icon(&self, window_icon: Option<Icon>) {
self.maybe_wait_on_main(|delegate| delegate.set_window_icon(window_icon));
}
@@ -261,6 +285,10 @@ impl CoreWindow for Window {
self.maybe_wait_on_main(|delegate| delegate.theme())
}
fn content_protected(&self) -> bool {
self.maybe_wait_on_main(|delegate| delegate.content_protected())
}
fn set_content_protected(&self, protected: bool) {
self.maybe_wait_on_main(|delegate| delegate.set_content_protected(protected));
}
@@ -269,6 +297,10 @@ impl CoreWindow for Window {
self.maybe_wait_on_main(|delegate| delegate.title())
}
fn cursor(&self) -> Cursor {
self.maybe_wait_on_main(|delegate| delegate.cursor())
}
fn set_cursor(&self, cursor: Cursor) {
self.maybe_wait_on_main(|delegate| delegate.set_cursor(cursor));
}

View File

@@ -934,6 +934,10 @@ impl WindowDelegate {
self.window().setTitle(&NSString::from_str(title))
}
pub fn is_transparent(&self) -> bool {
unsafe { !self.window().isOpaque() }
}
pub fn set_transparent(&self, transparent: bool) {
// This is just a hint for Quartz, it doesn't actually speculate with window alpha.
// Providing a wrong value here could result in visual artifacts, when the window is
@@ -954,6 +958,12 @@ impl WindowDelegate {
self.window().setBackgroundColor(Some(&color));
}
pub fn is_blurred(&self) -> bool {
// What API would we use to get this? `CGSGetWindowBackgroundBlurRadius` doesn't exist.
warn!("getting the background blur of the window is not supported on macOS");
false
}
pub fn set_blur(&self, blur: bool) {
// NOTE: in general we want to specify the blur radius, but the choice of 80
// should be a reasonable default.
@@ -1073,6 +1083,14 @@ impl WindowDelegate {
None
}
pub fn min_surface_size(&self) -> Option<PhysicalSize<u32>> {
let size = unsafe { self.window().contentMinSize() };
if size == NSSize::ZERO {
return None;
}
Some(LogicalSize::new(size.width, size.height).to_physical(self.scale_factor()))
}
pub fn set_min_surface_size(&self, dimensions: Option<Size>) {
let dimensions =
dimensions.unwrap_or(Size::Logical(LogicalSize { width: 0.0, height: 0.0 }));
@@ -1092,6 +1110,15 @@ impl WindowDelegate {
self.window().setContentSize(current_size);
}
pub fn max_surface_size(&self) -> Option<PhysicalSize<u32>> {
let size = unsafe { self.window().contentMaxSize() };
// AppKit sets the max size to f32::MAX by default.
if size == NSSize::new(f32::MAX as _, f32::MAX as _) {
return None;
}
Some(LogicalSize::new(size.width, size.height).to_physical(self.scale_factor()))
}
pub fn set_max_surface_size(&self, dimensions: Option<Size>) {
let dimensions = dimensions.unwrap_or(Size::Logical(LogicalSize {
width: f32::MAX as f64,
@@ -1213,6 +1240,11 @@ impl WindowDelegate {
buttons
}
pub fn cursor(&self) -> Cursor {
warn!("getting the cursor is unimplemented on macOS");
Cursor::default()
}
pub fn set_cursor(&self, cursor: Cursor) {
let view = self.view();
@@ -1643,6 +1675,20 @@ impl WindowDelegate {
self.ivars().decorations.get()
}
pub fn window_level(&self) -> WindowLevel {
let level = unsafe { self.window().level() };
if level == kCGFloatingWindowLevel as NSWindowLevel {
WindowLevel::AlwaysOnTop
} else if level == (kCGNormalWindowLevel - 1) as NSWindowLevel {
WindowLevel::AlwaysOnBottom
} else if level == kCGNormalWindowLevel as NSWindowLevel {
WindowLevel::Normal
} else {
warn!(?level, "cannot determine window level, it must've been set outside of Winit");
WindowLevel::default()
}
}
#[inline]
pub fn set_window_level(&self, level: WindowLevel) {
// Note: There are two different things at play here:
@@ -1662,6 +1708,11 @@ impl WindowDelegate {
self.window().setLevel(level);
}
#[inline]
pub fn window_icon(&self) -> Option<Icon> {
None
}
#[inline]
pub fn set_window_icon(&self, _icon: Option<Icon>) {
// macOS doesn't have window icons. Though, there is
@@ -1815,6 +1866,17 @@ impl WindowDelegate {
unsafe { self.window().setAppearance(theme_to_appearance(theme).as_deref()) };
}
pub fn content_protected(&self) -> bool {
match unsafe { self.window().sharingType() } {
NSWindowSharingType::None => true,
NSWindowSharingType::ReadOnly => false,
sharing_type => {
warn!(?sharing_type, "unknown sharing type");
false
},
}
}
#[inline]
pub fn set_content_protected(&self, protected: bool) {
self.window().setSharingType(if protected {