Skip to content

Commit

Permalink
Merge pull request DGP-Studio#1641 from DGP-Studio/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed May 26, 2024
2 parents b68462b + ec0abb4 commit db4b0d3
Show file tree
Hide file tree
Showing 434 changed files with 7,868 additions and 2,348 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/alpha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- main
- develop
- 'feat/*'
paths-ignore:
- '.gitattributes'
- '.github/**'
Expand Down Expand Up @@ -80,7 +81,7 @@ jobs:
> [!IMPORTANT]
> 请注意,从 Snap Hutao Alpha 2023.12.21.3 开始,我们将使用全新的 CI 证书,原有的 Snap.Hutao.CI.cer 将在几天后过期停止使用。
>
> 请安装 [DGP_Studio_CA.crt](https://github.com/DGP-Automation/Hutao-Auto-Release/releases/download/certificate-ca/DGP_Studio_CA.crt) 以安装测试版安装包
> 请安装 [DGP_Studio_CA.crt](https://github.com/DGP-Automation/Hutao-Auto-Release/releases/download/certificate-ca/DGP_Studio_CA.crt) 到 `受信任的根证书颁发机构` 以安装测试版安装包
"
echo $summary >> $Env:GITHUB_STEP_SUMMARY
10 changes: 10 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ var version = "version";
var repoDir = "repoDir";
var outputPath = "outputPath";

// Extension

static ProcessArgumentBuilder AppendIf(this ProcessArgumentBuilder builder, string text, bool condition)
{
return condition ? builder.Append(text) : builder;
}

// Properties

string solution
{
get => System.IO.Path.Combine(repoDir, "src", "Snap.Hutao", "Snap.Hutao.sln");
Expand Down Expand Up @@ -157,6 +166,7 @@ Task("Build binary package")
.Append("/p:AppxPackageSigningEnabled=false")
.Append("/p:AppxBundle=Never")
.Append("/p:AppxPackageOutput=" + outputPath)
.AppendIf("/p:AlphaConstants=IS_ALPHA_BUILD", !AppVeyor.IsRunningOnAppVeyor)
};
DotNetBuild(project, settings);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Runtime.CompilerServices;

namespace Snap.Hutao.Test.BaseClassLibrary;

[TestClass]
public class UnsafeAccessorTest
{
[TestMethod]
public void UnsafeAccessorCanGetInterfaceProperty()
{
TestClass test = new();
int value = InternalGetInterfaceProperty(test);
Assert.AreEqual(3, value);
}

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_TestProperty")]
private static extern int InternalGetInterfaceProperty(ITestInterface instance);

internal interface ITestInterface
{
internal int TestProperty { get; }
}

internal sealed class TestClass : ITestInterface
{
public int TestProperty { get; } = 3;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;

namespace Snap.Hutao.Test.PlatformExtensions;

Expand All @@ -11,6 +12,8 @@ public sealed class DependencyInjectionTest
.AddSingleton<IService, ServiceA>()
.AddSingleton<IService, ServiceB>()
.AddScoped<IScopedService, ServiceA>()
.AddKeyedTransient<IKeyedService, KeyedServiceA>("A")
.AddKeyedTransient<IKeyedService, KeyedServiceB>("B")
.AddTransient(typeof(IGenericService<>), typeof(GenericService<>))
.AddLogging(builder => builder.AddConsole())
.BuildServiceProvider();
Expand Down Expand Up @@ -50,6 +53,15 @@ public void LoggerWithInterfaceTypeCanBeResolved()
Assert.IsNotNull(services.GetRequiredService<ILoggerFactory>().CreateLogger(nameof(IScopedService)));
}

[TestMethod]
public void KeyedServicesCanBeResolvedAsEnumerable()
{
Assert.IsNotNull(services.GetRequiredKeyedService<IKeyedService>("A"));
Assert.IsNotNull(services.GetRequiredKeyedService<IKeyedService>("B"));

Assert.AreEqual(0, services.GetServices<IKeyedService>().Count());
}

private interface IService
{
Guid Id { get; }
Expand Down Expand Up @@ -95,4 +107,14 @@ public NonInjectedServiceB(NonInjectedServiceA? serviceA)
{
}
}

private interface IKeyedService;

private sealed class KeyedServiceA : IKeyedService
{
}

private sealed class KeyedServiceB : IKeyedService
{
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -46,7 +47,14 @@ public unsafe void ReadOnlyStructCanBeModifiedInCtor()
Assert.AreEqual(1212, testStruct.Value4);
}


[TestMethod]
public unsafe void UnsafeUtf8StringReference()
{
void* ptr = Unsafe.AsPointer(ref MemoryMarshal.GetReference("test"u8));
GC.Collect(GC.MaxGeneration);
ReadOnlySpan<byte> bytes = MemoryMarshal.CreateReadOnlySpanFromNullTerminated((byte*)ptr);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(bytes));
}

private readonly struct TestStruct
{
Expand Down
1 change: 1 addition & 0 deletions src/Snap.Hutao/Snap.Hutao/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources/>
<ResourceDictionary Source="ms-appx:///CommunityToolkit.WinUI.Controls.SettingsControls/SettingsCard/SettingsCard.xaml"/>
<ResourceDictionary Source="ms-appx:///CommunityToolkit.WinUI.Controls.TokenizingTextBox/TokenizingTextBox.xaml"/>
<ResourceDictionary Source="ms-appx:///Control/Loading.xaml"/>
<ResourceDictionary Source="ms-appx:///Control/Image/CachedImage.xaml"/>
Expand Down
22 changes: 13 additions & 9 deletions src/Snap.Hutao/Snap.Hutao/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Snap.Hutao.Core.LifeCycle;
using Snap.Hutao.Core.LifeCycle.InterProcess;
using Snap.Hutao.Core.Logging;
using Snap.Hutao.Core.Shell;
using Snap.Hutao.Core.Windowing;
using System.Diagnostics;

namespace Snap.Hutao;
Expand Down Expand Up @@ -39,7 +39,7 @@ public sealed partial class App : Application
""";

private readonly IServiceProvider serviceProvider;
private readonly IActivation activation;
private readonly IAppActivation activation;
private readonly ILogger<App> logger;

/// <summary>
Expand All @@ -50,13 +50,19 @@ public App(IServiceProvider serviceProvider)
{
// Load app resource
InitializeComponent();
activation = serviceProvider.GetRequiredService<IActivation>();
activation = serviceProvider.GetRequiredService<IAppActivation>();
logger = serviceProvider.GetRequiredService<ILogger<App>>();
serviceProvider.GetRequiredService<ExceptionRecorder>().Record(this);

this.serviceProvider = serviceProvider;
}

public new void Exit()
{
XamlWindowLifetime.ApplicationExiting = true;
base.Exit();
}

/// <inheritdoc/>
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
Expand All @@ -73,15 +79,13 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
logger.LogColorizedInformation((ConsoleBanner, ConsoleColor.DarkYellow));
LogDiagnosticInformation();

// manually invoke
// Manually invoke
activation.Activate(HutaoActivationArguments.FromAppActivationArguments(activatedEventArgs));
activation.Initialize();

serviceProvider.GetRequiredService<IJumpListInterop>().ConfigureAsync().SafeForget();
activation.PostInitialization();
}
catch
catch (Exception ex)
{
// AppInstance.GetCurrent() calls failed
Debug.WriteLine(ex);
Process.GetCurrentProcess().Kill();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Snap.Hutao.Control.Collection.AdvancedCollectionView;

internal sealed class VectorChangedEventArgs : IVectorChangedEventArgs
{
public VectorChangedEventArgs(CollectionChange cc, int index = -1, object item = null!)
public VectorChangedEventArgs(CollectionChange cc, int index = -1, object item = default!)
{
CollectionChange = cc;
Index = (uint)index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
using Snap.Hutao.Core.ExceptionService;

namespace Snap.Hutao.Control;

Expand Down Expand Up @@ -40,6 +41,6 @@ internal abstract class DependencyValueConverter<TFrom, TTo> : DependencyObject,
/// <returns>源</returns>
public virtual TFrom ConvertBack(TTo to)
{
throw Must.NeverHappen();
throw HutaoException.NotSupported();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public static void InitializeDataContext<TDataContext>(this FrameworkElement fra
}
catch (Exception ex)
{

ILogger? logger = service.GetRequiredService(typeof(ILogger<>).MakeGenericType([frameworkElement.GetType()])) as ILogger;
logger?.LogError(ex, "Failed to initialize DataContext");
throw;
Expand Down
20 changes: 20 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Control/Helper/FrameworkElementHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Snap.Hutao.Control.Helper;

[SuppressMessage("", "SH001")]
[DependencyProperty("SquareLength", typeof(double), 0D, nameof(OnSquareLengthChanged), IsAttached = true, AttachedType = typeof(FrameworkElement))]
[DependencyProperty("IsActualThemeBindingEnabled", typeof(bool), false, nameof(OnIsActualThemeBindingEnabled), IsAttached = true, AttachedType = typeof(FrameworkElement))]
[DependencyProperty("ActualTheme", typeof(ElementTheme), ElementTheme.Default, IsAttached = true, AttachedType = typeof(FrameworkElement))]
public sealed partial class FrameworkElementHelper
{
private static void OnSquareLengthChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
Expand All @@ -15,4 +17,22 @@ private static void OnSquareLengthChanged(DependencyObject dp, DependencyPropert
element.Width = (double)e.NewValue;
element.Height = (double)e.NewValue;
}

private static void OnIsActualThemeBindingEnabled(DependencyObject dp, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)dp;
if ((bool)e.NewValue)
{
element.ActualThemeChanged += OnActualThemeChanged;
}
else
{
element.ActualThemeChanged -= OnActualThemeChanged;
}

static void OnActualThemeChanged(FrameworkElement sender, object args)
{
SetActualTheme(sender, sender.ActualTheme);
}
}
}
9 changes: 2 additions & 7 deletions src/Snap.Hutao/Snap.Hutao/Control/Image/CachedImage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Snap.Hutao.Control.Extension;
using Snap.Hutao.Core.Caching;
using Snap.Hutao.Core.ExceptionService;
Expand All @@ -23,13 +21,10 @@ public CachedImage()
{
DefaultStyleKey = typeof(CachedImage);
DefaultStyleResourceUri = "ms-appx:///Control/Image/CachedImage.xaml".ToUri();

IsCacheEnabled = true;
EnableLazyLoading = false;
}

/// <inheritdoc/>
protected override async Task<ImageSource?> ProvideCachedResourceAsync(Uri imageUri, CancellationToken token)
protected override async Task<Uri?> ProvideCachedResourceAsync(Uri imageUri, CancellationToken token)
{
IImageCache imageCache = this.ServiceProvider().GetRequiredService<IImageCache>();

Expand All @@ -38,7 +33,7 @@ public CachedImage()
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.
return file.ToUri();
}
catch (COMException)
{
Expand Down
1 change: 0 additions & 1 deletion src/Snap.Hutao/Snap.Hutao/Control/Image/CachedImage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{ThemeResource ApplicationForegroundThemeBrush}"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="LazyLoadingThreshold" Value="256"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="shci:CachedImage">
Expand Down
Loading

0 comments on commit db4b0d3

Please sign in to comment.