Allow change FontStyle, FontWeight, FontStretch

This commit is contained in:
Yeechan Lu 2014-03-25 13:25:43 +08:00
parent 60d64ad2a6
commit b10caecbba
7 changed files with 201 additions and 8 deletions

View File

@ -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; }

68
Wox/Helper/FontHelper.cs Normal file
View File

@ -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();
}
}
}

View File

@ -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<TResult>(Func<TResult> callback)
{
return CallOrRescueDefault(callback, default(TResult));
}
public static TResult CallOrRescueDefault<TResult>(Func<TResult> callback, TResult def)
{
try
{
return callback();
}
catch
{
return def;
}
}
}
}

View File

@ -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();

View File

@ -201,13 +201,43 @@
</Grid>
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Margin="10">
<StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="10">
<TextBlock Text="Query Box Font:" />
<ComboBox x:Name="cbQueryBoxFont" ItemsSource="{x:Static Fonts.SystemFontFamilies}" SelectionChanged="CbQueryBoxFont_OnSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>
<ComboBox
x:Name="cbQueryBoxFontFaces"
ItemsSource="{Binding ElementName=cbQueryBoxFont,Path=SelectedValue.FamilyTypefaces}" SelectionChanged="CbQueryBoxFontFaces_OnSelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding AdjustedFaceNames}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0" Margin="10">
<StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="10">
<TextBlock Text="Result Item Font:" />
<ComboBox x:Name="cbResultItemFont" ItemsSource="{x:Static Fonts.SystemFontFamilies}" SelectionChanged="CbResultItemFont_OnSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>
<ComboBox
x:Name="cbResultItemFontFaces"
ItemsSource="{Binding ElementName=cbResultItemFont,Path=SelectedValue.FamilyTypefaces}" SelectionChanged="CbResultItemFontFaces_OnSelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding AdjustedFaceNames}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
</Grid>
</Grid>

View File

@ -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<Result>()
{
@ -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
}
}

View File

@ -110,6 +110,8 @@
<Compile Include="Commands\PluginCommand.cs" />
<Compile Include="Commands\SystemCommand.cs" />
<Compile Include="Helper\DataWebRequestFactory.cs" />
<Compile Include="Helper\FontHelper.cs" />
<Compile Include="Helper\SyntaxSugars.cs" />
<Compile Include="Helper\WindowIntelopHelper.cs" />
<Compile Include="ProgramSourceSetting.xaml.cs">
<DependentUpon>ProgramSourceSetting.xaml</DependentUpon>