|
| 1 | +using Avalonia.Controls; |
| 2 | +using Avalonia.Media; |
| 3 | +using Avalonia.Threading; |
| 4 | +using SpaceSharp.Util; |
| 5 | + |
| 6 | +namespace SpaceSharp; |
| 7 | + |
| 8 | +/// <summary>Hidden development aids: the right-to-left preview and the layout audit. Neither is in the help text or the Shortcuts window.</summary> |
| 9 | +public partial class MainWindow |
| 10 | +{ |
| 11 | + /// <summary>Mirrors every open window as a right-to-left language would, or back (Ctrl+Shift+Alt+R); windows opened afterwards follow.</summary> |
| 12 | + private void ToggleRightToLeft() |
| 13 | + { |
| 14 | + AppLanguages.IsRightToLeft = !AppLanguages.IsRightToLeft; |
| 15 | + var direction = AppLanguages.IsRightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; |
| 16 | + FlowDirection = direction; |
| 17 | + foreach (var owned in OwnedWindows) owned.FlowDirection = direction; |
| 18 | + StatusText.Text = AppLanguages.IsRightToLeft |
| 19 | + ? "Right-to-left preview on (Ctrl+Shift+Alt+R turns it off, Ctrl+Shift+Alt+L lists clipped text)" |
| 20 | + : "Right-to-left preview off"; |
| 21 | + _ = AuditLayoutAsync(quiet: true); |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary>Lists text the window cuts off, in the status bar and in a file next to the crash logs (Ctrl+Shift+Alt+L).</summary> |
| 25 | + private async Task AuditLayoutAsync(bool quiet = false) |
| 26 | + { |
| 27 | + await Dispatcher.UIThread.InvokeAsync(() => { }, DispatcherPriority.Background); // after the layout pass the toggle caused |
| 28 | + var windows = new List<Window> { this }; |
| 29 | + windows.AddRange(OwnedWindows); |
| 30 | + var findings = windows.SelectMany(LayoutAudit.ClippedText).ToList(); |
| 31 | + string title = $"Layout audit {DateTime.Now:yyyy-MM-dd HH:mm:ss} · {(AppLanguages.IsRightToLeft ? "right-to-left" : "left-to-right")} · {windows.Count} window(s) · {Bounds.Width:0}×{Bounds.Height:0}"; |
| 32 | + string report = LayoutAudit.Report(findings, title); |
| 33 | + Console.WriteLine(report); |
| 34 | + string? file = null; |
| 35 | + try |
| 36 | + { |
| 37 | + Directory.CreateDirectory(CrashLog.Folder); |
| 38 | + file = Path.Combine(CrashLog.Folder, "layout-audit.txt"); |
| 39 | + await File.WriteAllTextAsync(file, report); |
| 40 | + } |
| 41 | + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { } |
| 42 | + if (quiet && findings.Count == 0) return; |
| 43 | + StatusText.Text = findings.Count == 0 |
| 44 | + ? "Layout audit: no clipped text." |
| 45 | + : $"Layout audit: {findings.Count} clipped, first \"{findings[0].Text}\" in {findings[0].Where}" + (file is null ? string.Empty : $" · list in {file}"); |
| 46 | + } |
| 47 | + |
| 48 | + // ---------------------------------------------------------------- the automated audit (--audit) |
| 49 | + |
| 50 | + /// <summary>The scenes the audit walks: every screen and dialog that has words in it. The menu is a popup the audit cannot see; the palette sheet is a picture.</summary> |
| 51 | + private static readonly string[] AuditScenes = |
| 52 | + { |
| 53 | + "start", "none", "wiki-colorby-type", "wiki-zoomed", "insights", "changes", "wiki-changes-everything", "drawer", "wiki-rail", "wiki-filtered", |
| 54 | + "wiki-sidepanel-changes", "inspect", "wiki-inspect-selection", "settings", "wiki-settings-general", "wiki-settings-behavior", "wiki-settings-appearance", |
| 55 | + "wiki-settings-text", "wiki-settings-map", "wiki-settings-scanning", "wiki-shortcuts", "about", "update", "wiki-palette-editor", "light" |
| 56 | + }; |
| 57 | + |
| 58 | + private static readonly (int Width, int Height)[] AuditSizes = { (1600, 900), (1040, 620) }; |
| 59 | + |
| 60 | + /// <summary>Poses every scene in both directions and both sizes, audits each, writes one report and exits; the exit code is the number of distinct findings, capped at 250.</summary> |
| 61 | + private async Task RunAuditAsync(string file) |
| 62 | + { |
| 63 | + AppSettings.Frozen = true; |
| 64 | + if (file.Length == 0) file = Path.Combine(CrashLog.Folder, "layout-audit.txt"); |
| 65 | + var report = new System.Text.StringBuilder(); |
| 66 | + var distinct = new Dictionary<string, (LayoutAudit.Finding Finding, List<string> States)>(StringComparer.Ordinal); |
| 67 | + int states = 0; |
| 68 | + report.AppendLine($"SpaceSharp {AppInfo.Version} layout audit · {DateTime.Now:yyyy-MM-dd HH:mm} · language {System.Globalization.CultureInfo.CurrentUICulture.Name}"); |
| 69 | + foreach (bool rtl in new[] { false, true }) |
| 70 | + { |
| 71 | + AppLanguages.IsRightToLeft = rtl; |
| 72 | + FlowDirection = rtl ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; |
| 73 | + foreach (var (width, height) in AuditSizes) |
| 74 | + { |
| 75 | + Width = width; Height = height; |
| 76 | + await Settle(200); |
| 77 | + foreach (var scene in AuditScenes) |
| 78 | + { |
| 79 | + string state = $"{scene} · {(rtl ? "rtl" : "ltr")} · {width}×{height}"; |
| 80 | + Window? dialog = null; |
| 81 | + try |
| 82 | + { |
| 83 | + dialog = await PoseSceneAsync(scene); |
| 84 | + await Settle(350); |
| 85 | + var windows = new List<Window> { this }; windows.AddRange(OwnedWindows); |
| 86 | + var findings = windows.SelectMany(LayoutAudit.ClippedText).ToList(); |
| 87 | + states++; |
| 88 | + report.AppendLine().AppendLine(LayoutAudit.Report(findings, $"[{state}]")); |
| 89 | + foreach (var f in findings) |
| 90 | + { |
| 91 | + string key = f.Text + "|" + f.Where; |
| 92 | + if (!distinct.TryGetValue(key, out var entry)) distinct[key] = entry = (f, new List<string>()); |
| 93 | + entry.States.Add(state); |
| 94 | + } |
| 95 | + } |
| 96 | + catch (Exception ex) |
| 97 | + { |
| 98 | + report.AppendLine().AppendLine($"[{state}] could not be posed: {ex.GetType().Name}: {ex.Message}"); |
| 99 | + } |
| 100 | + finally |
| 101 | + { |
| 102 | + await UnposeSceneAsync(dialog); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // the summary first, since that is what gets read |
| 109 | + var summary = new System.Text.StringBuilder(); |
| 110 | + summary.AppendLine($"{states} states audited · {distinct.Count} distinct clipped label(s)"); |
| 111 | + foreach (var (f, where) in distinct.Values.OrderByDescending(v => v.States.Count).Select(v => (v.Finding, v.States))) |
| 112 | + summary.AppendLine($" \"{f.Text}\" needs {f.Needed:0} px, has {f.Given:0} px ({f.Where}) in {where.Count} state(s), e.g. {where[0]}"); |
| 113 | + string text = summary.ToString() + Environment.NewLine + "---- every state ----" + report; |
| 114 | + try |
| 115 | + { |
| 116 | + Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(file)) ?? "."); |
| 117 | + await File.WriteAllTextAsync(file, text); |
| 118 | + Console.WriteLine($"{summary} report: {file}"); |
| 119 | + } |
| 120 | + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) |
| 121 | + { |
| 122 | + Console.Error.WriteLine($"Could not write {file}: {ex.Message}"); |
| 123 | + Console.WriteLine(text); |
| 124 | + } |
| 125 | + Environment.ExitCode = Math.Min(250, distinct.Count); |
| 126 | + (Avalonia.Application.Current?.ApplicationLifetime as Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime)?.Shutdown(Environment.ExitCode); |
| 127 | + } |
| 128 | +} |
0 commit comments