diff --git a/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs b/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs index 294d59854d..02681dd841 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs @@ -360,38 +360,7 @@ 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}."); - } - } - } + _monitorSetup = Parent.GetMonitorForApp(this); } return _monitorSetup; diff --git a/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs b/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs index 07d7ad40a1..e65853f1e3 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs @@ -374,5 +374,44 @@ namespace WorkspacesEditor.Models app.IsExpanded = false; } } + + internal MonitorSetup GetMonitorForApp(Application app) + { + var monitorSetup = Monitors.Where(x => x.MonitorNumber == app.MonitorNumber).FirstOrDefault(); + if (monitorSetup == null) + { + // monitors changed: try to determine monitor id based on middle point + int middleX = app.Position.X + (app.Position.Width / 2); + int middleY = app.Position.Y + (app.Position.Height / 2); + var monitorCandidate = Monitors.Where(x => + (x.MonitorDpiUnawareBounds.Left < middleX) && + (x.MonitorDpiUnawareBounds.Right > middleX) && + (x.MonitorDpiUnawareBounds.Top < middleY) && + (x.MonitorDpiUnawareBounds.Bottom > middleY)).FirstOrDefault(); + if (monitorCandidate != null) + { + app.MonitorNumber = monitorCandidate.MonitorNumber; + return monitorCandidate; + } + 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 = Monitors.OrderBy(x => Math.Abs(x.MonitorDpiUnawareBounds.Left) + Math.Abs(x.MonitorDpiUnawareBounds.Top)).FirstOrDefault(); + if (monitorCandidate != null) + { + app.MonitorNumber = monitorCandidate.MonitorNumber; + return monitorCandidate; + } + else + { + // no monitors defined at all. + Logger.LogError($"Wrong workspace setup. No monitors defined for the workspace: {Name}."); + return null; + } + } + } + + return monitorSetup; + } } } diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs index 217a942ba5..45da7a6c2c 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs @@ -54,7 +54,13 @@ namespace WorkspacesEditor.Utils if (app.Maximized) { Project project = app.Parent; - var monitor = project.Monitors.Where(x => x.MonitorNumber == app.MonitorNumber).FirstOrDefault(); + var monitor = project.GetMonitorForApp(app); + if (monitor == null) + { + // unrealistic case, there are no monitors at all in the workspace, use original rect + return new Rectangle(TransformX(app.ScaledPosition.X), TransformY(app.ScaledPosition.Y), Scaled(app.ScaledPosition.Width), Scaled(app.ScaledPosition.Height)); + } + return new Rectangle(TransformX(monitor.MonitorDpiAwareBounds.Left), TransformY(monitor.MonitorDpiAwareBounds.Top), Scaled(monitor.MonitorDpiAwareBounds.Width), Scaled(monitor.MonitorDpiAwareBounds.Height)); } else