8
8
using Avalonia . Media . Imaging ;
9
9
using Avalonia . Platform ;
10
10
11
- namespace AsyncImageLoader ;
11
+ namespace AsyncImageLoader ;
12
12
13
13
public class AdvancedImage : ContentControl
14
14
{
@@ -177,16 +177,18 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
177
177
base . OnPropertyChanged ( change ) ;
178
178
}
179
179
180
- private void ClearSourceIfUserProvideImage ( ) {
181
- if ( CurrentImage is not null and not ImageWrapper ) {
180
+ private void ClearSourceIfUserProvideImage ( )
181
+ {
182
+ if ( CurrentImage is not null and not ImageWrapper )
183
+ {
182
184
// User provided image himself
183
185
Source = null ;
184
186
}
185
187
}
186
188
187
189
private async void UpdateImage ( string ? source , IAsyncImageLoader ? loader )
188
190
{
189
- var cancellationTokenSource = new CancellationTokenSource ( ) ;
191
+ var cancellationTokenSource = new CancellationTokenSource ( ) ;
190
192
191
193
var oldCancellationToken = Interlocked . Exchange ( ref _updateCancellationToken , cancellationTokenSource ) ;
192
194
@@ -197,59 +199,59 @@ private async void UpdateImage(string? source, IAsyncImageLoader? loader)
197
199
catch ( ObjectDisposedException )
198
200
{
199
201
}
200
-
201
- if ( source is null && CurrentImage is not ImageWrapper ) {
202
+
203
+ if ( source is null && CurrentImage is not ImageWrapper )
204
+ {
202
205
// User provided image himself
203
206
return ;
204
207
}
205
-
206
- IsLoading = true ;
207
- CurrentImage = null ;
208
-
209
-
210
- var bitmap = await Task . Run ( async ( ) =>
211
- {
212
- try
213
- {
214
- if ( source == null )
215
- return null ;
216
-
217
- // A small delay allows to cancel early if the image goes out of screen too fast (eg. scrolling)
218
- // The Bitmap constructor is expensive and cannot be cancelled
219
- await Task . Delay ( 10 , cancellationTokenSource . Token ) ;
220
-
221
- // Hack to support relative URI
222
- // TODO: Refactor IAsyncImageLoader to support BaseUri
223
- try
224
- {
225
- var uri = new Uri ( source , UriKind . RelativeOrAbsolute ) ;
226
- if ( AssetLoader . Exists ( uri , _baseUri ) )
208
+
209
+ IsLoading = true ;
210
+ CurrentImage = null ;
211
+
212
+
213
+ var bitmap = await Task . Run ( async ( ) =>
214
+ {
215
+ try
216
+ {
217
+ if ( source == null )
218
+ return null ;
219
+
220
+ // A small delay allows to cancel early if the image goes out of screen too fast (eg. scrolling)
221
+ // The Bitmap constructor is expensive and cannot be cancelled
222
+ await Task . Delay ( 10 , cancellationTokenSource . Token ) ;
223
+
224
+ // Hack to support relative URI
225
+ // TODO: Refactor IAsyncImageLoader to support BaseUri
226
+ try
227
+ {
228
+ var uri = new Uri ( source , UriKind . RelativeOrAbsolute ) ;
229
+ if ( AssetLoader . Exists ( uri , _baseUri ) )
227
230
return new Bitmap ( AssetLoader . Open ( uri , _baseUri ) ) ;
228
- }
229
- catch ( Exception )
230
- {
231
- // ignored
232
- }
233
-
234
- loader ??= ImageLoader . AsyncImageLoader ;
235
- return await loader . ProvideImageAsync ( source ) ;
236
- }
237
- catch ( TaskCanceledException )
238
- {
239
- return null ;
240
- }
241
- finally
242
- {
243
- cancellationTokenSource . Dispose ( ) ;
244
- }
245
-
246
- } , CancellationToken . None ) ;
247
-
248
- if ( cancellationTokenSource . IsCancellationRequested )
231
+ }
232
+ catch ( Exception )
233
+ {
234
+ // ignored
235
+ }
236
+
237
+ loader ??= ImageLoader . AsyncImageLoader ;
238
+ return await loader . ProvideImageAsync ( source ) ;
239
+ }
240
+ catch ( TaskCanceledException )
241
+ {
242
+ return null ;
243
+ }
244
+ finally
245
+ {
246
+ cancellationTokenSource . Dispose ( ) ;
247
+ }
248
+ } , CancellationToken . None ) ;
249
+
250
+ if ( cancellationTokenSource . IsCancellationRequested )
249
251
return ;
250
252
CurrentImage = bitmap is null ? null : new ImageWrapper ( bitmap ) ;
251
253
IsLoading = false ;
252
- }
254
+ }
253
255
254
256
private void UpdateCornerRadius ( CornerRadius radius )
255
257
{
@@ -308,18 +310,23 @@ protected override Size ArrangeOverride(Size finalSize)
308
310
? Stretch . CalculateSize ( finalSize , CurrentImage . Size )
309
311
: base . ArrangeOverride ( finalSize ) ;
310
312
}
311
-
312
- public sealed class ImageWrapper : IImage {
313
+
314
+ public sealed class ImageWrapper : IImage
315
+ {
313
316
public IImage ImageImplementation { get ; }
314
- internal ImageWrapper ( IImage imageImplementation ) {
317
+
318
+ internal ImageWrapper ( IImage imageImplementation )
319
+ {
315
320
ImageImplementation = imageImplementation ;
316
321
}
322
+
317
323
/// <inheritdoc />
318
- public void Draw ( DrawingContext context , Rect sourceRect , Rect destRect ) {
324
+ public void Draw ( DrawingContext context , Rect sourceRect , Rect destRect )
325
+ {
319
326
ImageImplementation . Draw ( context , sourceRect , destRect ) ;
320
327
}
321
328
322
329
/// <inheritdoc />
323
330
public Size Size => ImageImplementation . Size ;
324
331
}
325
- }
332
+ }
0 commit comments