Skip to content

Commit

Permalink
Merge pull request #5433 from AvaloniaUI/fixes/osx-multi-monitor-work…
Browse files Browse the repository at this point in the history
…ing-area-calc

Fixes/osx multi monitor working area calc
  • Loading branch information
danwalmsley authored Feb 7, 2021
2 parents fe03d52 + 4cf6231 commit b6ccad0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
10 changes: 2 additions & 8 deletions native/Avalonia.Native/src/OSX/Screens.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
public:
FORWARD_IUNKNOWN()

private:
CGFloat PrimaryDisplayHeight()
{
return NSMaxY([[[NSScreen screens] firstObject] frame]);
}

public:
virtual HRESULT GetScreenCount (int* ret) override
{
Expand All @@ -36,12 +30,12 @@ virtual HRESULT GetScreen (int index, AvnScreen* ret) override
ret->Bounds.Height = [screen frame].size.height;
ret->Bounds.Width = [screen frame].size.width;
ret->Bounds.X = [screen frame].origin.x;
ret->Bounds.Y = PrimaryDisplayHeight() - [screen frame].origin.y - ret->Bounds.Height;
ret->Bounds.Y = ConvertPointY(ToAvnPoint([screen frame].origin)).Y - ret->Bounds.Height;

ret->WorkingArea.Height = [screen visibleFrame].size.height;
ret->WorkingArea.Width = [screen visibleFrame].size.width;
ret->WorkingArea.X = [screen visibleFrame].origin.x;
ret->WorkingArea.Y = ret->Bounds.Height - [screen visibleFrame].origin.y - ret->WorkingArea.Height;
ret->WorkingArea.Y = ConvertPointY(ToAvnPoint([screen visibleFrame].origin)).Y - ret->WorkingArea.Height;

ret->PixelDensity = [screen backingScaleFactor];

Expand Down
1 change: 1 addition & 0 deletions native/Avalonia.Native/src/OSX/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern NSApplicationActivationPolicy AvnDesiredActivationPolicy;
extern NSPoint ToNSPoint (AvnPoint p);
extern AvnPoint ToAvnPoint (NSPoint p);
extern AvnPoint ConvertPointY (AvnPoint p);
extern CGFloat PrimaryDisplayHeight();
extern NSSize ToNSSize (AvnSize s);
#ifdef DEBUG
#define NSDebugLog(...) NSLog(__VA_ARGS__)
Expand Down
10 changes: 7 additions & 3 deletions native/Avalonia.Native/src/OSX/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,14 @@ AvnPoint ToAvnPoint (NSPoint p)

AvnPoint ConvertPointY (AvnPoint p)
{
auto sw = [NSScreen.screens objectAtIndex:0].frame;
auto primaryDisplayHeight = NSMaxY([[[NSScreen screens] firstObject] frame]);

auto t = MAX(sw.origin.y, sw.origin.y + sw.size.height);
p.Y = t - p.Y;
p.Y = primaryDisplayHeight - p.Y;

return p;
}

CGFloat PrimaryDisplayHeight()
{
return NSMaxY([[[NSScreen screens] firstObject] frame]);
}
16 changes: 7 additions & 9 deletions samples/ControlCatalog/Pages/ScreenPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override void Render(DrawingContext context)
var screens = w.Screens.All;
var scaling = ((IRenderRoot)w).RenderScaling;

var drawBrush = Brushes.Green;
var drawBrush = Brushes.Black;
Pen p = new Pen(drawBrush);
if (screens != null)
foreach (Screen screen in screens)
Expand All @@ -45,18 +45,16 @@ public override void Render(DrawingContext context)
screen.Bounds.Height / 10f);
Rect workingAreaRect = new Rect(screen.WorkingArea.X / 10f + Math.Abs(_leftMost), screen.WorkingArea.Y / 10f, screen.WorkingArea.Width / 10f,
screen.WorkingArea.Height / 10f);

context.DrawRectangle(p, boundsRect);
context.DrawRectangle(p, workingAreaRect);

FormattedText text = new FormattedText()
{
Typeface = Typeface.Default
};

text.Text = $"Bounds: {screen.Bounds.Width}:{screen.Bounds.Height}";
var text = new FormattedText() { Typeface = new Typeface("Arial"), FontSize = 18 };

text.Text = $"Bounds: {screen.Bounds.TopLeft} {screen.Bounds.Width}:{screen.Bounds.Height}";
context.DrawText(drawBrush, boundsRect.Position.WithY(boundsRect.Size.Height), text);

text.Text = $"WorkArea: {screen.WorkingArea.Width}:{screen.WorkingArea.Height}";
text.Text = $"WorkArea: {screen.WorkingArea.TopLeft} {screen.WorkingArea.Width}:{screen.WorkingArea.Height}";
context.DrawText(drawBrush, boundsRect.Position.WithY(boundsRect.Size.Height + 20), text);

text.Text = $"Scaling: {screen.PixelDensity * 100}%";
Expand All @@ -69,7 +67,7 @@ public override void Render(DrawingContext context)
context.DrawText(drawBrush, 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));
context.DrawRectangle(p, new Rect(w.Position.X / 10f + Math.Abs(_leftMost), w.Position.Y / 10f, w.Bounds.Width / 10, w.Bounds.Height / 10));
}
}
}
2 changes: 1 addition & 1 deletion src/Avalonia.Native/WindowImplBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public void BeginMoveDrag(PointerPressedEventArgs e)
_native.BeginMoveDrag();
}

public Size MaxAutoSizeHint => Screen.AllScreens.Select(s => s.Bounds.Size.ToSize(s.PixelDensity))
public Size MaxAutoSizeHint => Screen.AllScreens.Select(s => s.Bounds.Size.ToSize(1))
.OrderByDescending(x => x.Width + x.Height).FirstOrDefault();

public void SetTopmost(bool value)
Expand Down

0 comments on commit b6ccad0

Please sign in to comment.