Skip to content

Commit e13d6a7

Browse files
authored
CmdPal: Set image to surface immediately in BlurImageControl (#44222)
## Summary of the Pull Request This PR resolves issue when the background image is not loaded. When loading an image, BlurImageControl now sets the image to the surface immediately, as the LoadComplete handler is not guaranteed to be called. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #44221 <!-- - [ ] Closes: #yyy (add separate lines for additional resolved issues) --> - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed
1 parent 73786cd commit e13d6a7

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/BlurImageControl.cs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -368,32 +368,69 @@ private void LoadImageAsync(ImageSource imageSource)
368368
{
369369
try
370370
{
371-
if (imageSource is Microsoft.UI.Xaml.Media.Imaging.BitmapImage bitmapImage)
371+
if (imageSource is not Microsoft.UI.Xaml.Media.Imaging.BitmapImage bitmapImage)
372372
{
373-
_imageBrush ??= _compositor?.CreateSurfaceBrush();
374-
if (_imageBrush is null)
375-
{
376-
return;
377-
}
378-
379-
var loadedSurface = LoadedImageSurface.StartLoadFromUri(bitmapImage.UriSource);
380-
loadedSurface.LoadCompleted += (_, _) =>
381-
{
382-
if (_imageBrush is not null)
383-
{
384-
_imageBrush.Surface = loadedSurface;
385-
_imageBrush.Stretch = ConvertStretch(ImageStretch);
386-
_imageBrush.BitmapInterpolationMode = CompositionBitmapInterpolationMode.Linear;
387-
}
388-
};
373+
return;
374+
}
389375

390-
_effectBrush?.SetSourceParameter(ImageSourceParameterName, _imageBrush);
376+
_imageBrush ??= _compositor?.CreateSurfaceBrush();
377+
if (_imageBrush is null)
378+
{
379+
return;
391380
}
381+
382+
Logger.LogDebug($"Starting load of BlurImageControl from '{bitmapImage.UriSource}'");
383+
var loadedSurface = LoadedImageSurface.StartLoadFromUri(bitmapImage.UriSource);
384+
loadedSurface.LoadCompleted += OnLoadedSurfaceOnLoadCompleted;
385+
SetLoadedSurfaceToBrush(loadedSurface);
386+
_effectBrush?.SetSourceParameter(ImageSourceParameterName, _imageBrush);
392387
}
393388
catch (Exception ex)
394389
{
395390
Logger.LogError("Failed to load image for BlurImageControl: {0}", ex);
396391
}
392+
393+
return;
394+
395+
void OnLoadedSurfaceOnLoadCompleted(LoadedImageSurface loadedSurface, LoadedImageSourceLoadCompletedEventArgs e)
396+
{
397+
switch (e.Status)
398+
{
399+
case LoadedImageSourceLoadStatus.Success:
400+
Logger.LogDebug($"BlurImageControl loaded successfully: has _imageBrush? {_imageBrush != null}");
401+
402+
try
403+
{
404+
SetLoadedSurfaceToBrush(loadedSurface);
405+
}
406+
catch (Exception ex)
407+
{
408+
Logger.LogError("Failed to set surface in BlurImageControl", ex);
409+
throw;
410+
}
411+
412+
break;
413+
case LoadedImageSourceLoadStatus.NetworkError:
414+
case LoadedImageSourceLoadStatus.InvalidFormat:
415+
case LoadedImageSourceLoadStatus.Other:
416+
default:
417+
Logger.LogError($"Failed to load image for BlurImageControl: Load status {e.Status}");
418+
break;
419+
}
420+
}
421+
}
422+
423+
private void SetLoadedSurfaceToBrush(LoadedImageSurface loadedSurface)
424+
{
425+
var surfaceBrush = _imageBrush;
426+
if (surfaceBrush is null)
427+
{
428+
return;
429+
}
430+
431+
surfaceBrush.Surface = loadedSurface;
432+
surfaceBrush.Stretch = ConvertStretch(ImageStretch);
433+
surfaceBrush.BitmapInterpolationMode = CompositionBitmapInterpolationMode.Linear;
397434
}
398435

399436
private static CompositionStretch ConvertStretch(Stretch stretch)

0 commit comments

Comments
 (0)