mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-15 20:19:17 +08:00
38791e50ea
1. Refactoring blur, see discussion in : 7f8bb80 2. Releated issue: #330
205 lines
7.9 KiB
C#
205 lines
7.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Interop;
|
|
using System.Windows.Media;
|
|
using Wox.Core.UI;
|
|
using Wox.Core.UserSettings;
|
|
using Wox.Infrastructure.Logger;
|
|
|
|
namespace Wox.Core.Theme
|
|
{
|
|
public class Theme : IUIResource
|
|
{
|
|
public const string DirectoryName = "Themes";
|
|
private static List<string> themeDirectories = new List<string>();
|
|
|
|
static Theme()
|
|
{
|
|
themeDirectories.Add(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DirectoryName));
|
|
MakesureThemeDirectoriesExist();
|
|
}
|
|
|
|
private static void MakesureThemeDirectoriesExist()
|
|
{
|
|
foreach (string pluginDirectory in themeDirectories)
|
|
{
|
|
if (!Directory.Exists(pluginDirectory))
|
|
{
|
|
try
|
|
{
|
|
Directory.CreateDirectory(pluginDirectory);
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Log.Error(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ChangeTheme(string themeName)
|
|
{
|
|
string themePath = GetThemePath(themeName);
|
|
if (string.IsNullOrEmpty(themePath))
|
|
{
|
|
themePath = GetThemePath("Dark");
|
|
if (string.IsNullOrEmpty(themePath))
|
|
{
|
|
throw new System.Exception("Change theme failed");
|
|
}
|
|
}
|
|
|
|
UserSettingStorage.Instance.Theme = themeName;
|
|
UserSettingStorage.Instance.Save();
|
|
ResourceMerger.UpdateResource(this);
|
|
|
|
try
|
|
{
|
|
var isBlur = Application.Current.FindResource("ThemeBlurEnabled");
|
|
if (isBlur is bool)
|
|
{
|
|
SetBlurForWindow(Application.Current.MainWindow, (bool)isBlur);
|
|
}
|
|
}
|
|
catch (ResourceReferenceKeyNotFoundException e)
|
|
{
|
|
}
|
|
}
|
|
|
|
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));
|
|
|
|
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)));
|
|
}
|
|
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());
|
|
}
|
|
return themes.OrderBy(o => o).ToList();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
#region Blur Handling
|
|
/*
|
|
Found on https://github.com/riverar/sample-win10-aeroglass
|
|
*/
|
|
public enum AccentState
|
|
{
|
|
ACCENT_DISABLED = 0,
|
|
ACCENT_ENABLE_GRADIENT = 1,
|
|
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
|
|
ACCENT_ENABLE_BLURBEHIND = 3,
|
|
ACCENT_INVALID_STATE = 4
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
internal struct AccentPolicy
|
|
{
|
|
public AccentState AccentState;
|
|
public int AccentFlags;
|
|
public int GradientColor;
|
|
public int AnimationId;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
internal struct WindowCompositionAttributeData
|
|
{
|
|
public WindowCompositionAttribute Attribute;
|
|
public IntPtr Data;
|
|
public int SizeOfData;
|
|
}
|
|
|
|
internal enum WindowCompositionAttribute
|
|
{
|
|
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>
|
|
/// <param name="status">true/false - on or off correspondingly</param>
|
|
public void SetBlurForWindow(Window wind, bool status)
|
|
{
|
|
SetWindowAccent(wind, status ? AccentState.ACCENT_ENABLE_BLURBEHIND : AccentState.ACCENT_DISABLED);
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|