mirror of
https://github.com/2dust/v2rayN.git
synced 2026-11-04 19:45:00 +03:00
Update AppBuilderExtension.cs (#9514)
* Update AppBuilderExtension.cs * Update MainWindow.axaml.cs * Update MacAppUtils.cs * Update App.axaml.cs * Add regions for MacOS activation and app events --------- Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
This commit is contained in:
@@ -23,8 +23,8 @@ public partial class App : Application
|
|||||||
DataContext = StatusBarViewModel.Instance;
|
DataContext = StatusBarViewModel.Instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
desktop.Exit += OnExit;
|
var mainWindow = new MainWindow();
|
||||||
desktop.MainWindow = new MainWindow();
|
desktop.MainWindow = mainWindow;
|
||||||
|
|
||||||
if (OperatingSystem.IsMacOS())
|
if (OperatingSystem.IsMacOS())
|
||||||
{
|
{
|
||||||
@@ -35,6 +35,8 @@ public partial class App : Application
|
|||||||
base.OnFrameworkInitializationCompleted();
|
base.OnFrameworkInitializationCompleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region MacOS Activation
|
||||||
|
|
||||||
private void OnMacOSActivated(object? sender, ActivatedEventArgs args)
|
private void OnMacOSActivated(object? sender, ActivatedEventArgs args)
|
||||||
{
|
{
|
||||||
if (args.Kind != ActivationKind.Reopen)
|
if (args.Kind != ActivationKind.Reopen)
|
||||||
@@ -42,17 +44,69 @@ public partial class App : Application
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow is not MainWindow mainWindow)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var isMiniaturized = MacAppUtils.IsWindowMiniaturized(mainWindow);
|
||||||
|
|
||||||
Dispatcher.UIThread.Post(() =>
|
Dispatcher.UIThread.Post(() =>
|
||||||
{
|
{
|
||||||
|
if (isMiniaturized)
|
||||||
|
{
|
||||||
|
RestoreMacOSAccessoryPolicyAfterMiniaturize(mainWindow);
|
||||||
|
mainWindow.ShowHideWindow(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!AppManager.Instance.Config.UiItem.MacOSShowInDock)
|
if (!AppManager.Instance.Config.UiItem.MacOSShowInDock)
|
||||||
{
|
{
|
||||||
MacAppUtils.SetActivationPolicyAccessory();
|
MacAppUtils.SetActivationPolicyAccessory();
|
||||||
}
|
}
|
||||||
|
|
||||||
((ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow as MainWindow)?.ShowHideWindow(true);
|
mainWindow.ShowHideWindow(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void RestoreMacOSAccessoryPolicyAfterMiniaturize(MainWindow mainWindow)
|
||||||
|
{
|
||||||
|
if (AppManager.Instance.Config.UiItem.MacOSShowInDock)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mainWindow
|
||||||
|
.GetObservable(Window.WindowStateProperty)
|
||||||
|
.Skip(1)
|
||||||
|
.Where(state => state != WindowState.Minimized)
|
||||||
|
.Take(1)
|
||||||
|
.ObserveOn(RxSchedulers.MainThreadScheduler)
|
||||||
|
.Subscribe(_ => QueueMacOSAccessoryPolicyRestore(mainWindow));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void QueueMacOSAccessoryPolicyRestore(MainWindow mainWindow)
|
||||||
|
{
|
||||||
|
// AppKit may keep isMiniaturized set until the Dock restore animation finishes.
|
||||||
|
DispatcherTimer.RunOnce(() => RestoreMacOSAccessoryPolicy(mainWindow), TimeSpan.FromMilliseconds(300));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void RestoreMacOSAccessoryPolicy(MainWindow mainWindow)
|
||||||
|
{
|
||||||
|
if (AppManager.Instance.Config.UiItem.MacOSShowInDock || MacAppUtils.IsWindowMiniaturized(mainWindow))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MacAppUtils.SetActivationPolicyAccessory();
|
||||||
|
mainWindow.Activate();
|
||||||
|
mainWindow.Focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion MacOS Activation
|
||||||
|
|
||||||
|
#region App Event
|
||||||
|
|
||||||
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
||||||
{
|
{
|
||||||
if (e.ExceptionObject != null)
|
if (e.ExceptionObject != null)
|
||||||
@@ -66,10 +120,6 @@ public partial class App : Application
|
|||||||
Logging.SaveLog("TaskScheduler_UnobservedTaskException", e.Exception);
|
Logging.SaveLog("TaskScheduler_UnobservedTaskException", e.Exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnExit(object? sender, ControlledApplicationLifetimeExitEventArgs e)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private async void MenuAddServerViaClipboardClick(object? sender, EventArgs e)
|
private async void MenuAddServerViaClipboardClick(object? sender, EventArgs e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -91,4 +141,6 @@ public partial class App : Application
|
|||||||
await AppManager.Instance.AppExitAsync(false);
|
await AppManager.Instance.AppExitAsync(false);
|
||||||
AppManager.Instance.Shutdown(true);
|
AppManager.Instance.Shutdown(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion App Event
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,18 +18,18 @@ public static class AppBuilderExtension
|
|||||||
|
|
||||||
if (OperatingSystem.IsWindows())
|
if (OperatingSystem.IsWindows())
|
||||||
{
|
{
|
||||||
AddFontFallback(fallbacks, "Segoe UI Symbol");
|
|
||||||
AddFontFallback(fallbacks, "Segoe UI Emoji");
|
AddFontFallback(fallbacks, "Segoe UI Emoji");
|
||||||
|
AddFontFallback(fallbacks, "Segoe UI Symbol");
|
||||||
}
|
}
|
||||||
else if (OperatingSystem.IsMacOS())
|
else if (OperatingSystem.IsMacOS())
|
||||||
{
|
{
|
||||||
AddFontFallback(fallbacks, "Apple Symbols");
|
|
||||||
AddFontFallback(fallbacks, "Apple Color Emoji");
|
AddFontFallback(fallbacks, "Apple Color Emoji");
|
||||||
|
AddFontFallback(fallbacks, "Apple Symbols");
|
||||||
}
|
}
|
||||||
else if (OperatingSystem.IsLinux())
|
else if (OperatingSystem.IsLinux())
|
||||||
{
|
{
|
||||||
AddFontFallback(fallbacks, "Noto Sans Symbols");
|
|
||||||
AddFontFallback(fallbacks, "Noto Color Emoji");
|
AddFontFallback(fallbacks, "Noto Color Emoji");
|
||||||
|
AddFontFallback(fallbacks, "Noto Sans Symbols");
|
||||||
}
|
}
|
||||||
|
|
||||||
return appBuilder.With(new FontManagerOptions
|
return appBuilder.With(new FontManagerOptions
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ internal static class MacAppUtils
|
|||||||
sel_registerName("setActivationPolicy:"),
|
sel_registerName("setActivationPolicy:"),
|
||||||
ActivationPolicyAccessory);
|
ActivationPolicyAccessory);
|
||||||
|
|
||||||
|
public static bool IsWindowMiniaturized(Window window)
|
||||||
|
=> window.TryGetPlatformHandle() is IMacOSTopLevelPlatformHandle { NSWindow: not 0 } handle
|
||||||
|
&& objc_msgSend_bool(handle.NSWindow, sel_registerName("isMiniaturized"));
|
||||||
|
|
||||||
[DllImport(LibObjC)]
|
[DllImport(LibObjC)]
|
||||||
private static extern nint objc_getClass(string name);
|
private static extern nint objc_getClass(string name);
|
||||||
|
|
||||||
@@ -22,6 +26,10 @@ internal static class MacAppUtils
|
|||||||
[DllImport(LibObjC, EntryPoint = "objc_msgSend")]
|
[DllImport(LibObjC, EntryPoint = "objc_msgSend")]
|
||||||
private static extern nint objc_msgSend(nint receiver, nint selector);
|
private static extern nint objc_msgSend(nint receiver, nint selector);
|
||||||
|
|
||||||
|
[DllImport(LibObjC, EntryPoint = "objc_msgSend")]
|
||||||
|
[return: MarshalAs(UnmanagedType.I1)]
|
||||||
|
private static extern bool objc_msgSend_bool(nint receiver, nint selector);
|
||||||
|
|
||||||
[DllImport(LibObjC, EntryPoint = "objc_msgSend")]
|
[DllImport(LibObjC, EntryPoint = "objc_msgSend")]
|
||||||
private static extern void objc_msgSend(nint receiver, nint selector, nint argument);
|
private static extern void objc_msgSend(nint receiver, nint selector, nint argument);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -413,7 +413,7 @@ public partial class MainWindow : WindowBase<MainWindowViewModel>
|
|||||||
public void ShowHideWindow(bool? blShow)
|
public void ShowHideWindow(bool? blShow)
|
||||||
{
|
{
|
||||||
var bl = blShow ??
|
var bl = blShow ??
|
||||||
(Utils.IsLinux()
|
(Utils.IsLinux() || Utils.IsMacOS()
|
||||||
? (!AppManager.Instance.ShowInTaskbar ^ (WindowState == WindowState.Minimized))
|
? (!AppManager.Instance.ShowInTaskbar ^ (WindowState == WindowState.Minimized))
|
||||||
: !AppManager.Instance.ShowInTaskbar);
|
: !AppManager.Instance.ShowInTaskbar);
|
||||||
if (bl)
|
if (bl)
|
||||||
|
|||||||
Reference in New Issue
Block a user