Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
fix(macos): preserve native window class to prevent quit crash
wavever051b7366c4ca
diff --git a/src/Filee.Platform.MacOS/NativeMethods.cs b/src/Filee.Platform.MacOS/NativeMethods.csindex b343bab7..5c284f62 100644--- a/src/Filee.Platform.MacOS/NativeMethods.cs+++ b/src/Filee.Platform.MacOS/NativeMethods.cs@@ -131,17 +131,9 @@ internal static class NativeMethodsbehavior &= ~((1 << 16) | (1 << 17) | (1 << 18));// Finder can occupy a full-screen Space that would otherwise hide another app's toolbar.SendInteger(window, sel_registerName("setCollectionBehavior:"), behavior);- // While Filee shows in the Dock, AppKit keeps its windows off another app's full-screen Space and lets only a- // non-activating panel join it (#251). AvnPanel is Avalonia's NSPanel build of AvnWindow, same ivars and methods- // but canBecomeMainWindow, so the donut becomes one in place.- var panel = objc_getClass("AvnPanel");- if (panel != 0 && object_getClass(window) == objc_getClass("AvnWindow"))- {- object_setClass(window, panel);- const nint nonactivatingPanel = 1 << 7;- SendInteger(window, sel_registerName("setStyleMask:"), Send(window, sel_registerName("styleMask")) | nonactivatingPanel);- SendInteger(window, sel_registerName("setHidesOnDeactivate:"), 0);- }+ // Keep the native class selected when Avalonia created the window. Changing an initialized AvnWindow into+ // AvnPanel skips NSPanel initialization and breaks AppKit's KVO cleanup when the window is released (#805).+ // A non-activating panel for another app's full-screen Space (#251) must be created as a panel from the start.SendInteger(window, sel_registerName("setLevel:"), 3);SendVoid(window, sel_registerName("orderFrontRegardless"));}@@ -426,12 +418,6 @@ internal static class NativeMethods[DllImport(ObjC)]private static extern nint objc_getClass([MarshalAs(UnmanagedType.LPUTF8Str)] string name);- [DllImport(ObjC)]- private static extern nint object_getClass(nint instance);-- [DllImport(ObjC)]- private static extern nint object_setClass(nint instance, nint cls);-[DllImport(ObjC)]private static extern nint sel_registerName([MarshalAs(UnmanagedType.LPUTF8Str)] string name);diff --git a/tests/Filee.Platform.Tests/MacOSWindowTests.cs b/tests/Filee.Platform.Tests/MacOSWindowTests.csnew file mode 100644index 00000000..956fb224--- /dev/null+++ b/tests/Filee.Platform.Tests/MacOSWindowTests.cs@@ -0,0 +1,130 @@+// Raising an existing window must preserve its native class and avoid panel-only configuration (#805).+// Objective-C test doubles exercise the real interop calls without creating AppKit UI on xUnit's worker thread.++using System.Runtime.InteropServices;+using Filee.Platform.MacOS;++namespace Filee.Platform.Tests;++public sealed class MacOSWindowTests+{+ [Fact]+ public void Raising_a_window_repeatedly_preserves_its_native_class_and_space_behavior()+ {+ if (!OperatingSystem.IsMacOS())+ {+ Assert.Skip("Requires the macOS Objective-C runtime.");+ return;+ }++ _ = NativeLibrary.Load("/System/Library/Frameworks/Foundation.framework/Foundation");+ // Production checks these exact Avalonia class names. This test runs in the platform-only test process,+ // which does not load Avalonia.Native; never replace a real class if another host has loaded it.+ if (GetClass("AvnWindow") != 0 || GetClass("AvnPanel") != 0)+ {+ Assert.Skip("Requires a process without Avalonia.Native window classes.");+ return;+ }++ nint behavior = (1 << 1) | (1 << 6) | (1 << 7) | (1 << 9) | (1 << 16) | (1 << 17) | (1 << 18);+ nint level = 0;+ var orders = 0;+ var panelConfigurationCalls = 0;+ GetInteger getBehavior = (_, _) => behavior;+ GetInteger getStyle = (_, _) => 0;+ SetInteger setBehavior = (_, _, value) => behavior = value;+ SetInteger setLevel = (_, _, value) => level = value;+ SetInteger configurePanel = (_, _, _) => panelConfigurationCalls++;+ SendAction orderFront = (_, _) => orders++;+ Delegate[] callbacks = [getBehavior, getStyle, setBehavior, setLevel, configurePanel, orderFront];++ var baseClass = AllocateClass(GetClass("NSObject"), "FileeRaiseTopmostTestWindow", 0);+ Assert.NotEqual(0, baseClass);+ nint windowClass = 0, panelClass = 0, window = 0;+ try+ {+ AddMethod(baseClass, "collectionBehavior", getBehavior, "q@:");+ AddMethod(baseClass, "styleMask", getStyle, "q@:");+ AddMethod(baseClass, "setCollectionBehavior:", setBehavior, "v@:q");+ AddMethod(baseClass, "setLevel:", setLevel, "v@:q");+ AddMethod(baseClass, "setStyleMask:", configurePanel, "v@:q");+ AddMethod(baseClass, "setHidesOnDeactivate:", configurePanel, "v@:q");+ AddMethod(baseClass, "orderFrontRegardless", orderFront, "v@:");+ RegisterClass(baseClass);+ windowClass = RegisterSubclass(baseClass, "AvnWindow");+ panelClass = RegisterSubclass(baseClass, "AvnPanel");+ window = Send(Send(windowClass, Selector("alloc")), Selector("init"));+ Assert.NotEqual(0, window);++ var platform = new MacOSPlatformServices();+ for (var i = 1; i <= 3; i++)+ {+ platform.RaiseTopmost(window);+ Assert.Equal(windowClass, ObjectClass(window));+ Assert.Equal((nint)(1 | (1 << 6) | (1 << 8)), behavior);+ Assert.Equal((nint)3, level);+ Assert.Equal(i, orders);+ Assert.Equal(0, panelConfigurationCalls);+ }+ }+ finally+ {+ if (window != 0)+ Send(window, Selector("release"));+ if (panelClass != 0)+ DisposeClass(panelClass);+ if (windowClass != 0)+ DisposeClass(windowClass);+ DisposeClass(baseClass);+ GC.KeepAlive(callbacks);+ }+ }++ private static nint RegisterSubclass(nint superclass, string name)+ {+ var result = AllocateClass(superclass, name, 0);+ Assert.NotEqual(0, result);+ RegisterClass(result);+ return result;+ }++ private static void AddMethod(nint cls, string name, Delegate callback, string signature) =>+ Assert.True(AddNativeMethod(cls, Selector(name), Marshal.GetFunctionPointerForDelegate(callback), signature));++ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]+ private delegate nint GetInteger(nint receiver, nint selector);++ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]+ private delegate void SetInteger(nint receiver, nint selector, nint value);++ [UnmanagedFunctionPointer(CallingConvention.Cdecl)]+ private delegate void SendAction(nint receiver, nint selector);++ private const string ObjC = "/usr/lib/libobjc.A.dylib";++ [DllImport(ObjC, EntryPoint = "objc_getClass")]+ private static extern nint GetClass([MarshalAs(UnmanagedType.LPUTF8Str)] string name);++ [DllImport(ObjC, EntryPoint = "objc_allocateClassPair")]+ private static extern nint AllocateClass(nint superclass, [MarshalAs(UnmanagedType.LPUTF8Str)] string name, nuint extraBytes);++ [DllImport(ObjC, EntryPoint = "objc_registerClassPair")]+ private static extern void RegisterClass(nint cls);++ [DllImport(ObjC, EntryPoint = "objc_disposeClassPair")]+ private static extern void DisposeClass(nint cls);++ [DllImport(ObjC, EntryPoint = "object_getClass")]+ private static extern nint ObjectClass(nint instance);++ [DllImport(ObjC, EntryPoint = "class_addMethod")]+ [return: MarshalAs(UnmanagedType.I1)]+ private static extern bool AddNativeMethod(nint cls, nint selector, nint implementation,+ [MarshalAs(UnmanagedType.LPUTF8Str)] string signature);++ [DllImport(ObjC, EntryPoint = "sel_registerName")]+ private static extern nint Selector([MarshalAs(UnmanagedType.LPUTF8Str)] string name);++ [DllImport(ObjC, EntryPoint = "objc_msgSend")]+ private static extern nint Send(nint receiver, nint selector);+}