From b90e8d062c524a6a222048fccbe5cefeea59ece2 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Thu, 27 Jun 2024 23:34:21 +0800 Subject: [PATCH] refactor --- .../UnsafeRuntimeBehaviorTest.cs | 45 ++++ .../Core/Graphics/RectInt32Convert.cs | 29 +++ .../Core/Graphics/RectInt32Extension.cs | 11 + .../Snap.Hutao/Core/Graphics/RectInt32View.cs | 12 + .../Core/Graphics/SizeInt32Extension.cs | 7 + src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj | 229 ------------------ src/Snap.Hutao/Snap.Hutao/UI/ColorHelper.cs | 6 + .../UI/Shell/NotifyIconXamlHostWindow.cs | 3 +- .../UI/Windowing/AppWindowExtension.cs | 16 +- .../UI/Windowing/XamlWindowController.cs | 7 +- .../UI/Xaml/Control/Theme/KnownColors.cs | 11 +- .../UI/Xaml/Control/Theme/SystemColors.cs | 6 +- .../Int32ToGradientColorConverter.cs | 4 +- .../UInt32ToGradientColorConverter.cs | 4 +- .../UI/Xaml/View/Page/DailyNotePage.xaml | 21 +- .../UI/Xaml/View/Page/TestPage.xaml | 67 +++-- .../Snap.Hutao/UI/Xaml/View/TitleView.xaml | 2 +- .../Snap.Hutao/UI/Xaml/View/UserView.xaml | 62 +++-- .../View/Window/IdentifyMonitorWindow.xaml.cs | 4 +- .../AnnouncementWebView2ContentProvider.cs | 2 +- ...ource.cs => IJSBridgeUriSourceProvider.cs} | 2 +- .../WebView2/IWebView2ContentProvider.cs | 2 +- .../MiHoYoJSBridgeWebView2ContentProvider.cs | 80 ++++++ ....cs => StaticJSBridgeUriSourceProvider.cs} | 2 +- .../View/Window/WebView2/WebView2Window.xaml | 20 ++ .../Window/WebView2/WebView2Window.xaml.cs | 36 ++- .../Snap.Hutao/View/Control/WebViewer.xaml | 48 ---- .../Snap.Hutao/View/Control/WebViewer.xaml.cs | 163 ------------- .../ViewModel/DailyNote/DailyNoteViewModel.cs | 2 +- .../DailyNote/DailyNoteWebViewerSource.cs | 2 +- ....cs => SignInJSBridgeUriSourceProvider.cs} | 6 +- .../Snap.Hutao/Web/Bridge/SignInJSBridge.cs | 4 +- .../Web/Bridge/SignInJSBridgeOversea.cs | 4 +- .../Snap.Hutao/Win32/StructMarshal.cs | 70 ------ 34 files changed, 360 insertions(+), 629 deletions(-) create mode 100644 src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32Convert.cs create mode 100644 src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32View.cs rename src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/{IJSBridgeUriSource.cs => IJSBridgeUriSourceProvider.cs} (89%) create mode 100644 src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/MiHoYoJSBridgeWebView2ContentProvider.cs rename src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/{StaticWebview2ViewerSource.cs => StaticJSBridgeUriSourceProvider.cs} (88%) delete mode 100644 src/Snap.Hutao/Snap.Hutao/View/Control/WebViewer.xaml delete mode 100644 src/Snap.Hutao/Snap.Hutao/View/Control/WebViewer.xaml.cs rename src/Snap.Hutao/Snap.Hutao/ViewModel/User/{SignInWebViewerSouce.cs => SignInJSBridgeUriSourceProvider.cs} (69%) delete mode 100644 src/Snap.Hutao/Snap.Hutao/Win32/StructMarshal.cs diff --git a/src/Snap.Hutao/Snap.Hutao.Test/RuntimeBehavior/UnsafeRuntimeBehaviorTest.cs b/src/Snap.Hutao/Snap.Hutao.Test/RuntimeBehavior/UnsafeRuntimeBehaviorTest.cs index 4050b530..e4ef927f 100644 --- a/src/Snap.Hutao/Snap.Hutao.Test/RuntimeBehavior/UnsafeRuntimeBehaviorTest.cs +++ b/src/Snap.Hutao/Snap.Hutao.Test/RuntimeBehavior/UnsafeRuntimeBehaviorTest.cs @@ -56,6 +56,51 @@ public sealed class UnsafeRuntimeBehaviorTest Console.WriteLine(System.Text.Encoding.UTF8.GetString(bytes)); } + [TestMethod] + public unsafe void UnsafeSizeInt32ToRectInt32Test() + { + RectInt32 rectInt32 = ToRectInt32(new(100, 200)); + Assert.AreEqual(rectInt32.X, 0); + Assert.AreEqual(rectInt32.Y, 0); + Assert.AreEqual(rectInt32.Width, 100); + Assert.AreEqual(rectInt32.Height, 200); + + unsafe RectInt32 ToRectInt32(SizeInt32 sizeInt32) + { + byte* pBytes = stackalloc byte[sizeof(RectInt32)]; + *(SizeInt32*)(pBytes + 8) = sizeInt32; + return *(RectInt32*)pBytes; + } + } + + private struct RectInt32 + { + public int X; + public int Y; + public int Width; + public int Height; + + public RectInt32(int x, int y, int width, int height) + { + X = x; + Y = y; + Width = width; + Height = height; + } + } + + private struct SizeInt32 + { + public int Width; + public int Height; + + public SizeInt32(int width, int height) + { + Width = width; + Height = height; + } + } + private readonly struct TestStruct { public readonly int Value1; diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32Convert.cs b/src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32Convert.cs new file mode 100644 index 00000000..131b5399 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32Convert.cs @@ -0,0 +1,29 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Snap.Hutao.Win32.Foundation; +using System.Numerics; +using Windows.Graphics; + +namespace Snap.Hutao.Core.Graphics; + +internal static class RectInt32Convert +{ + public static RectInt32 RectInt32(RECT rect) + { + return new(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top); + } + + public static RectInt32 RectInt32(int x, int y, Vector2 size) + { + return new(x, y, (int)size.X, (int)size.Y); + } + + public static unsafe RectInt32 RectInt32(PointInt32 position, SizeInt32 size) + { + RectInt32View view = default; + view.Position = position; + view.Size = size; + return *(RectInt32*)&view; + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32Extension.cs b/src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32Extension.cs index ae7477e6..389ac3d5 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32Extension.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32Extension.cs @@ -1,6 +1,7 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. +using Snap.Hutao.Win32.Foundation; using Windows.Graphics; namespace Snap.Hutao.Core.Graphics; @@ -16,4 +17,14 @@ internal static class RectInt32Extension { return rectInt32.Width * rectInt32.Height; } + + public static unsafe SizeInt32 GetSizeInt32(this RectInt32 rectInt32) + { + return ((RectInt32View*)&rectInt32)->Size; + } + + public static RECT ToRECT(this RectInt32 rect) + { + return new(rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height); + } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32View.cs b/src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32View.cs new file mode 100644 index 00000000..c23be262 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Core/Graphics/RectInt32View.cs @@ -0,0 +1,12 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Windows.Graphics; + +namespace Snap.Hutao.Core.Graphics; + +internal struct RectInt32View +{ + public PointInt32 Position; + public SizeInt32 Size; +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Graphics/SizeInt32Extension.cs b/src/Snap.Hutao/Snap.Hutao/Core/Graphics/SizeInt32Extension.cs index 23e888fb..f6143b53 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Graphics/SizeInt32Extension.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Graphics/SizeInt32Extension.cs @@ -16,4 +16,11 @@ internal static class SizeInt32Extension { return sizeInt32.Width * sizeInt32.Height; } + + public static unsafe RectInt32 ToRectInt32(this SizeInt32 sizeInt32) + { + RectInt32View view = default; + view.Size = sizeInt32; + return *(RectInt32*)&view; + } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj index 81bdd8a3..d444c780 100644 --- a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj +++ b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj @@ -149,21 +149,6 @@ - - - - - - - - - - - - - - - @@ -187,27 +172,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -375,21 +339,11 @@ - - - MSBuild:Compile - - MSBuild:Compile - - - MSBuild:Compile - - @@ -397,30 +351,12 @@ - - - MSBuild:Compile - - - MSBuild:Compile - - - MSBuild:Compile - - - - - - MSBuild:Compile - - - MSBuild:Compile @@ -433,152 +369,12 @@ - - - MSBuild:Compile - - - - - - MSBuild:Compile - - - - - - MSBuild:Compile - - - - - - MSBuild:Compile - - - - - - MSBuild:Compile - - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - MSBuild:Compile - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - MSBuild:Compile @@ -589,31 +385,6 @@ MSBuild:Compile - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - - - - MSBuild:Compile - - MSBuild:Compile diff --git a/src/Snap.Hutao/Snap.Hutao/UI/ColorHelper.cs b/src/Snap.Hutao/Snap.Hutao/UI/ColorHelper.cs index aec73aed..4d44a9fc 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/ColorHelper.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/ColorHelper.cs @@ -36,6 +36,12 @@ internal static class ColorHelper return *(Color*)&rgba; } + public static unsafe Color ToColor(uint value) + { + uint reversed = BinaryPrimitives.ReverseEndianness(value); + return *(Color*)&reversed; + } + public static Hsla32 ToHsla32(Rgba32 rgba32) { const double toDouble = 1.0 / 255; diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconXamlHostWindow.cs b/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconXamlHostWindow.cs index bf359e32..7090593c 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconXamlHostWindow.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Shell/NotifyIconXamlHostWindow.cs @@ -5,6 +5,7 @@ using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls.Primitives; +using Snap.Hutao.Core.Graphics; using Snap.Hutao.UI.Xaml; using Snap.Hutao.UI.Xaml.Media.Backdrop; using Snap.Hutao.Win32; @@ -60,6 +61,6 @@ internal sealed class NotifyIconXamlHostWindow : Window, IWindowNeedEraseBackgro public void MoveAndResize(RECT icon) { - AppWindow.MoveAndResize(StructMarshal.RectInt32(icon)); + AppWindow.MoveAndResize(RectInt32Convert.RectInt32(icon)); } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Windowing/AppWindowExtension.cs b/src/Snap.Hutao/Snap.Hutao/UI/Windowing/AppWindowExtension.cs index 8cd48867..fc662bd0 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Windowing/AppWindowExtension.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Windowing/AppWindowExtension.cs @@ -2,7 +2,7 @@ // Licensed under the MIT license. using Microsoft.UI.Windowing; -using Snap.Hutao.Win32; +using Snap.Hutao.Core.Graphics; using Windows.Graphics; namespace Snap.Hutao.UI.Windowing; @@ -10,8 +10,18 @@ namespace Snap.Hutao.UI.Windowing; [HighQuality] internal static class AppWindowExtension { - public static RectInt32 GetRect(this AppWindow appWindow) + public static unsafe RectInt32 GetRect(this AppWindow appWindow) { - return StructMarshal.RectInt32(appWindow.Position, appWindow.Size); + RectInt32View view = default; + view.Position = appWindow.Position; + view.Size = appWindow.Size; + return *(RectInt32*)&view; + } + + public static unsafe void MoveThenResize(this AppWindow appWindow, RectInt32 rectInt32) + { + RectInt32View* pView = (RectInt32View*)&rectInt32; + appWindow.Move(pView->Position); + appWindow.Resize(pView->Size); } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs b/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs index 557ec26c..5634c2fc 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Windowing/XamlWindowController.cs @@ -154,7 +154,7 @@ internal sealed class XamlWindowController if (UniversalApiContract.IsPresent(WindowsVersion.Windows11)) { - RECT primaryRect = StructMarshal.RECT(DisplayArea.Primary.OuterBounds); + RECT primaryRect = DisplayArea.Primary.OuterBounds.ToRECT(); return IntersectRect(out _, in primaryRect, in iconRect); } @@ -246,7 +246,7 @@ internal sealed class XamlWindowController private void RecoverOrInitWindowSize(IXamlWindowHasInitSize xamlWindow) { double scale = window.GetRasterizationScale(); - RectInt32 rect = StructMarshal.RectInt32(xamlWindow.InitSize.Scale(scale)); + RectInt32 rect = xamlWindow.InitSize.Scale(scale).ToRectInt32(); if (window is IXamlWindowRectPersisted rectPersisted) { @@ -333,8 +333,7 @@ internal sealed class XamlWindowController return; } - // 48 is the navigation button leftInset - RectInt32 dragRect = StructMarshal.RectInt32(48, 0, xamlWindow.TitleBarAccess.ActualSize).Scale(window.GetRasterizationScale()); + RectInt32 dragRect = RectInt32Convert.RectInt32(0, 0, xamlWindow.TitleBarAccess.ActualSize).Scale(window.GetRasterizationScale()); window.GetInputNonClientPointerSource().SetRegionRects(NonClientRegionKind.Caption, [dragRect]); } #endregion diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/KnownColors.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/KnownColors.cs index 2d085674..adba782c 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/KnownColors.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/KnownColors.cs @@ -1,16 +1,15 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. -using Snap.Hutao.Win32; using Windows.UI; namespace Snap.Hutao.UI.Xaml.Control.Theme; internal static class KnownColors { - public static readonly Color Orange = StructMarshal.Color(0xFFBC6932); - public static readonly Color Purple = StructMarshal.Color(0xFFA156E0); - public static readonly Color Blue = StructMarshal.Color(0xFF5180CB); - public static readonly Color Green = StructMarshal.Color(0xFF2A8F72); - public static readonly Color White = StructMarshal.Color(0xFF72778B); + public static readonly Color Orange = ColorHelper.ToColor(0xFFBC6932); + public static readonly Color Purple = ColorHelper.ToColor(0xFFA156E0); + public static readonly Color Blue = ColorHelper.ToColor(0xFF5180CB); + public static readonly Color Green = ColorHelper.ToColor(0xFF2A8F72); + public static readonly Color White = ColorHelper.ToColor(0xFF72778B); } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/SystemColors.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/SystemColors.cs index 4bd3d686..3356c0c0 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/SystemColors.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/SystemColors.cs @@ -10,16 +10,16 @@ internal static class SystemColors { public static Color BaseLowColor(bool isDarkMode) { - return isDarkMode ? StructMarshal.Color(0x33FFFFFF) : StructMarshal.Color(0x33000000); + return isDarkMode ? ColorHelper.ToColor(0x33FFFFFF) : ColorHelper.ToColor(0x33000000); } public static Color BaseMediumLowColor(bool isDarkMode) { - return isDarkMode ? StructMarshal.Color(0x66FFFFFF) : StructMarshal.Color(0x66000000); + return isDarkMode ? ColorHelper.ToColor(0x66FFFFFF) : ColorHelper.ToColor(0x66000000); } public static Color BaseHighColor(bool isDarkMode) { - return isDarkMode ? StructMarshal.Color(0xFFFFFFFF) : StructMarshal.Color(0xFF000000); + return isDarkMode ? ColorHelper.ToColor(0xFFFFFFFF) : ColorHelper.ToColor(0xFF000000); } } diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/Converter/Specialized/Int32ToGradientColorConverter.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/Converter/Specialized/Int32ToGradientColorConverter.cs index e55830ad..63bb9da9 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/Converter/Specialized/Int32ToGradientColorConverter.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/Converter/Specialized/Int32ToGradientColorConverter.cs @@ -20,8 +20,8 @@ internal sealed partial class Int32ToGradientColorConverter : DependencyValueCon { public Int32ToGradientColorConverter() { - Maximum = StructMarshal.Color(0xFFFF4949); - Minimum = StructMarshal.Color(0xFF48FF7A); + Maximum = ColorHelper.ToColor(0xFFFF4949); + Minimum = ColorHelper.ToColor(0xFF48FF7A); } public override Color Convert(int from) diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/Converter/Specialized/UInt32ToGradientColorConverter.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/Converter/Specialized/UInt32ToGradientColorConverter.cs index 87006d3f..3a0d3987 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/Converter/Specialized/UInt32ToGradientColorConverter.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Data/Converter/Specialized/UInt32ToGradientColorConverter.cs @@ -20,8 +20,8 @@ internal sealed partial class UInt32ToGradientColorConverter : DependencyValueCo { public UInt32ToGradientColorConverter() { - Maximum = StructMarshal.Color(0xFFFD0093); - Minimum = StructMarshal.Color(0xFF4B00D9); + Maximum = ColorHelper.ToColor(0xFFFD0093); + Minimum = ColorHelper.ToColor(0xFF4B00D9); } public override Color Convert(uint from) diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/DailyNotePage.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/DailyNotePage.xaml index 4c751e16..71fb5b5a 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/DailyNotePage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/DailyNotePage.xaml @@ -8,13 +8,16 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mxi="using:Microsoft.Xaml.Interactivity" + xmlns:mxic="using:Microsoft.Xaml.Interactions.Core" xmlns:shme="using:Snap.Hutao.Model.Entity" xmlns:shux="using:Snap.Hutao.UI.Xaml" xmlns:shuxb="using:Snap.Hutao.UI.Xaml.Behavior" + xmlns:shuxba="using:Snap.Hutao.UI.Xaml.Behavior.Action" xmlns:shuxc="using:Snap.Hutao.UI.Xaml.Control" xmlns:shuxcc="using:Snap.Hutao.UI.Xaml.Control.Card" xmlns:shuxci="using:Snap.Hutao.UI.Xaml.Control.Image" xmlns:shuxm="using:Snap.Hutao.UI.Xaml.Markup" + xmlns:shuxvww="using:Snap.Hutao.UI.Xaml.View.Window.WebView2" xmlns:shvc="using:Snap.Hutao.View.Control" xmlns:shvd="using:Snap.Hutao.ViewModel.DailyNote" d:DataContext="{d:DesignInstance shvd:DailyNoteViewModel}" @@ -448,15 +451,15 @@ AllowFocusOnInteraction="True" Icon="{shuxm:FontIcon Glyph=}" Label="{shuxm:ResourceString Name=ViewPageDailyNoteVerify}"> - - - - - + + + + + + + + + diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/TestPage.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/TestPage.xaml index a36ded6e..ae6be3cb 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/TestPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/TestPage.xaml @@ -10,6 +10,7 @@ xmlns:mxic="using:Microsoft.Xaml.Interactions.Core" xmlns:shuxba="using:Snap.Hutao.UI.Xaml.Behavior.Action" xmlns:shuxc="using:Snap.Hutao.UI.Xaml.Control" + xmlns:shuxvww="using:Snap.Hutao.UI.Xaml.View.Window.WebView2" xmlns:shv="using:Snap.Hutao.ViewModel" xmlns:shvc="using:Snap.Hutao.View.Control" d:DataContext="{d:DesignInstance shv:TestViewModel}" @@ -20,61 +21,49 @@ - + + + + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - - - - - - + + + + + + + + + - - - - - - - - - diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/TitleView.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/TitleView.xaml index 7b5b07c5..7bbb1b91 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/TitleView.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/TitleView.xaml @@ -17,7 +17,7 @@ - + diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/UserView.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/UserView.xaml index d4e7bfb5..1d91d267 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/UserView.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/UserView.xaml @@ -9,8 +9,10 @@ xmlns:mxim="using:Microsoft.Xaml.Interactions.Media" xmlns:shux="using:Snap.Hutao.UI.Xaml" xmlns:shuxb="using:Snap.Hutao.UI.Xaml.Behavior" + xmlns:shuxba="using:Snap.Hutao.UI.Xaml.Behavior.Action" xmlns:shuxci="using:Snap.Hutao.UI.Xaml.Control.Image" xmlns:shuxm="using:Snap.Hutao.UI.Xaml.Markup" + xmlns:shuxvww="using:Snap.Hutao.UI.Xaml.View.Window.WebView2" xmlns:shvc="using:Snap.Hutao.View.Control" xmlns:shvu="using:Snap.Hutao.ViewModel.User" d:DataContext="{d:DesignInstance shvu:UserViewModel}" @@ -333,21 +335,19 @@ Icon="{shuxm:FontIcon Glyph={StaticResource FontIconContentHomeGroup}}" Label="{shuxm:ResourceString Name=ViewUserCookieOperationGameRecordIndexAction}" Style="{StaticResource DefaultAppBarButtonStyle}"> - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + + + diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/IdentifyMonitorWindow.xaml.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/IdentifyMonitorWindow.xaml.cs index 2c8020ec..61c3dea4 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/IdentifyMonitorWindow.xaml.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/IdentifyMonitorWindow.xaml.cs @@ -23,8 +23,8 @@ internal sealed partial class IdentifyMonitorWindow : Microsoft.UI.Xaml.Window AppWindow.SetPresenter(presenter); PointInt32 point = new(40, 32); - SizeInt32 size = StructMarshal.SizeInt32(displayArea.WorkArea).Scale(0.1); - AppWindow.MoveAndResize(StructMarshal.RectInt32(point, size), displayArea); + SizeInt32 size = displayArea.WorkArea.GetSizeInt32().Scale(0.1); + AppWindow.MoveAndResize(RectInt32Convert.RectInt32(point, size), displayArea); } public string Monitor { get; private set; } diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/AnnouncementWebView2ContentProvider.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/AnnouncementWebView2ContentProvider.cs index d4b92ee7..6e5f986d 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/AnnouncementWebView2ContentProvider.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/AnnouncementWebView2ContentProvider.cs @@ -41,7 +41,7 @@ internal sealed partial class AnnouncementWebView2ContentProvider : DependencyOb public CoreWebView2? CoreWebView2 { get; set; } - public async ValueTask InitializeAsync(CancellationToken token) + public async ValueTask InitializeAsync(IServiceProvider serviceProvider, CancellationToken token) { ArgumentNullException.ThrowIfNull(CoreWebView2); diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/IJSBridgeUriSource.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/IJSBridgeUriSourceProvider.cs similarity index 89% rename from src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/IJSBridgeUriSource.cs rename to src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/IJSBridgeUriSourceProvider.cs index c9e43f34..cc40a3b8 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/IJSBridgeUriSource.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/IJSBridgeUriSourceProvider.cs @@ -7,7 +7,7 @@ using Snap.Hutao.Web.Bridge; namespace Snap.Hutao.UI.Xaml.View.Window.WebView2; -internal interface IJSBridgeUriSource +internal interface IJSBridgeUriSourceProvider { MiHoYoJSBridgeFacade CreateJSBridge(IServiceProvider serviceProvider, CoreWebView2 coreWebView2, UserAndUid userAndUid); diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/IWebView2ContentProvider.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/IWebView2ContentProvider.cs index 2aef547f..05149ac3 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/IWebView2ContentProvider.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/IWebView2ContentProvider.cs @@ -13,7 +13,7 @@ internal interface IWebView2ContentProvider CoreWebView2? CoreWebView2 { get; set; } - ValueTask InitializeAsync(CancellationToken token); + ValueTask InitializeAsync(IServiceProvider serviceProvider, CancellationToken token); RectInt32 InitializePosition(RectInt32 parentRect); diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/MiHoYoJSBridgeWebView2ContentProvider.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/MiHoYoJSBridgeWebView2ContentProvider.cs new file mode 100644 index 00000000..ee6b3539 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/MiHoYoJSBridgeWebView2ContentProvider.cs @@ -0,0 +1,80 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Microsoft.UI.Xaml; +using Microsoft.Web.WebView2.Core; +using Snap.Hutao.Service.Notification; +using Snap.Hutao.Service.User; +using Snap.Hutao.ViewModel.User; +using Snap.Hutao.Web.Bridge; +using Windows.Graphics; + +namespace Snap.Hutao.UI.Xaml.View.Window.WebView2; + +[DependencyProperty("SourceProvider", typeof(IJSBridgeUriSourceProvider))] +internal sealed partial class MiHoYoJSBridgeWebView2ContentProvider : DependencyObject, IWebView2ContentProvider +{ + private MiHoYoJSBridgeFacade? jsBridge; + + public ElementTheme ActualTheme { get; set; } + + public CoreWebView2? CoreWebView2 { get; set; } + + public async ValueTask InitializeAsync(IServiceProvider serviceProvider, CancellationToken token) + { + ArgumentNullException.ThrowIfNull(CoreWebView2); + + if (SourceProvider is null) + { + return; + } + + User? user = serviceProvider.GetRequiredService().Current; + if (user is null || user.SelectedUserGameRole is null) + { + return; + } + + IInfoBarService infoBarService = serviceProvider.GetRequiredService(); + if (!UserAndUid.TryFromUser(user, out UserAndUid? userAndUid)) + { + infoBarService.Warning(SH.MustSelectUserAndUid); + return; + } + + string source = SourceProvider.GetSource(userAndUid); + if (!string.IsNullOrEmpty(source)) + { + CoreWebView2Navigator navigator = new(CoreWebView2); + await navigator.NavigateAsync("about:blank").ConfigureAwait(true); + + try + { + await CoreWebView2.Profile.ClearBrowsingDataAsync(); + } + catch (InvalidCastException) + { + infoBarService.Warning(SH.ViewControlWebViewerCoreWebView2ProfileQueryInterfaceFailed); + await CoreWebView2.DeleteCookiesAsync(userAndUid.IsOversea).ConfigureAwait(true); + } + + CoreWebView2 + .SetCookie(user.CookieToken, user.LToken, userAndUid.IsOversea) + .SetMobileUserAgent(userAndUid.IsOversea); + jsBridge = SourceProvider.CreateJSBridge(serviceProvider, CoreWebView2, userAndUid); + + await navigator.NavigateAsync(source).ConfigureAwait(true); + await CoreWebView2.Profile.ClearBrowsingDataAsync(CoreWebView2BrowsingDataKinds.BrowsingHistory); + } + } + + public void Unload() + { + jsBridge?.Detach(); + } + + public RectInt32 InitializePosition(RectInt32 parentRect) + { + return parentRect; + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/StaticWebview2ViewerSource.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/StaticJSBridgeUriSourceProvider.cs similarity index 88% rename from src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/StaticWebview2ViewerSource.cs rename to src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/StaticJSBridgeUriSourceProvider.cs index 759f82d9..cad0f852 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/StaticWebview2ViewerSource.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/StaticJSBridgeUriSourceProvider.cs @@ -11,7 +11,7 @@ namespace Snap.Hutao.UI.Xaml.View.Window.WebView2; [DependencyProperty("ChineseSource", typeof(string))] [DependencyProperty("OverseaSource", typeof(string))] -internal sealed partial class StaticJSBridgeUriSource : DependencyObject, IJSBridgeUriSource +internal sealed partial class StaticJSBridgeUriSourceProvider : DependencyObject, IJSBridgeUriSourceProvider { public MiHoYoJSBridgeFacade CreateJSBridge(IServiceProvider serviceProvider, CoreWebView2 coreWebView2, UserAndUid userAndUid) { diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/WebView2Window.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/WebView2Window.xaml index 79d6ed7f..6ddc0723 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/WebView2Window.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Window/WebView2/WebView2Window.xaml @@ -13,8 +13,28 @@ + + + + + +