tao 0.16.11

Cross-platform window manager library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// Copyright 2014-2021 The winit contributors
// Copyright 2021-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

#![cfg(target_os = "macos")]

use std::os::raw::c_void;

use crate::{
  dpi::LogicalSize,
  event_loop::{EventLoop, EventLoopWindowTarget},
  menu::CustomMenuItem,
  monitor::MonitorHandle,
  platform_impl::{get_aux_state_mut, Parent},
  window::{Window, WindowBuilder},
};

#[cfg(feature = "tray")]
use crate::system_tray::{SystemTray, SystemTrayBuilder};

use cocoa::{
  appkit::{
    self, NSApplicationActivationPolicy, NSApplicationActivationPolicyAccessory,
    NSApplicationActivationPolicyProhibited, NSApplicationActivationPolicyRegular,
  },
  base::id,
};

/// Additional methods on `Window` that are specific to MacOS.
pub trait WindowExtMacOS {
  /// Returns a pointer to the cocoa `NSWindow` that is used by this window.
  ///
  /// The pointer will become invalid when the `Window` is destroyed.
  fn ns_window(&self) -> *mut c_void;

  /// Returns a pointer to the cocoa `NSView` that is used by this window.
  ///
  /// The pointer will become invalid when the `Window` is destroyed.
  fn ns_view(&self) -> *mut c_void;

  /// Returns whether or not the window is in simple fullscreen mode.
  fn simple_fullscreen(&self) -> bool;

  /// Toggles a fullscreen mode that doesn't require a new macOS space.
  /// Returns a boolean indicating whether the transition was successful (this
  /// won't work if the window was already in the native fullscreen).
  ///
  /// This is how fullscreen used to work on macOS in versions before Lion.
  /// And allows the user to have a fullscreen window without using another
  /// space or taking control over the entire monitor.
  fn set_simple_fullscreen(&self, fullscreen: bool) -> bool;

  /// Returns whether or not the window has shadow.
  fn has_shadow(&self) -> bool;

  /// Sets whether or not the window has shadow.
  fn set_has_shadow(&self, has_shadow: bool);

  /// Put the window in a state which indicates a file save is required.
  ///
  /// <https://developer.apple.com/documentation/appkit/nswindow/1419311-isdocumentedited>
  fn set_is_document_edited(&self, edited: bool);

  /// Get the window's edit state
  fn is_document_edited(&self) -> bool;

  /// Sets whether the system can automatically organize windows into tabs.
  ///
  /// <https://developer.apple.com/documentation/appkit/nswindow/1646657-allowsautomaticwindowtabbing>
  fn set_allows_automatic_window_tabbing(&self, enabled: bool);

  /// Returns whether the system can automatically organize windows into tabs.
  fn allows_automatic_window_tabbing(&self) -> bool;

  /// Group windows together by using the same tabbing identifier.
  ///
  /// <https://developer.apple.com/documentation/appkit/nswindow/1644704-tabbingidentifier>
  fn set_tabbing_identifier(&self, identifier: &str);

  /// Returns the window's tabbing identifier.
  fn tabbing_identifier(&self) -> String;
}

impl WindowExtMacOS for Window {
  #[inline]
  fn ns_window(&self) -> *mut c_void {
    self.window.ns_window()
  }

  #[inline]
  fn ns_view(&self) -> *mut c_void {
    self.window.ns_view()
  }

  #[inline]
  fn simple_fullscreen(&self) -> bool {
    self.window.simple_fullscreen()
  }

  #[inline]
  fn set_simple_fullscreen(&self, fullscreen: bool) -> bool {
    self.window.set_simple_fullscreen(fullscreen)
  }

  #[inline]
  fn has_shadow(&self) -> bool {
    self.window.has_shadow()
  }

  #[inline]
  fn set_has_shadow(&self, has_shadow: bool) {
    self.window.set_has_shadow(has_shadow)
  }

  #[inline]
  fn set_is_document_edited(&self, edited: bool) {
    self.window.set_is_document_edited(edited)
  }

  #[inline]
  fn is_document_edited(&self) -> bool {
    self.window.is_document_edited()
  }

  #[inline]
  fn set_allows_automatic_window_tabbing(&self, enabled: bool) {
    self.window.set_allows_automatic_window_tabbing(enabled)
  }

  #[inline]
  fn allows_automatic_window_tabbing(&self) -> bool {
    self.window.allows_automatic_window_tabbing()
  }

  #[inline]
  fn set_tabbing_identifier(&self, identifier: &str) {
    self.window.set_tabbing_identifier(identifier)
  }

  #[inline]
  fn tabbing_identifier(&self) -> String {
    self.window.tabbing_identifier()
  }
}

/// Corresponds to `NSApplicationActivationPolicy`.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ActivationPolicy {
  /// Corresponds to `NSApplicationActivationPolicyRegular`.
  Regular,
  /// Corresponds to `NSApplicationActivationPolicyAccessory`.
  Accessory,
  /// Corresponds to `NSApplicationActivationPolicyProhibited`.
  Prohibited,
}

impl Default for ActivationPolicy {
  fn default() -> Self {
    ActivationPolicy::Regular
  }
}

impl From<ActivationPolicy> for NSApplicationActivationPolicy {
  fn from(act_pol: ActivationPolicy) -> Self {
    match act_pol {
      ActivationPolicy::Regular => NSApplicationActivationPolicyRegular,
      ActivationPolicy::Accessory => NSApplicationActivationPolicyAccessory,
      ActivationPolicy::Prohibited => NSApplicationActivationPolicyProhibited,
    }
  }
}

pub trait CustomMenuItemExtMacOS {
  fn set_native_image(&mut self, native_image: NativeImage);
}

impl CustomMenuItemExtMacOS for CustomMenuItem {
  fn set_native_image(&mut self, native_image: NativeImage) {
    self.0.set_native_image(native_image)
  }
}

/// Named images, defined by the system or you, for use in your app.
#[non_exhaustive]
pub enum NativeImage {
  /// An add item template image.
  Add,
  /// Advanced preferences toolbar icon for the preferences window.
  Advanced,
  /// A Bluetooth template image.
  Bluetooth,
  /// Bookmarks image suitable for a template.
  Bookmarks,
  /// A caution image.
  Caution,
  /// A color panel toolbar icon.
  ColorPanel,
  /// A column view mode template image.
  ColumnView,
  /// A computer icon.
  Computer,
  /// An enter full-screen mode template image.
  EnterFullScreen,
  /// Permissions for all users.
  Everyone,
  /// An exit full-screen mode template image.
  ExitFullScreen,
  /// A cover flow view mode template image.
  FlowView,
  /// A folder image.
  Folder,
  /// A burnable folder icon.
  FolderBurnable,
  /// A smart folder icon.
  FolderSmart,
  /// A link template image.
  FollowLinkFreestanding,
  /// A font panel toolbar icon.
  FontPanel,
  /// A `go back` template image.
  GoLeft,
  /// A `go forward` template image.
  GoRight,
  /// Home image suitable for a template.
  Home,
  /// An iChat Theater template image.
  IChatTheater,
  /// An icon view mode template image.
  IconView,
  /// An information toolbar icon.
  Info,
  /// A template image used to denote invalid data.
  InvalidDataFreestanding,
  /// A generic left-facing triangle template image.
  LeftFacingTriangle,
  /// A list view mode template image.
  ListView,
  /// A locked padlock template image.
  LockLocked,
  /// An unlocked padlock template image.
  LockUnlocked,
  /// A horizontal dash, for use in menus.
  MenuMixedState,
  /// A check mark template image, for use in menus.
  MenuOnState,
  /// A MobileMe icon.
  MobileMe,
  /// A drag image for multiple items.
  MultipleDocuments,
  /// A network icon.
  Network,
  /// A path button template image.
  Path,
  /// General preferences toolbar icon for the preferences window.
  PreferencesGeneral,
  /// A Quick Look template image.
  QuickLook,
  /// A refresh template image.
  RefreshFreestanding,
  /// A refresh template image.
  Refresh,
  /// A remove item template image.
  Remove,
  /// A reveal contents template image.
  RevealFreestanding,
  /// A generic right-facing triangle template image.
  RightFacingTriangle,
  /// A share view template image.
  Share,
  /// A slideshow template image.
  Slideshow,
  /// A badge for a `smart` item.
  SmartBadge,
  /// Small green indicator, similar to iChat’s available image.
  StatusAvailable,
  /// Small clear indicator.
  StatusNone,
  /// Small yellow indicator, similar to iChat’s idle image.
  StatusPartiallyAvailable,
  /// Small red indicator, similar to iChat’s unavailable image.
  StatusUnavailable,
  /// A stop progress template image.
  StopProgressFreestanding,
  /// A stop progress button template image.
  StopProgress,

  // todo add TouchBar icons
  // https://developer.apple.com/documentation/appkit/nsimagenameactiontemplate
  /// An image of the empty trash can.
  TrashEmpty,
  /// An image of the full trash can.
  TrashFull,
  /// Permissions for a single user.
  User,
  /// User account toolbar icon for the preferences window.
  UserAccounts,
  /// Permissions for a group of users.
  UserGroup,
  /// Permissions for guests.
  UserGuest,
}

impl NativeImage {
  pub(crate) unsafe fn get_ns_image(self) -> id {
    match self {
      NativeImage::Add => appkit::NSImageNameAddTemplate,
      NativeImage::StatusAvailable => appkit::NSImageNameStatusAvailable,
      NativeImage::StatusUnavailable => appkit::NSImageNameStatusUnavailable,
      NativeImage::StatusPartiallyAvailable => appkit::NSImageNameStatusPartiallyAvailable,
      NativeImage::Advanced => appkit::NSImageNameAdvanced,
      NativeImage::Bluetooth => appkit::NSImageNameBluetoothTemplate,
      NativeImage::Bookmarks => appkit::NSImageNameBookmarksTemplate,
      NativeImage::Caution => appkit::NSImageNameCaution,
      NativeImage::ColorPanel => appkit::NSImageNameColorPanel,
      NativeImage::ColumnView => appkit::NSImageNameColumnViewTemplate,
      NativeImage::Computer => appkit::NSImageNameComputer,
      NativeImage::EnterFullScreen => appkit::NSImageNameEnterFullScreenTemplate,
      NativeImage::Everyone => appkit::NSImageNameEveryone,
      NativeImage::ExitFullScreen => appkit::NSImageNameExitFullScreenTemplate,
      NativeImage::FlowView => appkit::NSImageNameFlowViewTemplate,
      NativeImage::Folder => appkit::NSImageNameFolder,
      NativeImage::FolderBurnable => appkit::NSImageNameFolderBurnable,
      NativeImage::FolderSmart => appkit::NSImageNameFolderSmart,
      NativeImage::FollowLinkFreestanding => appkit::NSImageNameFollowLinkFreestandingTemplate,
      NativeImage::FontPanel => appkit::NSImageNameFontPanel,
      NativeImage::GoLeft => appkit::NSImageNameGoLeftTemplate,
      NativeImage::GoRight => appkit::NSImageNameGoRightTemplate,
      NativeImage::Home => appkit::NSImageNameHomeTemplate,
      NativeImage::IChatTheater => appkit::NSImageNameIChatTheaterTemplate,
      NativeImage::IconView => appkit::NSImageNameIconViewTemplate,
      NativeImage::Info => appkit::NSImageNameInfo,
      NativeImage::InvalidDataFreestanding => appkit::NSImageNameInvalidDataFreestandingTemplate,
      NativeImage::LeftFacingTriangle => appkit::NSImageNameLeftFacingTriangleTemplate,
      NativeImage::ListView => appkit::NSImageNameListViewTemplate,
      NativeImage::LockLocked => appkit::NSImageNameLockLockedTemplate,
      NativeImage::LockUnlocked => appkit::NSImageNameLockUnlockedTemplate,
      NativeImage::MenuMixedState => appkit::NSImageNameMenuMixedStateTemplate,
      NativeImage::MenuOnState => appkit::NSImageNameMenuOnStateTemplate,
      NativeImage::MobileMe => appkit::NSImageNameMobileMe,
      NativeImage::MultipleDocuments => appkit::NSImageNameMultipleDocuments,
      NativeImage::Network => appkit::NSImageNameNetwork,
      NativeImage::Path => appkit::NSImageNamePathTemplate,
      NativeImage::PreferencesGeneral => appkit::NSImageNamePreferencesGeneral,
      NativeImage::QuickLook => appkit::NSImageNameQuickLookTemplate,
      NativeImage::RefreshFreestanding => appkit::NSImageNameRefreshFreestandingTemplate,
      NativeImage::Refresh => appkit::NSImageNameRefreshTemplate,
      NativeImage::Remove => appkit::NSImageNameRemoveTemplate,
      NativeImage::RevealFreestanding => appkit::NSImageNameRevealFreestandingTemplate,
      NativeImage::RightFacingTriangle => appkit::NSImageNameRightFacingTriangleTemplate,
      NativeImage::Share => appkit::NSImageNameShareTemplate,
      NativeImage::Slideshow => appkit::NSImageNameSlideshowTemplate,
      NativeImage::SmartBadge => appkit::NSImageNameSmartBadgeTemplate,
      NativeImage::StatusNone => appkit::NSImageNameStatusNone,
      NativeImage::StopProgressFreestanding => appkit::NSImageNameStopProgressFreestandingTemplate,
      NativeImage::StopProgress => appkit::NSImageNameStopProgressTemplate,
      NativeImage::TrashEmpty => appkit::NSImageNameTrashEmpty,
      NativeImage::TrashFull => appkit::NSImageNameTrashFull,
      NativeImage::User => appkit::NSImageNameUser,
      NativeImage::UserAccounts => appkit::NSImageNameUserAccounts,
      NativeImage::UserGroup => appkit::NSImageNameUserGroup,
      NativeImage::UserGuest => appkit::NSImageNameUserGuest,
    }
  }
}

/// Additional methods on `WindowBuilder` that are specific to MacOS.
///
/// **Note:** Properties dealing with the titlebar will be overwritten by the `with_decorations` method
/// on the base `WindowBuilder`:
///
///  - `with_titlebar_transparent`
///  - `with_title_hidden`
///  - `with_titlebar_hidden`
///  - `with_titlebar_buttons_hidden`
///  - `with_fullsize_content_view`
pub trait WindowBuilderExtMacOS {
  /// Sets a parent to the window to be created.
  fn with_parent_window(self, parent: *mut c_void) -> WindowBuilder;
  /// Enables click-and-drag behavior for the entire window, not just the titlebar.
  fn with_movable_by_window_background(self, movable_by_window_background: bool) -> WindowBuilder;
  /// Makes the titlebar transparent and allows the content to appear behind it.
  fn with_titlebar_transparent(self, titlebar_transparent: bool) -> WindowBuilder;
  /// Hides the window title.
  fn with_title_hidden(self, title_hidden: bool) -> WindowBuilder;
  /// Hides the window titlebar.
  fn with_titlebar_hidden(self, titlebar_hidden: bool) -> WindowBuilder;
  /// Hides the window titlebar buttons.
  fn with_titlebar_buttons_hidden(self, titlebar_buttons_hidden: bool) -> WindowBuilder;
  /// Makes the window content appear behind the titlebar.
  fn with_fullsize_content_view(self, fullsize_content_view: bool) -> WindowBuilder;
  /// Build window with `resizeIncrements` property. Values must not be 0.
  fn with_resize_increments(self, increments: LogicalSize<f64>) -> WindowBuilder;
  fn with_disallow_hidpi(self, disallow_hidpi: bool) -> WindowBuilder;
  /// Sets whether or not the window has shadow.
  fn with_has_shadow(self, has_shadow: bool) -> WindowBuilder;
  /// Sets whether the system can automatically organize windows into tabs.
  fn with_automatic_window_tabbing(self, automatic_tabbing: bool) -> WindowBuilder;
  /// Defines the window [tabbing identifier].
  ///
  /// [tabbing identifier]: <https://developer.apple.com/documentation/appkit/nswindow/1644704-tabbingidentifier>
  fn with_tabbing_identifier(self, identifier: &str) -> WindowBuilder;
}

impl WindowBuilderExtMacOS for WindowBuilder {
  #[inline]
  fn with_parent_window(mut self, parent: *mut c_void) -> WindowBuilder {
    self.platform_specific.parent = Parent::ChildOf(parent);
    self
  }

  #[inline]
  fn with_movable_by_window_background(
    mut self,
    movable_by_window_background: bool,
  ) -> WindowBuilder {
    self.platform_specific.movable_by_window_background = movable_by_window_background;
    self
  }

  #[inline]
  fn with_titlebar_transparent(mut self, titlebar_transparent: bool) -> WindowBuilder {
    self.platform_specific.titlebar_transparent = titlebar_transparent;
    self
  }

  #[inline]
  fn with_titlebar_hidden(mut self, titlebar_hidden: bool) -> WindowBuilder {
    self.platform_specific.titlebar_hidden = titlebar_hidden;
    self
  }

  #[inline]
  fn with_titlebar_buttons_hidden(mut self, titlebar_buttons_hidden: bool) -> WindowBuilder {
    self.platform_specific.titlebar_buttons_hidden = titlebar_buttons_hidden;
    self
  }

  #[inline]
  fn with_title_hidden(mut self, title_hidden: bool) -> WindowBuilder {
    self.platform_specific.title_hidden = title_hidden;
    self
  }

  #[inline]
  fn with_fullsize_content_view(mut self, fullsize_content_view: bool) -> WindowBuilder {
    self.platform_specific.fullsize_content_view = fullsize_content_view;
    self
  }

  #[inline]
  fn with_resize_increments(mut self, increments: LogicalSize<f64>) -> WindowBuilder {
    self.platform_specific.resize_increments = Some(increments);
    self
  }

  #[inline]
  fn with_disallow_hidpi(mut self, disallow_hidpi: bool) -> WindowBuilder {
    self.platform_specific.disallow_hidpi = disallow_hidpi;
    self
  }

  #[inline]
  fn with_has_shadow(mut self, has_shadow: bool) -> WindowBuilder {
    self.platform_specific.has_shadow = has_shadow;
    self
  }

  #[inline]
  fn with_automatic_window_tabbing(mut self, automatic_tabbing: bool) -> WindowBuilder {
    self.platform_specific.automatic_tabbing = automatic_tabbing;
    self
  }

  #[inline]
  fn with_tabbing_identifier(mut self, tabbing_identifier: &str) -> WindowBuilder {
    self
      .platform_specific
      .tabbing_identifier
      .replace(tabbing_identifier.into());
    self
  }
}

pub trait EventLoopExtMacOS {
  /// Sets the activation policy for the application. It is set to
  /// `NSApplicationActivationPolicyRegular` by default.
  ///
  /// This function only takes effect if it's called before calling
  /// [`run`](crate::event_loop::EventLoop::run) or
  /// [`run_return`](crate::platform::run_return::EventLoopExtRunReturn::run_return).
  /// To set the activation policy after that, use
  /// [`EventLoopWindowTargetExtMacOS::set_activation_policy_at_runtime`](crate::platform::macos::EventLoopWindowTargetExtMacOS::set_activation_policy_at_runtime).
  fn set_activation_policy(&mut self, activation_policy: ActivationPolicy);

  /// Used to prevent a default menubar menu from getting created
  ///
  /// The default menu creation is enabled by default.
  ///
  /// This function only takes effect if it's called before calling
  /// [`run`](crate::event_loop::EventLoop::run) or
  /// [`run_return`](crate::platform::run_return::EventLoopExtRunReturn::run_return)
  fn enable_default_menu_creation(&mut self, enable: bool);

  /// Used to prevent the application from automatically activating when launched if
  /// another application is already active
  ///
  /// The default behavior is to ignore other applications and activate when launched.
  ///
  /// This function only takes effect if it's called before calling
  /// [`run`](crate::event_loop::EventLoop::run) or
  /// [`run_return`](crate::platform::run_return::EventLoopExtRunReturn::run_return)
  fn set_activate_ignoring_other_apps(&mut self, ignore: bool);
}
impl<T> EventLoopExtMacOS for EventLoop<T> {
  #[inline]
  fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
    unsafe {
      get_aux_state_mut(&**self.event_loop.delegate).activation_policy = activation_policy;
    }
  }

  #[inline]
  fn enable_default_menu_creation(&mut self, enable: bool) {
    unsafe {
      get_aux_state_mut(&**self.event_loop.delegate).create_default_menu = enable;
    }
  }

  #[inline]
  fn set_activate_ignoring_other_apps(&mut self, ignore: bool) {
    unsafe {
      get_aux_state_mut(&**self.event_loop.delegate).activate_ignoring_other_apps = ignore;
    }
  }
}

/// Additional methods on `MonitorHandle` that are specific to MacOS.
pub trait MonitorHandleExtMacOS {
  /// Returns the identifier of the monitor for Cocoa.
  fn native_id(&self) -> u32;
  /// Returns a pointer to the NSScreen representing this monitor.
  fn ns_screen(&self) -> Option<*mut c_void>;
}

impl MonitorHandleExtMacOS for MonitorHandle {
  #[inline]
  fn native_id(&self) -> u32 {
    self.inner.native_identifier()
  }

  fn ns_screen(&self) -> Option<*mut c_void> {
    self.inner.ns_screen().map(|s| s as *mut c_void)
  }
}

/// Additional methods on `EventLoopWindowTarget` that are specific to macOS.
pub trait EventLoopWindowTargetExtMacOS {
  /// Hide the entire application. In most applications this is typically triggered with Command-H.
  fn hide_application(&self);
  /// Show the entire application.
  fn show_application(&self);
  /// Hide the other applications. In most applications this is typically triggered with Command+Option-H.
  fn hide_other_applications(&self);
  /// Sets the activation policy for the application. It is set to
  /// `NSApplicationActivationPolicyRegular` by default.
  ///
  /// To set the activation policy before the app starts running, see
  /// [`EventLoopExtMacOS::set_activation_policy`](crate::platform::macos::EventLoopExtMacOS::set_activation_policy).
  fn set_activation_policy_at_runtime(&self, activation_policy: ActivationPolicy);
}

impl<T> EventLoopWindowTargetExtMacOS for EventLoopWindowTarget<T> {
  fn hide_application(&self) {
    let cls = objc::runtime::Class::get("NSApplication").unwrap();
    let app: cocoa::base::id = unsafe { msg_send![cls, sharedApplication] };
    unsafe { msg_send![app, hide: 0] }
  }

  fn show_application(&self) {
    let cls = objc::runtime::Class::get("NSApplication").unwrap();
    let app: cocoa::base::id = unsafe { msg_send![cls, sharedApplication] };
    unsafe { msg_send![app, unhide: 0] }
  }

  fn hide_other_applications(&self) {
    let cls = objc::runtime::Class::get("NSApplication").unwrap();
    let app: cocoa::base::id = unsafe { msg_send![cls, sharedApplication] };
    unsafe { msg_send![app, hideOtherApplications: 0] }
  }

  fn set_activation_policy_at_runtime(&self, activation_policy: ActivationPolicy) {
    let cls = objc::runtime::Class::get("NSApplication").unwrap();
    let app: cocoa::base::id = unsafe { msg_send![cls, sharedApplication] };
    let ns_activation_policy: NSApplicationActivationPolicy = activation_policy.into();
    unsafe { msg_send![app, setActivationPolicy: ns_activation_policy] }
  }
}

#[cfg(feature = "tray")]
pub trait SystemTrayBuilderExtMacOS {
  /// Sets the icon as a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc).
  ///
  /// Images you mark as template images should consist of only black and clear colors.
  /// You can use the alpha channel in the image to adjust the opacity of black content.
  ///
  fn with_icon_as_template(self, is_template: bool) -> Self;

  /// Enables or disables showing the tray menu on left click, default is true.
  fn with_menu_on_left_click(self, enable: bool) -> Self;

  /// Sets the tray icon title
  fn with_title(self, title: &str) -> Self;
}

#[cfg(feature = "tray")]
impl SystemTrayBuilderExtMacOS for SystemTrayBuilder {
  fn with_icon_as_template(mut self, is_template: bool) -> Self {
    self.platform_tray_builder.icon_is_template = is_template;
    self
  }

  fn with_menu_on_left_click(mut self, enable: bool) -> Self {
    self.platform_tray_builder.menu_on_left_click = enable;
    self
  }

  fn with_title(mut self, title: &str) -> Self {
    self.platform_tray_builder.title = Some(title.to_owned());
    self
  }
}

#[cfg(feature = "tray")]
pub trait SystemTrayExtMacOS {
  /// Sets the icon as a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc).
  ///
  /// You need to update this value before changing the icon.
  ///
  fn set_icon_as_template(&mut self, is_template: bool);

  /// Enables or disables showing the tray menu on left click, default is true.
  fn enable_menu_on_left_click(&mut self, enable: bool);

  /// Sets the tray icon title
  fn set_title(&mut self, title: &str);
}

#[cfg(feature = "tray")]
impl SystemTrayExtMacOS for SystemTray {
  fn set_icon_as_template(&mut self, is_template: bool) {
    self.0.set_icon_as_template(is_template);
  }

  fn enable_menu_on_left_click(&mut self, enable: bool) {
    self.0.set_show_menu_on_left_click(enable);
  }

  fn set_title(&mut self, title: &str) {
    self.0.set_title(title)
  }
}