1+ using System . Collections . ObjectModel ;
12using System . Collections . Specialized ;
23using Avalonia ;
34using Avalonia . Automation ;
@@ -63,12 +64,24 @@ public partial class MainWindowViewModel : ViewModelBase
6364 public AvaloniaList < OperationViewModel > Operations => AvaloniaOperationRegistry . OperationViewModels ;
6465
6566 [ ObservableProperty ]
67+ [ NotifyPropertyChangedFor ( nameof ( ShowFailedOperationBadge ) ) ]
6668 private bool _operationsPanelVisible ;
6769
6870 [ ObservableProperty ]
71+ [ NotifyPropertyChangedFor ( nameof ( ShowFailedOperationBadge ) ) ]
6972 private bool _operationsPanelExpanded = true ;
7073
7174 private readonly List < AbstractOperation > _operationBatch = new ( ) ;
75+ private bool _batchSummaryShown ;
76+
77+ public bool HasFailedOperations => Operations . Any ( o => o . Operation . Status is OperationStatus . Failed ) ;
78+
79+ public OperationViewModel ? FirstFailedOperation =>
80+ Operations . FirstOrDefault ( o => o . Operation . Status is OperationStatus . Failed ) ;
81+
82+ // Red badge on the chevron when the panel is collapsed and an operation failed
83+ // (failures no longer pop a toast; expanded, the failure is already visible).
84+ public bool ShowFailedOperationBadge => OperationsPanelVisible && ! OperationsPanelExpanded && HasFailedOperations ;
7285
7386 public string OperationsHeaderText
7487 {
@@ -91,6 +104,7 @@ private void AddToBatch(AbstractOperation op)
91104 foreach ( var old in _operationBatch )
92105 old . StatusChanged -= OnOperationStatusChanged ;
93106 _operationBatch . Clear ( ) ;
107+ _batchSummaryShown = false ;
94108 }
95109
96110 if ( ! _operationBatch . Contains ( op ) )
@@ -101,7 +115,42 @@ private void AddToBatch(AbstractOperation op)
101115 }
102116
103117 private void OnOperationStatusChanged ( object ? sender , OperationStatus e )
104- => Dispatcher . UIThread . Post ( ( ) => OnPropertyChanged ( nameof ( OperationsHeaderText ) ) ) ;
118+ => Dispatcher . UIThread . Post ( ( ) =>
119+ {
120+ OnPropertyChanged ( nameof ( OperationsHeaderText ) ) ;
121+ OnPropertyChanged ( nameof ( HasFailedOperations ) ) ;
122+ OnPropertyChanged ( nameof ( ShowFailedOperationBadge ) ) ;
123+ MaybeShowBatchSummaryToast ( ) ;
124+ } ) ;
125+
126+ // Opt-in: one toast summarizing the batch once every operation in it has finished.
127+ private void MaybeShowBatchSummaryToast ( )
128+ {
129+ if ( _batchSummaryShown || _operationBatch . Count == 0 ) return ;
130+ if ( ! Settings . Get ( Settings . K . ShowOperationSummaryNotifications ) ) return ;
131+ if ( _operationBatch . Any ( o => o . Status is OperationStatus . InQueue or OperationStatus . Running ) ) return ;
132+
133+ _batchSummaryShown = true ;
134+
135+ int total = _operationBatch . Count ;
136+ int failed = _operationBatch . Count ( o => o . Status is OperationStatus . Failed ) ;
137+ int succeeded = _operationBatch . Count ( o => o . Status is OperationStatus . Succeeded ) ;
138+
139+ var toast = new InfoBarViewModel
140+ {
141+ Title = failed > 0
142+ ? CoreTools . Translate ( "Operations finished with errors" )
143+ : CoreTools . Translate ( "Operations completed" ) ,
144+ Message = failed > 0
145+ ? CoreTools . Translate ( "{0} of {1} succeeded, {2} failed" , succeeded , total , failed )
146+ : CoreTools . Translate ( "{0} of {1} operations completed" , succeeded , total ) ,
147+ Severity = failed > 0 ? InfoBarSeverity . Error : InfoBarSeverity . Success ,
148+ IsClosable = true ,
149+ IsOpen = true ,
150+ } ;
151+ toast . OnClosed = ( ) => DismissToast ( toast ) ;
152+ ShowToast ( toast ) ;
153+ }
105154
106155 [ RelayCommand ]
107156 private void ToggleOperationsPanel ( ) => OperationsPanelExpanded = ! OperationsPanelExpanded ;
@@ -169,12 +218,126 @@ private void OnPageViewModelPropertyChanged(object? sender, System.ComponentMode
169218 ? $ "UniGetUI { CoreTools . Translate ( "version {0}" , CoreData . VersionName ) } "
170219 : "UniGetUI" ;
171220
172- // ─── Banners ─────────────────────────────────────────────────────────────
221+ // ─── Notifications (bottom-right toasts) ───────────────────────────────────
222+ // Persistent banners kept as singletons; RegisterBannerToast mirrors their IsOpen into Toasts.
173223 public InfoBarViewModel UpdatesBanner { get ; } = new ( ) { Severity = InfoBarSeverity . Success } ;
174- public InfoBarViewModel ErrorBanner { get ; } = new ( ) { Severity = InfoBarSeverity . Error } ;
175224 public InfoBarViewModel WinGetWarningBanner { get ; } = new ( ) { Severity = InfoBarSeverity . Warning } ;
176225 public InfoBarViewModel TelemetryWarner { get ; } = new ( ) { Severity = InfoBarSeverity . Informational } ;
177226
227+ // Oldest first (rendered bottom-up so the newest sits nearest the corner).
228+ public ObservableCollection < InfoBarViewModel > Toasts { get ; } = new ( ) ;
229+ private readonly Dictionary < InfoBarViewModel , DispatcherTimer > _toastTimers = new ( ) ;
230+ private const int MaxVisibleToasts = 5 ;
231+
232+ // A toast with an action button stays until acted on; everything else auto-dismisses.
233+ private static bool IsSticky ( InfoBarViewModel t ) => ! string . IsNullOrEmpty ( t . ActionButtonText ) ;
234+ private static TimeSpan ToastDuration ( InfoBarViewModel t )
235+ => t . Severity is InfoBarSeverity . Error or InfoBarSeverity . Warning
236+ ? TimeSpan . FromSeconds ( 8 )
237+ : TimeSpan . FromSeconds ( 5 ) ;
238+
239+ public void ShowToast ( InfoBarViewModel toast )
240+ {
241+ if ( ! Dispatcher . UIThread . CheckAccess ( ) )
242+ {
243+ Dispatcher . UIThread . Post ( ( ) => ShowToast ( toast ) ) ;
244+ return ;
245+ }
246+ if ( ! Toasts . Contains ( toast ) )
247+ {
248+ Toasts . Add ( toast ) ;
249+ AnnounceToast ( toast ) ;
250+ }
251+ TrimToastStack ( keep : toast ) ;
252+ ArmToastTimer ( toast ) ;
253+ }
254+
255+ // Keep the stack from overflowing the screen: drop the oldest auto-dismissing toasts,
256+ // but never a sticky (action-button) one or the toast just shown.
257+ private void TrimToastStack ( InfoBarViewModel keep )
258+ {
259+ while ( Toasts . Count > MaxVisibleToasts )
260+ {
261+ var oldest = Toasts . FirstOrDefault ( t => t != keep && ! IsSticky ( t ) ) ;
262+ if ( oldest is null ) break ;
263+ DismissToast ( oldest ) ;
264+ }
265+ }
266+
267+ // Surface the toast to screen readers via the live region (assertive for errors so it
268+ // interrupts, polite otherwise) — the toast's own automation name isn't announced on its own.
269+ private static void AnnounceToast ( InfoBarViewModel toast )
270+ {
271+ string message = string . IsNullOrEmpty ( toast . Message )
272+ ? toast . Title
273+ : $ "{ toast . Title } . { toast . Message } ";
274+ AccessibilityAnnouncementService . Announce (
275+ message ,
276+ toast . Severity is InfoBarSeverity . Error
277+ ? AutomationLiveSetting . Assertive
278+ : AutomationLiveSetting . Polite ) ;
279+ }
280+
281+ public void DismissToast ( InfoBarViewModel toast )
282+ {
283+ if ( ! Dispatcher . UIThread . CheckAccess ( ) )
284+ {
285+ Dispatcher . UIThread . Post ( ( ) => DismissToast ( toast ) ) ;
286+ return ;
287+ }
288+ DisposeToastTimer ( toast ) ;
289+ Toasts . Remove ( toast ) ;
290+ }
291+
292+ // Pause the auto-dismiss countdown while the pointer hovers a toast; restart it on leave.
293+ public void PauseToast ( InfoBarViewModel toast )
294+ {
295+ if ( _toastTimers . TryGetValue ( toast , out var timer ) ) timer . Stop ( ) ;
296+ }
297+
298+ public void ResumeToast ( InfoBarViewModel toast )
299+ {
300+ if ( _toastTimers . TryGetValue ( toast , out var timer ) ) { timer . Stop ( ) ; timer . Start ( ) ; }
301+ }
302+
303+ private void ArmToastTimer ( InfoBarViewModel toast )
304+ {
305+ DisposeToastTimer ( toast ) ;
306+ if ( IsSticky ( toast ) )
307+ return ;
308+
309+ var timer = new DispatcherTimer { Interval = ToastDuration ( toast ) } ;
310+ timer . Tick += ( _ , _ ) => toast . IsOpen = false ;
311+ _toastTimers [ toast ] = timer ;
312+ timer . Start ( ) ;
313+ }
314+
315+ private void DisposeToastTimer ( InfoBarViewModel toast )
316+ {
317+ if ( _toastTimers . Remove ( toast , out var timer ) )
318+ timer . Stop ( ) ;
319+ }
320+
321+ // Mirrors a persistent banner's IsOpen into Toasts so code that just flips IsOpen keeps working.
322+ private void RegisterBannerToast ( InfoBarViewModel banner )
323+ {
324+ banner . PropertyChanged += ( _ , e ) =>
325+ {
326+ if ( e . PropertyName == nameof ( InfoBarViewModel . IsOpen ) )
327+ {
328+ if ( banner . IsOpen ) ShowToast ( banner ) ;
329+ else DismissToast ( banner ) ;
330+ }
331+ // Content can change in place while the banner stays open (the updater cycles
332+ // statuses on the same banner, finally adding the "Update now" action); re-arm so
333+ // stickiness/countdown track the new content instead of a stale timer dismissing it.
334+ else if ( banner . IsOpen && Toasts . Contains ( banner ) )
335+ {
336+ ArmToastTimer ( banner ) ;
337+ }
338+ } ;
339+ }
340+
178341 // ─── Constructor ─────────────────────────────────────────────────────────
179342 [ RelayCommand ]
180343 private void ToggleSidebar ( ) => Sidebar . IsPaneOpen = ! Sidebar . IsPaneOpen ;
@@ -183,6 +346,11 @@ public MainWindowViewModel()
183346 {
184347 AccessibilityAnnouncementService . AnnouncementRequested += OnAnnouncementRequested ;
185348
349+ // Wire before the blocks below flip the banners' IsOpen flags.
350+ RegisterBannerToast ( UpdatesBanner ) ;
351+ RegisterBannerToast ( WinGetWarningBanner ) ;
352+ RegisterBannerToast ( TelemetryWarner ) ;
353+
186354 DiscoverPage = new DiscoverSoftwarePage ( ) ;
187355 UpdatesPage = new SoftwareUpdatesPage ( ) ;
188356 InstalledPage = new InstalledPackagesPage ( ) ;
@@ -264,6 +432,8 @@ public MainWindowViewModel()
264432 AddToBatch ( vm . Operation ) ;
265433 OperationsPanelVisible = Operations . Count > 0 ;
266434 OnPropertyChanged ( nameof ( OperationsHeaderText ) ) ;
435+ OnPropertyChanged ( nameof ( HasFailedOperations ) ) ;
436+ OnPropertyChanged ( nameof ( ShowFailedOperationBadge ) ) ;
267437 } ;
268438
269439 if ( OperatingSystem . IsWindows ( ) && CoreTools . IsAdministrator ( ) && ! Settings . Get ( Settings . K . AlreadyWarnedAboutAdmin ) )
0 commit comments