using System.Collections.ObjectModel; using System.Windows.Input; using System.Windows.Threading; using SpdUp.Core; namespace SpdUp.ViewModels; /// /// Debug Log page – live scrolling log of every action the application performs. /// public sealed class DebugLogViewModel : ObservableObject { private const int MaxLines = 2000; private readonly Dispatcher _dispatcher; public ObservableCollection LogLines { get; } = new(); private string _filterText = string.Empty; public string FilterText { get => _filterText; set { if (SetProperty(ref _filterText, value)) OnPropertyChanged(nameof(FilteredLogLines)); } } public IEnumerable FilteredLogLines => string.IsNullOrWhiteSpace(_filterText) ? LogLines : LogLines.Where(l => l.Contains(_filterText, StringComparison.OrdinalIgnoreCase)); private bool _autoScroll = true; public bool AutoScroll { get => _autoScroll; set => SetProperty(ref _autoScroll, value); } public ICommand ClearCommand { get; } public ICommand CopyAllCommand { get; } public DebugLogViewModel() { _dispatcher = Dispatcher.CurrentDispatcher; ClearCommand = new RelayCommand(() => { LogLines.Clear(); OnPropertyChanged(nameof(FilteredLogLines)); }); CopyAllCommand = new RelayCommand(() => { try { var text = string.Join(Environment.NewLine, LogLines); System.Windows.Clipboard.SetText(text); } catch { } }); // Subscribe to all log events Logger.LogWritten += OnLogWritten; } private void OnLogWritten(string line) { _dispatcher.BeginInvoke(() => { LogLines.Add(line); while (LogLines.Count > MaxLines) LogLines.RemoveAt(0); OnPropertyChanged(nameof(FilteredLogLines)); }); } }