From 2182d39e5f65ce7d9c35ca24d8344b0f0c275490 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 8 Mar 2024 12:22:22 +0800 Subject: [PATCH] refactor: setup fonts based on operating system --- src/App.axaml.cs | 29 +++++------------------------ src/Native/Linux.cs | 9 ++++++++- src/Native/MacOS.cs | 13 ++++++++++++- src/Native/OS.cs | 9 ++++++++- src/Native/Windows.cs | 13 ++++++++++++- src/SourceGit.csproj | 5 +++++ 6 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 98425fc3..98df9dbf 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -42,6 +42,7 @@ namespace SourceGit { public static AppBuilder BuildAvaloniaApp() { var builder = AppBuilder.Configure(); 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; } } diff --git a/src/Native/Linux.cs b/src/Native/Linux.cs index bf1dcca2..7776a98f 100644 --- a/src/Native/Linux.cs +++ b/src/Native/Linux.cs @@ -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; diff --git a/src/Native/MacOS.cs b/src/Native/MacOS.cs index 4d64614d..c2ccf34b 100644 --- a/src/Native/MacOS.cs +++ b/src/Native/MacOS.cs @@ -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; diff --git a/src/Native/OS.cs b/src/Native/OS.cs index c73c7eb3..747606fc 100644 --- a/src/Native/OS.cs +++ b/src/Native/OS.cs @@ -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(); } diff --git a/src/Native/Windows.cs b/src/Native/Windows.cs index 9cf7cef7..fa7c2b73 100644 --- a/src/Native/Windows.cs +++ b/src/Native/Windows.cs @@ -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, diff --git a/src/SourceGit.csproj b/src/SourceGit.csproj index 74a14b2d..65138026 100644 --- a/src/SourceGit.csproj +++ b/src/SourceGit.csproj @@ -19,6 +19,10 @@ https://github.com/sourcegit-scm/sourcegit.git Public + + + $(DefineConstants);USE_FONT_INTER + @@ -27,6 +31,7 @@ +