Skip to content

Commit 460a354

Browse files
authored
Fix headless screens (#16348)
* Fix headless screens being null * Remove unused properties
1 parent 05ac6d2 commit 460a354

File tree

9 files changed

+40
-9
lines changed

9 files changed

+40
-9
lines changed

src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ public TopLevelImpl(AvaloniaView avaloniaView, bool placeOnTop = false)
101101
internal InvalidationAwareSurfaceView InternalView => _view;
102102

103103
public double DesktopScaling => RenderScaling;
104-
public IScreenImpl? Screen { get; }
105104
public IPlatformHandle Handle => _view;
106105

107106
public IEnumerable<object> Surfaces { get; }

src/Avalonia.Controls/Screens.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ public Screens(IScreenImpl iScreenImpl)
163163
var tl = visual.PointToScreen(visual.Bounds.TopLeft);
164164
var br = visual.PointToScreen(visual.Bounds.BottomRight);
165165

166-
return ScreenFromBounds(new PixelRect(tl, br));
166+
// Attempt to get screen from the physical position on any screen first. Fallback to the screen hosting top level.
167+
return ScreenFromBounds(new PixelRect(tl, br))
168+
?? ScreenFromTopLevel(topLevel);
167169
}
168170
else
169171
{

src/Browser/Avalonia.Browser/BrowserTopLevelImpl.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ public void SetTransparencyLevelHint(IReadOnlyList<WindowTransparencyLevel> tran
125125
}
126126

127127
public double DesktopScaling => RenderScaling;
128-
public IScreenImpl? Screen { get; }
129128
public IPlatformHandle? Handle { get; }
130129
public Size ClientSize => _surface?.ClientSize ?? new Size(1, 1);
131130
public Size? FrameSize => null;

src/Headless/Avalonia.Headless/HeadlessWindowImpl.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ internal class HeadlessWindowImpl : IWindowImpl, IPopupImpl, IFramebufferPlatfor
1919
private static int _nextGlobalZOrder = 1;
2020

2121
private readonly IKeyboardDevice _keyboard;
22+
private readonly IScreenImpl _screen;
2223
private readonly Stopwatch _st = Stopwatch.StartNew();
2324
private readonly Pointer _mousePointer;
2425
private WriteableBitmap? _lastRenderedFrame;
@@ -32,6 +33,7 @@ public HeadlessWindowImpl(bool isPopup, PixelFormat frameBufferFormat)
3233
IsPopup = isPopup;
3334
Surfaces = new object[] { this };
3435
_keyboard = AvaloniaLocator.Current.GetRequiredService<IKeyboardDevice>();
36+
_screen = new HeadlessScreensStub();
3537
_mousePointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true);
3638
MouseDevice = new MouseDevice(_mousePointer);
3739
ClientSize = new Size(1024, 768);
@@ -150,7 +152,6 @@ public void SetTopmost(bool value)
150152

151153
}
152154

153-
public IScreenImpl Screen { get; } = new HeadlessScreensStub();
154155
public WindowState WindowState { get; set; }
155156
public Action<WindowState>? WindowStateChanged { get; set; }
156157
public void SetTitle(string? title)
@@ -266,11 +267,16 @@ public ILockedFramebuffer Lock()
266267
public AcrylicPlatformCompensationLevels AcrylicCompensationLevels => new AcrylicPlatformCompensationLevels(1, 1, 1);
267268
public object? TryGetFeature(Type featureType)
268269
{
269-
if(featureType == typeof(IClipboard))
270+
if (featureType == typeof(IClipboard))
270271
{
271272
return AvaloniaLocator.Current.GetRequiredService<IClipboard>();
272273
}
273274

275+
if (featureType == typeof(IScreenImpl))
276+
{
277+
return _screen;
278+
}
279+
274280
return null;
275281
}
276282

src/Linux/Avalonia.LinuxFramebuffer/FramebufferToplevelImpl.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public void SetCursor(ICursorImpl cursor)
5454
}
5555

5656
public double DesktopScaling => 1;
57-
public IScreenImpl Screen { get; }
5857
public IPlatformHandle Handle { get; }
5958
public Size ClientSize => ScaledSize;
6059
public Size? FrameSize => null;

src/Tizen/Avalonia.Tizen/TopLevelImpl.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public TopLevelImpl(ITizenView view, IEnumerable<object> surfaces)
2727
}
2828

2929
public double DesktopScaling => RenderScaling;
30-
public IScreenImpl? Screen { get; }
3130
public IPlatformHandle? Handle { get; }
3231

3332
public Size ClientSize => _view.ClientSize;

src/iOS/Avalonia.iOS/AvaloniaView.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ public void SetTransparencyLevelHint(IReadOnlyList<WindowTransparencyLevel> tran
222222
}
223223

224224
public double DesktopScaling => RenderScaling;
225-
public IScreenImpl? Screen { get; }
226225
public IPlatformHandle? Handle { get; }
227226
public Size ClientSize => new Size(_view.Bounds.Width, _view.Bounds.Height);
228227
public Size? FrameSize => null;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Reactive.Disposables;
3+
using System.Threading;
4+
using Avalonia.Controls;
5+
using Avalonia.Input;
6+
using Avalonia.Layout;
7+
using Avalonia.Threading;
8+
9+
namespace Avalonia.Headless.UnitTests;
10+
11+
public class ServicesTests
12+
{
13+
#if NUNIT
14+
[AvaloniaTest, Timeout(10000)]
15+
#elif XUNIT
16+
[AvaloniaFact(Timeout = 10000)]
17+
#endif
18+
public void Can_Access_Screens()
19+
{
20+
var window = new Window();
21+
var screens = window.Screens;
22+
Assert.NotNull(screens);
23+
24+
var currentScreenFromWindow = screens.ScreenFromWindow(window);
25+
var currentScreenFromVisual = screens.ScreenFromVisual(window);
26+
27+
Assert.True(ReferenceEquals(currentScreenFromWindow, currentScreenFromVisual));
28+
}
29+
}

tests/Avalonia.UnitTests/CompositorTestServices.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ public void Dispose()
159159
}
160160

161161
public double DesktopScaling => 1;
162-
public IScreenImpl Screen { get; }
163162
public IPlatformHandle Handle { get; }
164163
public Size ClientSize { get; }
165164
public Size? FrameSize { get; }

0 commit comments

Comments
 (0)