From 09308417a8f4dd4129208d6db7d94e17e0102a07 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 7 May 2020 14:30:55 +1000 Subject: [PATCH] Add DropShadowEffect as a user option --- Flow.Launcher.Core/Resource/Theme.cs | 63 ++++++++++++++++--- Flow.Launcher.Infrastructure/Constant.cs | 2 + .../UserSettings/Settings.cs | 3 +- Flow.Launcher/MainWindow.xaml | 3 - Flow.Launcher/SettingWindow.xaml | 22 ++++++- .../ViewModel/SettingWindowViewModel.cs | 18 ++++++ 6 files changed, 97 insertions(+), 14 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index a4258474f..606e6712d 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -8,6 +8,7 @@ using System.Windows.Controls; using System.Windows.Interop; using System.Windows.Markup; using System.Windows.Media; +using System.Windows.Media.Effects; using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; @@ -68,7 +69,7 @@ namespace Flow.Launcher.Core.Resource public bool ChangeTheme(string theme) { - const string defaultTheme = "Dark"; + const string defaultTheme = Constant.DefaultTheme; string path = GetThemePath(theme); try @@ -78,16 +79,15 @@ namespace Flow.Launcher.Core.Resource Settings.Theme = theme; - var dicts = Application.Current.Resources.MergedDictionaries; //always allow re-loading default theme, in case of failure of switching to a new theme from default theme if (_oldTheme != theme || theme == defaultTheme) { - dicts.Remove(_oldResource); - var newResource = GetResourceDictionary(); - dicts.Add(newResource); - _oldResource = newResource; + UpdateResourceDictionary(GetResourceDictionary()); _oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath); } + + if (Settings.UseDropShadowEffect) + AddDropShadowEffectToCurrentTheme(); } catch (DirectoryNotFoundException e) { @@ -112,7 +112,16 @@ namespace Flow.Launcher.Core.Resource return true; } - public ResourceDictionary GetResourceDictionary() + private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate) + { + var dicts = Application.Current.Resources.MergedDictionaries; + + dicts.Remove(_oldResource); + dicts.Add(dictionaryToUpdate); + _oldResource = dictionaryToUpdate; + } + + private ResourceDictionary CurrentThemeResourceDictionary() { var uri = GetThemePath(Settings.Theme); var dict = new ResourceDictionary @@ -120,6 +129,13 @@ namespace Flow.Launcher.Core.Resource Source = new Uri(uri, UriKind.Absolute) }; + return dict; + } + + public ResourceDictionary GetResourceDictionary() + { + var dict = CurrentThemeResourceDictionary(); + Style queryBoxStyle = dict["QueryBoxStyle"] as Style; if (queryBoxStyle != null) { @@ -149,6 +165,7 @@ namespace Flow.Launcher.Core.Resource Setter[] setters = { fontFamily, fontStyle, fontWeight, fontStretch }; Array.ForEach(new[] { resultItemStyle, resultSubItemStyle, resultItemSelectedStyle, resultSubItemSelectedStyle }, o => Array.ForEach(setters, p => o.Setters.Add(p))); } + return dict; } @@ -179,6 +196,36 @@ namespace Flow.Launcher.Core.Resource return string.Empty; } + public void AddDropShadowEffectToCurrentTheme() + { + var dict = CurrentThemeResourceDictionary(); + + var windowBorderStyle = dict["WindowBorderStyle"] as Style; + + var effectSetter = new Setter(); + effectSetter.Property = Border.EffectProperty; + effectSetter.Value = new DropShadowEffect + { + Opacity = 0.9, + ShadowDepth = 2, + BlurRadius = 15 + }; + + windowBorderStyle.Setters.Add(effectSetter); + + UpdateResourceDictionary(dict); + } + + public void RemoveDropShadowEffectToCurrentTheme() + { + var dict = CurrentThemeResourceDictionary(); + var windowBorderStyle = dict["WindowBorderStyle"] as Style; + + dict.Remove(Border.EffectProperty); + + UpdateResourceDictionary(dict); + } + #region Blur Handling /* Found on https://github.com/riverar/sample-win10-aeroglass diff --git a/Flow.Launcher.Infrastructure/Constant.cs b/Flow.Launcher.Infrastructure/Constant.cs index 99b2bfb50..0936ce8c4 100644 --- a/Flow.Launcher.Infrastructure/Constant.cs +++ b/Flow.Launcher.Infrastructure/Constant.cs @@ -29,5 +29,7 @@ namespace Flow.Launcher.Infrastructure public static string EverythingSDKPath; public static readonly string QueryTextBoxIconImagePath = $"{ProgramDirectory}\\Images\\mainsearch.png"; + + public const string DefaultTheme = "Darker"; } } diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index a9cec8b25..f0ad0381d 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -11,7 +11,8 @@ namespace Flow.Launcher.Infrastructure.UserSettings { public string Hotkey { get; set; } = "Alt + Space"; public string Language { get; set; } = "en"; - public string Theme { get; set; } = "Dark"; + public string Theme { get; set; } = Constant.DefaultTheme; + public bool UseDropShadowEffect { get; set; } = false; public string QueryBoxFont { get; set; } = FontFamily.GenericSansSerif.Name; public string QueryBoxFontStyle { get; set; } public string QueryBoxFontWeight { get; set; } diff --git a/Flow.Launcher/MainWindow.xaml b/Flow.Launcher/MainWindow.xaml index f6e7ca170..b1b688913 100644 --- a/Flow.Launcher/MainWindow.xaml +++ b/Flow.Launcher/MainWindow.xaml @@ -61,9 +61,6 @@ - - - + - + + + + + + + + + + + + + + @@ -240,7 +258,7 @@ - + Themes => ThemeManager.Instance.LoadAvailableThemes().Select(Path.GetFileNameWithoutExtension).ToList(); + public bool DropShadowEffect + { + get { return Settings.UseDropShadowEffect; } + set + { + if (value) + { + ThemeManager.Instance.AddDropShadowEffectToCurrentTheme(); + } + else + { + ThemeManager.Instance.RemoveDropShadowEffectToCurrentTheme(); + } + + Settings.UseDropShadowEffect = value; + } + } + public Brush PreviewBackground { get