PowerToys/Wox.Core/Theme/ThemeManager.cs

152 lines
5.9 KiB
C#
Raw Normal View History

2014-12-27 12:34:51 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
2015-01-02 23:07:49 +08:00
using Wox.Core.UI;
using Wox.Infrastructure.Logger;
2014-12-27 12:34:51 +08:00
using Wox.Infrastructure.Storage.UserSettings;
2015-01-02 23:07:49 +08:00
namespace Wox.Core.Theme
2014-12-27 12:34:51 +08:00
{
2015-01-02 23:07:49 +08:00
public class ThemeManager : IUIResource
2014-12-27 12:34:51 +08:00
{
private static List<string> themeDirectories = new List<string>();
2015-01-02 23:07:49 +08:00
private static ThemeManager instance;
private static object syncObject = new object();
private ThemeManager() { }
public static ThemeManager Instance
{
get
{
if (instance == null)
{
lock (syncObject)
{
if (instance == null)
{
instance = new ThemeManager();
}
}
}
return instance;
}
}
2014-12-27 12:34:51 +08:00
static ThemeManager()
{
themeDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Themes"));
2014-12-27 12:34:51 +08:00
string userProfilePath = Environment.GetEnvironmentVariable("USERPROFILE");
if (userProfilePath != null)
{
themeDirectories.Add(Path.Combine(Path.Combine(userProfilePath, ".Wox"), "Themes"));
}
MakesureThemeDirectoriesExist();
}
private static void MakesureThemeDirectoriesExist()
{
foreach (string pluginDirectory in themeDirectories)
{
if (!Directory.Exists(pluginDirectory))
{
try
{
Directory.CreateDirectory(pluginDirectory);
}
2015-01-02 23:07:49 +08:00
catch (System.Exception e)
{
Log.Error(e.Message);
}
2014-12-27 12:34:51 +08:00
}
}
}
2015-01-02 23:07:49 +08:00
public void ChangeTheme(string themeName)
2014-12-27 12:34:51 +08:00
{
string themePath = GetThemePath(themeName);
if (string.IsNullOrEmpty(themePath))
{
themePath = GetThemePath("Dark");
if (string.IsNullOrEmpty(themePath))
{
2015-01-02 23:07:49 +08:00
throw new System.Exception("Change theme failed");
2014-12-27 12:34:51 +08:00
}
}
2015-01-02 16:16:09 +08:00
UserSettingStorage.Instance.Theme = themeName;
UserSettingStorage.Instance.Save();
ResourceMerger.ApplyResources();
}
2015-01-02 23:07:49 +08:00
public ResourceDictionary GetResourceDictionary()
2015-01-02 16:16:09 +08:00
{
2014-12-27 12:34:51 +08:00
var dict = new ResourceDictionary
{
2015-01-02 16:16:09 +08:00
Source = new Uri(GetThemePath(UserSettingStorage.Instance.Theme), UriKind.Absolute)
2014-12-27 12:34:51 +08:00
};
Style queryBoxStyle = dict["QueryBoxStyle"] as Style;
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;
Style resultSubItemSelectedStyle = dict["ItemSubTitleSelectedStyle"] as Style;
if (resultItemStyle != null && resultSubItemStyle != null && 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));
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)));
}
2015-01-02 16:16:09 +08:00
return dict;
}
2014-12-27 12:34:51 +08:00
2015-01-02 23:07:49 +08:00
public List<string> LoadAvailableThemes()
2015-01-02 16:16:09 +08:00
{
List<string> themes = new List<string>();
foreach (var themeDirectory in themeDirectories)
{
themes.AddRange(
Directory.GetFiles(themeDirectory)
2015-01-02 23:07:49 +08:00
.Where(filePath => filePath.EndsWith(".xaml") && !filePath.EndsWith("Base.xaml"))
2015-01-02 16:16:09 +08:00
.ToList());
}
return themes;
2014-12-27 12:34:51 +08:00
}
2015-01-02 23:07:49 +08:00
private string GetThemePath(string themeName)
2014-12-27 12:34:51 +08:00
{
foreach (string themeDirectory in themeDirectories)
{
string path = Path.Combine(themeDirectory, themeName + ".xaml");
if (File.Exists(path))
{
return path;
}
}
return string.Empty;
}
}
}