1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
- using System . Threading . Tasks ;
5
4
using Avalonia ;
6
- using Avalonia . Collections ;
7
5
using Avalonia . Controls ;
8
6
using Avalonia . Controls . Notifications ;
9
7
using Avalonia . Controls . Primitives ;
10
8
using Avalonia . Controls . Templates ;
11
9
using Avalonia . LogicalTree ;
12
10
using Avalonia . Rendering . Composition ;
13
- using Avalonia . Threading ;
14
11
using Hollow . Helpers ;
15
- using Hollow . Views . Controls . Toast ;
16
12
17
13
namespace Hollow . Views . Controls ;
18
14
@@ -81,27 +77,6 @@ public static void CloseDialog()
81
77
{
82
78
if ( _mainWindow != null ) CloseDialog ( _mainWindow ) ;
83
79
}
84
-
85
-
86
- // Toasts
87
- public static readonly AttachedProperty < int > ToastLimitProperty =
88
- AvaloniaProperty . RegisterAttached < HollowHost , Window , int > ( "ToastLimit" , defaultValue : 5 ) ;
89
-
90
- public static int GetToastLimit ( Control element ) => element . GetValue ( ToastLimitProperty ) ;
91
-
92
- public static void SetToastLimit ( Control element , int value ) =>
93
- element . SetValue ( ToastLimitProperty , value ) ;
94
-
95
- public static readonly StyledProperty < AvaloniaList < Toast . Toast > ? > ToastsCollectionProperty =
96
- AvaloniaProperty . Register < HollowHost , AvaloniaList < Toast . Toast > ? > ( nameof ( ToastsCollection ) ) ;
97
-
98
- public AvaloniaList < Toast . Toast > ? ToastsCollection
99
- {
100
- get => GetValue ( ToastsCollectionProperty ) ;
101
- set => SetValue ( ToastsCollectionProperty , value ) ;
102
- }
103
-
104
- private int _maxToasts ;
105
80
106
81
// Common
107
82
private static Window ? _mainWindow ;
@@ -118,99 +93,21 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
118
93
protected override void OnApplyTemplate ( TemplateAppliedEventArgs e )
119
94
{
120
95
base . OnApplyTemplate ( e ) ;
121
- if ( VisualRoot is not Window window )
96
+ if ( VisualRoot is not Window )
122
97
throw new InvalidOperationException ( "HollowHost must be hosted inside a Window" ) ;
123
- ToastsCollection ??= [ ] ;
124
- _maxToasts = GetToastLimit ( window ) ;
125
98
126
99
e . NameScope . Get < Border > ( "PART_DialogBackground" ) . PointerPressed += ( _ , _ ) => BackgroundRequestClose ( this ) ;
127
100
128
101
var b = e . NameScope . Get < Border > ( "PART_DialogBackground" ) ;
129
102
b . Loaded += ( _ , _ ) => ControlAnimationHelper . MakeOpacityAnimate ( ElementComposition . GetElementVisual ( b ) ! , 400 ) ;
130
103
}
131
-
132
- public static async Task ShowToast ( Window window , ToastModel model )
133
- {
134
- try
135
- {
136
- if ( ! Instances . TryGetValue ( window , out var host ) )
137
- throw new InvalidOperationException ( "No HollowHost present in this window" ) ;
138
-
139
- var toast = ToastPool . Get ( ) ;
140
- toast . Initialize ( model , host ) ;
141
- if ( host . ToastsCollection ! . Count >= host . _maxToasts )
142
- await ClearToast ( host . ToastsCollection . First ( ) ) ;
143
- Dispatcher . UIThread . Invoke ( ( ) =>
144
- {
145
- host . ToastsCollection . Add ( toast ) ;
146
- toast . Animate ( OpacityProperty , 0d , 1d , TimeSpan . FromMilliseconds ( 500 ) ) ;
147
- toast . Animate ( MarginProperty , new Thickness ( 0 , 10 , 0 , - 10 ) , new Thickness ( ) ,
148
- TimeSpan . FromMilliseconds ( 500 ) ) ;
149
- } ) ;
150
- }
151
- catch
152
- {
153
- // ignored
154
- }
155
- }
156
- public static Task ShowToast ( ToastModel model ) =>
157
- ShowToast ( _mainWindow ! , model ) ;
158
-
159
- public static Task ShowToast ( string title , string ? content , NotificationType ? type , TimeSpan ? duration = null , Action ? onClicked = null ) =>
160
- ShowToast ( new ToastModel (
161
- title ,
162
- content ?? "" ,
163
- type ?? NotificationType . Information ,
164
- duration ?? TimeSpan . FromSeconds ( 4 ) ,
165
- onClicked ) ) ;
166
-
167
- public static Task ShowToast ( Window window , string title , string ? content , NotificationType ? type , TimeSpan ? duration = null ,
168
- Action ? onClicked = null ) =>
169
- ShowToast ( window , new ToastModel (
170
- title ,
171
- content ?? "" ,
172
- type ?? NotificationType . Information ,
173
- duration ?? TimeSpan . FromSeconds ( 4 ) ,
174
- onClicked ) ) ;
175
-
176
- public static async Task ClearToast ( Toast . Toast toast )
177
- {
178
- var wasRemoved = await Task . Run ( async ( ) =>
179
- {
180
- Dispatcher . UIThread . Invoke ( ( ) =>
181
- {
182
- toast . Animate ( OpacityProperty , 1d , 0d , TimeSpan . FromMilliseconds ( 300 ) ) ;
183
- toast . Animate ( MarginProperty , new Thickness ( ) , new Thickness ( 0 , 20 , 0 , - 20 ) ,
184
- TimeSpan . FromMilliseconds ( 300 ) ) ;
185
- } ) ;
186
- await Task . Delay ( 300 ) ;
187
- return Dispatcher . UIThread . Invoke ( ( ) => toast . Host . ToastsCollection ! . Remove ( toast ) ) ;
188
- } ) ;
189
-
190
- if ( ! wasRemoved ) return ;
191
- ToastPool . Return ( toast ) ;
192
- }
193
-
194
- public static void ClearAllToasts ( Window window )
195
- {
196
- if ( ! Instances . TryGetValue ( window , out var host ) )
197
- throw new InvalidOperationException ( "No HollowHost present in this window" ) ;
198
- ToastPool . Return ( host . ToastsCollection ! ) ;
199
- Dispatcher . UIThread . Invoke ( ( ) => host . ToastsCollection ! . Clear ( ) ) ;
200
- }
201
-
202
- public static void ClearAllToasts ( ) => ClearAllToasts ( _mainWindow ! ) ;
203
-
204
- #region New Toasts
205
104
206
105
public static WindowNotificationManager NotificationManager { get ; set ; } = null ! ;
207
- public static void ShowAvaloniaToast ( string title , string message , NotificationType notificationType , TimeSpan ? timeSpan = null , Action ? onClick = null , Action ? onClose = null )
106
+ public static void ShowToast ( string title , string message , NotificationType notificationType , TimeSpan ? timeSpan = null , Action ? onClick = null , Action ? onClose = null )
208
107
{
209
108
NotificationManager . Show ( new Notification ( title , message , notificationType , timeSpan , onClick , onClose ) ) ;
210
109
}
211
110
212
- #endregion
213
-
214
111
protected override void OnDetachedFromLogicalTree ( LogicalTreeAttachmentEventArgs e )
215
112
{
216
113
base . OnDetachedFromLogicalTree ( e ) ;
0 commit comments