From 54a3388c99b1349abfe5e8bfc8ce8c8bcea95436 Mon Sep 17 00:00:00 2001 From: JieXu Date: Sun, 31 May 2026 17:57:59 +0800 Subject: [PATCH] Update App.axaml.cs (#9421) * Update App.axaml.cs * Update App.axaml.cs * Create MacOSAppHelper.cs * Update App.axaml.cs Removed unused macOS activation policy methods and constants. * Update and rename MacOSAppHelper.cs to MacAppUtils.cs * Update App.axaml.cs --- v2rayN/v2rayN.Desktop/App.axaml.cs | 24 ++++++++++++++++++ v2rayN/v2rayN.Desktop/Common/MacAppUtils.cs | 27 +++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 v2rayN/v2rayN.Desktop/Common/MacAppUtils.cs diff --git a/v2rayN/v2rayN.Desktop/App.axaml.cs b/v2rayN/v2rayN.Desktop/App.axaml.cs index 35371f0f..c1c448cb 100644 --- a/v2rayN/v2rayN.Desktop/App.axaml.cs +++ b/v2rayN/v2rayN.Desktop/App.axaml.cs @@ -1,3 +1,4 @@ +using v2rayN.Desktop.Common; using v2rayN.Desktop.Views; namespace v2rayN.Desktop; @@ -24,11 +25,34 @@ public partial class App : Application desktop.Exit += OnExit; desktop.MainWindow = new MainWindow(); + + if (OperatingSystem.IsMacOS()) + { + Current?.TryGetFeature()?.Activated += OnMacOSActivated; + } } base.OnFrameworkInitializationCompleted(); } + private void OnMacOSActivated(object? sender, ActivatedEventArgs args) + { + if (args.Kind != ActivationKind.Reopen) + { + return; + } + + Dispatcher.UIThread.Post(() => + { + ((ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow as MainWindow)?.ShowHideWindow(true); + + if (!AppManager.Instance.Config.UiItem.MacOSShowInDock) + { + MacAppUtils.SetActivationPolicyAccessory(); + } + }); + } + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { if (e.ExceptionObject != null) diff --git a/v2rayN/v2rayN.Desktop/Common/MacAppUtils.cs b/v2rayN/v2rayN.Desktop/Common/MacAppUtils.cs new file mode 100644 index 00000000..b38f7507 --- /dev/null +++ b/v2rayN/v2rayN.Desktop/Common/MacAppUtils.cs @@ -0,0 +1,27 @@ +using System.Runtime.InteropServices; + +namespace v2rayN.Desktop.Common; + +internal static class MacAppUtils +{ + private const string LibObjC = "/usr/lib/libobjc.dylib"; + private const nint ActivationPolicyAccessory = 1; + + public static void SetActivationPolicyAccessory() + => objc_msgSend( + objc_msgSend(objc_getClass("NSApplication"), sel_registerName("sharedApplication")), + sel_registerName("setActivationPolicy:"), + ActivationPolicyAccessory); + + [DllImport(LibObjC)] + private static extern nint objc_getClass(string name); + + [DllImport(LibObjC)] + private static extern nint sel_registerName(string name); + + [DllImport(LibObjC, EntryPoint = "objc_msgSend")] + private static extern nint objc_msgSend(nint receiver, nint selector); + + [DllImport(LibObjC, EntryPoint = "objc_msgSend")] + private static extern void objc_msgSend(nint receiver, nint selector, nint argument); +}