* Use MicroCom smart pointer wrappers in Avalonia.Native
Switch the native backend to the CppSmartPointerWrappers mode of
MicroCom 0.11.7. Implementations override ComPtr-shaped methods, and the
generated thunks open an autorelease pool and keep `this` and all
interface arguments alive for every COM call, replacing START_COM_CALL
and START_COM_ARP_CALL.
- ComPtr has no raw pointer conversions or constructors; comnew and
comself(this) are the ways in, getRetainedReference the way out
- ComPtr::dynamicCast goes through a private IID, so it is safe on
objects implemented in C#
- implementation classes derive only from MicroComImpl<I>
- factories, string helpers and Obj-C methods take and return ComPtr
Fixes found along the way: wrong IID for PlatformBehaviorInhibition,
missing AddRef in GetInputMethod, leaks in cursors, the input method and
automation peers, manual delete of a shared automation node,
over-release of C#-owned IAvnString arguments, null dereferences on
failed menu casts, and missing teardown of the display reconfiguration
callback and the render timer display link.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Use atomic reference counting in Avalonia.Native ComObject
C# releases native objects from other threads, for example the GC
finalizer and the render thread. The reference counter is now atomic.
ComObjectWeakPtr::tryGet() is safe against a concurrent last Release:
the weak token holds a mutex and a target pointer. The object clears
the target under the mutex before it is deleted. tryGet() increments
the counter only from a non-zero value, so it never resurrects an
object in destruction. The token is created with std::call_once.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Keep the macOS app delegate alive and do not release .NET callbacks at exit
NSApplication.delegate is weak. The delegate stayed alive only through an
autoreleased reference that leaked into a pool that never drained. The
generated COM call thunks drain an autorelease pool, so the delegate was
deallocated right after setDelegate:. App delegate callbacks did not run,
and ReleaseAvnAppEvents() did not release the C# events object.
The global ComPtr then released it from exit(), after .NET shut down.
With a managed debugger attached, the app hung on close.
Keep a strong static reference to the delegate. Hold the process-wide C#
callbacks in ComStaticPtr, so that exit() never calls into .NET.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
* MacOS: Make sure macOS child window layer is not lower than parent window.
* Add WindowOrder_Owned_Window_Stays_InFront_Of_Topmost_Owner integration test
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* Thread.Sleep should be done before the assert in WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
* osx: anchor new windows to the primary screen origin
WindowBaseImpl::CreateNSWindow anchors the new NSWindow's content rect to the
primary screen's frame.origin. The primary screen is at Cocoa (0,0) on a
healthy Mac, but can be offset (often negative) while the display server is
mid-reconfiguration -- e.g. CGMainDisplayID() briefly returns 0 after
wake-from-sleep -- or on multi-monitor layouts where the primary is not at the
origin. Anchoring keeps the window on a real screen so it is positioned
on-screen and its flipped Position lands inside a Screen.Bounds.
See #18895.
Co-authored-by: Cursor <cursoragent@cursor.com>
* osx: keep window construction resilient when no screen contains it
When locating the screen that sizes a new window, fall back to a default size
if AppKit reports no screens -- or none whose bounds contain the last-known
position -- during a display reconfiguration. The window is repositioned on the
next ScreenChanged event once the display server settles.
See #18895.
Co-authored-by: Cursor <cursoragent@cursor.com>
* Less verbose comments
---------
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
BeginMoveDrag on macOS drags from the Avalonia view's last recorded
mouse-down. A press that begins inside an embedded native view (for
example a webview hosted through NativeControlHost) is consumed by that
view and never delivered to the Avalonia view, so lastMouseDownEvent is
nil and BeginMoveDrag silently does nothing - applications embedding
native views cannot implement custom title-bar dragging through the
managed API and must reproduce performWindowDragWithEvent: through
platform interop.
When no mouse-down was recorded, fall back to the event the application
is currently tracking, guarded to left-button press or drag events that
belong to this window. Presses recorded by the Avalonia view behave
exactly as before.
* Bump MicroCOM to 0.11.4, add workaround for WinRT bug
* menu.h - use ComPtr for COM stored references
* Replaced raw COM pointers with smart ones
* fixes
* Use weak com ref from AvnMenuItem
* Better smart pointer handling
* Avalonia native readme update
* review
* missing dif
* Just call the ctor like a normal person
* Move Drag and Drop logic to TopLevelImpl
* Update src/Avalonia.Native/TopLevelImpl.cs
Co-authored-by: Max Katz <maxkatz6@outlook.com>
---------
Co-authored-by: Max Katz <maxkatz6@outlook.com>
Co-authored-by: Benedikt Stebner <Gillibald@users.noreply.github.com>
The modality of a window is decided at the time it is shown, and indicated by the `isDialog` parameter to `WindowImpl::Show`.
The code was also rather confusing in that `_isModal` was defined in `WindowBaseImpl` but `WindowBaseImpl.IsModal` always returned false. `WindowImpl.IsModal` then overrode `IsModal` to return WindowBaseImpl._isModal. That's messed up. Just define `_isModal` in `WindowImpl` not `WindowBaseImpl`.
The `AvaloniaNative.GlPlatformSurface.CreateGlRenderTarget` method can only be called on the UI thread. In normal circumstances this method is called in response to a call to `TopLevel.HandlePaint` from the native `AvnView.updateLayer` method. However, a customer was experiencing a problem where `AvnView.updateLayer` and by extension `TopLevel.HandlePaint` were being called before the window is shown. This broke the creation of the GL render target.
- Make naming more clear - it's not getting the style mask, it's calculating what it should be in order to update the mask
- Make it abstract to prevent it being called from the ctor in future
7eb95255c regressed disabling the minimize button on owned windows. Split what was previously `IsDialog` into two flags: `IsModal` and `IsOwned` and use `IsOwned` to control the disabled state of the minimize button.