2015-01-03 15:20:34 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
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;
|
2017-02-21 10:19:50 +08:00
|
|
|
|
using Wox.Infrastructure;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2016-06-19 23:18:43 +08:00
|
|
|
|
using Wox.Infrastructure.UserSettings;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
|
2016-01-08 04:04:37 +08:00
|
|
|
|
namespace Wox.Core.Resource
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
public class Theme
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:40:43 +08:00
|
|
|
|
private readonly List<string> _themeDirectories = new List<string>();
|
2017-02-28 08:12:53 +08:00
|
|
|
|
private ResourceDictionary _oldResource;
|
|
|
|
|
private string _oldTheme;
|
2016-06-19 23:18:43 +08:00
|
|
|
|
public Settings Settings { get; set; }
|
2017-02-28 08:12:53 +08:00
|
|
|
|
private const string Folder = "Themes";
|
|
|
|
|
private const string Extension = ".xaml";
|
|
|
|
|
private string DirectoryPath => Path.Combine(Constant.ProgramDirectory, Folder);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
|
2016-01-07 12:47:28 +08:00
|
|
|
|
public Theme()
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:40:43 +08:00
|
|
|
|
_themeDirectories.Add(DirectoryPath);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
MakesureThemeDirectoriesExist();
|
2017-02-28 08:12:53 +08:00
|
|
|
|
|
|
|
|
|
var dicts = Application.Current.Resources.MergedDictionaries;
|
|
|
|
|
_oldResource = dicts.First(d =>
|
|
|
|
|
{
|
|
|
|
|
var p = d.Source.AbsolutePath;
|
|
|
|
|
var dir = Path.GetDirectoryName(p).NonNull();
|
|
|
|
|
var info = new DirectoryInfo(dir);
|
|
|
|
|
var f = info.Name;
|
|
|
|
|
var e = Path.GetExtension(p);
|
|
|
|
|
var found = f == Folder && e == Extension;
|
|
|
|
|
return found;
|
|
|
|
|
});
|
|
|
|
|
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 10:40:43 +08:00
|
|
|
|
private void MakesureThemeDirectoriesExist()
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:40:43 +08:00
|
|
|
|
foreach (string dir in _themeDirectories)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:40:43 +08:00
|
|
|
|
if (!Directory.Exists(dir))
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-02-21 10:40:43 +08:00
|
|
|
|
Directory.CreateDirectory(dir);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
2016-01-07 05:34:42 +08:00
|
|
|
|
catch (Exception e)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:40:43 +08:00
|
|
|
|
Log.Exception($"|Theme.MakesureThemeDirectoriesExist|Exception when create directory <{dir}>", e);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 10:19:50 +08:00
|
|
|
|
public void ChangeTheme(string theme)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
const string dark = "Dark";
|
|
|
|
|
bool valid;
|
2015-11-29 14:29:32 +08:00
|
|
|
|
|
2017-02-21 10:19:50 +08:00
|
|
|
|
string path = GetThemePath(theme);
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
2015-11-29 14:29:32 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
Log.Error($"|Theme.ChangeTheme|Theme path can't be found <{path}>, use default dark theme");
|
|
|
|
|
path = GetThemePath(dark);
|
|
|
|
|
if (string.IsNullOrEmpty(path))
|
2017-02-20 06:28:42 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
valid = false;
|
|
|
|
|
Log.Error($"|Theme.ChangeTheme|Default theme path can't be found <{path}>");
|
2017-02-20 06:28:42 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
valid = true;
|
|
|
|
|
theme = dark;
|
2017-02-20 06:28:42 +08:00
|
|
|
|
}
|
2017-02-21 10:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
valid = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (valid)
|
|
|
|
|
{
|
|
|
|
|
Settings.Theme = theme;
|
|
|
|
|
|
|
|
|
|
var dicts = Application.Current.Resources.MergedDictionaries;
|
2017-02-28 08:12:53 +08:00
|
|
|
|
if (_oldTheme != theme)
|
|
|
|
|
{
|
|
|
|
|
dicts.Remove(_oldResource);
|
|
|
|
|
var newResource = GetResourceDictionary();
|
|
|
|
|
dicts.Add(newResource);
|
|
|
|
|
_oldResource = newResource;
|
|
|
|
|
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
|
|
|
|
|
}
|
2015-11-29 14:29:32 +08:00
|
|
|
|
}
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 10:19:50 +08:00
|
|
|
|
public ResourceDictionary GetResourceDictionary()
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-20 07:29:13 +08:00
|
|
|
|
var uri = GetThemePath(Settings.Theme);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
var dict = new ResourceDictionary
|
|
|
|
|
{
|
2017-02-20 07:29:13 +08:00
|
|
|
|
Source = new Uri(uri, UriKind.Absolute)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Style queryBoxStyle = dict["QueryBoxStyle"] as Style;
|
|
|
|
|
if (queryBoxStyle != null)
|
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
queryBoxStyle.Setters.Add(new Setter(TextBox.FontFamilyProperty, new FontFamily(Settings.QueryBoxFont)));
|
|
|
|
|
queryBoxStyle.Setters.Add(new Setter(TextBox.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(Settings.QueryBoxFontStyle)));
|
|
|
|
|
queryBoxStyle.Setters.Add(new Setter(TextBox.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(Settings.QueryBoxFontWeight)));
|
|
|
|
|
queryBoxStyle.Setters.Add(new Setter(TextBox.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(Settings.QueryBoxFontStretch)));
|
2015-01-03 15:20:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
Setter fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(Settings.ResultFont));
|
|
|
|
|
Setter fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(Settings.ResultFontStyle));
|
|
|
|
|
Setter fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(Settings.ResultFontWeight));
|
|
|
|
|
Setter fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(Settings.ResultFontStretch));
|
2015-01-03 15:20:34 +08:00
|
|
|
|
|
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>();
|
2017-02-21 10:40:43 +08:00
|
|
|
|
foreach (var themeDirectory in _themeDirectories)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
|
|
|
|
themes.AddRange(
|
|
|
|
|
Directory.GetFiles(themeDirectory)
|
2017-02-28 08:12:53 +08:00
|
|
|
|
.Where(filePath => filePath.EndsWith(Extension) && !filePath.EndsWith("Base.xaml"))
|
2015-01-03 15:20:34 +08:00
|
|
|
|
.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)
|
|
|
|
|
{
|
2017-02-21 10:40:43 +08:00
|
|
|
|
foreach (string themeDirectory in _themeDirectories)
|
2015-01-03 15:20:34 +08:00
|
|
|
|
{
|
2017-02-28 08:12:53 +08:00
|
|
|
|
string path = Path.Combine(themeDirectory, themeName + Extension);
|
2015-01-03 15:20:34 +08:00
|
|
|
|
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>
|
2017-02-21 10:19:50 +08:00
|
|
|
|
public void SetBlurForWindow()
|
2015-11-29 14:29:32 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
|
|
|
|
|
// Exception of FindResource can't be cathed if global exception handle is set
|
|
|
|
|
if (Environment.OSVersion.Version >= new Version(6, 2))
|
2015-11-29 20:47:04 +08:00
|
|
|
|
{
|
2017-02-21 10:19:50 +08:00
|
|
|
|
var resource = Application.Current.TryFindResource("ThemeBlurEnabled");
|
|
|
|
|
bool blur;
|
|
|
|
|
if (resource is bool)
|
|
|
|
|
{
|
|
|
|
|
blur = (bool)resource;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
blur = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (blur)
|
|
|
|
|
{
|
|
|
|
|
SetWindowAccent(Application.Current.MainWindow, AccentState.ACCENT_ENABLE_BLURBEHIND);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SetWindowAccent(Application.Current.MainWindow, AccentState.ACCENT_DISABLED);
|
|
|
|
|
}
|
2015-11-29 20:47:04 +08:00
|
|
|
|
}
|
2015-11-29 14:29:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-20 06:28:42 +08:00
|
|
|
|
private void SetWindowAccent(Window w, AccentState state)
|
2015-11-29 14:29:32 +08:00
|
|
|
|
{
|
2017-02-20 06:28:42 +08:00
|
|
|
|
var windowHelper = new WindowInteropHelper(w);
|
|
|
|
|
var accent = new AccentPolicy { AccentState = state };
|
2015-11-29 14:29:32 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|