|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Globalization; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | + |
| 5 | +namespace UiTestsPlayground.Helpers; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Utilities for discovering monitor layouts and translating them into Playwright launch arguments. |
| 9 | +/// </summary> |
| 10 | +internal static class MonitorUtilities |
| 11 | +{ |
| 12 | + internal readonly record struct MonitorInfo(string Name, int Width, int Height, int X, int Y, bool IsPrimary); |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Retrieves connected monitor metadata via <c>xrandr --query</c>. Returns an empty collection when |
| 16 | + /// the command is unavailable or fails. |
| 17 | + /// </summary> |
| 18 | + public static IReadOnlyList<MonitorInfo> DetectMonitors() |
| 19 | + { |
| 20 | + try |
| 21 | + { |
| 22 | + var psi = new ProcessStartInfo("bash") |
| 23 | + { |
| 24 | + RedirectStandardOutput = true, |
| 25 | + RedirectStandardError = true, |
| 26 | + UseShellExecute = false |
| 27 | + }; |
| 28 | + psi.ArgumentList.Add("-lc"); |
| 29 | + psi.ArgumentList.Add("xrandr --query"); |
| 30 | + |
| 31 | + using var process = Process.Start(psi); |
| 32 | + if (process is null) |
| 33 | + { |
| 34 | + return Array.Empty<MonitorInfo>(); |
| 35 | + } |
| 36 | + |
| 37 | + var output = process.StandardOutput.ReadToEnd(); |
| 38 | + process.WaitForExit(2000); |
| 39 | + |
| 40 | + var monitors = new List<MonitorInfo>(); |
| 41 | + foreach (var line in output.Split('\n', StringSplitOptions.RemoveEmptyEntries)) |
| 42 | + { |
| 43 | + if (!line.Contains(" connected", StringComparison.Ordinal)) |
| 44 | + { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + var parts = line.Split(" connected", 2, StringSplitOptions.TrimEntries); |
| 49 | + if (parts.Length < 2) |
| 50 | + { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + var name = parts[0].Trim(); |
| 55 | + var details = parts[1]; |
| 56 | + var isPrimary = details.Contains("primary", StringComparison.OrdinalIgnoreCase); |
| 57 | + |
| 58 | + var match = Regex.Match(details, @"(?<width>\d+)x(?<height>\d+)\+(?<x>-?\d+)\+(?<y>-?\d+)"); |
| 59 | + if (!match.Success) |
| 60 | + { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + var width = int.Parse(match.Groups["width"].Value, CultureInfo.InvariantCulture); |
| 65 | + var height = int.Parse(match.Groups["height"].Value, CultureInfo.InvariantCulture); |
| 66 | + var x = int.Parse(match.Groups["x"].Value, CultureInfo.InvariantCulture); |
| 67 | + var y = int.Parse(match.Groups["y"].Value, CultureInfo.InvariantCulture); |
| 68 | + |
| 69 | + monitors.Add(new MonitorInfo(name, width, height, x, y, isPrimary)); |
| 70 | + } |
| 71 | + |
| 72 | + return monitors; |
| 73 | + } |
| 74 | + catch |
| 75 | + { |
| 76 | + return Array.Empty<MonitorInfo>(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Constructs Chromium command-line parameters that control window position and size. The method |
| 82 | + /// prefers explicit environment overrides but falls back to the detected monitor geometry so each |
| 83 | + /// browser instance occupies a deterministic area of the desktop. |
| 84 | + /// </summary> |
| 85 | + public static List<string> BuildBrowserLaunchArgs(string? positionOverride, string? sizeOverride, MonitorInfo? monitor) |
| 86 | + { |
| 87 | + var args = new List<string> { "--start-maximized" }; |
| 88 | + |
| 89 | + int? posX = null, posY = null, width = null, height = null; |
| 90 | + |
| 91 | + if (!TryParsePair(positionOverride, out posX, out posY) && monitor.HasValue) |
| 92 | + { |
| 93 | + posX = monitor.Value.X; |
| 94 | + posY = monitor.Value.Y; |
| 95 | + } |
| 96 | + |
| 97 | + if (!TryParsePair(sizeOverride, out width, out height) && monitor.HasValue) |
| 98 | + { |
| 99 | + width = monitor.Value.Width; |
| 100 | + height = monitor.Value.Height; |
| 101 | + } |
| 102 | + |
| 103 | + if (posX.HasValue && posY.HasValue) |
| 104 | + { |
| 105 | + args.Add($"--window-position={posX.Value},{posY.Value}"); |
| 106 | + } |
| 107 | + |
| 108 | + if (width.HasValue && height.HasValue) |
| 109 | + { |
| 110 | + args.Add($"--window-size={width.Value},{height.Value}"); |
| 111 | + } |
| 112 | + |
| 113 | + return args; |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Attempts to parse a <c>"number,number"</c> string (e.g. axis or width/height) used for positioning |
| 118 | + /// browser windows. The method is intentionally tolerant: it returns <c>false</c> when parsing fails and |
| 119 | + /// leaves the nullable out parameters unset so the caller can fall back to monitor defaults. |
| 120 | + /// </summary> |
| 121 | + private static bool TryParsePair(string? value, out int? first, out int? second) |
| 122 | + { |
| 123 | + first = null; |
| 124 | + second = null; |
| 125 | + if (string.IsNullOrWhiteSpace(value)) |
| 126 | + { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + var parts = value.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries); |
| 131 | + if (parts.Length != 2) |
| 132 | + { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + if (int.TryParse(parts[0], out var firstValue) && int.TryParse(parts[1], out var secondValue)) |
| 137 | + { |
| 138 | + first = firstValue; |
| 139 | + second = secondValue; |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + return false; |
| 144 | + } |
| 145 | +} |
0 commit comments