33using System ;
44using System . Collections . Generic ;
55using System . Collections . ObjectModel ;
6+ using System . Collections . Specialized ;
67using Windows . Media . Core ;
8+ using Windows . System ;
79using Windows . UI . Xaml ;
810using Windows . UI . Xaml . Controls ;
11+ using Microsoft . Toolkit . Uwp . UI ;
12+ using Screenbox . Core . Playback ;
913using Screenbox . ViewModels ;
1014
1115// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
@@ -66,8 +70,11 @@ public int ChapterIndex
6670
6771 private const double Spacing = 1 ;
6872
73+ private readonly DispatcherQueueTimer _chaptersUpdateTimer ;
74+
6975 public ChapterProgressBar ( )
7076 {
77+ _chaptersUpdateTimer = DispatcherQueue . GetForCurrentThread ( ) . CreateTimer ( ) ;
7178 ProgressItems = new ObservableCollection < ChapterViewModel > ( ) ;
7279 this . InitializeComponent ( ) ;
7380 SizeChanged += OnSizeChanged ;
@@ -76,7 +83,17 @@ public ChapterProgressBar()
7683 private static void OnChaptersChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
7784 {
7885 ChapterProgressBar view = ( ChapterProgressBar ) d ;
79- view . PopulateProgressItems ( ) ;
86+ if ( e . NewValue is PlaybackChapterList { Count : 0 } chapterList )
87+ {
88+ INotifyCollectionChanged observableCollection = chapterList ;
89+ observableCollection . CollectionChanged -= view . ChaptersOnCollectionChanged ;
90+ observableCollection . CollectionChanged += view . ChaptersOnCollectionChanged ;
91+ }
92+ else
93+ {
94+ view . PopulateProgressItems ( ) ;
95+ }
96+
8097 }
8198
8299 private static void OnValueChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
@@ -106,6 +123,11 @@ private void OnSizeChanged(object sender, SizeChangedEventArgs e)
106123 }
107124 }
108125
126+ private void ChaptersOnCollectionChanged ( object sender , NotifyCollectionChangedEventArgs e )
127+ {
128+ _chaptersUpdateTimer . Debounce ( PopulateProgressItems , TimeSpan . FromMilliseconds ( 50 ) ) ;
129+ }
130+
109131 private void UpdateProgress ( )
110132 {
111133 if ( ProgressItems . Count == 1 )
0 commit comments