diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index db05467dd6..5bcabee4fe 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -79,7 +79,7 @@ namespace Wox.Core.Plugin Concat(new JsonRPCPluginLoader().LoadPlugin(metadatas)); //load plugin i18n languages - ResourceMerger.ApplyPluginLanguages(); + ResourceMerger.UpdatePluginLanguages(); foreach (PluginPair pluginPair in AllPlugins) { diff --git a/Wox.Core/Theme/ITheme.cs b/Wox.Core/Theme/ITheme.cs deleted file mode 100644 index 34409068e6..0000000000 --- a/Wox.Core/Theme/ITheme.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Collections.Generic; - -namespace Wox.Core.Theme -{ - interface ITheme - { - void ChangeTheme(string themeName); - List LoadAvailableThemes(); - } -} diff --git a/Wox.Core/Theme/Theme.cs b/Wox.Core/Theme/Theme.cs index 87c3406357..c43b417970 100644 --- a/Wox.Core/Theme/Theme.cs +++ b/Wox.Core/Theme/Theme.cs @@ -3,8 +3,10 @@ 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; @@ -12,7 +14,7 @@ using Wox.Infrastructure.Logger; namespace Wox.Core.Theme { - public class Theme : IUIResource,ITheme + public class Theme : IUIResource { public const string DirectoryName = "Themes"; private static List themeDirectories = new List(); @@ -53,11 +55,21 @@ namespace Wox.Core.Theme } } - ResourceMerger.ApplyThemeResource(this); - UserSettingStorage.Instance.Theme = themeName; - UserSettingStorage.Instance.ThemeBlurEnabled = (bool)Application.Current.Resources["ThemeBlurEnabled"]; 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() @@ -119,5 +131,74 @@ namespace Wox.Core.Theme 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); + + /// + /// Sets the blur for a window via SetWindowCompositionAttribute + /// + /// window to blur + /// true/false - on or off correspondingly + 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 } } diff --git a/Wox.Core/UI/ResourceMerger.cs b/Wox.Core/UI/ResourceMerger.cs index 2be07e50c6..9ce07a97ee 100644 --- a/Wox.Core/UI/ResourceMerger.cs +++ b/Wox.Core/UI/ResourceMerger.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Windows; using Wox.Core.i18n; @@ -9,34 +10,34 @@ namespace Wox.Core.UI { public static class ResourceMerger { - private static void RemoveResource(string resourceDirectoryName) + private static void RemoveResource(string directoryName) { - var mergedDictionaries = Application.Current.Resources.MergedDictionaries; - foreach (var resource in mergedDictionaries) + directoryName = $"{Path.DirectorySeparatorChar}{directoryName}"; + var dictionaries = Application.Current.Resources.MergedDictionaries; + foreach (var resource in dictionaries) { - int directoryPosition = resource.Source.Segments.Length - 2; - string currentDirectoryName = resource.Source.Segments[directoryPosition]; - if (currentDirectoryName == resourceDirectoryName) + string currentDirectoryName = Path.GetDirectoryName(resource.Source.AbsolutePath); + if (currentDirectoryName == directoryName) { - mergedDictionaries.Remove(resource); + dictionaries.Remove(resource); break; } } } - public static void ApplyThemeResource(Theme.Theme t) + public static void UpdateResource(Theme.Theme t) { RemoveResource(Theme.Theme.DirectoryName); Application.Current.Resources.MergedDictionaries.Add(t.GetResourceDictionary()); } - public static void ApplyLanguageResources(Internationalization i) + public static void UpdateResources(Internationalization i) { RemoveResource(Internationalization.DirectoryName); Application.Current.Resources.MergedDictionaries.Add(i.GetResourceDictionary()); } - internal static void ApplyPluginLanguages() + internal static void UpdatePluginLanguages() { RemoveResource(PluginManager.DirectoryName); foreach (var languageFile in PluginManager.GetPluginsForInterface(). diff --git a/Wox.Core/UserSettings/UserSettingStorage.cs b/Wox.Core/UserSettings/UserSettingStorage.cs index 7aa48a82df..13017c6ea1 100644 --- a/Wox.Core/UserSettings/UserSettingStorage.cs +++ b/Wox.Core/UserSettings/UserSettingStorage.cs @@ -55,9 +55,6 @@ namespace Wox.Core.UserSettings [JsonProperty] public string ResultItemFontStretch { get; set; } - [JsonProperty] - public bool ThemeBlurEnabled { get; set; } - [JsonProperty] public double WindowLeft { get; set; } diff --git a/Wox.Core/Wox.Core.csproj b/Wox.Core/Wox.Core.csproj index 2f55e3fb86..02376a0340 100644 --- a/Wox.Core/Wox.Core.csproj +++ b/Wox.Core/Wox.Core.csproj @@ -69,7 +69,6 @@ - diff --git a/Wox.Core/i18n/Internationalization.cs b/Wox.Core/i18n/Internationalization.cs index 0cfea890a3..5ce242f187 100644 --- a/Wox.Core/i18n/Internationalization.cs +++ b/Wox.Core/i18n/Internationalization.cs @@ -70,7 +70,7 @@ namespace Wox.Core.i18n UserSettingStorage.Instance.Language = language.LanguageCode; UserSettingStorage.Instance.Save(); - ResourceMerger.ApplyLanguageResources(this); + ResourceMerger.UpdateResources(this); } public ResourceDictionary GetResourceDictionary() diff --git a/Wox/Helper/WindowIntelopHelper.cs b/Wox/Helper/WindowIntelopHelper.cs index 6bd64615ee..4a0223a19e 100644 --- a/Wox/Helper/WindowIntelopHelper.cs +++ b/Wox/Helper/WindowIntelopHelper.cs @@ -88,75 +88,5 @@ namespace Wox.Helper public int Right; public int Bottom; } - - #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); - - /// - /// Sets the blur for a window via SetWindowCompositionAttribute - /// - /// window to blur - /// true/false - on or off correspondingly - public static void SetBlurForWindow(Window wind, bool status) - { - SetWindowAccent(wind, status ? AccentState.ACCENT_ENABLE_BLURBEHIND : AccentState.ACCENT_DISABLED); - } - - private static void SetWindowAccent(Window wind, AccentState themeAccentMode) - { - var windowHelper = new WindowInteropHelper(wind); - var accent = new AccentPolicy(); - accent.AccentState = themeAccentMode; - var accentStructSize = Marshal.SizeOf(accent); - - var accentPtr = Marshal.AllocHGlobal(accentStructSize); - Marshal.StructureToPtr(accent, accentPtr, false); - - var data = new WindowCompositionAttributeData(); - data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY; - data.SizeOfData = accentStructSize; - data.Data = accentPtr; - - SetWindowCompositionAttribute(windowHelper.Handle, ref data); - - Marshal.FreeHGlobal(accentPtr); - } - #endregion - - } } \ No newline at end of file diff --git a/Wox/Languages/en.xaml b/Wox/Languages/en.xaml index 8f53a74ff0..8040ad6bc7 100644 --- a/Wox/Languages/en.xaml +++ b/Wox/Languages/en.xaml @@ -43,7 +43,6 @@ Result Item Font Window Mode Opacity - Windows Accents diff --git a/Wox/Languages/ru.xaml b/Wox/Languages/ru.xaml index 0c25bfb5a2..13a7be86cc 100644 --- a/Wox/Languages/ru.xaml +++ b/Wox/Languages/ru.xaml @@ -43,7 +43,6 @@ Шрифт результатов Оконный режим Прозрачность - Windows Accents Горячие клавиши diff --git a/Wox/Languages/zh-cn.xaml b/Wox/Languages/zh-cn.xaml index 8db385c98d..40860846b0 100644 --- a/Wox/Languages/zh-cn.xaml +++ b/Wox/Languages/zh-cn.xaml @@ -43,7 +43,6 @@ 结果项字体 窗口模式 透明度 - Windows Accents 热键 diff --git a/Wox/Languages/zh-tw.xaml b/Wox/Languages/zh-tw.xaml index 780bc115a5..5d8907fe7a 100644 --- a/Wox/Languages/zh-tw.xaml +++ b/Wox/Languages/zh-tw.xaml @@ -43,7 +43,6 @@ 結果項字體 窗口模式 透明度 - Windows Accents 熱鍵 diff --git a/Wox/MainWindow.xaml.cs b/Wox/MainWindow.xaml.cs index eda169be96..39cf8b3c7a 100644 --- a/Wox/MainWindow.xaml.cs +++ b/Wox/MainWindow.xaml.cs @@ -198,16 +198,12 @@ namespace Wox pnlResult.ItemDropEvent += pnlResult_ItemDropEvent; pnlContextMenu.LeftMouseClickEvent += SelectResult; pnlResult.RightMouseClickEvent += pnlResult_RightMouseClickEvent; + Closing += MainWindow_Closing; - ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme); - InternationalizationManager.Instance.ChangeLanguage(UserSettingStorage.Instance.Language); SetHotkey(UserSettingStorage.Instance.Hotkey, OnHotkey); SetCustomPluginHotkey(); InitialTray(); - - Closing += MainWindow_Closing; - } void pnlResult_ItemDropEvent(Result result, IDataObject dropDataObject, DragEventArgs args) @@ -250,12 +246,14 @@ namespace Wox private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { + ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme); + InternationalizationManager.Instance.ChangeLanguage(UserSettingStorage.Instance.Language); + Left = GetWindowsLeft(); Top = GetWindowsTop(); InitProgressbarAnimation(); WindowIntelopHelper.DisableControlBox(this); - WindowIntelopHelper.SetBlurForWindow(this, UserSettingStorage.Instance.ThemeBlurEnabled); CheckUpdate(); } diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index c484adbeb0..20d2c02dd5 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -440,9 +440,7 @@ namespace Wox private void ThemeComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { string themeName = themeComboBox.SelectedItem.ToString(); - UserSettingStorage.Instance.Theme = themeName; - DelayChangeTheme(); - UserSettingStorage.Instance.Save(); + ThemeManager.Theme.ChangeTheme(themeName); } private void CbQueryBoxFont_OnSelectionChanged(object sender, SelectionChangedEventArgs e) @@ -451,19 +449,9 @@ namespace Wox string queryBoxFontName = cbQueryBoxFont.SelectedItem.ToString(); UserSettingStorage.Instance.QueryBoxFont = queryBoxFontName; this.cbQueryBoxFontFaces.SelectedItem = ((FontFamily)cbQueryBoxFont.SelectedItem).ChooseRegularFamilyTypeface(); - DelayChangeTheme(); UserSettingStorage.Instance.Save(); } - private void DelayChangeTheme() - { - Dispatcher.DelayInvoke("delayChangeTheme", () => - { - ThemeManager.Theme.ChangeTheme(UserSettingStorage.Instance.Theme); - WindowIntelopHelper.SetBlurForWindow(MainWindow, UserSettingStorage.Instance.ThemeBlurEnabled); - }, TimeSpan.FromMilliseconds(100)); - } - private void CbQueryBoxFontFaces_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (!settingsLoaded) return; @@ -481,7 +469,6 @@ namespace Wox UserSettingStorage.Instance.QueryBoxFontWeight = typeface.Weight.ToString(); UserSettingStorage.Instance.QueryBoxFontStyle = typeface.Style.ToString(); UserSettingStorage.Instance.Save(); - DelayChangeTheme(); } } @@ -491,9 +478,7 @@ namespace Wox string resultItemFont = cbResultItemFont.SelectedItem.ToString(); UserSettingStorage.Instance.ResultItemFont = resultItemFont; this.cbResultItemFontFaces.SelectedItem = ((FontFamily)cbResultItemFont.SelectedItem).ChooseRegularFamilyTypeface(); - UserSettingStorage.Instance.Save(); - DelayChangeTheme(); } private void CbResultItemFontFaces_OnSelectionChanged(object sender, SelectionChangedEventArgs e) @@ -511,7 +496,6 @@ namespace Wox UserSettingStorage.Instance.ResultItemFontWeight = typeface.Weight.ToString(); UserSettingStorage.Instance.ResultItemFontStyle = typeface.Style.ToString(); UserSettingStorage.Instance.Save(); - DelayChangeTheme(); } } diff --git a/Wox/Themes/Base.xaml b/Wox/Themes/Base.xaml index 5fa7c96c81..39866e052c 100644 --- a/Wox/Themes/Base.xaml +++ b/Wox/Themes/Base.xaml @@ -27,10 +27,6 @@ - - false - - - + - + + #356ef3 @@ -49,6 +47,6 @@ diff --git a/Wox/Themes/SimpleBlur-White.xaml b/Wox/Themes/SimpleBlur-White.xaml index 132f93333d..040bb0c2af 100644 --- a/Wox/Themes/SimpleBlur-White.xaml +++ b/Wox/Themes/SimpleBlur-White.xaml @@ -8,39 +8,37 @@ True - + - + + #356ef3 @@ -49,6 +47,6 @@