diff --git a/native/Avalonia.Native/inc/avalonia-native.h b/native/Avalonia.Native/inc/avalonia-native.h index 36e16c24d1..e54f3fa6a7 100644 --- a/native/Avalonia.Native/inc/avalonia-native.h +++ b/native/Avalonia.Native/inc/avalonia-native.h @@ -52,6 +52,7 @@ struct AvnScreen { AvnRect Bounds; AvnRect WorkingArea; + float PixelDensity; bool Primary; }; @@ -187,7 +188,6 @@ AVNCOM(IAvnWindowBase, 02) : IUnknown virtual HRESULT Close() = 0; virtual HRESULT Activate () = 0; virtual HRESULT GetClientSize(AvnSize*ret) = 0; - virtual HRESULT GetMaxClientSize(AvnSize* ret) = 0; virtual HRESULT GetScaling(double*ret)=0; virtual HRESULT SetMinMaxSize(AvnSize minSize, AvnSize maxSize) = 0; virtual HRESULT Resize(double width, double height) = 0; diff --git a/native/Avalonia.Native/src/OSX/Screens.mm b/native/Avalonia.Native/src/OSX/Screens.mm index 9d436b98c5..e7f009787a 100644 --- a/native/Avalonia.Native/src/OSX/Screens.mm +++ b/native/Avalonia.Native/src/OSX/Screens.mm @@ -38,6 +38,8 @@ class Screens : public ComSingleObject ret->WorkingArea.Height = [screen visibleFrame].size.height; ret->WorkingArea.Width = [screen visibleFrame].size.width; + ret->PixelDensity = [screen backingScaleFactor]; + ret->Primary = index == 0; return S_OK; diff --git a/native/Avalonia.Native/src/OSX/window.mm b/native/Avalonia.Native/src/OSX/window.mm index 340a04c02d..3347d58004 100644 --- a/native/Avalonia.Native/src/OSX/window.mm +++ b/native/Avalonia.Native/src/OSX/window.mm @@ -54,7 +54,6 @@ public: FORWARD_IUNKNOWN() virtual ~WindowBaseImpl() { - NSDebugLog(@"~WindowBaseImpl()"); View = NULL; Window = NULL; } @@ -161,22 +160,6 @@ public: } } - virtual HRESULT GetMaxClientSize(AvnSize* ret) override - { - @autoreleasepool - { - if(ret == nullptr) - return E_POINTER; - - auto size = [NSScreen.screens objectAtIndex:0].frame.size; - - ret->Height = size.height; - ret->Width = size.width; - - return S_OK; - } - } - virtual HRESULT GetScaling (double* ret) override { @autoreleasepool @@ -413,8 +396,8 @@ private: INHERIT_INTERFACE_MAP(WindowBaseImpl) INTERFACE_MAP_ENTRY(IAvnWindow, IID_IAvnWindow) END_INTERFACE_MAP() - virtual ~WindowImpl(){ - NSDebugLog(@"~WindowImpl"); + virtual ~WindowImpl() + { } ComPtr WindowEvents; @@ -662,10 +645,8 @@ NSArray* AllLoopModes = [NSArray arrayWithObjects: NSDefaultRunLoopMode, NSEvent - (void)dealloc { - NSDebugLog(@"AvnView dealloc"); } - - (void)onClosed { _parent = NULL; @@ -1065,7 +1046,6 @@ NSArray* AllLoopModes = [NSArray arrayWithObjects: NSDefaultRunLoopMode, NSEvent - (void)dealloc { - NSDebugLog(@"AvnWindow dealloc"); } - (void)pollModalSession:(nonnull NSModalSession)session diff --git a/samples/ControlCatalog/Pages/ScreenPage.cs b/samples/ControlCatalog/Pages/ScreenPage.cs index 13c1667ed2..d775eb9635 100644 --- a/samples/ControlCatalog/Pages/ScreenPage.cs +++ b/samples/ControlCatalog/Pages/ScreenPage.cs @@ -57,12 +57,15 @@ namespace ControlCatalog.Pages text.Text = $"WorkArea: {screen.WorkingArea.Width}:{screen.WorkingArea.Height}"; context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 20), text); + + text.Text = $"Scaling: {screen.PixelDensity * 100}%"; + context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 40), text); text.Text = $"Primary: {screen.Primary}"; - context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 40), text); + context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 60), text); text.Text = $"Current: {screen.Equals(w.Screens.ScreenFromBounds(new PixelRect(w.Position, PixelSize.FromSize(w.Bounds.Size, scaling))))}"; - context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 60), text); + context.DrawText(Brushes.Black, boundsRect.Position.WithY(boundsRect.Size.Height + 80), text); } context.DrawRectangle(p, new Rect(w.Position.X / 10f + Math.Abs(_leftMost), w.Position.Y / 10, w.Bounds.Width / 10, w.Bounds.Height / 10)); diff --git a/src/Avalonia.Controls/Platform/Screen.cs b/src/Avalonia.Controls/Platform/Screen.cs index e7c811235c..976faed3fd 100644 --- a/src/Avalonia.Controls/Platform/Screen.cs +++ b/src/Avalonia.Controls/Platform/Screen.cs @@ -2,14 +2,17 @@ { public class Screen { + public double PixelDensity { get; } + public PixelRect Bounds { get; } public PixelRect WorkingArea { get; } public bool Primary { get; } - public Screen(PixelRect bounds, PixelRect workingArea, bool primary) + public Screen(double pixelDensity, PixelRect bounds, PixelRect workingArea, bool primary) { + this.PixelDensity = pixelDensity; this.Bounds = bounds; this.WorkingArea = workingArea; this.Primary = primary; diff --git a/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositionerPopupImplHelper.cs b/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositionerPopupImplHelper.cs index bb701da651..8e7e429a73 100644 --- a/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositionerPopupImplHelper.cs +++ b/src/Avalonia.Controls/Primitives/PopupPositioning/ManagedPopupPositionerPopupImplHelper.cs @@ -32,7 +32,7 @@ namespace Avalonia.Controls.Primitives.PopupPositioning { // Popup positioner operates with abstract coordinates, but in our case they are pixel ones var point = _parent.PointToScreen(default); - var size = PixelSize.FromSize(_parent.ClientSize, _parent.Scaling); + var size = TranslateSize(_parent.ClientSize); return new Rect(point.X, point.Y, size.Width, size.Height); } @@ -43,8 +43,8 @@ namespace Avalonia.Controls.Primitives.PopupPositioning _moveResize(new PixelPoint((int)devicePoint.X, (int)devicePoint.Y), virtualSize, _parent.Scaling); } - public Point TranslatePoint(Point pt) => pt * _parent.Scaling; + public virtual Point TranslatePoint(Point pt) => pt * _parent.Scaling; - public Size TranslateSize(Size size) => size * _parent.Scaling; + public virtual Size TranslateSize(Size size) => size * _parent.Scaling; } } diff --git a/src/Avalonia.DesignerSupport/Remote/Stubs.cs b/src/Avalonia.DesignerSupport/Remote/Stubs.cs index 4ce0da60a2..16d434b614 100644 --- a/src/Avalonia.DesignerSupport/Remote/Stubs.cs +++ b/src/Avalonia.DesignerSupport/Remote/Stubs.cs @@ -178,6 +178,6 @@ namespace Avalonia.DesignerSupport.Remote public int ScreenCount => 1; public IReadOnlyList AllScreens { get; } = - new Screen[] { new Screen(new PixelRect(0, 0, 4000, 4000), new PixelRect(0, 0, 4000, 4000), true) }; + new Screen[] { new Screen(1, new PixelRect(0, 0, 4000, 4000), new PixelRect(0, 0, 4000, 4000), true) }; } } diff --git a/src/Avalonia.Native/AvaloniaNativePlatform.cs b/src/Avalonia.Native/AvaloniaNativePlatform.cs index 0da97b915c..6d48ab3829 100644 --- a/src/Avalonia.Native/AvaloniaNativePlatform.cs +++ b/src/Avalonia.Native/AvaloniaNativePlatform.cs @@ -1,7 +1,6 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; -using System.Diagnostics.Contracts; using System.Runtime.InteropServices; using Avalonia.Controls.Platform; using Avalonia.Input; diff --git a/src/Avalonia.Native/OsxManagedPopupPositionerPopupImplHelper.cs b/src/Avalonia.Native/OsxManagedPopupPositionerPopupImplHelper.cs new file mode 100644 index 0000000000..6c98e3c0cc --- /dev/null +++ b/src/Avalonia.Native/OsxManagedPopupPositionerPopupImplHelper.cs @@ -0,0 +1,18 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Controls.Primitives.PopupPositioning; +using Avalonia.Platform; + +namespace Avalonia.Native +{ + class OsxManagedPopupPositionerPopupImplHelper : ManagedPopupPositionerPopupImplHelper + { + public OsxManagedPopupPositionerPopupImplHelper(IWindowBaseImpl parent, MoveResizeDelegate moveResize) : base(parent, moveResize) + { + + } + public override Point TranslatePoint(Point pt) => pt; + + public override Size TranslateSize(Size size) => size; + } +} diff --git a/src/Avalonia.Native/PopupImpl.cs b/src/Avalonia.Native/PopupImpl.cs index f776ee0132..d7fa1052ff 100644 --- a/src/Avalonia.Native/PopupImpl.cs +++ b/src/Avalonia.Native/PopupImpl.cs @@ -22,7 +22,7 @@ namespace Avalonia.Native { Init(factory.CreatePopup(e), factory.CreateScreens()); } - PopupPositioner = new ManagedPopupPositioner(new ManagedPopupPositionerPopupImplHelper(parent, MoveResize)); + PopupPositioner = new ManagedPopupPositioner(new OsxManagedPopupPositionerPopupImplHelper(parent, MoveResize)); } private void MoveResize(PixelPoint position, Size size, double scaling) diff --git a/src/Avalonia.Native/ScreenImpl.cs b/src/Avalonia.Native/ScreenImpl.cs index 2afa71753e..3ec3be20d3 100644 --- a/src/Avalonia.Native/ScreenImpl.cs +++ b/src/Avalonia.Native/ScreenImpl.cs @@ -31,6 +31,7 @@ namespace Avalonia.Native var screen = _native.GetScreen(i); result[i] = new Screen( + screen.PixelDensity, screen.Bounds.ToAvaloniaPixelRect(), screen.WorkingArea.ToAvaloniaPixelRect(), screen.Primary); diff --git a/src/Avalonia.Native/WindowImplBase.cs b/src/Avalonia.Native/WindowImplBase.cs index 217fb4b078..d8ff370c45 100644 --- a/src/Avalonia.Native/WindowImplBase.cs +++ b/src/Avalonia.Native/WindowImplBase.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Linq; using Avalonia.Controls; using Avalonia.Controls.Platform.Surfaces; using Avalonia.Input; @@ -48,6 +49,11 @@ namespace Avalonia.Native Screen = new ScreenImpl(screens); _savedLogicalSize = ClientSize; _savedScaling = Scaling; + + var monitor = Screen.AllScreens.OrderBy(x => x.PixelDensity) + .FirstOrDefault(m => m.Bounds.Contains(Position)); + + Resize(new Size(monitor.WorkingArea.Width * 0.75d, monitor.WorkingArea.Height * 0.7d)); } public Size ClientSize @@ -300,7 +306,8 @@ namespace Avalonia.Native _native.BeginMoveDrag(); } - public Size MaxClientSize => _native.GetMaxClientSize().ToAvaloniaSize(); + public Size MaxClientSize => Screen.AllScreens.Select(s => s.Bounds.Size.ToSize(s.PixelDensity)) + .OrderByDescending(x => x.Width + x.Height).FirstOrDefault(); public void SetTopmost(bool value) { diff --git a/src/Avalonia.Themes.Default/ProgressBar.xaml b/src/Avalonia.Themes.Default/ProgressBar.xaml index a77a2f3ad5..72271e785a 100644 --- a/src/Avalonia.Themes.Default/ProgressBar.xaml +++ b/src/Avalonia.Themes.Default/ProgressBar.xaml @@ -7,9 +7,7 @@ - + diff --git a/src/Avalonia.X11/X11Screens.cs b/src/Avalonia.X11/X11Screens.cs index 6f860145d3..e247a4241a 100644 --- a/src/Avalonia.X11/X11Screens.cs +++ b/src/Avalonia.X11/X11Screens.cs @@ -157,7 +157,7 @@ namespace Avalonia.X11 public int ScreenCount => _impl.Screens.Length; public IReadOnlyList AllScreens => - _impl.Screens.Select(s => new Screen(s.Bounds, s.WorkingArea, s.Primary)).ToArray(); + _impl.Screens.Select(s => new Screen(s.PixelDensity, s.Bounds, s.WorkingArea, s.Primary)).ToArray(); } interface IX11Screens diff --git a/src/Avalonia.X11/X11Window.cs b/src/Avalonia.X11/X11Window.cs index 975b3d11d7..00761dfce8 100644 --- a/src/Avalonia.X11/X11Window.cs +++ b/src/Avalonia.X11/X11Window.cs @@ -98,14 +98,26 @@ namespace Avalonia.X11 valueMask |= SetWindowValuemask.ColorMap; } - _handle = XCreateWindow(_x11.Display, _x11.RootWindow, 10, 10, 300, 200, 0, + int defaultWidth = 300, defaultHeight = 200; + + if (!_popup) + { + var monitor = Screen.AllScreens.OrderBy(x => x.PixelDensity) + .FirstOrDefault(m => m.Bounds.Contains(Position)); + + // Emulate Window 7+'s default window size behavior. + defaultWidth = (int)(monitor.WorkingArea.Width * 0.75d); + defaultHeight = (int)(monitor.WorkingArea.Height * 0.7d); + } + + _handle = XCreateWindow(_x11.Display, _x11.RootWindow, 10, 10, defaultWidth, defaultHeight, 0, depth, (int)CreateWindowArgs.InputOutput, visual, new UIntPtr((uint)valueMask), ref attr); if (_useRenderWindow) - _renderHandle = XCreateWindow(_x11.Display, _handle, 0, 0, 300, 200, 0, depth, + _renderHandle = XCreateWindow(_x11.Display, _handle, 0, 0, defaultWidth, defaultHeight, 0, depth, (int)CreateWindowArgs.InputOutput, visual, new UIntPtr((uint)(SetWindowValuemask.BorderPixel | SetWindowValuemask.BitGravity | diff --git a/src/Windows/Avalonia.Win32/ScreenImpl.cs b/src/Windows/Avalonia.Win32/ScreenImpl.cs index 1833e21e23..e77aa07bcd 100644 --- a/src/Windows/Avalonia.Win32/ScreenImpl.cs +++ b/src/Windows/Avalonia.Win32/ScreenImpl.cs @@ -30,6 +30,8 @@ namespace Avalonia.Win32 MONITORINFO monitorInfo = MONITORINFO.Create(); if (GetMonitorInfo(monitor,ref monitorInfo)) { + GetDpiForMonitor(monitor, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out var x, out _); + RECT bounds = monitorInfo.rcMonitor; RECT workingArea = monitorInfo.rcWork; PixelRect avaloniaBounds = new PixelRect(bounds.left, bounds.top, bounds.right - bounds.left, @@ -38,7 +40,7 @@ namespace Avalonia.Win32 new PixelRect(workingArea.left, workingArea.top, workingArea.right - workingArea.left, workingArea.bottom - workingArea.top); screens[index] = - new WinScreen(avaloniaBounds, avaloniaWorkArea, monitorInfo.dwFlags == 1, + new WinScreen((double)x / 96.0d, avaloniaBounds, avaloniaWorkArea, monitorInfo.dwFlags == 1, monitor); index++; } diff --git a/src/Windows/Avalonia.Win32/WinScreen.cs b/src/Windows/Avalonia.Win32/WinScreen.cs index e849800e62..0cf9fe31db 100644 --- a/src/Windows/Avalonia.Win32/WinScreen.cs +++ b/src/Windows/Avalonia.Win32/WinScreen.cs @@ -7,7 +7,7 @@ namespace Avalonia.Win32 { private readonly IntPtr _hMonitor; - public WinScreen(PixelRect bounds, PixelRect workingArea, bool primary, IntPtr hMonitor) : base(bounds, workingArea, primary) + public WinScreen(double pixelDensity, PixelRect bounds, PixelRect workingArea, bool primary, IntPtr hMonitor) : base(pixelDensity, bounds, workingArea, primary) { this._hMonitor = hMonitor; } diff --git a/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs b/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs index ac80fc6c7a..f44f89e91f 100644 --- a/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs @@ -198,7 +198,7 @@ namespace Avalonia.Controls.UnitTests var screen = new PixelRect(new PixelPoint(), new PixelSize(100, 100)); var screenImpl = new Mock(); screenImpl.Setup(x => x.ScreenCount).Returns(1); - screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(screen, screen, true) }); + screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(1, screen, screen, true) }); popupImpl = MockWindowingPlatform.CreatePopupMock(); popupImpl.SetupGet(x => x.Scaling).Returns(1); diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs index 75239f014f..d87014f646 100644 --- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs +++ b/tests/Avalonia.Controls.UnitTests/WindowTests.cs @@ -271,8 +271,8 @@ namespace Avalonia.Controls.UnitTests [Fact] public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen() { - var screen1 = new Mock(new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 1040)), true); - var screen2 = new Mock(new PixelRect(new PixelSize(1366, 768)), new PixelRect(new PixelSize(1366, 728)), false); + var screen1 = new Mock(1.0, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 1040)), true); + var screen2 = new Mock(1.0, new PixelRect(new PixelSize(1366, 768)), new PixelRect(new PixelSize(1366, 728)), false); var screens = new Mock(); screens.Setup(x => x.AllScreens).Returns(new Screen[] { screen1.Object, screen2.Object });