Skip to content

Commit

Permalink
feat: Add font modification function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Jan 18, 2024
1 parent a7ee7e9 commit 98e62f6
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 16 deletions.
5 changes: 5 additions & 0 deletions STranslate.Model/ConfigModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public partial class ConfigModel : ObservableObject
/// </summary>
public string Position { get; set; } = string.Empty;

/// <summary>
/// 自定义字体
/// </summary>
public string CustomFont { get; set; } = ConstStr.DEFAULTFONTNAME;

/// <summary>
/// 服务
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions STranslate.Model/ConstStr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ public static class ConstStr
public const string DEFAULTOPENHOTKEY = "Alt + G";
public const string DEFAULTMOUSEHOOKHOTKEY = "Alt + Shift + D";
public const string DEFAULTOCRHOTKEY = "Alt + Shift + S";

public const string USERDEFINEFONTKEY = "UserFont";
public const string DEFAULTFONTNAME = "LXGW WenKai";
}
}
2 changes: 1 addition & 1 deletion STranslate.Style/Controls/MessageBox_S.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Background="{DynamicResource NavigationBackground}"
BorderBrush="{DynamicResource BorderBrushColor}"
BorderThickness="1"
FontFamily="{DynamicResource FontFamily_LXGW}"
FontFamily="{DynamicResource UserFont}"
Icon="{DynamicResource Icon}"
ResizeMode="NoResize"
Topmost="True"
Expand Down
3 changes: 2 additions & 1 deletion STranslate.Style/Styles/FontStyle.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns:sys="clr-namespace:System;assembly=System.Runtime">

<FontFamily x:Key="IconFont">/STranslate.Style;component/Fonts/#iconfont</FontFamily>
<FontFamily x:Key="FontFamily_LXGW">pack://application:,,,/STranslate.Style;component/Fonts/#LXGW WenKai</FontFamily>
<FontFamily x:Key="UserFont">pack://application:,,,/STranslate.Style;component/Fonts/#LXGW WenKai</FontFamily>
<FontFamily x:Key="LXGW WenKai">pack://application:,,,/STranslate.Style;component/Fonts/#LXGW WenKai</FontFamily>

<sys:Double x:Key="FontSize_About">30</sys:Double>
<sys:Double x:Key="FontSize_Title">20</sys:Double>
Expand Down
13 changes: 13 additions & 0 deletions STranslate.Updater/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net8.0-windows\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
</PropertyGroup>
</Project>
15 changes: 15 additions & 0 deletions STranslate/Helper/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Media;

namespace STranslate
{
Expand Down Expand Up @@ -47,6 +48,18 @@ public void InitialOperate()
? ConstStr.LIGHTURI
: ConstStr.DARKURI;

//初始化字体
try
{
Application.Current.Resources[ConstStr.USERDEFINEFONTKEY] = CurrentConfig!.CustomFont.Equals(ConstStr.DEFAULTFONTNAME)
? Application.Current.Resources[ConstStr.DEFAULTFONTNAME] : new FontFamily(CurrentConfig!.CustomFont);
}
catch (Exception)
{
Application.Current.Resources[ConstStr.USERDEFINEFONTKEY] = Application.Current.Resources[ConstStr.DEFAULTFONTNAME];
CurrentConfig!.CustomFont = ConstStr.DEFAULTFONTNAME;
}

//初始化代理设置
ProxyUtil.UpdateDynamicProxy(CurrentConfig?.IsDisableSystemProxy ?? false);
}
Expand Down Expand Up @@ -146,6 +159,7 @@ public bool WriteConfig(CommonViewModel model)
CurrentConfig.IsAdjustContentTranslate = model.IsAdjustContentTranslate;
CurrentConfig.IsRemoveLineBreakGettingWords = model.IsRemoveLineBreakGettingWords;
CurrentConfig.DoubleTapTrayFunc = model.DoubleTapTrayFunc;
CurrentConfig.CustomFont = model.CustomFont;

WriteConfig(CurrentConfig);
isSuccess = true;
Expand Down Expand Up @@ -242,6 +256,7 @@ private ConfigModel InitialConfig()
IsAdjustContentTranslate = false,
IsRemoveLineBreakGettingWords = false,
DoubleTapTrayFunc = DoubleTapFuncEnum.InputFunc,
CustomFont = ConstStr.DEFAULTFONTNAME,
SourceLanguage = LanguageEnum.AUTO.GetDescription(),
TargetLanguage = LanguageEnum.AUTO.GetDescription(),
Services =
Expand Down
4 changes: 2 additions & 2 deletions STranslate/STranslate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
<PlatformTarget>x64</PlatformTarget>
<PackageReadmeFile>README.md</PackageReadmeFile>
<AssemblyVersion>1.0.2.0114</AssemblyVersion>
<FileVersion>1.0.2.0114</FileVersion>
<AssemblyVersion>1.0.2.0118</AssemblyVersion>
<FileVersion>1.0.2.0118</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
52 changes: 49 additions & 3 deletions STranslate/ViewModels/Preference/CommonViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Newtonsoft.Json;
Expand Down Expand Up @@ -65,6 +67,17 @@ private void Reset()
}
}

public CommonViewModel()
{
// 获取系统已安装字体
GetFontFamilys = Fonts.SystemFontFamilies.Select(font => font.Source).ToList();
// 判断是否已安装软件字体,没有则插入到列表中
if (!GetFontFamilys.Contains(ConstStr.DEFAULTFONTNAME))
{
GetFontFamilys.Insert(0, ConstStr.DEFAULTFONTNAME);
}
}

[ObservableProperty]
private bool isStartup = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsStartup ?? false;

Expand Down Expand Up @@ -134,6 +147,7 @@ public bool IsBright
private bool unconventionalScreen = Singleton<ConfigHelper>.Instance.CurrentConfig?.UnconventionalScreen ?? false;

private bool isDisableSystemProxy = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsDisableSystemProxy ?? false;

public bool IsDisableSystemProxy
{
get => isDisableSystemProxy;
Expand Down Expand Up @@ -164,7 +178,39 @@ public Dictionary<string, DoubleTapFuncEnum> FuncDict
}

[ObservableProperty]
private DoubleTapFuncEnum doubleTapTrayFunc =
Singleton<ConfigHelper>.Instance.CurrentConfig?.DoubleTapTrayFunc ?? DoubleTapFuncEnum.InputFunc;
private DoubleTapFuncEnum doubleTapTrayFunc = Singleton<ConfigHelper>.Instance.CurrentConfig?.DoubleTapTrayFunc ?? DoubleTapFuncEnum.InputFunc;

[ObservableProperty]
private List<string> _getFontFamilys;

private string _customFont = Singleton<ConfigHelper>.Instance.CurrentConfig?.CustomFont ?? ConstStr.DEFAULTFONTNAME;

public string CustomFont
{
get => _customFont;
set
{
if (_customFont != value)
{
OnPropertyChanging(nameof(CustomFont));

try
{
// 切换字体
Application.Current.Resources[ConstStr.USERDEFINEFONTKEY] = value.Equals(ConstStr.DEFAULTFONTNAME)
? Application.Current.Resources[ConstStr.DEFAULTFONTNAME] : new FontFamily(value);
_customFont = value;
}
catch (Exception)
{
Application.Current.Resources[ConstStr.USERDEFINEFONTKEY] = Application.Current.Resources[ConstStr.DEFAULTFONTNAME];
_customFont = ConstStr.DEFAULTFONTNAME;
}


OnPropertyChanged(nameof(CustomFont));
}
}
}
}
}
2 changes: 1 addition & 1 deletion STranslate/Views/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
AllowsTransparency="True"
Background="Transparent"
Deactivated="Mwin_Deactivated"
FontFamily="{DynamicResource FontFamily_LXGW}"
FontFamily="{DynamicResource UserFont}"
FontSize="{DynamicResource FontSize_Content}"
Icon="{DynamicResource STranslate}"
MouseLeftButtonDown="Mwin_MouseLeftButtonDown"
Expand Down
2 changes: 1 addition & 1 deletion STranslate/Views/OCRView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
Background="{DynamicResource BorderBackground}"
BorderBrush="{DynamicResource BorderBrushColor}"
BorderThickness="1"
FontFamily="{DynamicResource FontFamily_LXGW}"
FontFamily="{DynamicResource UserFont}"
Icon="{DynamicResource Icon}"
WindowChrome.IsHitTestVisibleInChrome="True"
WindowStartupLocation="CenterScreen"
Expand Down
2 changes: 1 addition & 1 deletion STranslate/Views/Preference/AboutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
d:DataContext="{d:DesignInstance Type=vm:AboutViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
d:FontFamily="{DynamicResource FontFamily_LXGW}"
d:FontFamily="{DynamicResource UserFont}"
d:FontSize="{DynamicResource FontSize_Content}"
Style="{StaticResource ResourceKey=Page_Style}"
mc:Ignorable="d">
Expand Down
11 changes: 10 additions & 1 deletion STranslate/Views/Preference/CommonPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
d:DataContext="{d:DesignInstance Type=vm:CommonViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
d:FontFamily="{DynamicResource FontFamily_LXGW}"
d:FontFamily="{DynamicResource UserFont}"
d:FontSize="{DynamicResource FontSize_Content}"
Style="{StaticResource ResourceKey=Page_Style}"
mc:Ignorable="d">
Expand Down Expand Up @@ -95,6 +95,15 @@
<ToggleButton Height="26" HorizontalAlignment="Right" IsChecked="{Binding IsBright}" />
</DockPanel>

<DockPanel Margin="20,10">
<TextBlock Text="字体选择" />
<TextBlock Style="{DynamicResource InfoTextBlock}" ToolTip="选择系统中自带字体" />
<ComboBox Height="30"
HorizontalAlignment="Right"
ItemsSource="{Binding GetFontFamilys}"
SelectedValue="{Binding CustomFont, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DockPanel>

<DockPanel Margin="20,10">
<TextBlock Text="窗口跟随鼠标" />
<TextBlock Style="{DynamicResource InfoTextBlock}" ToolTip="主窗口弹出位置跟随鼠标位置" />
Expand Down
2 changes: 1 addition & 1 deletion STranslate/Views/Preference/FavoritePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
d:DataContext="{d:DesignInstance Type=vm:FavoriteViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
d:FontFamily="{DynamicResource FontFamily_LXGW}"
d:FontFamily="{DynamicResource UserFont}"
d:FontSize="{DynamicResource FontSize_Content}"
Style="{StaticResource ResourceKey=Page_Style}"
mc:Ignorable="d">
Expand Down
2 changes: 1 addition & 1 deletion STranslate/Views/Preference/HistoryPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
d:DataContext="{d:DesignInstance Type=vm:HistoryViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
d:FontFamily="{DynamicResource FontFamily_LXGW}"
d:FontFamily="{DynamicResource UserFont}"
d:FontSize="{DynamicResource FontSize_Content}"
Style="{StaticResource ResourceKey=Page_Style}"
mc:Ignorable="d">
Expand Down
2 changes: 1 addition & 1 deletion STranslate/Views/Preference/HotkeyPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
d:DataContext="{d:DesignInstance Type=vm:HotkeyViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
d:FontFamily="{DynamicResource FontFamily_LXGW}"
d:FontFamily="{DynamicResource UserFont}"
d:FontSize="{DynamicResource FontSize_Content}"
Loaded="UserControl_Loaded"
Style="{StaticResource ResourceKey=Page_Style}"
Expand Down
2 changes: 1 addition & 1 deletion STranslate/Views/Preference/ServicePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
d:DataContext="{d:DesignInstance Type=vm:ServiceViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
d:FontFamily="{DynamicResource FontFamily_LXGW}"
d:FontFamily="{DynamicResource UserFont}"
d:FontSize="{DynamicResource FontSize_Content}"
Style="{StaticResource ResourceKey=Page_Style}"
mc:Ignorable="d">
Expand Down
2 changes: 1 addition & 1 deletion STranslate/Views/PreferenceView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Background="{DynamicResource BorderBackground}"
BorderBrush="{DynamicResource BorderBrushColor}"
BorderThickness="1"
FontFamily="{DynamicResource FontFamily_LXGW}"
FontFamily="{DynamicResource UserFont}"
Icon="{DynamicResource Icon}"
WindowChrome.IsHitTestVisibleInChrome="True"
WindowStartupLocation="CenterScreen"
Expand Down

0 comments on commit 98e62f6

Please sign in to comment.