refactor: setup fonts based on operating system

This commit is contained in:
leo 2024-03-08 12:22:22 +08:00
parent 267c955c88
commit 2182d39e5f
6 changed files with 50 additions and 28 deletions

View file

@ -42,6 +42,7 @@ namespace SourceGit {
public static AppBuilder BuildAvaloniaApp() {
var builder = AppBuilder.Configure<App>();
builder.UsePlatformDetect();
builder.LogToTrace();
builder.ConfigureFonts(manager => {
var monospace = new EmbeddedFontCollection(
new Uri("fonts:SourceGit", UriKind.Absolute),
@ -49,27 +50,7 @@ namespace SourceGit {
manager.AddFontCollection(monospace);
});
if (OperatingSystem.IsWindows()) {
builder.With(new FontManagerOptions() {
DefaultFamilyName = "Microsoft YaHei UI",
FontFallbacks = [
new FontFallback { FontFamily = new FontFamily("Microsoft YaHei UI") }
]
});
} else if (OperatingSystem.IsMacOS()) {
builder.With(new FontManagerOptions() {
DefaultFamilyName = "PingFang SC",
FontFallbacks = [
new FontFallback { FontFamily = new FontFamily("PingFang SC") }
]
});
builder.With(new MacOSPlatformOptions() {
DisableDefaultApplicationMenuItems = true,
DisableNativeMenus = true,
});
}
builder.LogToTrace();
Native.OS.SetupFonts(builder);
return builder;
}
@ -104,11 +85,11 @@ namespace SourceGit {
public static void SetTheme(string theme) {
if (theme.Equals("Light", StringComparison.OrdinalIgnoreCase)) {
App.Current.RequestedThemeVariant = ThemeVariant.Light;
Current.RequestedThemeVariant = ThemeVariant.Light;
} else if (theme.Equals("Dark", StringComparison.OrdinalIgnoreCase)) {
App.Current.RequestedThemeVariant = ThemeVariant.Dark;
Current.RequestedThemeVariant = ThemeVariant.Dark;
} else {
App.Current.RequestedThemeVariant = ThemeVariant.Default;
Current.RequestedThemeVariant = ThemeVariant.Default;
}
}

View file

@ -1,10 +1,17 @@
using System.Diagnostics;
using Avalonia;
using System.Diagnostics;
using System.IO;
using System.Runtime.Versioning;
namespace SourceGit.Native {
[SupportedOSPlatform("linux")]
internal class Linux : OS.IBackend {
public void SetupFonts(AppBuilder builder) {
#if USE_FONT_INTER
builder.WithInterFont();
#endif
}
public string FindGitExecutable() {
if (File.Exists("/usr/bin/git")) return "/usr/bin/git";
return string.Empty;

View file

@ -1,4 +1,6 @@
using System.Diagnostics;
using Avalonia;
using Avalonia.Media;
using System.Diagnostics;
using System.IO;
using System.Runtime.Versioning;
using System.Text;
@ -6,6 +8,15 @@ using System.Text;
namespace SourceGit.Native {
[SupportedOSPlatform("macOS")]
internal class MacOS : OS.IBackend {
public void SetupFonts(AppBuilder builder) {
builder.With(new FontManagerOptions() {
DefaultFamilyName = "PingFang SC",
FontFallbacks = [
new FontFallback { FontFamily = new FontFamily("PingFang SC") }
]
});
}
public string FindGitExecutable() {
if (File.Exists("/usr/bin/git")) return "/usr/bin/git";
return string.Empty;

View file

@ -1,9 +1,12 @@
using System;
using Avalonia;
using System;
using System.Diagnostics;
namespace SourceGit.Native {
public static class OS {
public interface IBackend {
void SetupFonts(AppBuilder builder);
string FindGitExecutable();
string FindVSCode();
@ -38,6 +41,10 @@ namespace SourceGit.Native {
}
}
public static void SetupFonts(AppBuilder builder) {
_backend?.SetupFonts(builder);
}
public static string FindGitExecutable() {
return _backend?.FindGitExecutable();
}

View file

@ -1,4 +1,6 @@
using System;
using Avalonia;
using Avalonia.Media;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
@ -11,6 +13,15 @@ namespace SourceGit.Native {
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs);
public void SetupFonts(AppBuilder builder) {
builder.With(new FontManagerOptions() {
DefaultFamilyName = "Microsoft YaHei UI",
FontFallbacks = [
new FontFallback { FontFamily = new FontFamily("Microsoft YaHei UI") }
]
});
}
public string FindGitExecutable() {
var reg = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.LocalMachine,

View file

@ -20,6 +20,10 @@
<RepositoryType>Public</RepositoryType>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
<DefineConstants>$(DefineConstants);USE_FONT_INTER</DefineConstants>
</PropertyGroup>
<ItemGroup>
<AvaloniaResource Include="App.ico" />
<AvaloniaResource Include="Resources/Fonts/*" />
@ -27,6 +31,7 @@
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.10" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.10" Condition="$([MSBuild]::IsOSPlatform('Linux'))" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.10" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.10" />
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.0.10" />