using System.Collections; using System.Collections.Specialized; using Avalonia; using Avalonia.Controls; namespace ClaudeDo.Ui.Views.Islands; public partial class SessionTerminalView : UserControl { public static readonly StyledProperty EntriesProperty = AvaloniaProperty.Register(nameof(Entries)); public static readonly StyledProperty LabelProperty = AvaloniaProperty.Register(nameof(Label)); public static readonly StyledProperty IsRunningProperty = AvaloniaProperty.Register(nameof(IsRunning)); public static readonly StyledProperty IsDoneProperty = AvaloniaProperty.Register(nameof(IsDone)); public static readonly StyledProperty IsFailedProperty = AvaloniaProperty.Register(nameof(IsFailed)); public IEnumerable? Entries { get => GetValue(EntriesProperty); set => SetValue(EntriesProperty, value); } public string? Label { get => GetValue(LabelProperty); set => SetValue(LabelProperty, value); } public bool IsRunning { get => GetValue(IsRunningProperty); set => SetValue(IsRunningProperty, value); } public bool IsDone { get => GetValue(IsDoneProperty); set => SetValue(IsDoneProperty, value); } public bool IsFailed { get => GetValue(IsFailedProperty); set => SetValue(IsFailedProperty, value); } private INotifyCollectionChanged? _subscribedCollection; public SessionTerminalView() { InitializeComponent(); } protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { base.OnPropertyChanged(change); if (change.Property != EntriesProperty) return; if (_subscribedCollection is not null) _subscribedCollection.CollectionChanged -= OnEntriesChanged; _subscribedCollection = change.NewValue as INotifyCollectionChanged; if (_subscribedCollection is not null) _subscribedCollection.CollectionChanged += OnEntriesChanged; } private void OnEntriesChanged(object? sender, NotifyCollectionChangedEventArgs e) { if (e.Action != NotifyCollectionChangedAction.Add) return; EventHandler? handler = null; handler = (_, _) => { LogScroll.LayoutUpdated -= handler; LogScroll.ScrollToEnd(); }; LogScroll.LayoutUpdated += handler; } }