Browse Source

Add more platform specific screen from X methods.

pull/7165/head
Dariusz Komosinski 4 years ago
parent
commit
a7ace8f57b
  1. 8
      src/Avalonia.Controls/Platform/IScreenImpl.cs
  2. 51
      src/Avalonia.Controls/Screens.cs
  3. 2
      src/Avalonia.Controls/Window.cs
  4. 15
      src/Avalonia.DesignerSupport/Remote/Stubs.cs
  5. 15
      src/Avalonia.Headless/HeadlessPlatformStubs.cs
  6. 15
      src/Avalonia.Native/ScreenImpl.cs
  7. 15
      src/Avalonia.X11/X11Screens.cs
  8. 15
      src/Web/Avalonia.Web.Blazor/WinStubs.cs
  9. 39
      src/Windows/Avalonia.Win32/ScreenImpl.cs
  10. 6
      src/Windows/Avalonia.Win32/WinScreen.cs

8
src/Avalonia.Controls/Platform/IScreenImpl.cs

@ -1,5 +1,7 @@
using System.Collections.Generic;
#nullable enable
namespace Avalonia.Platform
{
public interface IScreenImpl
@ -7,5 +9,11 @@ namespace Avalonia.Platform
int ScreenCount { get; }
IReadOnlyList<Screen> AllScreens { get; }
Screen? ScreenFromWindow(IWindowBaseImpl window);
Screen? ScreenFromPoint(PixelPoint point);
Screen? ScreenFromRect(PixelRect rect);
}
}

51
src/Avalonia.Controls/Screens.cs

@ -20,30 +20,53 @@ namespace Avalonia.Controls
_iScreenImpl = iScreenImpl;
}
public Screen ScreenFromBounds(PixelRect bounds){
Screen currMaxScreen = null;
double maxAreaSize = 0;
foreach (Screen screen in All)
public Screen ScreenFromBounds(PixelRect bounds)
{
Screen currMaxScreen = _iScreenImpl.ScreenFromRect(bounds);
if (currMaxScreen == null)
{
double left = MathUtilities.Clamp(bounds.X, screen.Bounds.X, screen.Bounds.X + screen.Bounds.Width);
double top = MathUtilities.Clamp(bounds.Y, screen.Bounds.Y, screen.Bounds.Y + screen.Bounds.Height);
double right = MathUtilities.Clamp(bounds.X + bounds.Width, screen.Bounds.X, screen.Bounds.X + screen.Bounds.Width);
double bottom = MathUtilities.Clamp(bounds.Y + bounds.Height, screen.Bounds.Y, screen.Bounds.Y + screen.Bounds.Height);
double area = (right - left) * (bottom - top);
if (area > maxAreaSize)
double maxAreaSize = 0;
foreach (Screen screen in All)
{
maxAreaSize = area;
currMaxScreen = screen;
double left = MathUtilities.Clamp(bounds.X, screen.Bounds.X, screen.Bounds.X + screen.Bounds.Width);
double top = MathUtilities.Clamp(bounds.Y, screen.Bounds.Y, screen.Bounds.Y + screen.Bounds.Height);
double right = MathUtilities.Clamp(bounds.X + bounds.Width, screen.Bounds.X, screen.Bounds.X + screen.Bounds.Width);
double bottom = MathUtilities.Clamp(bounds.Y + bounds.Height, screen.Bounds.Y, screen.Bounds.Y + screen.Bounds.Height);
double area = (right - left) * (bottom - top);
if (area > maxAreaSize)
{
maxAreaSize = area;
currMaxScreen = screen;
}
}
}
return currMaxScreen;
}
public Screen ScreenFromWindow(IWindowBaseImpl window)
{
var screen = _iScreenImpl.ScreenFromWindow(window);
if (screen == null && window.Position is { } position)
{
screen = ScreenFromPoint(position);
}
return screen;
}
public Screen ScreenFromPoint(PixelPoint point)
{
return All.FirstOrDefault(x => x.Bounds.Contains(point));
var screen = _iScreenImpl.ScreenFromPoint(point);
if (screen == null)
{
screen = All.FirstOrDefault(x => x.Bounds.Contains(point));
}
return screen;
}
public Screen ScreenFromVisual(IVisual visual)

2
src/Avalonia.Controls/Window.cs

@ -863,7 +863,7 @@ namespace Avalonia.Controls
if (WindowStartupLocation == WindowStartupLocation.CenterScreen)
{
var screen = Screens.ScreenFromPoint(owner?.Position ?? Position);
var screen = Screens.ScreenFromWindow(owner);
if (screen != null)
{

15
src/Avalonia.DesignerSupport/Remote/Stubs.cs

@ -236,5 +236,20 @@ namespace Avalonia.DesignerSupport.Remote
public IReadOnlyList<Screen> AllScreens { get; } =
new Screen[] { new Screen(1, new PixelRect(0, 0, 4000, 4000), new PixelRect(0, 0, 4000, 4000), true) };
public Screen ScreenFromPoint(PixelPoint point)
{
return null;
}
public Screen ScreenFromRect(PixelRect rect)
{
return null;
}
public Screen ScreenFromWindow(IWindowBaseImpl window)
{
return null;
}
}
}

15
src/Avalonia.Headless/HeadlessPlatformStubs.cs

@ -203,5 +203,20 @@ namespace Avalonia.Headless
new Screen(1, new PixelRect(0, 0, 1920, 1280),
new PixelRect(0, 0, 1920, 1280), true),
};
public Screen ScreenFromPoint(PixelPoint point)
{
return null;
}
public Screen ScreenFromRect(PixelRect rect)
{
return null;
}
public Screen ScreenFromWindow(IWindowBaseImpl window)
{
return null;
}
}
}

15
src/Avalonia.Native/ScreenImpl.cs

@ -48,5 +48,20 @@ namespace Avalonia.Native
_native?.Dispose();
_native = null;
}
public Screen ScreenFromPoint(PixelPoint point)
{
return null;
}
public Screen ScreenFromRect(PixelRect rect)
{
return null;
}
public Screen ScreenFromWindow(IWindowBaseImpl window)
{
return null;
}
}
}

15
src/Avalonia.X11/X11Screens.cs

@ -200,6 +200,21 @@ namespace Avalonia.X11
}
public Screen ScreenFromWindow(IWindowBaseImpl window)
{
return null;
}
public Screen ScreenFromPoint(PixelPoint point)
{
return null;
}
public Screen ScreenFromRect(PixelRect rect)
{
return null;
}
public int ScreenCount => _impl.Screens.Length;
public IReadOnlyList<Screen> AllScreens =>

15
src/Web/Avalonia.Web.Blazor/WinStubs.cs

@ -55,5 +55,20 @@ namespace Avalonia.Web.Blazor
public IReadOnlyList<Screen> AllScreens { get; } =
new[] { new Screen(96, new PixelRect(0, 0, 4000, 4000), new PixelRect(0, 0, 4000, 4000), true) };
public Screen? ScreenFromPoint(PixelPoint point)
{
return null;
}
public Screen? ScreenFromRect(PixelRect rect)
{
return null;
}
public Screen? ScreenFromWindow(IWindowBaseImpl window)
{
return null;
}
}
}

39
src/Windows/Avalonia.Win32/ScreenImpl.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Platform;
using Avalonia.Win32.Interop;
using static Avalonia.Win32.Interop.UnmanagedMethods;
@ -70,5 +71,43 @@ namespace Avalonia.Win32
{
_allScreens = null;
}
public Screen ScreenFromWindow(IWindowBaseImpl window)
{
var handle = window.Handle.Handle;
var monitor = MonitorFromWindow(handle, MONITOR.MONITOR_DEFAULTTONEAREST);
return FindScreenByHandle(monitor);
}
public Screen ScreenFromPoint(PixelPoint point)
{
var monitor = MonitorFromPoint(new POINT
{
X = point.X,
Y = point.Y
}, MONITOR.MONITOR_DEFAULTTONEAREST);
return FindScreenByHandle(monitor);
}
public Screen ScreenFromRect(PixelRect rect)
{
var monitor = MonitorFromRect(new RECT
{
left = rect.TopLeft.X,
top = rect.TopLeft.Y,
right = rect.TopRight.X,
bottom = rect.BottomRight.Y
}, MONITOR.MONITOR_DEFAULTTONEAREST);
return FindScreenByHandle(monitor);
}
private Screen FindScreenByHandle(IntPtr handle)
{
return AllScreens.Cast<WinScreen>().FirstOrDefault(m => m.Handle == handle);
}
}
}

6
src/Windows/Avalonia.Win32/WinScreen.cs

@ -9,9 +9,11 @@ namespace Avalonia.Win32
public WinScreen(double pixelDensity, PixelRect bounds, PixelRect workingArea, bool primary, IntPtr hMonitor) : base(pixelDensity, bounds, workingArea, primary)
{
this._hMonitor = hMonitor;
_hMonitor = hMonitor;
}
public IntPtr Handle => _hMonitor;
public override int GetHashCode()
{
return (int)_hMonitor;
@ -19,7 +21,7 @@ namespace Avalonia.Win32
public override bool Equals(object obj)
{
return (obj is WinScreen screen) ? this._hMonitor == screen._hMonitor : base.Equals(obj);
return (obj is WinScreen screen) ? _hMonitor == screen._hMonitor : base.Equals(obj);
}
}
}

Loading…
Cancel
Save