Skip to content

Commit

Permalink
fix: Ensure Renderer is initialized when Window HWND is already avail…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
MartinZikmund committed Aug 21, 2024
1 parent 6bb40b4 commit b8b9fd7
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Uno.UI.Runtime.Skia.Wpf/UI/Controls/UnoCompositeWindowHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using WpfCanvas = System.Windows.Controls.Canvas;
using WpfContentPresenter = System.Windows.Controls.ContentPresenter;
using WpfControl = System.Windows.Controls.Control;
using WpfWindow = System.Windows.Window;
using WpfFrameworkPropertyMetadata = System.Windows.FrameworkPropertyMetadata;
using MUX = Microsoft.UI.Xaml;

Expand Down Expand Up @@ -55,7 +56,7 @@ internal class UnoWpfWindowHost : WpfControl, IWpfWindowHost
private readonly MUX.Window _winUIWindow;

private readonly WpfCanvas _nativeOverlayLayer;
private readonly RenderingLayerHost _renderLayer;
private RenderingLayerHost _renderLayer;

private readonly SerialDisposable _backgroundDisposable = new();

Expand All @@ -73,20 +74,25 @@ public UnoWpfWindowHost(UnoWpfWindow wpfWindow, MUX.Window winUIWindow)

_nativeOverlayLayer = new WpfCanvas();
// Transparency doesn't work with the OpenGL renderer, so we have to use the software renderer for the top layer
_renderLayer = new RenderingLayerHost(WpfRendererProvider.CreateForHost(this));
_renderLayer = new RenderingLayerHost();

Loaded += WpfHost_Loaded;
Unloaded += (_, _) => _backgroundDisposable.Dispose();

UpdateRendererBackground();
_backgroundDisposable.Disposable = _winUIWindow.RegisterBackgroundChangedEvent((_, _) => UpdateRendererBackground());
}

public WpfWindow Window => _wpfWindow;

public WpfControl RenderLayer => _renderLayer;
public WpfControl BottomLayer => _renderLayer;

private void WpfHost_Loaded(object _, System.Windows.RoutedEventArgs __)
{
_renderLayer.Renderer = WpfRendererProvider.CreateForHost(this);
UpdateRendererBackground();
_renderLayer.InvalidateVisual();

// Avoid dotted border on focus.
if (Parent is WpfControl control)
{
Expand All @@ -97,6 +103,11 @@ private void WpfHost_Loaded(object _, System.Windows.RoutedEventArgs __)

void UpdateRendererBackground()
{
if (_renderLayer.Renderer is null)
{
return;
}

// the flyout layer always has a transparent background so that elements underneath can be seen.
_renderLayer.Renderer.BackgroundColor = SKColors.Transparent;

Expand Down Expand Up @@ -142,14 +153,14 @@ void IXamlRootHost.InvalidateRender()

RenderSurfaceType? IWpfXamlRootHost.RenderSurfaceType => WpfHost.Current?.RenderSurfaceType ?? null;

private class RenderingLayerHost(IWpfRenderer renderer) : WpfControl
private class RenderingLayerHost : WpfControl
{
public IWpfRenderer Renderer { get; } = renderer;
public IWpfRenderer? Renderer { get; set; }

protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
base.OnRender(drawingContext);
Renderer.Render(drawingContext);
Renderer?.Render(drawingContext);
}
}
}

0 comments on commit b8b9fd7

Please sign in to comment.