From 605a16d95cc22718de1ece90905f70c977de04eb Mon Sep 17 00:00:00 2001 From: Laszlo Nemeth <57342539+donlaci@users.noreply.github.com> Date: Wed, 25 Sep 2024 13:38:57 +0200 Subject: [PATCH] [Workspaces] add monitor detection (#35018) --- .../WorkspacesEditor/Models/Application.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs b/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs index c2fbd2ae5b..9f7be13742 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs @@ -358,6 +358,37 @@ namespace WorkspacesEditor.Models if (_monitorSetup == null) { _monitorSetup = Parent.Monitors.Where(x => x.MonitorNumber == MonitorNumber).FirstOrDefault(); + if (_monitorSetup == null) + { + // monitors changed: try to determine monitor id based on middle point + int middleX = Position.X + (Position.Width / 2); + int middleY = Position.Y + (Position.Height / 2); + var monitorCandidate = Parent.Monitors.Where(x => + (x.MonitorDpiUnawareBounds.Left < middleX) && + (x.MonitorDpiUnawareBounds.Right > middleX) && + (x.MonitorDpiUnawareBounds.Top < middleY) && + (x.MonitorDpiUnawareBounds.Bottom > middleY)).FirstOrDefault(); + if (monitorCandidate != null) + { + _monitorSetup = monitorCandidate; + MonitorNumber = monitorCandidate.MonitorNumber; + } + else + { + // monitors and even the app's area unknown, set the main monitor (which is closer to (0,0)) as the app's monitor + monitorCandidate = Parent.Monitors.OrderBy(x => Math.Abs(x.MonitorDpiUnawareBounds.Left) + Math.Abs(x.MonitorDpiUnawareBounds.Top)).FirstOrDefault(); + if (monitorCandidate != null) + { + _monitorSetup = monitorCandidate; + MonitorNumber = monitorCandidate.MonitorNumber; + } + else + { + // no monitors defined at all. + Logger.LogError($"Wrong workspace setup. No monitors defined for the workspace: {Parent.Name}."); + } + } + } } return _monitorSetup;