From 08cf3ad55862fa144fd0d1840240a4a8c2013fea Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 27 Oct 2022 15:01:43 +0100 Subject: [PATCH] Merge branch 'disableSetProcessName-feature' into tmp-merge --- native/Avalonia.Native/src/OSX/app.mm | 11 +++-- native/Avalonia.Native/src/OSX/common.h | 2 +- native/Avalonia.Native/src/OSX/main.mm | 41 ++++++++++++++++--- src/Avalonia.Native/AvaloniaNativePlatform.cs | 2 + .../AvaloniaNativePlatformExtensions.cs | 8 ++++ src/Avalonia.Native/avn.idl | 2 + 6 files changed, 55 insertions(+), 11 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/app.mm b/native/Avalonia.Native/src/OSX/app.mm index a15d0c9601..9cc9fc9523 100644 --- a/native/Avalonia.Native/src/OSX/app.mm +++ b/native/Avalonia.Native/src/OSX/app.mm @@ -95,11 +95,14 @@ ComPtr _events; } @end -extern void InitializeAvnApp(IAvnApplicationEvents* events) +extern void InitializeAvnApp(IAvnApplicationEvents* events, bool disableAppDelegate) { - NSApplication* app = [AvnApplication sharedApplication]; - id delegate = [[AvnAppDelegate alloc] initWithEvents:events]; - [app setDelegate:delegate]; + if(!disableAppDelegate) + { + NSApplication* app = [AvnApplication sharedApplication]; + id delegate = [[AvnAppDelegate alloc] initWithEvents:events]; + [app setDelegate:delegate]; + } } HRESULT AvnApplicationCommands::HideApp() diff --git a/native/Avalonia.Native/src/OSX/common.h b/native/Avalonia.Native/src/OSX/common.h index af3ffff63d..bac68f1a4b 100644 --- a/native/Avalonia.Native/src/OSX/common.h +++ b/native/Avalonia.Native/src/OSX/common.h @@ -32,7 +32,7 @@ extern void SetServicesMenu (IAvnMenu* menu); extern IAvnMenu* GetAppMenu (); extern NSMenuItem* GetAppMenuItem (); -extern void InitializeAvnApp(IAvnApplicationEvents* events); +extern void InitializeAvnApp(IAvnApplicationEvents* events, bool disableAppDelegate); extern NSApplicationActivationPolicy AvnDesiredActivationPolicy; extern NSPoint ToNSPoint (AvnPoint p); extern AvnPoint ToAvnPoint (NSPoint p); diff --git a/native/Avalonia.Native/src/OSX/main.mm b/native/Avalonia.Native/src/OSX/main.mm index 43e4d46c32..e14a119a28 100644 --- a/native/Avalonia.Native/src/OSX/main.mm +++ b/native/Avalonia.Native/src/OSX/main.mm @@ -3,6 +3,8 @@ #include "common.h" static NSString* s_appTitle = @"Avalonia"; +static int disableSetProcessName = 0; +static bool disableAppDelegate = false; // Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be @@ -111,11 +113,17 @@ public: @autoreleasepool { auto appTitle = [NSString stringWithUTF8String: utf8String]; - - [[NSProcessInfo processInfo] setProcessName:appTitle]; - - - SetProcessName(appTitle); + if (disableSetProcessName == 0) + { + [[NSProcessInfo processInfo] setProcessName:appTitle]; + + SetProcessName(appTitle); + } + if (disableSetProcessName == 1) + { + auto rootMenu = [NSApp mainMenu]; + [rootMenu setTitle:appTitle]; + } return S_OK; } @@ -133,6 +141,27 @@ public: } } + virtual HRESULT SetDisableSetProcessName(int disable) override + { + START_COM_CALL; + + @autoreleasepool + { + disableSetProcessName = disable; + return S_OK; + } + } + + virtual HRESULT SetDisableAppDelegate(int disable) override + { + START_COM_CALL; + + @autoreleasepool { + disableAppDelegate = disable; + return S_OK; + } + } + }; /// See "Using POSIX Threads in a Cocoa Application" section here: @@ -175,7 +204,7 @@ public: @autoreleasepool{ [[ThreadingInitializer new] do]; } - InitializeAvnApp(events); + InitializeAvnApp(events, disableAppDelegate); return S_OK; }; diff --git a/src/Avalonia.Native/AvaloniaNativePlatform.cs b/src/Avalonia.Native/AvaloniaNativePlatform.cs index d64fde8e17..4972b23103 100644 --- a/src/Avalonia.Native/AvaloniaNativePlatform.cs +++ b/src/Avalonia.Native/AvaloniaNativePlatform.cs @@ -98,6 +98,8 @@ namespace Avalonia.Native var macOpts = AvaloniaLocator.Current.GetService() ?? new MacOSPlatformOptions(); _factory.MacOptions.SetShowInDock(macOpts.ShowInDock ? 1 : 0); + _factory.MacOptions.SetDisableSetProcessName(macOpts.DisableSetProcessName ? 1 : 0); + _factory.MacOptions.SetDisableAppDelegate(macOpts.DisableAvaloniaAppDelegate ? 1 : 0); } AvaloniaLocator.CurrentMutable diff --git a/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs b/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs index 10619d675b..84c180c1d7 100644 --- a/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs +++ b/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs @@ -78,5 +78,13 @@ namespace Avalonia /// Gets or sets a value indicating whether the native macOS menu bar will be enabled for the application. /// public bool DisableNativeMenus { get; set; } + + public bool DisableSetProcessName { get; set; } + + /// + /// Gets or sets a value indicating whether Avalonia can install its own AppDelegate. + /// Disabling this can be useful in some scenarios like when running as a plugin inside an existing macOS application. + /// + public bool DisableAvaloniaAppDelegate { get; set; } } } diff --git a/src/Avalonia.Native/avn.idl b/src/Avalonia.Native/avn.idl index e02b244db6..e4f73413e6 100644 --- a/src/Avalonia.Native/avn.idl +++ b/src/Avalonia.Native/avn.idl @@ -542,6 +542,8 @@ interface IAvnMacOptions : IUnknown { HRESULT SetShowInDock(int show); HRESULT SetApplicationTitle(char* utf8string); + HRESULT SetDisableSetProcessName(int disable); + HRESULT SetDisableAppDelegate(int disable); } [uuid(04c1b049-1f43-418a-9159-cae627ec1367)]