Skip to content

Commit

Permalink
Merge pull request #1589 from DGP-Studio/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Apr 30, 2024
2 parents d8310b7 + fbffadd commit 2fdeaa2
Show file tree
Hide file tree
Showing 342 changed files with 9,799 additions and 3,383 deletions.
Binary file added res/Banner3-large-cn.psd
Binary file not shown.
Binary file added res/Banner3-large.psd
Binary file not shown.
Binary file added res/Store/chs/abyss.psd
Binary file not shown.
Binary file added res/Store/chs/achievement.psd
Binary file not shown.
Binary file added res/Store/chs/character-data.psd
Binary file not shown.
Binary file added res/Store/chs/lancher.psd
Binary file not shown.
Binary file added res/Store/chs/realtime-notes.psd
Binary file not shown.
Binary file added res/Store/chs/wish.psd
Binary file not shown.
Binary file added res/Store/en/abyss.psd
Binary file not shown.
Binary file added res/Store/en/achievement.psd
Binary file not shown.
Binary file added res/Store/en/character-data.psd
Binary file not shown.
Binary file added res/Store/en/lancher.psd
Binary file not shown.
Binary file added res/Store/en/realtime-notes.psd
Binary file not shown.
Binary file added res/Store/en/wish.psd
Binary file not shown.
4 changes: 2 additions & 2 deletions src/Snap.Hutao/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ dotnet_diagnostic.SA1642.severity = none

dotnet_diagnostic.IDE0005.severity = warning
dotnet_diagnostic.IDE0060.severity = none
dotnet_diagnostic.IDE0290.severity = none

# SA1208: System using directives should be placed before other using directives
dotnet_diagnostic.SA1208.severity = none
Expand Down Expand Up @@ -321,7 +320,8 @@ dotnet_diagnostic.CA2227.severity = suggestion

# CA2251: 使用 “string.Equals”
dotnet_diagnostic.CA2251.severity = suggestion
csharp_style_prefer_primary_constructors = true:suggestion

csharp_style_prefer_primary_constructors = false:none

[*.vb]
#### 命名样式 ####
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;

namespace Snap.Hutao.Test.PlatformExtensions;
Expand All @@ -11,6 +12,7 @@ public sealed class DependencyInjectionTest
.AddSingleton<IService, ServiceB>()
.AddScoped<IScopedService, ServiceA>()
.AddTransient(typeof(IGenericService<>), typeof(GenericService<>))
.AddLogging(builder => builder.AddConsole())
.BuildServiceProvider();

[TestMethod]
Expand Down Expand Up @@ -41,6 +43,13 @@ public void ScopedServiceInitializeMultipleTimesInScope()
}
}

[TestMethod]
public void LoggerWithInterfaceTypeCanBeResolved()
{
Assert.IsNotNull(services.GetService<ILogger<IScopedService>>());
Assert.IsNotNull(services.GetRequiredService<ILoggerFactory>().CreateLogger(nameof(IScopedService)));
}

private interface IService
{
Guid Id { get; }
Expand Down
5 changes: 3 additions & 2 deletions src/Snap.Hutao/Snap.Hutao.Test/Snap.Hutao.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 0 additions & 2 deletions src/Snap.Hutao/Snap.Hutao/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
using Snap.Hutao.Core.Logging;
using Snap.Hutao.Core.Shell;
using System.Diagnostics;
using System.Text;
using static Snap.Hutao.Core.Logging.ConsoleVirtualTerminalSequences;

namespace Snap.Hutao;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.Control.Builder.ButtonBase;

internal class ButtonBaseBuilder<TButton> : IButtonBaseBuilder<TButton>
where TButton : Microsoft.UI.Xaml.Controls.Primitives.ButtonBase, new()
{
public TButton Button { get; } = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Core.Abstraction.Extension;

namespace Snap.Hutao.Control.Builder.ButtonBase;

internal static class ButtonBaseBuilderExtension
{
public static TBuilder SetContent<TBuilder, TButton>(this TBuilder builder, object? content)
where TBuilder : IButtonBaseBuilder<TButton>
where TButton : Microsoft.UI.Xaml.Controls.Primitives.ButtonBase
{
builder.Configure(builder => builder.Button.Content = content);
return builder;
}

public static TBuilder SetCommand<TBuilder, TButton>(this TBuilder builder, ICommand command)
where TBuilder : IButtonBaseBuilder<TButton>
where TButton : Microsoft.UI.Xaml.Controls.Primitives.ButtonBase
{
builder.Configure(builder => builder.Button.Command = command);
return builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Microsoft.UI.Xaml.Controls;

namespace Snap.Hutao.Control.Builder.ButtonBase;

internal sealed class ButtonBuilder : ButtonBaseBuilder<Button>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Microsoft.UI.Xaml.Controls;

namespace Snap.Hutao.Control.Builder.ButtonBase;

internal static class ButtonBuilderExtension
{
public static ButtonBuilder SetContent(this ButtonBuilder builder, object? content)
{
return builder.SetContent<ButtonBuilder, Button>(content);
}

public static ButtonBuilder SetCommand(this ButtonBuilder builder, ICommand command)
{
return builder.SetCommand<ButtonBuilder, Button>(command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Core.Abstraction;

namespace Snap.Hutao.Control.Builder.ButtonBase;

internal interface IButtonBaseBuilder<TButton> : IBuilder
where TButton : Microsoft.UI.Xaml.Controls.Primitives.ButtonBase
{
TButton Button { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ public static void DisableInteraction(this FrameworkElement frameworkElement)
frameworkElement.IsRightTapEnabled = false;
frameworkElement.IsTabStop = false;
}

public static void InitializeDataContext<TDataContext>(this FrameworkElement frameworkElement, IServiceProvider? serviceProvider = default)
where TDataContext : class
{
IServiceProvider service = serviceProvider ?? Ioc.Default;
try
{
frameworkElement.DataContext = service.GetRequiredService<TDataContext>();
}
catch (Exception ex)
{

ILogger? logger = service.GetRequiredService(typeof(ILogger<>).MakeGenericType([frameworkElement.GetType()])) as ILogger;
logger?.LogError(ex, "Failed to initialize DataContext");
throw;
}
}
}
2 changes: 1 addition & 1 deletion src/Snap.Hutao/Snap.Hutao/Control/Image/CachedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CachedImage()

try
{
HutaoException.ThrowIf(string.IsNullOrEmpty(imageUri.Host), HutaoExceptionKind.ImageCacheInvalidUri, SH.ControlImageCachedImageInvalidResourceUri);
HutaoException.ThrowIf(string.IsNullOrEmpty(imageUri.Host), SH.ControlImageCachedImageInvalidResourceUri);
string file = await imageCache.GetFileFromCacheAsync(imageUri).ConfigureAwait(true); // BitmapImage need to be created by main thread.
token.ThrowIfCancellationRequested(); // check token state to determine whether the operation should be canceled.
return new BitmapImage(file.ToUri()); // BitmapImage initialize with a uri will increase image quality and loading speed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System.Collections.Specialized;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Windows.Foundation;

Expand Down
19 changes: 11 additions & 8 deletions src/Snap.Hutao/Snap.Hutao/Control/Panel/HorizontalEqualPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using Microsoft.UI.Xaml;
using System.Runtime.InteropServices;
using Windows.Foundation;

namespace Snap.Hutao.Control.Panel;
Expand All @@ -18,28 +19,29 @@ public HorizontalEqualPanel()

protected override Size MeasureOverride(Size availableSize)
{
foreach (UIElement child in Children)
List<UIElement> visibleChildren = Children.Where(child => child.Visibility is Visibility.Visible).ToList();
foreach (ref readonly UIElement visibleChild in CollectionsMarshal.AsSpan(visibleChildren))
{
// ScrollViewer will always return an Infinity Size, we should use ActualWidth for this situation.
double availableWidth = double.IsInfinity(availableSize.Width) ? ActualWidth : availableSize.Width;
double childAvailableWidth = (availableWidth + Spacing) / Children.Count;
double childAvailableWidth = (availableWidth + Spacing) / visibleChildren.Count;
double childMaxAvailableWidth = Math.Max(MinItemWidth, childAvailableWidth);
child.Measure(new(childMaxAvailableWidth - Spacing, ActualHeight));
visibleChild.Measure(new(childMaxAvailableWidth - Spacing, ActualHeight));
}

return base.MeasureOverride(availableSize);
}

protected override Size ArrangeOverride(Size finalSize)
{
int itemCount = Children.Count;
double availableItemWidth = (finalSize.Width - (Spacing * (itemCount - 1))) / itemCount;
List<UIElement> visibleChildren = Children.Where(child => child.Visibility is Visibility.Visible).ToList();
double availableItemWidth = (finalSize.Width - (Spacing * (visibleChildren.Count - 1))) / visibleChildren.Count;
double actualItemWidth = Math.Max(MinItemWidth, availableItemWidth);

double offset = 0;
foreach (UIElement child in Children)
foreach (ref readonly UIElement visibleChild in CollectionsMarshal.AsSpan(visibleChildren))
{
child.Arrange(new Rect(offset, 0, actualItemWidth, finalSize.Height));
visibleChild.Arrange(new Rect(offset, 0, actualItemWidth, finalSize.Height));
offset += actualItemWidth + Spacing;
}

Expand All @@ -49,7 +51,8 @@ protected override Size ArrangeOverride(Size finalSize)
private static void OnLoaded(object sender, RoutedEventArgs e)
{
HorizontalEqualPanel panel = (HorizontalEqualPanel)sender;
panel.MinWidth = (panel.MinItemWidth * panel.Children.Count) + (panel.Spacing * (panel.Children.Count - 1));
int vivibleChildrenCount = panel.Children.Count(child => child.Visibility is Visibility.Visible);
panel.MinWidth = (panel.MinItemWidth * vivibleChildrenCount) + (panel.Spacing * (vivibleChildrenCount - 1));
}

private static void OnSizeChanged(object sender, SizeChangedEventArgs e)
Expand Down
2 changes: 0 additions & 2 deletions src/Snap.Hutao/Snap.Hutao/Control/Panel/UniformPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Licensed under the MIT license.

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Windows.Foundation;

namespace Snap.Hutao.Control.Panel;
Expand Down
21 changes: 15 additions & 6 deletions src/Snap.Hutao/Snap.Hutao/Control/ScopedPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ internal class ScopedPage : Page
{
private readonly RoutedEventHandler unloadEventHandler;
private readonly CancellationTokenSource viewCancellationTokenSource = new();
private readonly IServiceScope currentScope;
private readonly IServiceScope pageScope;

private bool inFrame = true;

protected ScopedPage()
{
unloadEventHandler = OnUnloaded;
Unloaded += unloadEventHandler;
currentScope = Ioc.Default.GetRequiredService<IScopedPageScopeReferenceTracker>().CreateScope();
pageScope = Ioc.Default.GetRequiredService<IScopedPageScopeReferenceTracker>().CreateScope();
}

public async ValueTask NotifyRecipientAsync(INavigationData extra)
Expand All @@ -44,9 +44,17 @@ public async ValueTask NotifyRecipientAsync(INavigationData extra)
protected void InitializeWith<TViewModel>()
where TViewModel : class, IViewModel
{
IViewModel viewModel = currentScope.ServiceProvider.GetRequiredService<TViewModel>();
viewModel.CancellationToken = viewCancellationTokenSource.Token;
DataContext = viewModel;
try
{
IViewModel viewModel = pageScope.ServiceProvider.GetRequiredService<TViewModel>();
viewModel.CancellationToken = viewCancellationTokenSource.Token;
DataContext = viewModel;
}
catch (Exception ex)
{
pageScope.ServiceProvider.GetRequiredService<ILogger<ScopedPage>>().LogError(ex, "Failed to initialize view model.");
throw;
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -95,7 +103,8 @@ private void DisposeViewModel()
viewModel.IsViewDisposed = true;

// Dispose the scope
currentScope.Dispose();
pageScope.Dispose();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public void Dispose()

public IServiceScope CreateScope()
{
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true);
IServiceScope currentScope = serviceProvider.CreateScope();

// In case previous one is not disposed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Snap.Hutao.Web.Request.Builder.Abstraction;
using System.Diagnostics;

namespace Snap.Hutao.Web.Request.Builder;
namespace Snap.Hutao.Core.Abstraction.Extension;

internal static class BuilderExtension
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.Web.Request.Builder.Abstraction;
namespace Snap.Hutao.Core.Abstraction;

internal interface IBuilder;
Loading

0 comments on commit 2fdeaa2

Please sign in to comment.