Skip to content

Commit 1cd6998

Browse files
committed
Cleanup codes
1 parent 9981fab commit 1cd6998

File tree

3 files changed

+83
-79
lines changed

3 files changed

+83
-79
lines changed

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 9 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
using System.Collections.Generic;
22
using System.Collections.ObjectModel;
3-
using System.Diagnostics;
4-
using System.Drawing;
5-
using System.Globalization;
6-
using System.Linq;
73
using System.Text.Json.Serialization;
84
using System.Windows;
9-
using System.Windows.Media;
105
using CommunityToolkit.Mvvm.DependencyInjection;
116
using Flow.Launcher.Infrastructure.Hotkey;
127
using Flow.Launcher.Infrastructure.Logger;
138
using Flow.Launcher.Infrastructure.Storage;
149
using Flow.Launcher.Plugin;
1510
using Flow.Launcher.Plugin.SharedModels;
1611
using Flow.Launcher.ViewModel;
17-
using SystemFonts = System.Windows.SystemFonts;
1812

1913
namespace Flow.Launcher.Infrastructure.UserSettings
2014
{
@@ -37,67 +31,6 @@ public void Save()
3731
{
3832
_storage.Save();
3933
}
40-
41-
private string language = Constant.SystemLanguageCode;
42-
private static readonly Dictionary<string, string> LanguageToNotoSans = new()
43-
{
44-
{ "ko", "Noto Sans KR" },
45-
{ "ja", "Noto Sans JP" },
46-
{ "zh-CN", "Noto Sans SC" },
47-
{ "zh-SG", "Noto Sans SC" },
48-
{ "zh-Hans", "Noto Sans SC" },
49-
{ "zh-TW", "Noto Sans TC" },
50-
{ "zh-HK", "Noto Sans TC" },
51-
{ "zh-MO", "Noto Sans TC" },
52-
{ "zh-Hant", "Noto Sans TC" },
53-
{ "th", "Noto Sans Thai" },
54-
{ "ar", "Noto Sans Arabic" },
55-
{ "he", "Noto Sans Hebrew" },
56-
{ "hi", "Noto Sans Devanagari" },
57-
{ "bn", "Noto Sans Bengali" },
58-
{ "ta", "Noto Sans Tamil" },
59-
{ "el", "Noto Sans Greek" },
60-
{ "ru", "Noto Sans" },
61-
{ "en", "Noto Sans" },
62-
{ "fr", "Noto Sans" },
63-
{ "de", "Noto Sans" },
64-
{ "es", "Noto Sans" },
65-
{ "pt", "Noto Sans" }
66-
};
67-
68-
public static string GetSystemDefaultFont()
69-
{
70-
try
71-
{
72-
var culture = CultureInfo.CurrentCulture;
73-
var language = culture.Name; // e.g., "zh-TW"
74-
var langPrefix = language.Split('-')[0]; // e.g., "zh"
75-
76-
// First, try to find by full name, and if not found, fallback to prefix
77-
if (TryGetNotoFont(language, out var notoFont) || TryGetNotoFont(langPrefix, out notoFont))
78-
{
79-
if (Fonts.SystemFontFamilies.Any(f => f.Source.Equals(notoFont)))
80-
return notoFont;
81-
}
82-
83-
var font = SystemFonts.MessageFontFamily;
84-
if (font.FamilyNames.TryGetValue(System.Windows.Markup.XmlLanguage.GetLanguage("en-US"), out var englishName))
85-
{
86-
return englishName;
87-
}
88-
89-
return font.Source ?? "Segoe UI";
90-
}
91-
catch
92-
{
93-
return "Segoe UI";
94-
}
95-
}
96-
97-
private static bool TryGetNotoFont(string langKey, out string notoFont)
98-
{
99-
return LanguageToNotoSans.TryGetValue(langKey, out notoFont);
100-
}
10134

10235
private string _theme = Constant.DefaultTheme;
10336
public string Hotkey { get; set; } = $"{KeyConstant.Alt} + {KeyConstant.Space}";
@@ -119,12 +52,13 @@ private static bool TryGetNotoFont(string langKey, out string notoFont)
11952
public string CycleHistoryUpHotkey { get; set; } = $"{KeyConstant.Alt} + Up";
12053
public string CycleHistoryDownHotkey { get; set; } = $"{KeyConstant.Alt} + Down";
12154

55+
private string _language = Constant.SystemLanguageCode;
12256
public string Language
12357
{
124-
get => language;
58+
get => _language;
12559
set
12660
{
127-
language = value;
61+
_language = value;
12862
OnPropertyChanged();
12963
}
13064
}
@@ -150,15 +84,15 @@ public string Theme
15084
public double QueryBoxFontSize { get; set; } = 16;
15185
public double ResultItemFontSize { get; set; } = 16;
15286
public double ResultSubItemFontSize { get; set; } = 13;
153-
public string QueryBoxFont { get; set; } = GetSystemDefaultFont();
87+
public string QueryBoxFont { get; set; } = Win32Helper.GetSystemDefaultFont();
15488
public string QueryBoxFontStyle { get; set; }
15589
public string QueryBoxFontWeight { get; set; }
15690
public string QueryBoxFontStretch { get; set; }
157-
public string ResultFont { get; set; } = GetSystemDefaultFont();
91+
public string ResultFont { get; set; } = Win32Helper.GetSystemDefaultFont();
15892
public string ResultFontStyle { get; set; }
15993
public string ResultFontWeight { get; set; }
16094
public string ResultFontStretch { get; set; }
161-
public string ResultSubFont { get; set; } = GetSystemDefaultFont();
95+
public string ResultSubFont { get; set; } = Win32Helper.GetSystemDefaultFont();
16296
public string ResultSubFontStyle { get; set; }
16397
public string ResultSubFontWeight { get; set; }
16498
public string ResultSubFontStretch { get; set; }
@@ -181,7 +115,7 @@ public string Theme
181115
public double? SettingWindowLeft { get; set; } = null;
182116
public WindowState SettingWindowState { get; set; } = WindowState.Normal;
183117

184-
bool _showPlaceholder { get; set; } = true;
118+
private bool _showPlaceholder { get; set; } = true;
185119
public bool ShowPlaceholder
186120
{
187121
get => _showPlaceholder;
@@ -194,7 +128,7 @@ public bool ShowPlaceholder
194128
}
195129
}
196130
}
197-
string _placeholderText { get; set; } = string.Empty;
131+
private string _placeholderText { get; set; } = string.Empty;
198132
public string PlaceholderText
199133
{
200134
get => _placeholderText;
@@ -373,7 +307,7 @@ public bool KeepMaxResults
373307
public bool StartFlowLauncherOnSystemStartup { get; set; } = false;
374308
public bool UseLogonTaskForStartup { get; set; } = false;
375309
public bool HideOnStartup { get; set; } = true;
376-
bool _hideNotifyIcon { get; set; }
310+
private bool _hideNotifyIcon;
377311
public bool HideNotifyIcon
378312
{
379313
get => _hideNotifyIcon;
@@ -551,5 +485,4 @@ public enum BackdropTypes
551485
Mica,
552486
MicaAlt
553487
}
554-
555488
}

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using System.Diagnostics;
45
using System.Globalization;
6+
using System.Linq;
57
using System.Runtime.InteropServices;
68
using System.Windows;
79
using System.Windows.Interop;
10+
using System.Windows.Markup;
811
using System.Windows.Media;
912
using Flow.Launcher.Infrastructure.UserSettings;
1013
using Microsoft.Win32;
@@ -14,6 +17,7 @@
1417
using Windows.Win32.UI.Input.KeyboardAndMouse;
1518
using Windows.Win32.UI.WindowsAndMessaging;
1619
using Point = System.Windows.Point;
20+
using SystemFonts = System.Windows.SystemFonts;
1721

1822
namespace Flow.Launcher.Infrastructure
1923
{
@@ -595,5 +599,73 @@ public static void OpenImeSettings()
595599
}
596600

597601
#endregion
602+
603+
#region System Font
604+
605+
private static readonly Dictionary<string, string> _languageToNotoSans = new()
606+
{
607+
{ "ko", "Noto Sans KR" },
608+
{ "ja", "Noto Sans JP" },
609+
{ "zh-CN", "Noto Sans SC" },
610+
{ "zh-SG", "Noto Sans SC" },
611+
{ "zh-Hans", "Noto Sans SC" },
612+
{ "zh-TW", "Noto Sans TC" },
613+
{ "zh-HK", "Noto Sans TC" },
614+
{ "zh-MO", "Noto Sans TC" },
615+
{ "zh-Hant", "Noto Sans TC" },
616+
{ "th", "Noto Sans Thai" },
617+
{ "ar", "Noto Sans Arabic" },
618+
{ "he", "Noto Sans Hebrew" },
619+
{ "hi", "Noto Sans Devanagari" },
620+
{ "bn", "Noto Sans Bengali" },
621+
{ "ta", "Noto Sans Tamil" },
622+
{ "el", "Noto Sans Greek" },
623+
{ "ru", "Noto Sans" },
624+
{ "en", "Noto Sans" },
625+
{ "fr", "Noto Sans" },
626+
{ "de", "Noto Sans" },
627+
{ "es", "Noto Sans" },
628+
{ "pt", "Noto Sans" }
629+
};
630+
631+
public static string GetSystemDefaultFont()
632+
{
633+
try
634+
{
635+
var culture = CultureInfo.CurrentCulture;
636+
var language = culture.Name; // e.g., "zh-TW"
637+
var langPrefix = language.Split('-')[0]; // e.g., "zh"
638+
639+
// First, try to find by full name, and if not found, fallback to prefix
640+
if (TryGetNotoFont(language, out var notoFont) || TryGetNotoFont(langPrefix, out notoFont))
641+
{
642+
// If the font is installed, return it
643+
if (Fonts.SystemFontFamilies.Any(f => f.Source.Equals(notoFont)))
644+
{
645+
return notoFont;
646+
}
647+
}
648+
649+
// If Noto font is not found, fallback to the system default font
650+
var font = SystemFonts.MessageFontFamily;
651+
if (font.FamilyNames.TryGetValue(XmlLanguage.GetLanguage("en-US"), out var englishName))
652+
{
653+
return englishName;
654+
}
655+
656+
return font.Source ?? "Segoe UI";
657+
}
658+
catch
659+
{
660+
return "Segoe UI";
661+
}
662+
}
663+
664+
private static bool TryGetNotoFont(string langKey, out string notoFont)
665+
{
666+
return _languageToNotoSans.TryGetValue(langKey, out notoFont);
667+
}
668+
669+
#endregion
598670
}
599671
}

Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.Windows;
4+
using System.Windows.Controls;
55
using System.Globalization;
66
using System.IO;
77
using System.Linq;
@@ -17,13 +17,12 @@
1717
using Flow.Launcher.ViewModel;
1818
using ModernWpf;
1919
using ThemeManagerForColorSchemeSwitch = ModernWpf.ThemeManager;
20-
using System.Windows.Controls;
2120

2221
namespace Flow.Launcher.SettingPages.ViewModels;
2322

2423
public partial class SettingsPaneThemeViewModel : BaseModel
2524
{
26-
private string DefaultFont = Settings.GetSystemDefaultFont();
25+
private readonly string DefaultFont = Win32Helper.GetSystemDefaultFont();
2726
public string BackdropSubText => !Win32Helper.IsBackdropSupported() ? App.API.GetTranslation("BackdropTypeDisabledToolTip") : "";
2827
public Settings Settings { get; }
2928
private readonly Theme _theme = Ioc.Default.GetRequiredService<Theme>();

0 commit comments

Comments
 (0)