using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Controls.Shapes; using Avalonia.Layout; using Avalonia.Markup.Xaml; using Avalonia.Media; using Avalonia.Platform.Storage; using Avalonia.Threading; namespace RynthCore.App.Avalonia; /// /// Live process-monitor for acclient.exe. Mirrors the columns and trend logic /// from scripts\Watch-AcMemory.ps1 and scripts\Show-AcMemoryTrend.ps1: /// all-time slope, recent slope (~75 s), STABLE/GROWING/SHRINKING status with /// text+arrow (mandatory — user is colorblind). /// /// Lifecycle: /// * polls the OS for acclient.exe PIDs every /// 2 s and refreshes the PID dropdown without disturbing the user's selection. /// * takes a sample at the user-chosen interval and /// pushes it to . Pause stops sampling but keeps the /// timer ticking so resume is instant. /// * When the watched process disappears, freezes /// the chart and shows the exit banner. User picks a new PID to restart. /// /// History buffer: full session in memory. 24h @ 5s = 17,280 samples ~= 700 KB. /// The zoom window selects what gets rendered, not what's stored. /// internal sealed partial class MemoryTabView : UserControl { // ---- Metric definitions ---- // Order here drives both the metric grid row layout and the legend. // Stroke palette is teal / amber / violet / pale-blue / pale-green; each // has a distinct DashPattern so colorblind users can still match line to // metric via the dash key in the legend. private static readonly MetricDef WsDef = new("WorkingSet", "MB", Brushes.MediumTurquoise, DashPattern.Solid, IsHandleMetric: false, IsVirtualMetric: false); private static readonly MetricDef PrivateDef = new("Private", "MB", Brushes.Khaki, DashPattern.Dash, IsHandleMetric: false, IsVirtualMetric: false); private static readonly MetricDef VirtualDef = new("Virtual", "MB", Brushes.LightCoral, DashPattern.LongDash, IsHandleMetric: false, IsVirtualMetric: true); private static readonly MetricDef HandlesDef = new("Handles", "", Brushes.LightSkyBlue, DashPattern.Dot, IsHandleMetric: true, IsVirtualMetric: false); private static readonly MetricDef ThreadsDef = new("Threads", "", Brushes.LightGreen, DashPattern.DashDot, IsHandleMetric: false, IsVirtualMetric: false); private static readonly MetricDef[] AllMetrics = new[] { WsDef, PrivateDef, VirtualDef, HandlesDef, ThreadsDef }; // ---- State ---- // Full history for the watched PID. Cleared when the user picks a different // PID (the old samples belonged to a different process). private readonly List _history = new(); // Refresh-rate options shown in the dropdown. Default 5 s matches the // PowerShell viewer. private static readonly int[] RefreshSecondsOptions = { 1, 2, 5, 10, 30 }; // Recent-trend slope window: last RecentTrendSeconds of samples. private const int RecentTrendSeconds = 75; private readonly DispatcherTimer _processListTimer = new() { Interval = TimeSpan.FromSeconds(2) }; private readonly DispatcherTimer _sampleTimer = new() { Interval = TimeSpan.FromSeconds(5) }; private int? _watchedPid; private DateTime? _watchedStartTimeLocal; private bool _isPaused; private bool _processHasExited; private DateTime? _processExitedAtLocal; private double _memoryThresholdMbPerMin = 0.5; private double _handlesThresholdPerHour = 1.0; private ZoomWindow _zoomWindow = ZoomWindow.FiveMin; // Per-metric "show in chart" checkbox state. private readonly Dictionary _showInChart = new(StringComparer.Ordinal); // Per-metric grid row controls so we can update text in place per tick. private readonly Dictionary _metricRows = new(StringComparer.Ordinal); // Internal flag set when we mutate PidComboBox.SelectedItem ourselves so // the SelectionChanged handler doesn't treat it as a user pick. private bool _suppressPidSelectionEvent; // Last bottom-up screen of available PIDs. Lets us reconcile the dropdown // without flickering it on every poll when nothing changed. private List _lastKnownPids = new(); // List of the four zoom buttons — keeps the active-style toggling // logic in one place. private List