1 #![cfg(target_os = "macos")]
2 
3 use std::os::raw::c_void;
4 use {LogicalSize, MonitorId, Window, WindowBuilder};
5 
6 /// Additional methods on `Window` that are specific to MacOS.
7 pub trait WindowExt {
8     /// Returns a pointer to the cocoa `NSWindow` that is used by this window.
9     ///
10     /// The pointer will become invalid when the `Window` is destroyed.
get_nswindow(&self) -> *mut c_void11     fn get_nswindow(&self) -> *mut c_void;
12 
13     /// Returns a pointer to the cocoa `NSView` that is used by this window.
14     ///
15     /// The pointer will become invalid when the `Window` is destroyed.
get_nsview(&self) -> *mut c_void16     fn get_nsview(&self) -> *mut c_void;
17 
18     /// Request user attention, causing the application's dock icon to bounce.
19     /// Note that this has no effect if the application is already focused.
20     ///
21     /// The `is_critical` flag has the following effects:
22     /// - `false`: the dock icon will only bounce once.
23     /// - `true`: the dock icon will bounce until the application is focused.
request_user_attention(&self, is_critical: bool)24     fn request_user_attention(&self, is_critical: bool);
25 
26     /// Toggles a fullscreen mode that doesn't require a new macOS space.
27     /// Returns a boolean indicating whether the transition was successful (this
28     /// won't work if the window was already in the native fullscreen).
29     ///
30     /// This is how fullscreen used to work on macOS in versions before Lion.
31     /// And allows the user to have a fullscreen window without using another
32     /// space or taking control over the entire monitor.
set_simple_fullscreen(&self, fullscreen: bool) -> bool33     fn set_simple_fullscreen(&self, fullscreen: bool) -> bool;
34 }
35 
36 impl WindowExt for Window {
37     #[inline]
get_nswindow(&self) -> *mut c_void38     fn get_nswindow(&self) -> *mut c_void {
39         self.window.get_nswindow()
40     }
41 
42     #[inline]
get_nsview(&self) -> *mut c_void43     fn get_nsview(&self) -> *mut c_void {
44         self.window.get_nsview()
45     }
46 
47     #[inline]
request_user_attention(&self, is_critical: bool)48     fn request_user_attention(&self, is_critical: bool) {
49         self.window.request_user_attention(is_critical)
50     }
51 
52     #[inline]
set_simple_fullscreen(&self, fullscreen: bool) -> bool53     fn set_simple_fullscreen(&self, fullscreen: bool) -> bool {
54         self.window.set_simple_fullscreen(fullscreen)
55     }
56 }
57 
58 /// Corresponds to `NSApplicationActivationPolicy`.
59 #[derive(Debug, Clone, Copy, PartialEq)]
60 pub enum ActivationPolicy {
61     /// Corresponds to `NSApplicationActivationPolicyRegular`.
62     Regular,
63     /// Corresponds to `NSApplicationActivationPolicyAccessory`.
64     Accessory,
65     /// Corresponds to `NSApplicationActivationPolicyProhibited`.
66     Prohibited,
67 }
68 
69 impl Default for ActivationPolicy {
default() -> Self70     fn default() -> Self {
71         ActivationPolicy::Regular
72     }
73 }
74 
75 /// Additional methods on `WindowBuilder` that are specific to MacOS.
76 ///
77 /// **Note:** Properties dealing with the titlebar will be overwritten by the `with_decorations` method
78 /// on the base `WindowBuilder`:
79 ///
80 ///  - `with_titlebar_transparent`
81 ///  - `with_title_hidden`
82 ///  - `with_titlebar_hidden`
83 ///  - `with_titlebar_buttons_hidden`
84 ///  - `with_fullsize_content_view`
85 pub trait WindowBuilderExt {
86     /// Sets the activation policy for the window being built.
with_activation_policy(self, activation_policy: ActivationPolicy) -> WindowBuilder87     fn with_activation_policy(self, activation_policy: ActivationPolicy) -> WindowBuilder;
88     /// Enables click-and-drag behavior for the entire window, not just the titlebar.
with_movable_by_window_background(self, movable_by_window_background: bool) -> WindowBuilder89     fn with_movable_by_window_background(self, movable_by_window_background: bool) -> WindowBuilder;
90     /// Makes the titlebar transparent and allows the content to appear behind it.
with_titlebar_transparent(self, titlebar_transparent: bool) -> WindowBuilder91     fn with_titlebar_transparent(self, titlebar_transparent: bool) -> WindowBuilder;
92     /// Hides the window title.
with_title_hidden(self, title_hidden: bool) -> WindowBuilder93     fn with_title_hidden(self, title_hidden: bool) -> WindowBuilder;
94     /// Hides the window titlebar.
with_titlebar_hidden(self, titlebar_hidden: bool) -> WindowBuilder95     fn with_titlebar_hidden(self, titlebar_hidden: bool) -> WindowBuilder;
96     /// Hides the window titlebar buttons.
with_titlebar_buttons_hidden(self, titlebar_buttons_hidden: bool) -> WindowBuilder97     fn with_titlebar_buttons_hidden(self, titlebar_buttons_hidden: bool) -> WindowBuilder;
98     /// Makes the window content appear behind the titlebar.
with_fullsize_content_view(self, fullsize_content_view: bool) -> WindowBuilder99     fn with_fullsize_content_view(self, fullsize_content_view: bool) -> WindowBuilder;
100     /// Build window with `resizeIncrements` property. Values must not be 0.
with_resize_increments(self, increments: LogicalSize) -> WindowBuilder101     fn with_resize_increments(self, increments: LogicalSize) -> WindowBuilder;
102 }
103 
104 impl WindowBuilderExt for WindowBuilder {
105     #[inline]
with_activation_policy(mut self, activation_policy: ActivationPolicy) -> WindowBuilder106     fn with_activation_policy(mut self, activation_policy: ActivationPolicy) -> WindowBuilder {
107         self.platform_specific.activation_policy = activation_policy;
108         self
109     }
110 
111     #[inline]
with_movable_by_window_background(mut self, movable_by_window_background: bool) -> WindowBuilder112     fn with_movable_by_window_background(mut self, movable_by_window_background: bool) -> WindowBuilder {
113         self.platform_specific.movable_by_window_background = movable_by_window_background;
114         self
115     }
116 
117     #[inline]
with_titlebar_transparent(mut self, titlebar_transparent: bool) -> WindowBuilder118     fn with_titlebar_transparent(mut self, titlebar_transparent: bool) -> WindowBuilder {
119         self.platform_specific.titlebar_transparent = titlebar_transparent;
120         self
121     }
122 
123     #[inline]
with_titlebar_hidden(mut self, titlebar_hidden: bool) -> WindowBuilder124     fn with_titlebar_hidden(mut self, titlebar_hidden: bool) -> WindowBuilder {
125         self.platform_specific.titlebar_hidden = titlebar_hidden;
126         self
127     }
128 
129     #[inline]
with_titlebar_buttons_hidden(mut self, titlebar_buttons_hidden: bool) -> WindowBuilder130     fn with_titlebar_buttons_hidden(mut self, titlebar_buttons_hidden: bool) -> WindowBuilder {
131         self.platform_specific.titlebar_buttons_hidden = titlebar_buttons_hidden;
132         self
133     }
134 
135     #[inline]
with_title_hidden(mut self, title_hidden: bool) -> WindowBuilder136     fn with_title_hidden(mut self, title_hidden: bool) -> WindowBuilder {
137         self.platform_specific.title_hidden = title_hidden;
138         self
139     }
140 
141     #[inline]
with_fullsize_content_view(mut self, fullsize_content_view: bool) -> WindowBuilder142     fn with_fullsize_content_view(mut self, fullsize_content_view: bool) -> WindowBuilder {
143         self.platform_specific.fullsize_content_view = fullsize_content_view;
144         self
145     }
146 
147     #[inline]
with_resize_increments(mut self, increments: LogicalSize) -> WindowBuilder148     fn with_resize_increments(mut self, increments: LogicalSize) -> WindowBuilder {
149         self.platform_specific.resize_increments = Some(increments.into());
150         self
151     }
152 }
153 
154 /// Additional methods on `MonitorId` that are specific to MacOS.
155 pub trait MonitorIdExt {
156     /// Returns the identifier of the monitor for Cocoa.
native_id(&self) -> u32157     fn native_id(&self) -> u32;
158     /// Returns a pointer to the NSScreen representing this monitor.
get_nsscreen(&self) -> Option<*mut c_void>159     fn get_nsscreen(&self) -> Option<*mut c_void>;
160 }
161 
162 impl MonitorIdExt for MonitorId {
163     #[inline]
native_id(&self) -> u32164     fn native_id(&self) -> u32 {
165         self.inner.get_native_identifier()
166     }
167 
get_nsscreen(&self) -> Option<*mut c_void>168     fn get_nsscreen(&self) -> Option<*mut c_void> {
169         self.inner.get_nsscreen().map(|s| s as *mut c_void)
170     }
171 }
172