Add font settings for Desktop

pull/5940/head
2dust 2024-10-27 14:59:06 +08:00
parent 1273d2aee1
commit b7f4fd7469
8 changed files with 135 additions and 125 deletions

View File

@ -818,6 +818,13 @@ namespace ServiceLib.Common
return await GetCliWrapOutput("/bin/bash", arg); return await GetCliWrapOutput("/bin/bash", arg);
} }
public static async Task<string?> GetLinuxFontFamily(string lang)
{
// var arg = new List<string>() { "-c", $"fc-list :lang={lang} family" };
var arg = new List<string>() { "-c", $"fc-list : family" };
return await GetCliWrapOutput("/bin/bash", arg);
}
#endregion Platform #endregion Platform
} }
} }

View File

@ -2860,6 +2860,15 @@ namespace ServiceLib.Resx {
} }
} }
/// <summary>
/// 查找类似 Install the font to the system and restart the settings 的本地化字符串。
/// </summary>
public static string TbSettingsCurrentFontFamilyLinuxTip {
get {
return ResourceManager.GetString("TbSettingsCurrentFontFamilyLinuxTip", resourceCulture);
}
}
/// <summary> /// <summary>
/// 查找类似 Copy the font TTF/TTC file to the directory guiFonts, restart the settings 的本地化字符串。 /// 查找类似 Copy the font TTF/TTC file to the directory guiFonts, restart the settings 的本地化字符串。
/// </summary> /// </summary>

View File

@ -1360,4 +1360,7 @@
<data name="InsecureUrlProtocol" xml:space="preserve"> <data name="InsecureUrlProtocol" xml:space="preserve">
<value>Please do not use the insecure HTTP protocol subscription address</value> <value>Please do not use the insecure HTTP protocol subscription address</value>
</data> </data>
<data name="TbSettingsCurrentFontFamilyLinuxTip" xml:space="preserve">
<value>Install the font to the system and restart the settings</value>
</data>
</root> </root>

View File

@ -1357,4 +1357,7 @@
<data name="InsecureUrlProtocol" xml:space="preserve"> <data name="InsecureUrlProtocol" xml:space="preserve">
<value>请不要使用不安全的HTTP协议订阅地址</value> <value>请不要使用不安全的HTTP协议订阅地址</value>
</data> </data>
<data name="TbSettingsCurrentFontFamilyLinuxTip" xml:space="preserve">
<value>安装字体到系统中,重启设置</value>
</data>
</root> </root>

View File

@ -1237,4 +1237,7 @@
<data name="InsecureUrlProtocol" xml:space="preserve"> <data name="InsecureUrlProtocol" xml:space="preserve">
<value>請不要使用不安全的HTTP協定訂閱位址</value> <value>請不要使用不安全的HTTP協定訂閱位址</value>
</data> </data>
<data name="TbSettingsCurrentFontFamilyLinuxTip" xml:space="preserve">
<value>安裝字體到系統中,重新啟動設定</value>
</data>
</root> </root>

View File

@ -1,5 +1,7 @@
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media;
using Avalonia.Styling; using Avalonia.Styling;
using ReactiveUI; using ReactiveUI;
using ReactiveUI.Fody.Helpers; using ReactiveUI.Fody.Helpers;
@ -9,14 +11,11 @@ namespace v2rayN.Desktop.ViewModels
{ {
public class ThemeSettingViewModel : MyReactiveObject public class ThemeSettingViewModel : MyReactiveObject
{ {
[Reactive] [Reactive] public bool ColorModeDark { get; set; }
public bool ColorModeDark { get; set; }
[Reactive] [Reactive] public int CurrentFontSize { get; set; }
public int CurrentFontSize { get; set; }
[Reactive] [Reactive] public string CurrentLanguage { get; set; }
public string CurrentLanguage { get; set; }
public ThemeSettingViewModel() public ThemeSettingViewModel()
{ {
@ -29,6 +28,7 @@ namespace v2rayN.Desktop.ViewModels
private void RestoreUI() private void RestoreUI()
{ {
ModifyTheme(_config.UiItem.ColorModeDark); ModifyTheme(_config.UiItem.ColorModeDark);
ModifyFontFamily();
} }
private void BindingUI() private void BindingUI()
@ -38,34 +38,34 @@ namespace v2rayN.Desktop.ViewModels
CurrentLanguage = _config.UiItem.CurrentLanguage; CurrentLanguage = _config.UiItem.CurrentLanguage;
this.WhenAnyValue(x => x.ColorModeDark) this.WhenAnyValue(x => x.ColorModeDark)
.Subscribe(c => .Subscribe(c =>
{ {
if (_config.UiItem.ColorModeDark != ColorModeDark) if (_config.UiItem.ColorModeDark != ColorModeDark)
{ {
_config.UiItem.ColorModeDark = ColorModeDark; _config.UiItem.ColorModeDark = ColorModeDark;
ModifyTheme(ColorModeDark); ModifyTheme(ColorModeDark);
ConfigHandler.SaveConfig(_config); ConfigHandler.SaveConfig(_config);
} }
}); });
this.WhenAnyValue( this.WhenAnyValue(
x => x.CurrentFontSize, x => x.CurrentFontSize,
y => y > 0) y => y > 0)
.Subscribe(c => .Subscribe(c =>
{ {
if (CurrentFontSize >= Global.MinFontSize) if (CurrentFontSize >= Global.MinFontSize)
{ {
_config.UiItem.CurrentFontSize = CurrentFontSize; _config.UiItem.CurrentFontSize = CurrentFontSize;
double size = CurrentFontSize; double size = CurrentFontSize;
ModifyFontSize(size); ModifyFontSize(size);
ConfigHandler.SaveConfig(_config); ConfigHandler.SaveConfig(_config);
} }
}); });
this.WhenAnyValue( this.WhenAnyValue(
x => x.CurrentLanguage, x => x.CurrentLanguage,
y => y != null && !y.IsNullOrEmpty()) y => y != null && !y.IsNullOrEmpty())
.Subscribe(c => .Subscribe(c =>
{ {
if (Utils.IsNotEmpty(CurrentLanguage) && _config.UiItem.CurrentLanguage != CurrentLanguage) if (Utils.IsNotEmpty(CurrentLanguage) && _config.UiItem.CurrentLanguage != CurrentLanguage)
@ -89,53 +89,51 @@ namespace v2rayN.Desktop.ViewModels
private void ModifyFontSize(double size) private void ModifyFontSize(double size)
{ {
Style buttonStyle = new(x => x.OfType<Button>()); Style style = new(x => Selectors.Or(
buttonStyle.Add(new Setter() x.OfType<Button>(),
x.OfType<TextBox>(),
x.OfType<TextBlock>(),
x.OfType<Menu>(),
x.OfType<DataGridRow>(),
x.OfType<ListBoxItem>()
));
style.Add(new Setter()
{ {
Property = Button.FontSizeProperty, Property = TemplatedControl.FontSizeProperty,
Value = size, Value = size,
}); });
Application.Current?.Styles.Add(buttonStyle); Application.Current?.Styles.Add(style);
}
Style textStyle = new(x => x.OfType<TextBox>()); private void ModifyFontFamily()
textStyle.Add(new Setter() {
var currentFontFamily = _config.UiItem.CurrentFontFamily;
if (currentFontFamily.IsNullOrEmpty())
{ {
Property = TextBox.FontSizeProperty, return;
Value = size, }
});
Application.Current?.Styles.Add(textStyle);
Style textBlockStyle = new(x => x.OfType<TextBlock>()); try
textBlockStyle.Add(new Setter()
{ {
Property = TextBlock.FontSizeProperty, Style style = new(x => Selectors.Or(
Value = size, x.OfType<Button>(),
}); x.OfType<TextBox>(),
Application.Current?.Styles.Add(textBlockStyle); x.OfType<TextBlock>(),
x.OfType<Menu>(),
Style menuStyle = new(x => x.OfType<Menu>()); x.OfType<DataGridRow>(),
menuStyle.Add(new Setter() x.OfType<ListBoxItem>()
));
style.Add(new Setter()
{
Property = TemplatedControl.FontFamilyProperty,
Value = new FontFamily(currentFontFamily),
});
Application.Current?.Styles.Add(style);
}
catch (Exception ex)
{ {
Property = Menu.FontSizeProperty, Logging.SaveLog("ModifyFontFamily", ex);
Value = size, }
});
Application.Current?.Styles.Add(menuStyle);
Style dataStyle = new(x => x.OfType<DataGridRow>());
dataStyle.Add(new Setter()
{
Property = DataGridRow.FontSizeProperty,
Value = size,
});
Application.Current?.Styles.Add(dataStyle);
Style listStyle = new(x => x.OfType<ListBoxItem>());
listStyle.Add(new Setter()
{
Property = ListBoxItem.FontSizeProperty,
Value = size,
});
Application.Current?.Styles.Add(listStyle);
} }
} }
} }

View File

@ -533,7 +533,6 @@
Grid.Column="0" Grid.Column="0"
VerticalAlignment="Center" VerticalAlignment="Center"
Classes="Margin8" Classes="Margin8"
IsVisible="False"
Text="{x:Static resx:ResUI.TbSettingsCurrentFontFamily}" /> Text="{x:Static resx:ResUI.TbSettingsCurrentFontFamily}" />
<ComboBox <ComboBox
x:Name="cmbcurrentFontFamily" x:Name="cmbcurrentFontFamily"
@ -541,15 +540,13 @@
Grid.Column="1" Grid.Column="1"
Width="200" Width="200"
Classes="Margin8" Classes="Margin8"
IsVisible="False"
MaxDropDownHeight="1000" /> MaxDropDownHeight="1000" />
<TextBlock <TextBlock
Grid.Row="16" Grid.Row="16"
Grid.Column="2" Grid.Column="2"
VerticalAlignment="Center" VerticalAlignment="Center"
Classes="Margin8" Classes="Margin8"
IsVisible="False" Text="{x:Static resx:ResUI.TbSettingsCurrentFontFamilyLinuxTip}"
Text="{x:Static resx:ResUI.TbSettingsCurrentFontFamilyTip}"
TextWrapping="Wrap" /> TextWrapping="Wrap" />
<TextBlock <TextBlock
@ -677,7 +674,6 @@
Classes="Margin8" Classes="Margin8"
Text="{x:Static resx:ResUI.TbSettingsChinaUserTip}" Text="{x:Static resx:ResUI.TbSettingsChinaUserTip}"
TextWrapping="Wrap" /> TextWrapping="Wrap" />
</Grid> </Grid>
</ScrollViewer> </ScrollViewer>
</TabItem> </TabItem>

View File

@ -15,7 +15,6 @@ namespace v2rayN.Desktop.Views
btnCancel.Click += (s, e) => this.Close(); btnCancel.Click += (s, e) => this.Close();
_config = AppHandler.Instance.Config; _config = AppHandler.Instance.Config;
// var lstFonts = GetFonts(Utils.GetFontsPath());
ViewModel = new OptionSettingViewModel(UpdateViewHandler); ViewModel = new OptionSettingViewModel(UpdateViewHandler);
@ -100,9 +99,6 @@ namespace v2rayN.Desktop.Views
cmbMainGirdOrientation.Items.Add(it.ToString()); cmbMainGirdOrientation.Items.Add(it.ToString());
} }
//lstFonts.ForEach(it => { cmbcurrentFontFamily.Items.Add(it); });
//cmbcurrentFontFamily.Items.Add(string.Empty);
this.WhenActivated(disposables => this.WhenActivated(disposables =>
{ {
this.Bind(ViewModel, vm => vm.localPort, v => v.txtlocalPort.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.localPort, v => v.txtlocalPort.Text).DisposeWith(disposables);
@ -182,58 +178,53 @@ namespace v2rayN.Desktop.Views
// WindowsUtils.SetAutoRun(Global.AutoRunRegPath, Global.AutoRunName, togAutoRun.IsChecked ?? false); // WindowsUtils.SetAutoRun(Global.AutoRunRegPath, Global.AutoRunName, togAutoRun.IsChecked ?? false);
this.Close(true); this.Close(true);
break; break;
case EViewAction.InitSettingFont:
await InitSettingFont();
break;
} }
return await Task.FromResult(true); return await Task.FromResult(true);
} }
//private List<string> GetFonts(string path) private async Task InitSettingFont()
//{ {
// var lstFonts = new List<string>(); var lstFonts = await GetFonts();
// try lstFonts.ForEach(it => { cmbcurrentFontFamily.Items.Add(it); });
// { cmbcurrentFontFamily.Items.Add(string.Empty);
// string[] searchPatterns = { "*.ttf", "*.ttc" }; }
// var files = new List<string>();
// foreach (var pattern in searchPatterns) private async Task<List<string>> GetFonts()
// { {
// files.AddRange(Directory.GetFiles(path, pattern)); var lstFonts = new List<string>();
// } try
// var culture = _config.uiItem.currentLanguage == Global.Languages[0] ? "zh-cn" : "en-us"; {
// var culture2 = "en-us"; if (Utils.IsWindows())
// foreach (var ttf in files) {
// { return lstFonts;
// var families = Fonts.GetFontFamilies(Utils.GetFontsPath(ttf)); }
// foreach (FontFamily family in families) else if (Utils.IsLinux())
// { {
// var typefaces = family.GetTypefaces(); var result = await Utils.GetLinuxFontFamily("zh");
// foreach (Typeface typeface in typefaces) if (result.IsNullOrEmpty())
// { {
// typeface.TryGetGlyphTypeface(out GlyphTypeface glyph); return lstFonts;
// //var fontFace = glyph.Win32FaceNames[new CultureInfo("en-us")]; }
// //if (!fontFace.Equals("Regular") && !fontFace.Equals("Normal"))
// //{ var lst = result.Split(Environment.NewLine)
// // continue; .Where(t => t.IsNotEmpty())
// //} .ToList()
// var fontFamily = glyph.Win32FamilyNames[new CultureInfo(culture)]; .Select(t => t.Split(",").FirstOrDefault() ?? "")
// if (Utils.IsNullOrEmpty(fontFamily)) .OrderBy(t => t)
// { .ToList();
// fontFamily = glyph.Win32FamilyNames[new CultureInfo(culture2)]; return lst;
// if (Utils.IsNullOrEmpty(fontFamily)) }
// { }
// continue; catch (Exception ex)
// } {
// } Logging.SaveLog("fill fonts error", ex);
// lstFonts.Add(fontFamily); }
// break; return lstFonts;
// } }
// }
// }
// }
// catch (Exception ex)
// {
// Logging.SaveLog("fill fonts error", ex);
// }
// return lstFonts;
//}
private void ClbdestOverride_SelectionChanged(object? sender, SelectionChangedEventArgs e) private void ClbdestOverride_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{ {