WinUI3 引用

改进字体管理器
改进托盘窗口弹出方式
This commit is contained in:
rmbgame
2021-08-20 13:32:48 +08:00
parent 0e9f4eb979
commit bf531bb8b8
14 changed files with 108 additions and 44 deletions
+2 -1
View File
@@ -2,6 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="clr-namespace:System.Application.UI"
xmlns:converters="clr-namespace:System.Application.Converters;assembly=System.Application.SteamTools.Client.Desktop.Avalonia"
xmlns:sty="using:FluentAvalonia.Styling"
x:Class="System.Application.UI.App">
<Application.DataTemplates>
<ui:ViewLocator/>
@@ -24,7 +25,7 @@
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml" />
<StyleInclude Source="avares://System.Application.SteamTools.Client.Desktop.Avalonia/Application/UI/Styles/Controls.xaml" />
<!--<sty:FluentAvaloniaTheme ControlsVersion="2"/>-->
<sty:FluentAvaloniaTheme ControlsVersion="2"/>
<!--<StyleInclude Source="avares://AvaloniaEdit/AvaloniaEdit.xaml"/>-->
<Style Selector="Window">
@@ -29,6 +29,7 @@ using System.Threading.Tasks;
using System.Application.Security;
using Avalonia;
using Avalonia.Platform;
using FluentAvalonia.Styling;
#if WINDOWS
//using WpfApplication = System.Windows.Application;
#endif
@@ -117,6 +118,9 @@ namespace System.Application.UI
{
Source = uri_1,
};
var thm = AvaloniaLocator.Current.GetService<FluentAvaloniaTheme>();
thm.RequestedTheme = the;
}
readonly Dictionary<string, ICommand> mNotifyIconMenus = new();
@@ -88,6 +88,16 @@ namespace System.Application.Services
}
}
bool IsVisibleWindow(WindowViewModel vm)
{
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var window = desktop.Windows.FirstOrDefault(x => x.DataContext == vm);
return window?.IsVisible == true;
}
return false;
}
void HideWindow(WindowViewModel vm)
{
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
@@ -197,6 +197,19 @@ namespace System.Application.Services.Implementation
"CloseWindow fail, vmType: {0}", vm.GetType().Name);
}
}
public bool IsVisibleWindow(WindowViewModel vm)
{
try
{
return IDesktopAvaloniaAppService.Instance.IsVisibleWindow(vm);
}
catch (Exception e)
{
Log.Error(nameof(ShowWindowServiceImpl), e,
"HideWindow fail, vmType: {0}", vm.GetType().Name);
return false;
}
}
public void HideWindow(WindowViewModel vm)
{
@@ -5,12 +5,14 @@ using SkiaSharp;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace System.Application.UI
{
public class CustomFontManagerImpl: IFontManagerImpl
public class CustomFontManagerImpl : IFontManagerImpl
{
private readonly Typeface[] _customTypefaces;
//private readonly Typeface[] _customTypefaces;
private SKFontManager _skFontManager = SKFontManager.Default;
private readonly string _defaultFamilyName;
//Load font resources in the project, you can load multiple font resources
@@ -19,7 +21,7 @@ namespace System.Application.UI
public CustomFontManagerImpl()
{
_customTypefaces = new[] { _defaultTypeface };
//_customTypefaces = new[] { _defaultTypeface };
_defaultFamilyName = _defaultTypeface.FontFamily.FamilyNames.PrimaryFamilyName;
}
@@ -30,7 +32,13 @@ namespace System.Application.UI
public IEnumerable<string> GetInstalledFontFamilyNames(bool checkForUpdates = false)
{
return _customTypefaces.Select(x => x.FontFamily.Name);
if (checkForUpdates)
{
_skFontManager = SKFontManager.CreateDefault();
}
return _skFontManager.FontFamilies;
//return _customTypefaces.Select(x => x.FontFamily.Name);
}
private readonly string[] _bcp47 = { CultureInfo.CurrentCulture.ThreeLetterISOLanguageName, CultureInfo.CurrentCulture.TwoLetterISOLanguageName };
@@ -38,17 +46,17 @@ namespace System.Application.UI
public bool TryMatchCharacter(int codepoint, FontStyle fontStyle, FontWeight fontWeight, FontFamily fontFamily,
CultureInfo culture, out Typeface typeface)
{
foreach (var customTypeface in _customTypefaces)
{
if (customTypeface.GlyphTypeface.GetGlyph((uint)codepoint) == 0)
{
continue;
}
//foreach (var customTypeface in _customTypefaces)
//{
// if (customTypeface.GlyphTypeface.GetGlyph((uint)codepoint) == 0)
// {
// continue;
// }
typeface = new Typeface(customTypeface.FontFamily.Name, fontStyle, fontWeight);
// typeface = new Typeface(customTypeface.FontFamily.Name, fontStyle, fontWeight);
return true;
}
// return true;
//}
var fallback = SKFontManager.Default.MatchCharacter(fontFamily?.Name, (SKFontStyleWeight)fontWeight,
SKFontStyleWidth.Normal, (SKFontStyleSlant)fontStyle, _bcp47, codepoint);
@@ -58,18 +66,49 @@ namespace System.Application.UI
return true;
}
private SKTypeface GetSkTypefaceByFontFamily(Typeface typeface)
{
SKTypeface? skTypeface = SKTypeface.FromFamilyName(typeface.FontFamily.Name,
(SKFontStyleWeight)typeface.Weight, SKFontStyleWidth.Normal, (SKFontStyleSlant)typeface.Style);
if (skTypeface.FamilyName != typeface.FontFamily.Name)
{
try
{
Assembly asm = Assembly.Load("Avalonia.Skia");
var t = asm.GetType("Avalonia.Skia.SKTypefaceCollectionCache");
var fontCollection = t?.InvokeMember("GetOrAddTypefaceCollection", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, new object[] { typeface.FontFamily });
var type = fontCollection?.GetType();
var oMethod = type?.GetMethod("Get");
var r = oMethod?.Invoke(fontCollection, new object[] { typeface });
skTypeface = r as SKTypeface;
}
catch
{
//Log.Error(nameof(CustomFontManagerImpl), ex, nameof(GetSkTypefaceByFontFamily));
skTypeface = null;
}
}
if (skTypeface == null)
{
skTypeface = SKTypeface.FromFamilyName(_defaultTypeface.FontFamily.Name);
}
return skTypeface;
}
public IGlyphTypefaceImpl CreateGlyphTypeface(Typeface typeface)
{
SKTypeface skTypeface;
SKTypeface? skTypeface = null;
switch (typeface.FontFamily.Name)
{
case FontFamily.DefaultFontFamilyName:
case "WenQuanYi Micro Hei": //font family name
skTypeface = SKTypeface.FromFamilyName(_defaultTypeface.FontFamily.Name); break;
skTypeface = GetSkTypefaceByFontFamily(_defaultTypeface); break;
default:
skTypeface = SKTypeface.FromFamilyName(typeface.FontFamily.Name,
(SKFontStyleWeight)typeface.Weight, SKFontStyleWidth.Normal, (SKFontStyleSlant)typeface.Style);
skTypeface = GetSkTypefaceByFontFamily(typeface);
break;
}
@@ -154,7 +154,8 @@ namespace System.Application.UI.Views.Controls
if (nC.Intersects(nR))
using (context.PushPostTransform(Matrix.CreateTranslation(offset, padding.Top)))
TextLayout.Draw(context, new Point(Padding.Left, Padding.Top));
//TextLayout.Draw(context, new Point(Padding.Left, Padding.Top));
TextLayout.Draw(context);
}
}
else
@@ -162,7 +163,8 @@ namespace System.Application.UI.Views.Controls
_animate = false;
using (context.PushPostTransform(Matrix.CreateTranslation(padding.Left, padding.Top)))
TextLayout.Draw(context, new Point(Padding.Left, Padding.Top));
//TextLayout.Draw(context, new Point(Padding.Left, Padding.Top));
TextLayout.Draw(context);
}
}
}
@@ -360,13 +360,13 @@
IsVisible="{Binding LibraryLogoStream^,Converter={StaticResource IsNullConverter}}"/>
<Border IsVisible="{Binding IsInstalled}"
Margin="20 0 20 -3"
Margin="10 0 10 -3"
CornerRadius="3"
Classes="paper"
Background="#3D4450"
HorizontalAlignment="Center"
VerticalAlignment="Bottom">
<WrapPanel Margin="15 3"
<WrapPanel Margin="10 3"
Orientation="Horizontal"
HorizontalAlignment="Center">
<TextBlock Text="{Binding InstalledDrive}"
@@ -208,15 +208,15 @@
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
</StackPanel.Styles>
<WrapPanel IsVisible="{Binding MostRecent}">
<StackPanel Orientation="Horizontal" IsVisible="{Binding MostRecent}">
<PathIcon Data="{StaticResource CheckmarkDrawing}"/>
<TextBlock Text="{ReflectionBinding Path=Res.UserChange_RecentLogin,Mode=OneWay,Source={x:Static resx:R.Current}}"/>
</WrapPanel>
</StackPanel>
<WrapPanel IsVisible="{Binding RememberPassword}">
<StackPanel Orientation="Horizontal" IsVisible="{Binding RememberPassword}">
<PathIcon Data="{StaticResource CheckmarkDrawing}"/>
<TextBlock Text="{ReflectionBinding Path=Res.UserChange_RememberMe,Mode=OneWay,Source={x:Static resx:R.Current}}"/>
</WrapPanel>
</StackPanel>
</StackPanel>
</Panel>
</Panel>
@@ -72,7 +72,6 @@ namespace System.Application.UI.Views.Windows
{
if (this.DataContext is TaskBarWindowViewModel vm)
{
vm.IsVisible = false;
this.Hide();
}
}
@@ -73,19 +73,19 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.999-cibuild0015286-beta" />
<PackageReference Include="Avalonia" Version="0.10.7" />
<!--<PackageReference Include="Avalonia.AvaloniaEdit" Version="0.10.4" />-->
<PackageReference Include="Avalonia.Desktop" Version="0.10.999-cibuild0015286-beta" />
<PackageReference Include="Avalonia.Controls.DataGrid" Version="0.10.999-cibuild0015286-beta" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.999-cibuild0015286-beta" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="0.10.6.10" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.7" />
<PackageReference Include="Avalonia.Controls.DataGrid" Version="0.10.7" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.7" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="0.10.7" />
<!--<PackageReference Include="CefNet.Avalonia" Version="90.6.21124.1851" />-->
<!--<PackageReference Include="LibVLCSharp.Avalonia" Version="3.5.1" />-->
<!--<PackageReference Include="FluentAvaloniaUI" Version="1.1.1" />-->
<PackageReference Include="FluentAvaloniaUI" Version="1.1.1" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='Debug'">
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.999-cibuild0015286-beta" />
<PackageReference Include="Avalonia.Diagnostics" Version="0.10.7" />
</ItemGroup>
<ItemGroup>
@@ -73,6 +73,7 @@ namespace System.Application.Services
#if !__MOBILE__
void CloseWindow(WindowViewModel vm);
bool IsVisibleWindow(WindowViewModel vm);
void HideWindow(WindowViewModel vm);
void ShowWindow(WindowViewModel vm);
#else
@@ -226,7 +226,7 @@
<comment>Comment=;Author=沙中金</comment>
</data>
<data name="Achievement_TimeFormat" xml:space="preserve">
<value>Unlocked at s0:yyy-MM-dd HH:mm:ss</value>
<value>Unlocked at {0:yyyy-MM-dd HH:mm:ss}</value>
<comment>Comment=;Author=沙中金</comment>
</data>
<data name="Achievement_Unlock" xml:space="preserve">
@@ -78,6 +78,10 @@ namespace System.Application.UI.ViewModels
[IgnoreDataMember]
public bool IsInitialized { get; protected set; }
[IgnoreDataMember]
public bool IsVisible =>
IShowWindowService.Instance.IsVisibleWindow(this);
/// <summary>
/// 关闭当前viewmodel绑定的窗口
/// </summary>
@@ -11,13 +11,6 @@ namespace System.Application.UI.ViewModels
{
public class TaskBarWindowViewModel : WindowViewModel
{
private bool _IsVisible;
public bool IsVisible
{
get => _IsVisible;
set => this.RaiseAndSetIfChanged(ref _IsVisible, value);
}
public new string Title => ThisAssembly.AssemblyTrademark;
public string Version => ThisAssembly.Version;
@@ -32,7 +25,6 @@ namespace System.Application.UI.ViewModels
public async void Show(int x, int y)
{
SetPosition(x, y);
IsVisible = true;
await IShowWindowService.Instance.Show(CustomWindow.TaskBar, this, string.Empty, ResizeModeCompat.NoResize, isParent: false);
}
@@ -44,7 +36,6 @@ namespace System.Application.UI.ViewModels
public override void Hide()
{
IsVisible = false;
base.Hide();
}