Skip to content

Commit b23e38e

Browse files
committed
Add some missing Window getters
Implemented properly when it was easy for me, but a lot of places are stubbed out for now.
1 parent e99d59f commit b23e38e

File tree

12 files changed

+562
-16
lines changed

12 files changed

+562
-16
lines changed

winit-android/src/event_loop.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,16 @@ impl CoreWindow for Window {
876876
PhysicalInsets::new(0, 0, 0, 0)
877877
}
878878

879+
fn min_surface_size(&self) -> Option<PhysicalSize<u32>> {
880+
None
881+
}
882+
879883
fn set_min_surface_size(&self, _: Option<Size>) {}
880884

885+
fn max_surface_size(&self) -> Option<PhysicalSize<u32>> {
886+
None
887+
}
888+
881889
fn set_max_surface_size(&self, _: Option<Size>) {}
882890

883891
fn surface_resize_increments(&self) -> Option<PhysicalSize<u32>> {
@@ -888,8 +896,16 @@ impl CoreWindow for Window {
888896

889897
fn set_title(&self, _title: &str) {}
890898

899+
fn is_transparent(&self) -> bool {
900+
false
901+
}
902+
891903
fn set_transparent(&self, _transparent: bool) {}
892904

905+
fn is_blurred(&self) -> bool {
906+
false
907+
}
908+
893909
fn set_blur(&self, _blur: bool) {}
894910

895911
fn set_visible(&self, _visibility: bool) {}
@@ -936,8 +952,16 @@ impl CoreWindow for Window {
936952
true
937953
}
938954

955+
fn window_level(&self) -> WindowLevel {
956+
WindowLevel::default()
957+
}
958+
939959
fn set_window_level(&self, _level: WindowLevel) {}
940960

961+
fn window_icon(&self) -> Option<winit_core::icon::Icon> {
962+
None
963+
}
964+
941965
fn set_window_icon(&self, _window_icon: Option<winit_core::icon::Icon>) {}
942966

943967
fn set_ime_cursor_area(&self, _position: Position, _size: Size) {}
@@ -977,6 +1001,10 @@ impl CoreWindow for Window {
9771001

9781002
fn request_user_attention(&self, _request_type: Option<window::UserAttentionType>) {}
9791003

1004+
fn cursor(&self) -> Cursor {
1005+
Cursor::default()
1006+
}
1007+
9801008
fn set_cursor(&self, _: Cursor) {}
9811009

9821010
fn set_cursor_position(&self, _: Position) -> Result<(), RequestError> {
@@ -1010,6 +1038,10 @@ impl CoreWindow for Window {
10101038
None
10111039
}
10121040

1041+
fn content_protected(&self) -> bool {
1042+
false
1043+
}
1044+
10131045
fn set_content_protected(&self, _protected: bool) {}
10141046

10151047
fn has_focus(&self) -> bool {

winit-appkit/src/window.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::sync::Arc;
44

55
use dispatch2::MainThreadBound;
6-
use dpi::{Position, Size};
6+
use dpi::{PhysicalSize, Position, Size};
77
use objc2::rc::{autoreleasepool, Retained};
88
use objc2::{define_class, MainThreadMarker, Message};
99
use objc2_app_kit::{NSPanel, NSResponder, NSWindow};
@@ -141,10 +141,18 @@ impl CoreWindow for Window {
141141
self.maybe_wait_on_main(|delegate| delegate.safe_area())
142142
}
143143

144+
fn min_surface_size(&self) -> Option<PhysicalSize<u32>> {
145+
self.maybe_wait_on_main(|delegate| delegate.min_surface_size())
146+
}
147+
144148
fn set_min_surface_size(&self, min_size: Option<Size>) {
145149
self.maybe_wait_on_main(|delegate| delegate.set_min_surface_size(min_size))
146150
}
147151

152+
fn max_surface_size(&self) -> Option<PhysicalSize<u32>> {
153+
self.maybe_wait_on_main(|delegate| delegate.max_surface_size())
154+
}
155+
148156
fn set_max_surface_size(&self, max_size: Option<Size>) {
149157
self.maybe_wait_on_main(|delegate| delegate.set_max_surface_size(max_size));
150158
}
@@ -161,10 +169,18 @@ impl CoreWindow for Window {
161169
self.maybe_wait_on_main(|delegate| delegate.set_title(title));
162170
}
163171

172+
fn is_transparent(&self) -> bool {
173+
self.maybe_wait_on_main(|delegate| delegate.is_transparent())
174+
}
175+
164176
fn set_transparent(&self, transparent: bool) {
165177
self.maybe_wait_on_main(|delegate| delegate.set_transparent(transparent));
166178
}
167179

180+
fn is_blurred(&self) -> bool {
181+
self.maybe_wait_on_main(|delegate| delegate.is_blurred())
182+
}
183+
168184
fn set_blur(&self, blur: bool) {
169185
self.maybe_wait_on_main(|delegate| delegate.set_blur(blur));
170186
}
@@ -225,10 +241,18 @@ impl CoreWindow for Window {
225241
self.maybe_wait_on_main(|delegate| delegate.is_decorated())
226242
}
227243

244+
fn window_level(&self) -> WindowLevel {
245+
self.maybe_wait_on_main(|delegate| delegate.window_level())
246+
}
247+
228248
fn set_window_level(&self, level: WindowLevel) {
229249
self.maybe_wait_on_main(|delegate| delegate.set_window_level(level));
230250
}
231251

252+
fn window_icon(&self) -> Option<Icon> {
253+
self.maybe_wait_on_main(|delegate| delegate.window_icon())
254+
}
255+
232256
fn set_window_icon(&self, window_icon: Option<Icon>) {
233257
self.maybe_wait_on_main(|delegate| delegate.set_window_icon(window_icon));
234258
}
@@ -261,6 +285,10 @@ impl CoreWindow for Window {
261285
self.maybe_wait_on_main(|delegate| delegate.theme())
262286
}
263287

288+
fn content_protected(&self) -> bool {
289+
self.maybe_wait_on_main(|delegate| delegate.content_protected())
290+
}
291+
264292
fn set_content_protected(&self, protected: bool) {
265293
self.maybe_wait_on_main(|delegate| delegate.set_content_protected(protected));
266294
}
@@ -269,6 +297,10 @@ impl CoreWindow for Window {
269297
self.maybe_wait_on_main(|delegate| delegate.title())
270298
}
271299

300+
fn cursor(&self) -> Cursor {
301+
self.maybe_wait_on_main(|delegate| delegate.cursor())
302+
}
303+
272304
fn set_cursor(&self, cursor: Cursor) {
273305
self.maybe_wait_on_main(|delegate| delegate.set_cursor(cursor));
274306
}

winit-appkit/src/window_delegate.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,10 @@ impl WindowDelegate {
934934
self.window().setTitle(&NSString::from_str(title))
935935
}
936936

937+
pub fn is_transparent(&self) -> bool {
938+
unsafe { !self.window().isOpaque() }
939+
}
940+
937941
pub fn set_transparent(&self, transparent: bool) {
938942
// This is just a hint for Quartz, it doesn't actually speculate with window alpha.
939943
// Providing a wrong value here could result in visual artifacts, when the window is
@@ -954,6 +958,12 @@ impl WindowDelegate {
954958
self.window().setBackgroundColor(Some(&color));
955959
}
956960

961+
pub fn is_blurred(&self) -> bool {
962+
// What API would we use to get this? `CGSGetWindowBackgroundBlurRadius` doesn't exist.
963+
warn!("getting the background blur of the window is not supported on macOS");
964+
false
965+
}
966+
957967
pub fn set_blur(&self, blur: bool) {
958968
// NOTE: in general we want to specify the blur radius, but the choice of 80
959969
// should be a reasonable default.
@@ -1073,6 +1083,14 @@ impl WindowDelegate {
10731083
None
10741084
}
10751085

1086+
pub fn min_surface_size(&self) -> Option<PhysicalSize<u32>> {
1087+
let size = unsafe { self.window().contentMinSize() };
1088+
if size == NSSize::ZERO {
1089+
return None;
1090+
}
1091+
Some(LogicalSize::new(size.width, size.height).to_physical(self.scale_factor()))
1092+
}
1093+
10761094
pub fn set_min_surface_size(&self, dimensions: Option<Size>) {
10771095
let dimensions =
10781096
dimensions.unwrap_or(Size::Logical(LogicalSize { width: 0.0, height: 0.0 }));
@@ -1092,6 +1110,15 @@ impl WindowDelegate {
10921110
self.window().setContentSize(current_size);
10931111
}
10941112

1113+
pub fn max_surface_size(&self) -> Option<PhysicalSize<u32>> {
1114+
let size = unsafe { self.window().contentMaxSize() };
1115+
// AppKit sets the max size to f32::MAX by default.
1116+
if size == NSSize::new(f32::MAX as _, f32::MAX as _) {
1117+
return None;
1118+
}
1119+
Some(LogicalSize::new(size.width, size.height).to_physical(self.scale_factor()))
1120+
}
1121+
10951122
pub fn set_max_surface_size(&self, dimensions: Option<Size>) {
10961123
let dimensions = dimensions.unwrap_or(Size::Logical(LogicalSize {
10971124
width: f32::MAX as f64,
@@ -1213,6 +1240,11 @@ impl WindowDelegate {
12131240
buttons
12141241
}
12151242

1243+
pub fn cursor(&self) -> Cursor {
1244+
warn!("getting the cursor is unimplemented on macOS");
1245+
Cursor::default()
1246+
}
1247+
12161248
pub fn set_cursor(&self, cursor: Cursor) {
12171249
let view = self.view();
12181250

@@ -1643,6 +1675,20 @@ impl WindowDelegate {
16431675
self.ivars().decorations.get()
16441676
}
16451677

1678+
pub fn window_level(&self) -> WindowLevel {
1679+
let level = unsafe { self.window().level() };
1680+
if level == kCGFloatingWindowLevel as NSWindowLevel {
1681+
WindowLevel::AlwaysOnTop
1682+
} else if level == (kCGNormalWindowLevel - 1) as NSWindowLevel {
1683+
WindowLevel::AlwaysOnBottom
1684+
} else if level == kCGNormalWindowLevel as NSWindowLevel {
1685+
WindowLevel::Normal
1686+
} else {
1687+
warn!(?level, "cannot determine window level, it must've been set outside of Winit");
1688+
WindowLevel::default()
1689+
}
1690+
}
1691+
16461692
#[inline]
16471693
pub fn set_window_level(&self, level: WindowLevel) {
16481694
// Note: There are two different things at play here:
@@ -1662,6 +1708,11 @@ impl WindowDelegate {
16621708
self.window().setLevel(level);
16631709
}
16641710

1711+
#[inline]
1712+
pub fn window_icon(&self) -> Option<Icon> {
1713+
None
1714+
}
1715+
16651716
#[inline]
16661717
pub fn set_window_icon(&self, _icon: Option<Icon>) {
16671718
// macOS doesn't have window icons. Though, there is
@@ -1815,6 +1866,17 @@ impl WindowDelegate {
18151866
unsafe { self.window().setAppearance(theme_to_appearance(theme).as_deref()) };
18161867
}
18171868

1869+
pub fn content_protected(&self) -> bool {
1870+
match unsafe { self.window().sharingType() } {
1871+
NSWindowSharingType::None => true,
1872+
NSWindowSharingType::ReadOnly => false,
1873+
sharing_type => {
1874+
warn!(?sharing_type, "unknown sharing type");
1875+
false
1876+
},
1877+
}
1878+
}
1879+
18181880
#[inline]
18191881
pub fn set_content_protected(&self, protected: bool) {
18201882
self.window().setSharingType(if protected {

0 commit comments

Comments
 (0)