From 99c14c8f5dea9c4429d74fb4f6b5290c0f43fabc Mon Sep 17 00:00:00 2001 From: Exidex <16986685+Exidex@users.noreply.github.com> Date: Sat, 3 Aug 2024 18:23:56 +0200 Subject: [PATCH] Expose macOS ActivationPolicy and activate_ignoring_other_apps --- core/src/window/settings.rs | 4 ++++ core/src/window/settings/macos.rs | 33 ++++++++++++++++++++++++++++++- winit/src/application.rs | 25 ++++++++++++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/core/src/window/settings.rs b/core/src/window/settings.rs index fbbf86abd8..d55ced642a 100644 --- a/core/src/window/settings.rs +++ b/core/src/window/settings.rs @@ -27,7 +27,11 @@ mod platform; use crate::window::{Icon, Level, Position}; use crate::Size; +#[cfg(target_os = "macos")] +pub use platform::ActivationPolicy; + pub use platform::PlatformSpecific; + /// The window settings of an application. #[derive(Debug, Clone)] pub struct Settings { diff --git a/core/src/window/settings/macos.rs b/core/src/window/settings/macos.rs index f86e63ad05..a1b6026116 100644 --- a/core/src/window/settings/macos.rs +++ b/core/src/window/settings/macos.rs @@ -1,7 +1,7 @@ //! Platform specific settings for macOS. /// The platform specific window settings of an application. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct PlatformSpecific { /// Hides the window title. pub title_hidden: bool, @@ -9,4 +9,35 @@ pub struct PlatformSpecific { pub titlebar_transparent: bool, /// Makes the window content appear behind the titlebar. pub fullsize_content_view: bool, + /// Activation policy for the application. + pub activation_policy: ActivationPolicy, + /// 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. + pub activate_ignoring_other_apps: bool, +} + +impl Default for PlatformSpecific { + fn default() -> Self { + Self { + title_hidden: false, + titlebar_transparent: false, + fullsize_content_view: false, + activation_policy: Default::default(), // Regular + activate_ignoring_other_apps: true, + } + } +} + +/// Corresponds to `NSApplicationActivationPolicy`. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] +pub enum ActivationPolicy { + /// Corresponds to `NSApplicationActivationPolicyRegular`. + #[default] + Regular, + /// Corresponds to `NSApplicationActivationPolicyAccessory`. + Accessory, + /// Corresponds to `NSApplicationActivationPolicyProhibited`. + Prohibited, } diff --git a/winit/src/application.rs b/winit/src/application.rs index 7fe880ec75..3554ccd16c 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -25,6 +25,7 @@ use futures::channel::mpsc; use std::mem::ManuallyDrop; use std::sync::Arc; +use winit::event_loop::EventLoop; use iced_runtime::command::Action; use winit::monitor::MonitorHandle; @@ -116,9 +117,31 @@ where let mut debug = Debug::new(); debug.startup_started(); - let event_loop = EventLoopBuilder::with_user_event() + let mut event_loop_builder = EventLoopBuilder::with_user_event(); + + #[cfg(target_os = "macos")] + let event_loop_builder = { + use winit::platform::macos::{ActivationPolicy, EventLoopBuilderExtMacOS}; + + let macos_settings = settings.window.platform_specific; + + let activation_policy = match macos_settings.activation_policy { + window::settings::ActivationPolicy::Regular => ActivationPolicy::Regular, + window::settings::ActivationPolicy::Accessory => ActivationPolicy::Accessory, + window::settings::ActivationPolicy::Prohibited => ActivationPolicy::Prohibited + }; + + let activate_ignoring_other_apps = macos_settings.activate_ignoring_other_apps; + + event_loop_builder + .with_activation_policy(activation_policy) + .with_activate_ignoring_other_apps(activate_ignoring_other_apps) + }; + + let event_loop = event_loop_builder .build() .expect("Create event loop"); + let proxy = event_loop.create_proxy(); let runtime = {