mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
Refactoring blur
1. Refactoring blur, see discussion in : 7f8bb80 2. Releated issue: #330
This commit is contained in:
@@ -79,7 +79,7 @@ namespace Wox.Core.Plugin
|
||||
Concat(new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(metadatas));
|
||||
|
||||
//load plugin i18n languages
|
||||
ResourceMerger.ApplyPluginLanguages();
|
||||
ResourceMerger.UpdatePluginLanguages();
|
||||
|
||||
foreach (PluginPair pluginPair in AllPlugins)
|
||||
{
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Wox.Core.Theme
|
||||
{
|
||||
interface ITheme
|
||||
{
|
||||
void ChangeTheme(string themeName);
|
||||
List<string> LoadAvailableThemes();
|
||||
}
|
||||
}
|
||||
@@ -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<string> themeDirectories = new List<string>();
|
||||
@@ -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);
|
||||
|
||||
/// <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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<IPluginI18n>().
|
||||
|
||||
@@ -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; }
|
||||
|
||||
|
||||
@@ -69,7 +69,6 @@
|
||||
<Compile Include="i18n\Internationalization.cs" />
|
||||
<Compile Include="i18n\InternationalizationManager.cs" />
|
||||
<Compile Include="i18n\Language.cs" />
|
||||
<Compile Include="Theme\ITheme.cs" />
|
||||
<Compile Include="Theme\ThemeManager.cs" />
|
||||
<Compile Include="UI\IUIResource.cs" />
|
||||
<Compile Include="UI\ResourceMerger.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()
|
||||
|
||||
Reference in New Issue
Block a user