Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat(domain): add `MonitorService` method for getting a monitor in a given direction

Lars Berger81bdef5144d5
diff --git a/LarsWM.Domain/Monitors/MonitorService.cs b/LarsWM.Domain/Monitors/MonitorService.cs
index b8fe3ea2..bed319e8 100644
--- a/LarsWM.Domain/Monitors/MonitorService.cs
+++ b/LarsWM.Domain/Monitors/MonitorService.cs
@@ -1,7 +1,7 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
+using LarsWM.Domain.Common.Enums;
using LarsWM.Domain.Containers;
using LarsWM.Domain.Windows;
@@ -49,5 +49,66 @@ namespace LarsWM.Domain.Monitors
var focusedContainer = _containerService.FocusedContainer;
return GetMonitorFromChildContainer(focusedContainer);
}
+
+ /// <summary>
+ /// Get monitor in a given direction. Use i3wm's algorithm for finding best guess.
+ /// </summary>
+ /// <param name="direction">Direction to search in.</param>
+ /// <param name="originMonitor">The monitor to search from.</param>
+ /// <returns></returns>
+ public Monitor GetMonitorInDirection(Direction direction, Monitor originMonitor)
+ {
+ Monitor monitorInDirection = null;
+
+ foreach (var monitor in GetMonitors())
+ {
+ // Check whether the monitor is to the right/left of the origin monitor.
+ if (
+ (direction == Direction.RIGHT && monitor.X > originMonitor.X) ||
+ (direction == Direction.LEFT && monitor.X < originMonitor.X)
+ )
+ {
+ // Check whether the y-coordinate overlaps with the y-coordinate of the origin monitor.
+ if (
+ monitor.Y + monitor.Height <= originMonitor.Y ||
+ originMonitor.Y + originMonitor.Height <= monitor.Y
+ )
+ continue;
+ }
+ // Check whether the monitor is below/above the origin monitor.
+ else if (
+ (direction == Direction.DOWN && monitor.Y > originMonitor.Y) ||
+ (direction == Direction.UP && monitor.Y < originMonitor.Y)
+ )
+ {
+ // Check whether the x-coordinate overlaps with the x-coordinate of the origin monitor.
+ if (
+ monitor.X + monitor.Width <= originMonitor.X ||
+ originMonitor.X + originMonitor.Width <= monitor.X
+ )
+ continue;
+ }
+ else
+ continue;
+
+ // Initialize `monitorInDirection` if no other suitable monitors have been found yet.
+ if (monitorInDirection == null)
+ {
+ monitorInDirection = monitor;
+ continue;
+ }
+
+ // Reassign `monitorInDirection` if the monitor is closer.
+ if ((direction == Direction.RIGHT && monitor.X < monitorInDirection.X) ||
+ (direction == Direction.LEFT && monitor.X > monitorInDirection.X) ||
+ (direction == Direction.DOWN && monitor.Y < monitorInDirection.Y) ||
+ (direction == Direction.UP && monitor.Y > monitorInDirection.Y))
+ {
+ monitorInDirection = monitor;
+ }
+ }
+
+ return monitorInDirection;
+ }
}
}