2015-01-03 15:20:34 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2015-11-29 14:29:32 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
2015-11-29 14:29:32 +08:00
|
|
|
|
using System.Windows.Interop;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using Wox.Core.UI;
|
2015-01-05 22:41:17 +08:00
|
|
|
|
using Wox.Core.UserSettings;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
|
|
|
|
|
|
|
|
|
namespace Wox.Core.Theme
|
|
|
|
|
{
|
2015-11-29 14:29:32 +08:00
|
|
|
|
public class Theme : IUIResource
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2015-11-02 10:49:38 +08:00
|
|
|
|
public const string DirectoryName = "Themes";
|
2015-01-03 15:20:34 +08:00
|
|
|
|
private static List<string> themeDirectories = new List<string>();
|
|
|
|
|
|
|
|
|
|
static Theme()
|
|
|
|
|
{
|
2015-11-02 10:49:38 +08:00
|
|
|
|
themeDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DirectoryName));
|
2015-01-03 15:20:34 +08:00
|
|
|
|
MakesureThemeDirectoriesExist();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void MakesureThemeDirectoriesExist()
|
|
|
|
|
{
|
|
|
|
|
foreach (string pluginDirectory in themeDirectories)
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(pluginDirectory))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(pluginDirectory);
|
|
|
|
|
}
|
2016-01-07 05:34:42 +08:00
|
|
|
|
catch (Exception e)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2015-11-08 01:32:58 +08:00
|
|
|
|
Log.Error(e);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeTheme(string themeName)
|
|
|
|
|
{
|
|
|
|
|
string themePath = GetThemePath(themeName);
|
|
|
|
|
if (string.IsNullOrEmpty(themePath))
|
|
|
|
|
{
|
|
|
|
|
themePath = GetThemePath("Dark");
|
|
|
|
|
if (string.IsNullOrEmpty(themePath))
|
|
|
|
|
{
|
2016-01-07 05:34:42 +08:00
|
|
|
|
throw new Exception("Change theme failed");
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UserSettingStorage.Instance.Theme = themeName;
|
|
|
|
|
UserSettingStorage.Instance.Save();
|
2015-11-29 14:29:32 +08:00
|
|
|
|
ResourceMerger.UpdateResource(this);
|
|
|
|
|
|
2015-11-29 16:20:13 +08:00
|
|
|
|
// Exception of FindResource can't be cathed if global exception handle is set
|
|
|
|
|
var isBlur = Application.Current.TryFindResource("ThemeBlurEnabled");
|
|
|
|
|
if (isBlur is bool)
|
2015-11-29 14:29:32 +08:00
|
|
|
|
{
|
2015-11-29 16:20:13 +08:00
|
|
|
|
SetBlurForWindow(Application.Current.MainWindow, (bool)isBlur);
|
2015-11-29 14:29:32 +08:00
|
|
|
|
}
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResourceDictionary GetResourceDictionary()
|
|
|
|
|
{
|
|
|
|
|
var dict = new ResourceDictionary
|
|
|
|
|
{
|
|
|
|
|
Source = new Uri(GetThemePath(UserSettingStorage.Instance.Theme), UriKind.Absolute)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
2016-01-07 05:34:42 +08:00
|
|
|
|
Setter[] setters = { fontFamily, fontStyle, fontWeight, fontStretch };
|
|
|
|
|
Array.ForEach(new[] { resultItemStyle, resultSubItemStyle, resultItemSelectedStyle, resultSubItemSelectedStyle }, o => Array.ForEach(setters, p => o.Setters.Add(p)));
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<string> LoadAvailableThemes()
|
|
|
|
|
{
|
|
|
|
|
List<string> themes = new List<string>();
|
|
|
|
|
foreach (var themeDirectory in themeDirectories)
|
|
|
|
|
{
|
|
|
|
|
themes.AddRange(
|
|
|
|
|
Directory.GetFiles(themeDirectory)
|
|
|
|
|
.Where(filePath => filePath.EndsWith(".xaml") && !filePath.EndsWith("Base.xaml"))
|
|
|
|
|
.ToList());
|
|
|
|
|
}
|
2015-01-22 20:24:15 +08:00
|
|
|
|
return themes.OrderBy(o => o).ToList();
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetThemePath(string themeName)
|
|
|
|
|
{
|
|
|
|
|
foreach (string themeDirectory in themeDirectories)
|
|
|
|
|
{
|
|
|
|
|
string path = Path.Combine(themeDirectory, themeName + ".xaml");
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2015-11-29 14:29:32 +08:00
|
|
|
|
|
|
|
|
|
#region Blur Handling
|
|
|
|
|
/*
|
|
|
|
|
Found on https://github.com/riverar/sample-win10-aeroglass
|
|
|
|
|
*/
|
2015-11-29 15:19:58 +08:00
|
|
|
|
private enum AccentState
|
2015-11-29 14:29:32 +08:00
|
|
|
|
{
|
|
|
|
|
ACCENT_DISABLED = 0,
|
|
|
|
|
ACCENT_ENABLE_GRADIENT = 1,
|
|
|
|
|
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
|
|
|
|
|
ACCENT_ENABLE_BLURBEHIND = 3,
|
|
|
|
|
ACCENT_INVALID_STATE = 4
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
2015-11-29 15:19:58 +08:00
|
|
|
|
private struct AccentPolicy
|
2015-11-29 14:29:32 +08:00
|
|
|
|
{
|
|
|
|
|
public AccentState AccentState;
|
|
|
|
|
public int AccentFlags;
|
|
|
|
|
public int GradientColor;
|
|
|
|
|
public int AnimationId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
2015-11-29 15:19:58 +08:00
|
|
|
|
private struct WindowCompositionAttributeData
|
2015-11-29 14:29:32 +08:00
|
|
|
|
{
|
|
|
|
|
public WindowCompositionAttribute Attribute;
|
|
|
|
|
public IntPtr Data;
|
|
|
|
|
public int SizeOfData;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 15:19:58 +08:00
|
|
|
|
private enum WindowCompositionAttribute
|
2015-11-29 14:29:32 +08:00
|
|
|
|
{
|
|
|
|
|
WCA_ACCENT_POLICY = 19
|
|
|
|
|
}
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the blur for a window via SetWindowCompositionAttribute
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="wind">window to blur</param>
|
2015-11-29 20:47:04 +08:00
|
|
|
|
/// <param name="isBlur">true/false - on or off correspondingly</param>
|
|
|
|
|
private void SetBlurForWindow(Window wind, bool isBlur)
|
2015-11-29 14:29:32 +08:00
|
|
|
|
{
|
2015-11-29 20:47:04 +08:00
|
|
|
|
if (isBlur)
|
|
|
|
|
{
|
|
|
|
|
SetWindowAccent(wind, AccentState.ACCENT_ENABLE_BLURBEHIND);
|
|
|
|
|
}
|
2015-11-29 14:29:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetWindowAccent(Window wind, AccentState themeAccentMode)
|
|
|
|
|
{
|
|
|
|
|
var windowHelper = new WindowInteropHelper(wind);
|
|
|
|
|
var accent = new AccentPolicy { AccentState = themeAccentMode };
|
|
|
|
|
var accentStructSize = Marshal.SizeOf(accent);
|
|
|
|
|
|
|
|
|
|
var accentPtr = Marshal.AllocHGlobal(accentStructSize);
|
|
|
|
|
Marshal.StructureToPtr(accent, accentPtr, false);
|
|
|
|
|
|
|
|
|
|
var data = new WindowCompositionAttributeData
|
|
|
|
|
{
|
|
|
|
|
Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
|
|
|
|
|
SizeOfData = accentStructSize,
|
|
|
|
|
Data = accentPtr
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SetWindowCompositionAttribute(windowHelper.Handle, ref data);
|
|
|
|
|
|
|
|
|
|
Marshal.FreeHGlobal(accentPtr);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|