diff --git a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs index 673406896f..3585009111 100644 --- a/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs +++ b/Wox.Infrastructure/Storage/UserSettings/UserSettingStorage.cs @@ -14,13 +14,30 @@ namespace Wox.Infrastructure.Storage.UserSettings [JsonProperty] public string Theme { get; set; } - [JsonProperty] public string QueryBoxFont { get; set; } + [JsonProperty] + public string QueryBoxFontStyle { get; set; } + + [JsonProperty] + public string QueryBoxFontWeight { get; set; } + + [JsonProperty] + public string QueryBoxFontStretch { get; set; } + [JsonProperty] public string ResultItemFont { get; set; } + [JsonProperty] + public string ResultItemFontStyle { get; set; } + + [JsonProperty] + public string ResultItemFontWeight { get; set; } + + [JsonProperty] + public string ResultItemFontStretch { get; set; } + [JsonProperty] public bool ReplaceWinR { get; set; } diff --git a/Wox/Helper/FontHelper.cs b/Wox/Helper/FontHelper.cs new file mode 100644 index 0000000000..9064a2bf34 --- /dev/null +++ b/Wox/Helper/FontHelper.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Media; +using Wox.Helper; + +namespace Wox.Helper +{ + public static class FontHelper + { + static FontWeightConverter fontWeightConverter = new FontWeightConverter(); + public static FontWeight GetFontWeightFromInvariantStringOrNormal(string value) + { + try + { + return (FontWeight) fontWeightConverter.ConvertFromInvariantString(value); + } + catch { + return FontWeights.Normal; + } + } + + static FontStyleConverter fontStyleConverter = new FontStyleConverter(); + public static FontStyle GetFontStyleFromInvariantStringOrNormal(string value) + { + try + { + return (FontStyle)fontStyleConverter.ConvertFromInvariantString(value); + } + catch + { + return FontStyles.Normal; + } + } + + static FontStretchConverter fontStretchConverter = new FontStretchConverter(); + public static FontStretch GetFontStretchFromInvariantStringOrNormal(string value) + { + try + { + return (FontStretch)fontStretchConverter.ConvertFromInvariantString(value); + } + catch + { + return FontStretches.Normal; + } + } + + public static FamilyTypeface ChooseRegularFamilyTypeface(this FontFamily family) + { + return family.FamilyTypefaces.OrderBy(o => + { + return Math.Abs(o.Stretch.ToOpenTypeStretch() - FontStretches.Normal.ToOpenTypeStretch()) * 100 + + Math.Abs(o.Weight.ToOpenTypeWeight() - FontWeights.Normal.ToOpenTypeWeight()) + + (o.Style == FontStyles.Normal ? 0 : o.Style == FontStyles.Oblique ? 1 : 2) * 1000; + }).FirstOrDefault(); + } + + public static FamilyTypeface ConvertFromInvariantStringsOrNormal(this FontFamily family, string style, string weight, string stretch) + { + return family.FamilyTypefaces.FirstOrDefault(o => o.Style == GetFontStyleFromInvariantStringOrNormal(style) && o.Weight == GetFontWeightFromInvariantStringOrNormal(weight) && o.Stretch == GetFontStretchFromInvariantStringOrNormal(stretch)) + ?? family.ChooseRegularFamilyTypeface(); + } + + } +} diff --git a/Wox/Helper/SyntaxSugars.cs b/Wox/Helper/SyntaxSugars.cs new file mode 100644 index 0000000000..e7caf8560a --- /dev/null +++ b/Wox/Helper/SyntaxSugars.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Wox.Helper +{ + public static class SyntaxSugars + { + public static TResult CallOrRescueDefault(Func callback) + { + return CallOrRescueDefault(callback, default(TResult)); + } + + public static TResult CallOrRescueDefault(Func callback, TResult def) + { + try + { + return callback(); + } + catch + { + return def; + } + } + } +} diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index 5e83f84d5b..e42174d99c 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -438,7 +438,11 @@ namespace Wox if (queryBoxStyle != null) { queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.QueryBoxFont))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontStyle))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontWeight))); + queryBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.QueryBoxFontStretch))); } + Style resultItemStyle = dict["ItemTitleStyle"] as Style; Style resultSubItemStyle = dict["ItemSubTitleStyle"] as Style; Style resultItemSelectedStyle = dict["ItemTitleSelectedStyle"] as Style; @@ -447,11 +451,12 @@ namespace Wox && resultSubItemSelectedStyle != null && resultItemSelectedStyle != null) { Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(UserSettingStorage.Instance.ResultItemFont)); + Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontStyle)); + Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontWeight)); + Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(UserSettingStorage.Instance.ResultItemFontStretch)); - resultItemStyle.Setters.Add(fontFamily); - resultSubItemStyle.Setters.Add(fontFamily); - resultItemSelectedStyle.Setters.Add(fontFamily); - resultSubItemSelectedStyle.Setters.Add(fontFamily); + Setter[] setters = new Setter[] { fontFamily, fontStyle, fontWeight, fontStretch }; + Array.ForEach(new Style[] { resultItemStyle, resultSubItemStyle, resultItemSelectedStyle, resultSubItemSelectedStyle }, o => Array.ForEach(setters, p => o.Setters.Add(p))); } Application.Current.Resources.MergedDictionaries.Clear(); diff --git a/Wox/SettingWindow.xaml b/Wox/SettingWindow.xaml index a0fba8dde6..9b9b4aafb7 100644 --- a/Wox/SettingWindow.xaml +++ b/Wox/SettingWindow.xaml @@ -201,13 +201,43 @@ - + + + + + + + + + + + + + + - + + + + + + + + + + + + + + diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index 1f576cb2f5..d01f024753 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -12,6 +12,7 @@ using Wox.Infrastructure; using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage.UserSettings; using Wox.Plugin; +using Wox.Helper; using Application = System.Windows.Forms.Application; using File = System.IO.File; using MessageBox = System.Windows.MessageBox; @@ -35,6 +36,7 @@ namespace Wox Loaded += Setting_Loaded; } + bool settingsLoaded = false; private void Setting_Loaded(object sender, RoutedEventArgs ev) { ctlHotkey.OnHotkeyChanged += ctlHotkey_OnHotkeyChanged; @@ -67,11 +69,23 @@ namespace Wox Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.QueryBoxFont)) > 0) { cbQueryBoxFont.Text = UserSettingStorage.Instance.QueryBoxFont; + + cbQueryBoxFontFaces.SelectedItem = SyntaxSugars.CallOrRescueDefault(() => ((FontFamily)cbQueryBoxFont.SelectedItem).ConvertFromInvariantStringsOrNormal( + UserSettingStorage.Instance.QueryBoxFontStyle, + UserSettingStorage.Instance.QueryBoxFontWeight, + UserSettingStorage.Instance.QueryBoxFontStretch + )); } if (!string.IsNullOrEmpty(UserSettingStorage.Instance.ResultItemFont) && - Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.ResultItemFont)) > 0) + Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.ResultItemFont)) > 0) { cbResultItemFont.Text = UserSettingStorage.Instance.ResultItemFont; + + cbResultItemFontFaces.SelectedItem = SyntaxSugars.CallOrRescueDefault(() => ((FontFamily)cbResultItemFont.SelectedItem).ConvertFromInvariantStringsOrNormal( + UserSettingStorage.Instance.ResultItemFontStyle, + UserSettingStorage.Instance.ResultItemFontWeight, + UserSettingStorage.Instance.ResultItemFontStretch + )); } resultPanelPreview.AddResults(new List() { @@ -127,6 +141,9 @@ namespace Wox lvCustomHotkey.ItemsSource = UserSettingStorage.Instance.CustomPluginHotkeys; cbEnablePythonPlugins.IsChecked = UserSettingStorage.Instance.EnablePythonPlugins; cbStartWithWindows.IsChecked = File.Exists(woxLinkPath); + + settingsLoaded = true; + App.Window.SetTheme(UserSettingStorage.Instance.Theme); } public void ReloadWebSearchView() @@ -336,20 +353,47 @@ namespace Wox private void CbQueryBoxFont_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { + if (!settingsLoaded) return; string queryBoxFontName = cbQueryBoxFont.SelectedItem.ToString(); UserSettingStorage.Instance.QueryBoxFont = queryBoxFontName; + this.cbQueryBoxFontFaces.SelectedItem = ((FontFamily)cbQueryBoxFont.SelectedItem).ChooseRegularFamilyTypeface(); + + UserSettingStorage.Instance.Save(); + App.Window.SetTheme(UserSettingStorage.Instance.Theme); + } + + private void CbQueryBoxFontFaces_OnSelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (!settingsLoaded) return; + FamilyTypeface typeface = (FamilyTypeface) cbQueryBoxFontFaces.SelectedItem; + UserSettingStorage.Instance.QueryBoxFontStretch = typeface.Stretch.ToString(); + UserSettingStorage.Instance.QueryBoxFontWeight = typeface.Weight.ToString(); + UserSettingStorage.Instance.QueryBoxFontStyle = typeface.Style.ToString(); UserSettingStorage.Instance.Save(); App.Window.SetTheme(UserSettingStorage.Instance.Theme); } private void CbResultItemFont_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { + if (!settingsLoaded) return; string resultItemFont = cbResultItemFont.SelectedItem.ToString(); UserSettingStorage.Instance.ResultItemFont = resultItemFont; + this.cbResultItemFontFaces.SelectedItem = ((FontFamily)cbResultItemFont.SelectedItem).ChooseRegularFamilyTypeface(); + UserSettingStorage.Instance.Save(); App.Window.SetTheme(UserSettingStorage.Instance.Theme); } + private void CbResultItemFontFaces_OnSelectionChanged(object sender, SelectionChangedEventArgs e) + { + if (!settingsLoaded) return; + FamilyTypeface typeface = (FamilyTypeface)cbResultItemFontFaces.SelectedItem; + UserSettingStorage.Instance.ResultItemFontStretch = typeface.Stretch.ToString(); + UserSettingStorage.Instance.ResultItemFontWeight = typeface.Weight.ToString(); + UserSettingStorage.Instance.ResultItemFontStyle = typeface.Style.ToString(); + UserSettingStorage.Instance.Save(); + App.Window.SetTheme(UserSettingStorage.Instance.Theme); + } #endregion } } diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index ffe7596421..ad221a4b7b 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -110,6 +110,8 @@ + + ProgramSourceSetting.xaml