rio_winit_fork/platform/
macos.rs1use std::os::raw::c_void;
18
19#[cfg(feature = "serde")]
20use serde::{Deserialize, Serialize};
21
22use crate::event_loop::{ActiveEventLoop, EventLoopBuilder};
23use crate::monitor::MonitorHandle;
24use crate::window::{Window, WindowAttributes};
25
26pub trait WindowExtMacOS {
28 fn simple_fullscreen(&self) -> bool;
30
31 fn set_simple_fullscreen(&self, fullscreen: bool) -> bool;
39
40 fn has_shadow(&self) -> bool;
42
43 fn set_has_shadow(&self, has_shadow: bool);
45
46 fn set_tabbing_identifier(&self, identifier: &str);
50
51 fn tabbing_identifier(&self) -> String;
53
54 fn select_next_tab(&self);
56
57 fn select_previous_tab(&self);
59
60 fn select_tab_at_index(&self, index: usize);
64
65 fn num_tabs(&self) -> usize;
67
68 fn is_document_edited(&self) -> bool;
83
84 fn set_document_edited(&self, edited: bool);
86
87 fn set_option_as_alt(&self, option_as_alt: OptionAsAlt);
94
95 fn option_as_alt(&self) -> OptionAsAlt;
97}
98
99impl WindowExtMacOS for Window {
100 #[inline]
101 fn simple_fullscreen(&self) -> bool {
102 self.window.maybe_wait_on_main(|w| w.simple_fullscreen())
103 }
104
105 #[inline]
106 fn set_simple_fullscreen(&self, fullscreen: bool) -> bool {
107 self.window.maybe_wait_on_main(move |w| w.set_simple_fullscreen(fullscreen))
108 }
109
110 #[inline]
111 fn has_shadow(&self) -> bool {
112 self.window.maybe_wait_on_main(|w| w.has_shadow())
113 }
114
115 #[inline]
116 fn set_has_shadow(&self, has_shadow: bool) {
117 self.window.maybe_queue_on_main(move |w| w.set_has_shadow(has_shadow))
118 }
119
120 #[inline]
121 fn set_tabbing_identifier(&self, identifier: &str) {
122 self.window.maybe_wait_on_main(|w| w.set_tabbing_identifier(identifier))
123 }
124
125 #[inline]
126 fn tabbing_identifier(&self) -> String {
127 self.window.maybe_wait_on_main(|w| w.tabbing_identifier())
128 }
129
130 #[inline]
131 fn select_next_tab(&self) {
132 self.window.maybe_queue_on_main(|w| w.select_next_tab())
133 }
134
135 #[inline]
136 fn select_previous_tab(&self) {
137 self.window.maybe_queue_on_main(|w| w.select_previous_tab())
138 }
139
140 #[inline]
141 fn select_tab_at_index(&self, index: usize) {
142 self.window.maybe_queue_on_main(move |w| w.select_tab_at_index(index))
143 }
144
145 #[inline]
146 fn num_tabs(&self) -> usize {
147 self.window.maybe_wait_on_main(|w| w.num_tabs())
148 }
149
150 #[inline]
151 fn is_document_edited(&self) -> bool {
152 self.window.maybe_wait_on_main(|w| w.is_document_edited())
153 }
154
155 #[inline]
156 fn set_document_edited(&self, edited: bool) {
157 self.window.maybe_queue_on_main(move |w| w.set_document_edited(edited))
158 }
159
160 #[inline]
161 fn set_option_as_alt(&self, option_as_alt: OptionAsAlt) {
162 self.window.maybe_queue_on_main(move |w| w.set_option_as_alt(option_as_alt))
163 }
164
165 #[inline]
166 fn option_as_alt(&self) -> OptionAsAlt {
167 self.window.maybe_wait_on_main(|w| w.option_as_alt())
168 }
169}
170
171#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
173pub enum ActivationPolicy {
174 #[default]
176 Regular,
177
178 Accessory,
180
181 Prohibited,
183}
184
185pub trait WindowAttributesExtMacOS {
195 fn with_movable_by_window_background(self, movable_by_window_background: bool) -> Self;
197 fn with_titlebar_transparent(self, titlebar_transparent: bool) -> Self;
199 fn with_title_hidden(self, title_hidden: bool) -> Self;
201 fn with_titlebar_hidden(self, titlebar_hidden: bool) -> Self;
203 fn with_titlebar_buttons_hidden(self, titlebar_buttons_hidden: bool) -> Self;
205 fn with_fullsize_content_view(self, fullsize_content_view: bool) -> Self;
207 fn with_disallow_hidpi(self, disallow_hidpi: bool) -> Self;
208 fn with_has_shadow(self, has_shadow: bool) -> Self;
209 fn with_accepts_first_mouse(self, accepts_first_mouse: bool) -> Self;
211 fn with_tabbing_identifier(self, identifier: &str) -> Self;
215 fn with_option_as_alt(self, option_as_alt: OptionAsAlt) -> Self;
219}
220
221impl WindowAttributesExtMacOS for WindowAttributes {
222 #[inline]
223 fn with_movable_by_window_background(mut self, movable_by_window_background: bool) -> Self {
224 self.platform_specific.movable_by_window_background = movable_by_window_background;
225 self
226 }
227
228 #[inline]
229 fn with_titlebar_transparent(mut self, titlebar_transparent: bool) -> Self {
230 self.platform_specific.titlebar_transparent = titlebar_transparent;
231 self
232 }
233
234 #[inline]
235 fn with_titlebar_hidden(mut self, titlebar_hidden: bool) -> Self {
236 self.platform_specific.titlebar_hidden = titlebar_hidden;
237 self
238 }
239
240 #[inline]
241 fn with_titlebar_buttons_hidden(mut self, titlebar_buttons_hidden: bool) -> Self {
242 self.platform_specific.titlebar_buttons_hidden = titlebar_buttons_hidden;
243 self
244 }
245
246 #[inline]
247 fn with_title_hidden(mut self, title_hidden: bool) -> Self {
248 self.platform_specific.title_hidden = title_hidden;
249 self
250 }
251
252 #[inline]
253 fn with_fullsize_content_view(mut self, fullsize_content_view: bool) -> Self {
254 self.platform_specific.fullsize_content_view = fullsize_content_view;
255 self
256 }
257
258 #[inline]
259 fn with_disallow_hidpi(mut self, disallow_hidpi: bool) -> Self {
260 self.platform_specific.disallow_hidpi = disallow_hidpi;
261 self
262 }
263
264 #[inline]
265 fn with_has_shadow(mut self, has_shadow: bool) -> Self {
266 self.platform_specific.has_shadow = has_shadow;
267 self
268 }
269
270 #[inline]
271 fn with_accepts_first_mouse(mut self, accepts_first_mouse: bool) -> Self {
272 self.platform_specific.accepts_first_mouse = accepts_first_mouse;
273 self
274 }
275
276 #[inline]
277 fn with_tabbing_identifier(mut self, tabbing_identifier: &str) -> Self {
278 self.platform_specific.tabbing_identifier.replace(tabbing_identifier.to_string());
279 self
280 }
281
282 #[inline]
283 fn with_option_as_alt(mut self, option_as_alt: OptionAsAlt) -> Self {
284 self.platform_specific.option_as_alt = option_as_alt;
285 self
286 }
287}
288
289pub trait EventLoopBuilderExtMacOS {
290 fn with_activation_policy(&mut self, activation_policy: ActivationPolicy) -> &mut Self;
311
312 fn with_default_menu(&mut self, enable: bool) -> &mut Self;
333
334 fn with_activate_ignoring_other_apps(&mut self, ignore: bool) -> &mut Self;
339}
340
341impl<T> EventLoopBuilderExtMacOS for EventLoopBuilder<T> {
342 #[inline]
343 fn with_activation_policy(&mut self, activation_policy: ActivationPolicy) -> &mut Self {
344 self.platform_specific.activation_policy = activation_policy;
345 self
346 }
347
348 #[inline]
349 fn with_default_menu(&mut self, enable: bool) -> &mut Self {
350 self.platform_specific.default_menu = enable;
351 self
352 }
353
354 #[inline]
355 fn with_activate_ignoring_other_apps(&mut self, ignore: bool) -> &mut Self {
356 self.platform_specific.activate_ignoring_other_apps = ignore;
357 self
358 }
359}
360
361pub trait MonitorHandleExtMacOS {
363 fn native_id(&self) -> u32;
365 fn ns_screen(&self) -> Option<*mut c_void>;
367}
368
369impl MonitorHandleExtMacOS for MonitorHandle {
370 #[inline]
371 fn native_id(&self) -> u32 {
372 self.inner.native_identifier()
373 }
374
375 fn ns_screen(&self) -> Option<*mut c_void> {
376 let mtm = unsafe { objc2_foundation::MainThreadMarker::new_unchecked() };
378 self.inner.ns_screen(mtm).map(|s| objc2::rc::Retained::as_ptr(&s) as _)
379 }
380}
381
382pub trait ActiveEventLoopExtMacOS {
384 fn hide_application(&self);
387 fn hide_other_applications(&self);
390 fn set_allows_automatic_window_tabbing(&self, enabled: bool);
394 fn allows_automatic_window_tabbing(&self) -> bool;
396}
397
398impl ActiveEventLoopExtMacOS for ActiveEventLoop {
399 fn hide_application(&self) {
400 self.p.hide_application()
401 }
402
403 fn hide_other_applications(&self) {
404 self.p.hide_other_applications()
405 }
406
407 fn set_allows_automatic_window_tabbing(&self, enabled: bool) {
408 self.p.set_allows_automatic_window_tabbing(enabled);
409 }
410
411 fn allows_automatic_window_tabbing(&self) -> bool {
412 self.p.allows_automatic_window_tabbing()
413 }
414}
415
416#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
420#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
421pub enum OptionAsAlt {
422 OnlyLeft,
424
425 OnlyRight,
427
428 Both,
430
431 #[default]
433 None,
434}