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() { public static AppBuilder BuildAvaloniaApp() {
var builder = AppBuilder.Configure<App>(); var builder = AppBuilder.Configure<App>();
builder.UsePlatformDetect(); builder.UsePlatformDetect();
builder.LogToTrace();
builder.ConfigureFonts(manager => { builder.ConfigureFonts(manager => {
var monospace = new EmbeddedFontCollection( var monospace = new EmbeddedFontCollection(
new Uri("fonts:SourceGit", UriKind.Absolute), new Uri("fonts:SourceGit", UriKind.Absolute),
@ -49,27 +50,7 @@ namespace SourceGit {
manager.AddFontCollection(monospace); manager.AddFontCollection(monospace);
}); });
if (OperatingSystem.IsWindows()) { Native.OS.SetupFonts(builder);
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();
return builder; return builder;
} }
@ -104,11 +85,11 @@ namespace SourceGit {
public static void SetTheme(string theme) { public static void SetTheme(string theme) {
if (theme.Equals("Light", StringComparison.OrdinalIgnoreCase)) { if (theme.Equals("Light", StringComparison.OrdinalIgnoreCase)) {
App.Current.RequestedThemeVariant = ThemeVariant.Light; Current.RequestedThemeVariant = ThemeVariant.Light;
} else if (theme.Equals("Dark", StringComparison.OrdinalIgnoreCase)) { } else if (theme.Equals("Dark", StringComparison.OrdinalIgnoreCase)) {
App.Current.RequestedThemeVariant = ThemeVariant.Dark; Current.RequestedThemeVariant = ThemeVariant.Dark;
} else { } 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.IO;
using System.Runtime.Versioning; using System.Runtime.Versioning;
namespace SourceGit.Native { namespace SourceGit.Native {
[SupportedOSPlatform("linux")] [SupportedOSPlatform("linux")]
internal class Linux : OS.IBackend { internal class Linux : OS.IBackend {
public void SetupFonts(AppBuilder builder) {
#if USE_FONT_INTER
builder.WithInterFont();
#endif
}
public string FindGitExecutable() { public string FindGitExecutable() {
if (File.Exists("/usr/bin/git")) return "/usr/bin/git"; if (File.Exists("/usr/bin/git")) return "/usr/bin/git";
return string.Empty; 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.IO;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using System.Text; using System.Text;
@ -6,6 +8,15 @@ using System.Text;
namespace SourceGit.Native { namespace SourceGit.Native {
[SupportedOSPlatform("macOS")] [SupportedOSPlatform("macOS")]
internal class MacOS : OS.IBackend { 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() { public string FindGitExecutable() {
if (File.Exists("/usr/bin/git")) return "/usr/bin/git"; if (File.Exists("/usr/bin/git")) return "/usr/bin/git";
return string.Empty; return string.Empty;

View file

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

View file

@ -1,4 +1,6 @@
using System; using Avalonia;
using Avalonia.Media;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -11,6 +13,15 @@ namespace SourceGit.Native {
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)] [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs); 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() { public string FindGitExecutable() {
var reg = Microsoft.Win32.RegistryKey.OpenBaseKey( var reg = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.LocalMachine, Microsoft.Win32.RegistryHive.LocalMachine,

View file

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