Skip to content

Commit

Permalink
Add font size and font family select
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Feb 14, 2023
1 parent cc0ddbb commit 9ef371e
Show file tree
Hide file tree
Showing 22 changed files with 346 additions and 67 deletions.
46 changes: 34 additions & 12 deletions clashN/clashN/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
ShutdownMode="OnExplicitShutdown"
StartupUri="Views/MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary xmlns:system="clr-namespace:System;assembly=mscorlib">
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme
BaseTheme="Dark"
PrimaryColor="DeepPurple"
SecondaryColor="Lime" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
<system:Double x:Key="StdFontSize1">11</system:Double>
<system:Double x:Key="StdFontSize2">12</system:Double>
<system:Double x:Key="StdFontSize3">13</system:Double>
<system:Double x:Key="StdFontSize4">14</system:Double>
<Style
x:Key="MyMaterialDesignNavigationRailTabItem"
BasedOn="{StaticResource MaterialDesignNavigationRailTabItem}"
Expand All @@ -26,37 +30,37 @@
x:Key="ModuleTitle"
BasedOn="{StaticResource MaterialDesignTextBlock}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="16" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize4}" />
</Style>
<Style
x:Key="TabItemTitle"
BasedOn="{StaticResource MaterialDesignTextBlock}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="14" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize4}" />
</Style>
<Style
x:Key="ListItemTitle"
BasedOn="{StaticResource MaterialDesignTextBlock}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="13" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize3}" />
</Style>
<Style
x:Key="ListItemSubTitle"
BasedOn="{StaticResource MaterialDesignTextBlock}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="12" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize2}" />
</Style>
<Style
x:Key="ListItemSubTitle2"
BasedOn="{StaticResource MaterialDesignTextBlock}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="11" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize1}" />
</Style>
<Style
x:Key="ToolbarItem"
BasedOn="{StaticResource MaterialDesignTextBlock}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="12" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize2}" />
</Style>
<Style TargetType="{x:Type TextElement}">
<Setter Property="FontWeight" Value="Regular" />
Expand All @@ -82,29 +86,47 @@
x:Key="ListItemCheckBox"
BasedOn="{StaticResource MaterialDesignUserForegroundCheckBox}"
TargetType="{x:Type CheckBox}">
<Setter Property="FontSize" Value="12" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize2}" />
</Style>
<Style x:Key="ListItemChip" TargetType="{x:Type materialDesign:Chip}">
<Setter Property="FontSize" Value="11" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize1}" />
</Style>
<Style
x:Key="DefButton"
BasedOn="{StaticResource MaterialDesignRaisedButton}"
TargetType="{x:Type ButtonBase}">
<Setter Property="FontSize" Value="14" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize3}" />
</Style>
<Style
x:Key="DefMenu"
BasedOn="{StaticResource MaterialDesignContextMenu}"
TargetType="{x:Type ContextMenu}">
<Setter Property="FontSize" Value="13" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize3}" />
<Setter Property="FontFamily" Value="{x:Static conv:MaterialDesignFonts.MyFont}" />
</Style>
<Style
x:Key="DefComboBox"
BasedOn="{StaticResource MaterialDesignComboBox}"
TargetType="{x:Type ComboBox}">
<Setter Property="FontSize" Value="12" />
<Setter Property="FontSize" Value="{DynamicResource StdFontSize2}" />
</Style>
<Style
x:Key="DefTextBox"
BasedOn="{StaticResource MaterialDesignTextBox}"
TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="{DynamicResource StdFontSize2}" />
</Style>
<Style
x:Key="MyOutlinedTextBox"
BasedOn="{StaticResource MaterialDesignOutlinedTextBox}"
TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="{DynamicResource StdFontSize2}" />
</Style>
<Style
x:Key="DefDataGrid"
BasedOn="{StaticResource MaterialDesignDataGrid}"
TargetType="{x:Type DataGrid}">
<Setter Property="FontSize" Value="{DynamicResource StdFontSize2}" />
</Style>
</ResourceDictionary>

Expand Down
18 changes: 16 additions & 2 deletions clashN/clashN/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using clashN.Tool;
using clashN.Handler;
using clashN.Mode;
using clashN.Tool;
using System.Windows;
using System.Windows.Threading;

Expand All @@ -10,7 +12,7 @@ namespace clashN
public partial class App : Application
{
public static EventWaitHandle ProgramStarted;

private static Config _config;
public App()
{
// Locator.CurrentMutable.RegisterViewsForViewModels(Assembly.GetCallingAssembly());
Expand Down Expand Up @@ -46,12 +48,24 @@ protected override void OnStartup(StartupEventArgs e)
Utils.SaveLog($"clashN start up | {Utils.GetVersion()} | {Utils.GetExePath()}");
Logging.ClearLogs();

Init();

string lang = Utils.RegReadValue(Global.MyRegPath, Global.MyRegKeyLanguage, Global.Languages[0]);
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);

base.OnStartup(e);
}

private void Init()
{
if (ConfigHandler.LoadConfig(ref _config) != 0)
{
UI.ShowWarning($"Loading GUI configuration file is abnormal,please restart the application{Environment.NewLine}加载GUI配置文件异常,请重启应用");
Application.Current.Shutdown();
Environment.Exit(0);
return;
}
}
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Utils.SaveLog("App_DispatcherUnhandledException", e.Exception);
Expand Down
21 changes: 17 additions & 4 deletions clashN/clashN/Converters/MaterialDesignFonts.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using clashN.Handler;
using System.Windows.Media;

namespace clashN.Converters
Expand All @@ -10,8 +9,22 @@ public class MaterialDesignFonts

static MaterialDesignFonts()
{
var fontPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Resources\Fonts\");
MyFont = new FontFamily(new Uri($"file:///{fontPath}"), "./#Source Han Sans CN");
try
{
var fontFamily = LazyConfig.Instance.GetConfig().uiItem.currentFontFamily;
if (!string.IsNullOrEmpty(fontFamily))
{
var fontPath = Utils.GetFontsPath();
MyFont = new FontFamily(new Uri(@$"file:///{fontPath}\"), $"./#{fontFamily}");
}
}
catch
{
}
if (MyFont is null)
{
MyFont = new FontFamily("Microsoft YaHei");
}
}
}
}
3 changes: 2 additions & 1 deletion clashN/clashN/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Global
/// Icon
/// </summary>
public const string CustomIconName = "clashN.ico";
public const int MinFontSize = 10;


public const string StatisticLogOverall = "StatisticLogOverall.json";
Expand All @@ -108,7 +109,7 @@ class Global

public static readonly List<string> proxyVehicleType = new List<string> { "file", "http" };

public static readonly List<string> Languages = new List<string> { "zh-Hans", "en","fa-IR" };
public static readonly List<string> Languages = new List<string> { "zh-Hans", "en", "fa-IR" };

public static readonly List<string> LogLevel = new List<string> { "debug", "info", "warning", "error", "silent" };

Expand Down
8 changes: 8 additions & 0 deletions clashN/clashN/Mode/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ public class UIItem

public bool colorModeDark { get; set; }
public string? colorPrimaryName { get; set; }
public string currentFontFamily { get; set; }
public int currentFontSize { get; set; }

public int proxiesSorting { get; set; }
public bool proxiesAutoRefresh { get; set; }

public int connectionsSorting { get; set; }
public bool connectionsAutoRefresh { get; set; }

}

Expand Down
Binary file not shown.
27 changes: 27 additions & 0 deletions clashN/clashN/Resx/ResUI.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions clashN/clashN/Resx/ResUI.resx
Original file line number Diff line number Diff line change
Expand Up @@ -664,4 +664,13 @@
<data name="TbSortingType" xml:space="preserve">
<value>Type</value>
</data>
<data name="TbSettingsFontFamily" xml:space="preserve">
<value>FontFamily(Require restart)</value>
</data>
<data name="TbSettingsFontFamilyTip" xml:space="preserve">
<value>Copy the font TTF file to the directory guiFonts, restart the settings</value>
</data>
<data name="TbSettingsFontSize" xml:space="preserve">
<value>FontSize</value>
</data>
</root>
9 changes: 9 additions & 0 deletions clashN/clashN/Resx/ResUI.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,13 @@
<data name="TbSortingType" xml:space="preserve">
<value>类型</value>
</data>
<data name="TbSettingsFontFamily" xml:space="preserve">
<value>当前字体(需重启)</value>
</data>
<data name="TbSettingsFontFamilyTip" xml:space="preserve">
<value>拷贝字体TTF文件到目录guiFonts,重启</value>
</data>
<data name="TbSettingsFontSize" xml:space="preserve">
<value>字体大小</value>
</data>
</root>
16 changes: 16 additions & 0 deletions clashN/clashN/Tool/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,22 @@ public static string GetBinPath(string filename, ECoreType? coreType = null)
}
return Path.Combine(_tempPath, filename);
}
public static string GetFontsPath(string filename = "")
{
string _tempPath = Path.Combine(StartupPath(), "guiFonts");
if (!Directory.Exists(_tempPath))
{
Directory.CreateDirectory(_tempPath);
}
if (string.IsNullOrEmpty(filename))
{
return _tempPath;
}
else
{
return Path.Combine(_tempPath, filename);
}
}
#endregion

#region Log
Expand Down
19 changes: 14 additions & 5 deletions clashN/clashN/ViewModels/ConnectionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public ConnectionsViewModel()
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
_config = LazyConfig.Instance.GetConfig();

AutoRefreshInterval = 10;
SortingSelected = _config.uiItem.connectionsSorting;
AutoRefresh = _config.uiItem.connectionsAutoRefresh;

var canEditRemove = this.WhenAnyValue(
x => x.SelectedSource,
selectedSource => selectedSource != null && !selectedSource.id.IsNullOrEmpty());
Expand All @@ -46,6 +50,11 @@ public ConnectionsViewModel()
y => y != null && y >= 0)
.Subscribe(c => DoSortingSelected(c));

this.WhenAnyValue(
x => x.AutoRefresh,
y => y == true)
.Subscribe(c => { _config.uiItem.connectionsAutoRefresh = AutoRefresh; });

ConnectionCloseCmd = ReactiveCommand.Create(() =>
{
ClashConnectionClose(false);
Expand All @@ -56,10 +65,6 @@ public ConnectionsViewModel()
ClashConnectionClose(true);
});

AutoRefreshInterval = 10;
SortingSelected = 4;
AutoRefresh = true;

Init();
}

Expand All @@ -69,6 +74,10 @@ void DoSortingSelected(bool c)
{
return;
}
if (SortingSelected != _config.uiItem.connectionsSorting)
{
_config.uiItem.connectionsSorting = SortingSelected;
}

GetClashConnections();
}
Expand Down Expand Up @@ -128,7 +137,7 @@ private void RefreshConnections(List<ConnectionItem> connections)
model.id = item.id;
model.network = item.metadata.network;
model.type = item.metadata.type;
model.host = $"{item.metadata.host}:{item.metadata.destinationPort}";
model.host = $"{(item.metadata.host.IsNullOrEmpty() ? item.metadata.destinationIP : item.metadata.host)}:{item.metadata.destinationPort}";
var sp = (dtNow - item.start);
model.time = sp.TotalSeconds < 0 ? 1 : sp.TotalSeconds;
model.upload = item.upload;
Expand Down
12 changes: 5 additions & 7 deletions clashN/clashN/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public class MainWindowViewModel : ReactiveObject

public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
{
_config = LazyConfig.Instance.GetConfig();

Locator.CurrentMutable.RegisterLazySingleton(() => new NoticeHandler(snackbarMessageQueue), typeof(NoticeHandler));
_noticeHandler = Locator.Current.GetService<NoticeHandler>();

Expand Down Expand Up @@ -264,12 +266,8 @@ private void OnHotkeyHandler(object sender, HotkeyEventArgs e)

private void Init()
{
if (ConfigHandler.LoadConfig(ref _config) != 0)
{
UI.ShowWarning($"Loading GUI configuration file is abnormal,please restart the application{Environment.NewLine}加载GUI配置文件异常,请重启应用");
Environment.Exit(0);
return;
}


MainFormHandler.Instance.BackupGuiNConfig(_config, true);
MainFormHandler.Instance.InitRegister(_config);

Expand All @@ -293,7 +291,7 @@ private void UpdateHandler(bool notify, string msg)
{
if (notify)
{
_noticeHandler?.Enqueue(msg);
_noticeHandler?.Enqueue(msg);
_noticeHandler?.SendMessage(msg);
}
else
Expand Down
Loading

0 comments on commit 9ef371e

Please sign in to comment.