using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls; using System.Windows.Interop; using System.Windows.Media; using Wox.Core.UserSettings; using Wox.Infrastructure.Logger; namespace Wox.Core.Resource { public class Theme : Resource { private static List themeDirectories = new List(); public UserSettings.Settings Settings { get; set; } public Theme() { DirectoryName = "Themes"; themeDirectories.Add(DirectoryPath); MakesureThemeDirectoriesExist(); } private static void MakesureThemeDirectoriesExist() { foreach (string pluginDirectory in themeDirectories) { if (!Directory.Exists(pluginDirectory)) { try { Directory.CreateDirectory(pluginDirectory); } catch (Exception e) { Log.Exception(e); } } } } public void ChangeTheme(string themeName) { string themePath = GetThemePath(themeName); if (string.IsNullOrEmpty(themePath)) { themePath = GetThemePath("Dark"); if (string.IsNullOrEmpty(themePath)) { throw new Exception("Change theme failed"); } } Settings.Theme = themeName; ResourceMerger.UpdateResource(this); // Exception of FindResource can't be cathed if global exception handle is set var isBlur = Application.Current.TryFindResource("ThemeBlurEnabled"); if (isBlur is bool) { SetBlurForWindow(Application.Current.MainWindow, (bool)isBlur); } } public override ResourceDictionary GetResourceDictionary() { var dict = new ResourceDictionary { Source = new Uri(GetThemePath(Settings.Theme), UriKind.Absolute) }; Style queryBoxStyle = dict["QueryBoxStyle"] as Style; if (queryBoxStyle != null) { 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))); } 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(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)); Setter[] setters = { fontFamily, fontStyle, fontWeight, fontStretch }; Array.ForEach(new[] { resultItemStyle, resultSubItemStyle, resultItemSelectedStyle, resultSubItemSelectedStyle }, o => Array.ForEach(setters, p => o.Setters.Add(p))); } return dict; } public List LoadAvailableThemes() { List themes = new List(); 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 */ private enum AccentState { ACCENT_DISABLED = 0, ACCENT_ENABLE_GRADIENT = 1, ACCENT_ENABLE_TRANSPARENTGRADIENT = 2, ACCENT_ENABLE_BLURBEHIND = 3, ACCENT_INVALID_STATE = 4 } [StructLayout(LayoutKind.Sequential)] private struct AccentPolicy { public AccentState AccentState; public int AccentFlags; public int GradientColor; public int AnimationId; } [StructLayout(LayoutKind.Sequential)] private struct WindowCompositionAttributeData { public WindowCompositionAttribute Attribute; public IntPtr Data; public int SizeOfData; } private enum WindowCompositionAttribute { WCA_ACCENT_POLICY = 19 } [DllImport("user32.dll")] private static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data); /// /// Sets the blur for a window via SetWindowCompositionAttribute /// /// window to blur /// true/false - on or off correspondingly private void SetBlurForWindow(Window wind, bool isBlur) { if (isBlur) { SetWindowAccent(wind, AccentState.ACCENT_ENABLE_BLURBEHIND); } } 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 } }