Skip to content

Commit

Permalink
fix: startup issues on WSL
Browse files Browse the repository at this point in the history
  • Loading branch information
ramezgerges committed Aug 26, 2024
1 parent 859241f commit 48b9c04
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
33 changes: 20 additions & 13 deletions src/Uno.UI.Runtime.Skia.X11/X11NativeElementHostingExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ internal partial class X11NativeElementHostingExtension : ContentPresenter.INati
private Rect? _lastClipRect;
private bool _layoutDirty = true;
private readonly ContentPresenter _presenter;
private readonly IntPtr _display;

private readonly Lazy<IntPtr> _display;
private IntPtr Display => _display.Value;

public X11NativeElementHostingExtension(ContentPresenter contentPresenter)
{
_presenter = contentPresenter;
_display = ((X11XamlRootHost)X11Manager.XamlRootMap.GetHostForRoot(_presenter.XamlRoot!)!).RootX11Window.Display;

// _presenter.XamlRoot might not be set at the time of construction, so we need to make this lazy.
// This implicitly means that we expect that the XamlRoot must be set by the time we need to do any
// X11 calls (like attaching).
_display = new Lazy<IntPtr>(
() => ((X11XamlRootHost)X11Manager.XamlRootMap.GetHostForRoot(_presenter.XamlRoot!)!).RootX11Window.Display);
}

private XamlRoot? XamlRoot => _presenter.XamlRoot;
Expand All @@ -34,10 +41,10 @@ public void AttachNativeElement(object content)
&& XamlRoot is { } xamlRoot
&& X11Manager.XamlRootMap.GetHostForRoot(xamlRoot) is X11XamlRootHost host)
{
using var lockDiposable = X11Helper.XLock(_display);
using var lockDiposable = X11Helper.XLock(Display);

host.AttachSubWindow(nativeWindow.WindowId);
_ = XLib.XMapWindow(_display, nativeWindow.WindowId);
_ = XLib.XMapWindow(Display, nativeWindow.WindowId);
_ = X11Helper.XRaiseWindow(host.TopX11Window.Display, host.TopX11Window.Window);

if (!_hostToNativeElementHosts.TryGetValue(host, out var set))
Expand All @@ -61,11 +68,11 @@ public void DetachNativeElement(object content)
&& XamlRoot is { } xamlRoot
&& X11Manager.XamlRootMap.GetHostForRoot(xamlRoot) is X11XamlRootHost host)
{
using var lockDiposable = X11Helper.XLock(_display);
_ = XLib.XQueryTree(_display, nativeWindow.WindowId, out IntPtr root, out _, out var children, out _);
using var lockDiposable = X11Helper.XLock(Display);
_ = XLib.XQueryTree(Display, nativeWindow.WindowId, out IntPtr root, out _, out var children, out _);
_ = XLib.XFree(children);
_ = X11Helper.XReparentWindow(_display, nativeWindow.WindowId, root, 0, 0);
_ = XLib.XSync(_display, false);
_ = X11Helper.XReparentWindow(Display, nativeWindow.WindowId, root, 0, 0);
_ = XLib.XSync(Display, false);

var set = _hostToNativeElementHosts[host];
set.Remove(this);
Expand Down Expand Up @@ -109,23 +116,23 @@ _lastClipRect is { } clipRect &&
XamlRoot is { } xamlRoot &&
X11Manager.XamlRootMap.GetHostForRoot(xamlRoot) is X11XamlRootHost host)
{
using var lockDiposable = X11Helper.XLock(_display);
using var lockDiposable = X11Helper.XLock(Display);
if (arrangeRect.Width <= 0 || arrangeRect.Height <= 0)
{
arrangeRect.Size = new Size(1, 1);
}
_ = XLib.XResizeWindow(_display, nativeWindow.WindowId, (int)arrangeRect.Width, (int)arrangeRect.Height);
_ = X11Helper.XMoveWindow(_display, nativeWindow.WindowId, (int)arrangeRect.X, (int)arrangeRect.Y);
_ = XLib.XResizeWindow(Display, nativeWindow.WindowId, (int)arrangeRect.Width, (int)arrangeRect.Height);
_ = X11Helper.XMoveWindow(Display, nativeWindow.WindowId, (int)arrangeRect.X, (int)arrangeRect.Y);

XLib.XSync(_display, false);
XLib.XSync(Display, false);
}
}

public Size MeasureNativeElement(object content, Size childMeasuredSize, Size availableSize) => availableSize;

public void ChangeNativeElementVisibility(object content, bool visible)
{
// no need to do anything here, X11AirspaceRenderHelper will take care of it automatically
// no need to do anything here, airspace clipping logic will take care of it automatically
}

// This doesn't seem to work as most (all?) WMs won't change the opacity for subwindows, only top-level windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ internal partial class X11NativeElementHostingExtension : ContentPresenter.INati

process.Start();

using var lockDiposable = X11Helper.XLock(_display);
using var lockDiposable = X11Helper.XLock(Display);

_ = XLib.XQueryTree(_display, host.RootX11Window.Window, out IntPtr root, out _, out var children, out _);
_ = XLib.XQueryTree(Display, host.RootX11Window.Window, out IntPtr root, out _, out var children, out _);
_ = XLib.XFree(children);

// Wait for the window to open.
Thread.Sleep(500);
IntPtr window = FindWindowByTitle(_display, root, title);
IntPtr window = FindWindowByTitle(Display, root, title);

if (window == IntPtr.Zero)
{
Expand Down
15 changes: 2 additions & 13 deletions src/Uno.UI/UI/Xaml/Controls/ContentPresenter/ContentPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -781,10 +781,7 @@ protected virtual void OnContentChanged(object oldValue, object newValue)

TrySetDataContextFromContent(newValue);

if (IsInLiveTree)
{
TryRegisterNativeElement(oldValue, newValue);
}
TryRegisterNativeElement(oldValue, newValue);

SetUpdateTemplate();
}
Expand Down Expand Up @@ -969,11 +966,7 @@ internal override void EnterImpl(EnterParams @params, int depth)

// We do this in Enter not Loaded since Loaded is a lot more tricky
// (e.g. you can have Unloaded without Loaded, you can have multiple loaded events without unloaded in between, etc.)
if (!IsNativeHost)
{
TryRegisterNativeElement(null, Content);
}
if (IsNativeHost) // IsNativeHost can become true after the above TryRegisterNativeElement call, so this is not an if-else situation
if (IsNativeHost)
{
AttachNativeElement();
}
Expand Down Expand Up @@ -1018,10 +1011,6 @@ private protected override void OnLoaded()
#if !UNO_HAS_ENHANCED_LIFECYCLE
UpdateBorder();

if (!IsNativeHost)
{
TryRegisterNativeElement(null, Content);
}
if (IsNativeHost)
{
AttachNativeElement();
Expand Down

0 comments on commit 48b9c04

Please sign in to comment.