From d4dced4f1f5c82da123b376eca06891302182a2c Mon Sep 17 00:00:00 2001 From: Alex <15199219+muskit@users.noreply.github.com> Date: Tue, 19 Aug 2025 22:10:16 -0700 Subject: [PATCH] add selected songs count display --- src/UI/MainWindow.axaml | 2 +- src/UI/MainWindow.axaml.cs | 13 ++++++++ src/UI/Views/Selection/Selection.axaml.cs | 16 ++++++++-- src/Utils/ObservableRangeCollection.cs | 38 +++++++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 src/Utils/ObservableRangeCollection.cs diff --git a/src/UI/MainWindow.axaml b/src/UI/MainWindow.axaml index d42e0f1..eb382ac 100644 --- a/src/UI/MainWindow.axaml +++ b/src/UI/MainWindow.axaml @@ -39,7 +39,7 @@ - + diff --git a/src/UI/MainWindow.axaml.cs b/src/UI/MainWindow.axaml.cs index 197ae9e..3c4da04 100644 --- a/src/UI/MainWindow.axaml.cs +++ b/src/UI/MainWindow.axaml.cs @@ -1,8 +1,10 @@ using System; +using System.Collections.Specialized; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Styling; using Avalonia.Threading; +using MercuryConverter.Data; using MercuryConverter.UI.Dialogs; using MercuryConverter.UI.Views; @@ -30,6 +32,9 @@ public partial class MainWindow : Window // Show dialog on startup Activated += OnActivated; + + Selection.selections.CollectionChanged += OnDbSelChanged; + Database.Songs.CollectionChanged += OnDbSelChanged; } private void OnActivated(object? sender, EventArgs e) @@ -41,6 +46,14 @@ public partial class MainWindow : Window Dialog.IsOpen = true; } + private void OnDbSelChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + if (Database.Songs.Count > 0) + { + TabSelection.Header = $"selection ({Selection.selections.Count}/{Database.Songs.Count})"; + } + } + public void OpenDataHandler() { Dialog.IsOpen = true; diff --git a/src/UI/Views/Selection/Selection.axaml.cs b/src/UI/Views/Selection/Selection.axaml.cs index bae2e43..4af7588 100644 --- a/src/UI/Views/Selection/Selection.axaml.cs +++ b/src/UI/Views/Selection/Selection.axaml.cs @@ -11,7 +11,7 @@ namespace MercuryConverter.UI.Views; public partial class Selection : Panel { - private List selections; + public static ObservableRangeCollection selections = new(); public Selection() { @@ -62,9 +62,19 @@ public partial class Selection : Panel private void OnSelectionChange(object? sender, SelectionChangedEventArgs e) { - if (e.AddedItems.Count > 0) + selections.Clear(); + List sels = new(); + foreach (Song s in ListingTable.SelectedItems) { - var song = (Song) e.AddedItems[e.AddedItems.Count-1]!; + sels.Add(s); + } + selections.AddRange(sels); + + if (ListingTable.SelectedItems.Count > 0) + { + Song song = e.AddedItems.Count > 0 ? + (Song) e.AddedItems[e.AddedItems.Count - 1]! : + (Song) ListingTable.SelectedItems[ListingTable.SelectedItems.Count - 1]!; Dispatcher.UIThread.Post(() => { diff --git a/src/Utils/ObservableRangeCollection.cs b/src/Utils/ObservableRangeCollection.cs new file mode 100644 index 0000000..6a98606 --- /dev/null +++ b/src/Utils/ObservableRangeCollection.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Collections.Specialized; + +public class ObservableRangeCollection : ObservableCollection +{ + private bool _suppressNotification = false; + + protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) + { + // Only raise the event if notifications are not suppressed + if (!_suppressNotification) + { + base.OnCollectionChanged(e); + } + } + + public void AddRange(IEnumerable collection) + { + if (collection == null) + { + throw new ArgumentNullException(nameof(collection)); + } + + _suppressNotification = true; // Suppress notifications during bulk add + + foreach (var item in collection) + { + Items.Add(item); // Add items using the underlying IList + } + + _suppressNotification = false; // Re-enable notifications + + // Raise a single Reset event to notify about the bulk change + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + } +} \ No newline at end of file