From 8f3e1789856bf475e96e961f653287d4d6377f9b Mon Sep 17 00:00:00 2001 From: walterlv Date: Thu, 28 Mar 2024 19:20:50 +0800 Subject: [PATCH] Use system API to get default font name --- src/SourceGit/Native/Windows.cs | 6 +- .../Native/WindowsNative/SystemParameters.cs | 75 +++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/SourceGit/Native/WindowsNative/SystemParameters.cs diff --git a/src/SourceGit/Native/Windows.cs b/src/SourceGit/Native/Windows.cs index 7ae2971d..248798d5 100644 --- a/src/SourceGit/Native/Windows.cs +++ b/src/SourceGit/Native/Windows.cs @@ -9,6 +9,8 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Media; +using SourceGit.Native.WindowsNative; + namespace SourceGit.Native { [SupportedOSPlatform("windows")] @@ -57,8 +59,8 @@ namespace SourceGit.Native { builder.With(new FontManagerOptions() { - DefaultFamilyName = "Microsoft YaHei UI", - FontFallbacks = [new FontFallback { FontFamily = "Microsoft YaHei" }], + DefaultFamilyName = SystemParameters.GetSystemDefaultFontName() ?? "Segoe UI", + FontFallbacks = [new FontFallback { FontFamily = "Segoe UI" }], }); // Fix drop shadow issue on Windows 10 diff --git a/src/SourceGit/Native/WindowsNative/SystemParameters.cs b/src/SourceGit/Native/WindowsNative/SystemParameters.cs new file mode 100644 index 00000000..cf4e577b --- /dev/null +++ b/src/SourceGit/Native/WindowsNative/SystemParameters.cs @@ -0,0 +1,75 @@ +#nullable enable + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Text; +using System.Threading.Tasks; + +namespace SourceGit.Native.WindowsNative +{ + [SupportedOSPlatform("windows")] + internal static class SystemParameters + { + public static string? GetSystemDefaultFontName() + { + var ncm = new NONCLIENTMETRICS + { + cbSize = Marshal.SizeOf(typeof(NONCLIENTMETRICS)) + }; + + return SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, ref ncm, 0) + ? ncm.lfMessageFont.lfFaceName + : null; + } + + private const int SPI_GETNONCLIENTMETRICS = 0x0029; + + [DllImport("user32.dll", CharSet = CharSet.Auto)] + private static extern bool SystemParametersInfo(int uAction, int uParam, ref NONCLIENTMETRICS lpvParam, int fuWinIni); + + [StructLayout(LayoutKind.Sequential)] + internal struct NONCLIENTMETRICS + { + public int cbSize; + public int iBorderWidth; + public int iScrollWidth; + public int iScrollHeight; + public int iCaptionWidth; + public int iCaptionHeight; + public LOGFONT lfCaptionFont; + public int iSmCaptionWidth; + public int iSmCaptionHeight; + public LOGFONT lfSmCaptionFont; + public int iMenuWidth; + public int iMenuHeight; + public LOGFONT lfMenuFont; + public LOGFONT lfStatusFont; + public LOGFONT lfMessageFont; + [SupportedOSPlatform("windows6.0.6000" /* Vista */)] + public int iPaddedBorderWidth; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + internal struct LOGFONT + { + public int lfHeight; + public int lfWidth; + public int lfEscapement; + public int lfOrientation; + public int lfWeight; + public byte lfItalic; + public byte lfUnderline; + public byte lfStrikeOut; + public byte lfCharSet; + public byte lfOutPrecision; + public byte lfClipPrecision; + public byte lfQuality; + public byte lfPitchAndFamily; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] + public string lfFaceName; + } + } +}