|
| 1 | +using Microsoft.UI.Composition; |
| 2 | +using Microsoft.UI.Composition.SystemBackdrops; |
| 3 | +using Microsoft.UI.Xaml; |
| 4 | +using Microsoft.UI.Xaml.Media; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics.CodeAnalysis; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace Files.App.Helpers |
| 13 | +{ |
| 14 | + internal sealed class AppSystemBackdrop : SystemBackdrop |
| 15 | + { |
| 16 | + private bool isSecondaryWindow; |
| 17 | + private IUserSettingsService userSettingsService; |
| 18 | + private ISystemBackdropControllerWithTargets? controller; |
| 19 | + private ICompositionSupportsSystemBackdrop target; |
| 20 | + private XamlRoot root; |
| 21 | + |
| 22 | + public AppSystemBackdrop(bool isSecondaryWindow = false) |
| 23 | + { |
| 24 | + this.isSecondaryWindow = isSecondaryWindow; |
| 25 | + userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>(); |
| 26 | + userSettingsService.OnSettingChangedEvent += OnSettingChanged; |
| 27 | + } |
| 28 | + |
| 29 | + [MemberNotNull(nameof(target), nameof(root))] |
| 30 | + protected override void OnTargetConnected(ICompositionSupportsSystemBackdrop connectedTarget, XamlRoot xamlRoot) |
| 31 | + { |
| 32 | + if (target is not null) |
| 33 | + throw new InvalidOperationException("AppSystemBackdrop cannot be used with more than one target"); |
| 34 | + |
| 35 | + base.OnTargetConnected(connectedTarget, xamlRoot); |
| 36 | + this.target = connectedTarget; |
| 37 | + this.root = xamlRoot; |
| 38 | + controller = GetSystemBackdropController(userSettingsService.AppearanceSettingsService.AppThemeBackdropMaterial); |
| 39 | + controller?.SetSystemBackdropConfiguration(GetDefaultSystemBackdropConfiguration(connectedTarget, xamlRoot)); |
| 40 | + controller?.AddSystemBackdropTarget(connectedTarget); |
| 41 | + } |
| 42 | + |
| 43 | + protected override void OnTargetDisconnected(ICompositionSupportsSystemBackdrop disconnectedTarget) |
| 44 | + { |
| 45 | + base.OnTargetDisconnected(disconnectedTarget); |
| 46 | + this.target = null!; |
| 47 | + this.root = null!; |
| 48 | + controller?.RemoveSystemBackdropTarget(disconnectedTarget); |
| 49 | + controller?.Dispose(); |
| 50 | + userSettingsService.OnSettingChangedEvent -= OnSettingChanged; |
| 51 | + } |
| 52 | + |
| 53 | + private void OnSettingChanged(object? sender, Shared.EventArguments.SettingChangedEventArgs e) |
| 54 | + { |
| 55 | + if (target is null) |
| 56 | + return; |
| 57 | + |
| 58 | + switch (e.SettingName) |
| 59 | + { |
| 60 | + case nameof(IAppearanceSettingsService.AppThemeBackdropMaterial): |
| 61 | + controller?.RemoveAllSystemBackdropTargets(); |
| 62 | + controller?.Dispose(); |
| 63 | + var newController = GetSystemBackdropController((BackdropMaterialType)e.NewValue!); |
| 64 | + newController?.SetSystemBackdropConfiguration(GetDefaultSystemBackdropConfiguration(target, root)); |
| 65 | + newController?.AddSystemBackdropTarget(target); |
| 66 | + controller = newController; |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private ISystemBackdropControllerWithTargets? GetSystemBackdropController(BackdropMaterialType backdropType) |
| 72 | + { |
| 73 | + if (isSecondaryWindow && backdropType == BackdropMaterialType.MicaAlt) |
| 74 | + backdropType = BackdropMaterialType.Mica; |
| 75 | + |
| 76 | + switch (backdropType) |
| 77 | + { |
| 78 | + case BackdropMaterialType.MicaAlt: |
| 79 | + return new MicaController() |
| 80 | + { |
| 81 | + Kind = MicaKind.BaseAlt |
| 82 | + }; |
| 83 | + |
| 84 | + case BackdropMaterialType.Mica: |
| 85 | + return new MicaController() |
| 86 | + { |
| 87 | + Kind = MicaKind.Base |
| 88 | + }; |
| 89 | + |
| 90 | + case BackdropMaterialType.Acrylic: |
| 91 | + return new DesktopAcrylicController(); |
| 92 | + |
| 93 | + default: |
| 94 | + return null; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments