Skip to content

Commit c795771

Browse files
authored
Code Quality: Fixed an issue where backdrop was not active when resuming from background (#16089)
1 parent 2b4a76d commit c795771

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

src/Files.App/App.xaml.cs

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ async Task ActivateAsync()
159159
}
160160

161161
await AppLifecycleHelper.InitializeAppComponentsAsync();
162+
163+
AppLifecycleHelper.IsLaunchInitialized = true;
162164
}
163165
}
164166

src/Files.App/Data/Items/WindowEx.cs

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
using Microsoft.UI.Composition.SystemBackdrops;
45
using Microsoft.UI.Windowing;
56
using Microsoft.UI.Xaml;
67
using System.Runtime.InteropServices;
@@ -17,7 +18,7 @@ namespace Files.App.Data.Items
1718
/// <summary>
1819
/// Represents base <see cref="Window"/> class to extend its features.
1920
/// </summary>
20-
public unsafe class WindowEx : Window
21+
public unsafe class WindowEx : Window, IDisposable
2122
{
2223
private bool _isInitialized;
2324
private readonly WNDPROC _oldWndProc;
@@ -104,7 +105,8 @@ public unsafe WindowEx(int minWidth = 400, int minHeight = 300)
104105
var pOldWndProc = PInvoke.SetWindowLongPtr(new(WindowHandle), WINDOW_LONG_PTR_INDEX.GWL_WNDPROC, pNewWndProc);
105106
_oldWndProc = Marshal.GetDelegateForFunctionPointer<WNDPROC>(pOldWndProc);
106107

107-
Closed += (s, e) => { StoreWindowPlacementData(); };
108+
Closed += WindowEx_Closed;
109+
Activated += WindowEx_Activated; ;
108110
}
109111

110112
private unsafe void StoreWindowPlacementData()
@@ -290,5 +292,22 @@ private LRESULT NewWindowProc(HWND param0, uint param1, WPARAM param2, LPARAM pa
290292

291293
return PInvoke.CallWindowProc(pfnWndProc, param0, param1, param2, param3);
292294
}
295+
296+
private void WindowEx_Closed(object sender, WindowEventArgs args)
297+
{
298+
StoreWindowPlacementData();
299+
}
300+
301+
private void WindowEx_Activated(object sender, WindowActivatedEventArgs args)
302+
{
303+
if (AppLifecycleHelper.IsLaunchInitialized && SystemBackdrop is AppSystemBackdrop appSystemBackdrop)
304+
appSystemBackdrop.SystemBackdropConfiguration.IsInputActive = args.WindowActivationState is not WindowActivationState.Deactivated;
305+
}
306+
307+
public void Dispose()
308+
{
309+
Closed -= WindowEx_Closed;
310+
Activated -= WindowEx_Activated;
311+
}
293312
}
294313
}

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ public static class AppLifecycleHelper
5858
_ => Constants.AssetPaths.StableLogo
5959
});
6060

61+
/// <summary>
62+
/// Gets or sets a value that indicates whether the application is ready to be interacted with.
63+
/// This is primarily used for DI container initialization check.
64+
/// </summary>
65+
public static bool IsLaunchInitialized { get; set; }
66+
6167
/// <summary>
6268
/// Initializes the app components.
6369
/// </summary>

src/Files.App/Helpers/UI/AppSystemBackdrop.cs

+12-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ internal sealed class AppSystemBackdrop : SystemBackdrop
1616
private XamlRoot root;
1717
private SystemBackdropTheme? prevTheme = null;
1818

19+
public SystemBackdropConfiguration SystemBackdropConfiguration { get; set; }
20+
1921
public AppSystemBackdrop(bool isSecondaryWindow = false)
2022
{
2123
this.isSecondaryWindow = isSecondaryWindow;
@@ -32,21 +34,21 @@ protected override void OnTargetConnected(ICompositionSupportsSystemBackdrop con
3234
base.OnTargetConnected(connectedTarget, xamlRoot);
3335
this.target = connectedTarget;
3436
this.root = xamlRoot;
35-
var configuration = GetDefaultSystemBackdropConfiguration(connectedTarget, xamlRoot);
36-
controller = GetSystemBackdropController(userSettingsService.AppearanceSettingsService.AppThemeBackdropMaterial, configuration.Theme);
37-
controller?.SetSystemBackdropConfiguration(configuration);
37+
SystemBackdropConfiguration = GetDefaultSystemBackdropConfiguration(connectedTarget, xamlRoot);
38+
controller = GetSystemBackdropController(userSettingsService.AppearanceSettingsService.AppThemeBackdropMaterial, SystemBackdropConfiguration.Theme);
39+
controller?.SetSystemBackdropConfiguration(SystemBackdropConfiguration);
3840
controller?.AddSystemBackdropTarget(connectedTarget);
3941
}
4042

4143
protected override void OnDefaultSystemBackdropConfigurationChanged(ICompositionSupportsSystemBackdrop target, XamlRoot xamlRoot)
4244
{
4345
base.OnDefaultSystemBackdropConfigurationChanged(target, xamlRoot);
44-
var configuration = GetDefaultSystemBackdropConfiguration(target, xamlRoot);
45-
if (controller is not DesktopAcrylicController acrylicController || acrylicController.Kind != DesktopAcrylicKind.Thin || configuration.Theme == prevTheme)
46+
SystemBackdropConfiguration = GetDefaultSystemBackdropConfiguration(target, xamlRoot);
47+
if (controller is not DesktopAcrylicController acrylicController || acrylicController.Kind != DesktopAcrylicKind.Thin || SystemBackdropConfiguration.Theme == prevTheme)
4648
return;
4749

48-
prevTheme = configuration.Theme;
49-
SetThinAcrylicBackdropProperties(acrylicController, configuration.Theme);
50+
prevTheme = SystemBackdropConfiguration.Theme;
51+
SetThinAcrylicBackdropProperties(acrylicController, SystemBackdropConfiguration.Theme);
5052
}
5153

5254
protected override void OnTargetDisconnected(ICompositionSupportsSystemBackdrop disconnectedTarget)
@@ -79,9 +81,9 @@ private void OnSettingChanged(object? sender, SettingChangedEventArgs e)
7981
case nameof(IAppearanceSettingsService.AppThemeBackdropMaterial):
8082
controller?.RemoveAllSystemBackdropTargets();
8183
controller?.Dispose();
82-
var configuration = GetDefaultSystemBackdropConfiguration(target, root);
83-
var newController = GetSystemBackdropController((BackdropMaterialType)e.NewValue!, configuration.Theme);
84-
newController?.SetSystemBackdropConfiguration(configuration);
84+
SystemBackdropConfiguration = GetDefaultSystemBackdropConfiguration(target, root);
85+
var newController = GetSystemBackdropController((BackdropMaterialType)e.NewValue!, SystemBackdropConfiguration.Theme);
86+
newController?.SetSystemBackdropConfiguration(SystemBackdropConfiguration);
8587
newController?.AddSystemBackdropTarget(target);
8688
controller = newController;
8789
break;

0 commit comments

Comments
 (0)