DesktopWindowXamlSourceAccess

This commit is contained in:
Lightczx
2024-05-14 17:31:16 +08:00
parent d3acbcde24
commit 3ee729eacf
6 changed files with 83 additions and 21 deletions

View File

@@ -0,0 +1,50 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Microsoft.UI.Composition;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Media;
using System.Runtime.CompilerServices;
using WinRT;
namespace Snap.Hutao.Core.Windowing.Backdrop;
internal sealed class SystemBackdropDesktopWindowXamlSourceAccess : SystemBackdrop
{
private readonly SystemBackdrop? innerBackdrop;
public SystemBackdropDesktopWindowXamlSourceAccess(SystemBackdrop? systemBackdrop)
{
innerBackdrop = systemBackdrop;
}
public DesktopWindowXamlSource? DesktopWindowXamlSource
{
get; private set;
}
protected override void OnTargetConnected(ICompositionSupportsSystemBackdrop target, XamlRoot xamlRoot)
{
DesktopWindowXamlSource = DesktopWindowXamlSource.FromAbi(target.As<IInspectable>().ThisPtr);
if (innerBackdrop is not null)
{
ProtectedOnTargetConnected(innerBackdrop, target, xamlRoot);
}
}
protected override void OnTargetDisconnected(ICompositionSupportsSystemBackdrop target)
{
DesktopWindowXamlSource = null;
if (innerBackdrop is not null)
{
ProtectedOnTargetDisconnected(innerBackdrop, target);
}
}
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = nameof(OnTargetConnected))]
private static extern void ProtectedOnTargetConnected(SystemBackdrop systemBackdrop, ICompositionSupportsSystemBackdrop target, XamlRoot xamlRoot);
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = nameof(OnTargetDisconnected))]
private static extern void ProtectedOnTargetDisconnected(SystemBackdrop systemBackdrop, ICompositionSupportsSystemBackdrop target);
}

View File

@@ -9,9 +9,9 @@ using Windows.UI;
namespace Snap.Hutao.Core.Windowing.Backdrop;
internal sealed class TransparentBackdrop : SystemBackdrop, IDisposable, IBackdropNeedEraseBackground
internal sealed class TransparentBackdrop : SystemBackdrop, IBackdropNeedEraseBackground
{
private readonly object compositorLock = new();
private object? compositorLock;
private Color tintColor;
private Windows.UI.Composition.CompositionColorBrush? brush;
@@ -31,27 +31,14 @@ internal sealed class TransparentBackdrop : SystemBackdrop, IDisposable, IBackdr
{
get
{
if (compositor is null)
return LazyInitializer.EnsureInitialized(ref compositor, ref compositorLock, () =>
{
lock (compositorLock)
{
if (compositor is null)
{
DispatcherQueue.EnsureSystemDispatcherQueue();
compositor = new Windows.UI.Composition.Compositor();
}
}
}
return compositor;
DispatcherQueue.EnsureSystemDispatcherQueue();
return new Windows.UI.Composition.Compositor();
});
}
}
public void Dispose()
{
compositor?.Dispose();
}
protected override void OnTargetConnected(ICompositionSupportsSystemBackdrop connectedTarget, XamlRoot xamlRoot)
{
brush ??= Compositor.CreateColorBrush(tintColor);
@@ -61,5 +48,13 @@ internal sealed class TransparentBackdrop : SystemBackdrop, IDisposable, IBackdr
protected override void OnTargetDisconnected(ICompositionSupportsSystemBackdrop disconnectedTarget)
{
disconnectedTarget.SystemBackdrop = null;
if (compositorLock is not null)
{
lock (compositorLock)
{
compositor?.Dispose();
}
}
}
}

View File

@@ -2,6 +2,8 @@
// Licensed under the MIT license.
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Hosting;
using Snap.Hutao.Core.Windowing.Backdrop;
using Snap.Hutao.Win32.Foundation;
using Snap.Hutao.Win32.UI.WindowsAndMessaging;
using System.Runtime.CompilerServices;
@@ -47,6 +49,16 @@ internal static class WindowExtension
ShowWindow(GetWindowHandle(window), SHOW_WINDOW_CMD.SW_HIDE);
}
public static DesktopWindowXamlSource? GetDesktopWindowXamlSource(this Window window)
{
if (window.SystemBackdrop is SystemBackdropDesktopWindowXamlSourceAccess access)
{
return access.DesktopWindowXamlSource;
}
return default;
}
public static HWND GetWindowHandle(this Window? window)
{
return window is IXamlWindowOptionsSource optionsSource

View File

@@ -169,7 +169,7 @@ internal sealed class XamlWindowController
private bool UpdateSystemBackdrop(BackdropType backdropType)
{
window.SystemBackdrop = backdropType switch
SystemBackdrop? actualBackdop = backdropType switch
{
BackdropType.Transparent => new Backdrop.TransparentBackdrop(),
BackdropType.MicaAlt => new MicaBackdrop() { Kind = MicaKind.BaseAlt },
@@ -178,6 +178,8 @@ internal sealed class XamlWindowController
_ => null,
};
window.SystemBackdrop = new Backdrop.SystemBackdropDesktopWindowXamlSourceAccess(actualBackdop);
return true;
}

View File

@@ -29,6 +29,9 @@ internal sealed partial class MainWindow : Window, IXamlWindowOptionsSource, IMi
InitializeComponent();
windowOptions = new(this, TitleBarView.DragArea, new(1200, 741), SettingKeys.WindowRect);
this.InitializeController(serviceProvider);
var policy = this.GetDesktopWindowXamlSource()!.SiteBridge.ResizePolicy;
_ = 1;
}
/// <inheritdoc/>

View File

@@ -2,7 +2,7 @@
"profiles": {
"Snap.Hutao": {
"commandName": "MsixPackage",
"nativeDebugging": true,
"nativeDebugging": false,
"doNotLaunchApp": false,
"allowLocalNetworkLoopbackProperty": true
},