Flow.Launcher/Flow.Launcher.Core/Resource/Theme.cs

924 lines
39 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2024-06-02 12:17:42 +00:00
using System.Xml;
2025-03-17 11:58:32 +00:00
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
2025-03-24 14:07:19 +00:00
using System.Windows.Controls.Primitives;
2018-12-22 17:53:13 +00:00
using System.Windows.Markup;
using System.Windows.Media;
2020-05-07 04:30:55 +00:00
using System.Windows.Media.Effects;
2025-02-19 09:13:51 +00:00
using System.Windows.Shell;
2025-03-17 11:58:32 +00:00
using System.Windows.Threading;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
2025-04-08 07:56:54 +00:00
using Flow.Launcher.Plugin.SharedModels;
2025-03-17 11:58:32 +00:00
using Microsoft.Win32;
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Core.Resource
{
public class Theme
{
2025-03-16 06:52:55 +00:00
#region Properties & Fields
2025-04-13 09:26:21 +00:00
private readonly string ClassName = nameof(Theme);
public bool BlurEnabled { get; private set; }
2025-03-16 06:52:55 +00:00
2024-06-03 07:25:42 +00:00
private const string ThemeMetadataNamePrefix = "Name:";
private const string ThemeMetadataIsDarkPrefix = "IsDark:";
private const string ThemeMetadataHasBlurPrefix = "HasBlur:";
2022-09-15 02:01:13 +00:00
private const int ShadowExtraMargin = 32;
2021-06-12 14:38:40 +00:00
2025-02-23 11:48:38 +00:00
private readonly IPublicAPI _api;
private readonly Settings _settings;
private readonly List<string> _themeDirectories = new();
2017-02-28 00:12:53 +00:00
private ResourceDictionary _oldResource;
private string _oldTheme;
2020-11-15 19:29:24 +00:00
private const string Folder = Constant.Themes;
2017-02-28 00:12:53 +00:00
private const string Extension = ".xaml";
2025-03-16 06:43:53 +00:00
private static string DirectoryPath => Path.Combine(Constant.ProgramDirectory, Folder);
private static string UserDirectoryPath => Path.Combine(DataLocation.DataDirectory(), Folder);
private Thickness _themeResizeBorderThickness;
2025-03-16 06:52:55 +00:00
#endregion
#region Constructor
public Theme(IPublicAPI publicAPI, Settings settings)
{
2025-02-23 11:48:38 +00:00
_api = publicAPI;
_settings = settings;
2017-02-21 02:40:43 +00:00
_themeDirectories.Add(DirectoryPath);
_themeDirectories.Add(UserDirectoryPath);
2023-01-09 15:56:48 +00:00
MakeSureThemeDirectoriesExist();
2017-02-28 00:12:53 +00:00
var dicts = Application.Current.Resources.MergedDictionaries;
_oldResource = dicts.FirstOrDefault(d =>
2017-02-28 00:12:53 +00:00
{
2025-03-24 14:07:19 +00:00
if (d.Source == null) return false;
2020-04-28 11:10:42 +00:00
2017-02-28 00:12:53 +00:00
var p = d.Source.AbsolutePath;
return p.Contains(Folder) && Path.GetExtension(p) == Extension;
2017-02-28 00:12:53 +00:00
});
if (_oldResource != null)
{
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
}
else
{
2025-04-13 09:26:21 +00:00
_api.LogError(ClassName, "Current theme resource not found. Initializing with default theme.");
_oldTheme = Constant.DefaultTheme;
}
}
#endregion
2025-03-16 06:52:55 +00:00
#region Theme Resources
2025-03-14 01:00:31 +00:00
2025-03-16 06:52:55 +00:00
private void MakeSureThemeDirectoriesExist()
{
2025-03-16 06:52:55 +00:00
foreach (var dir in _themeDirectories.Where(dir => !Directory.Exists(dir)))
2025-03-14 01:00:31 +00:00
{
2025-03-16 06:52:55 +00:00
try
2025-03-14 01:00:31 +00:00
{
2025-03-16 06:52:55 +00:00
Directory.CreateDirectory(dir);
2025-03-14 01:00:31 +00:00
}
2025-03-16 06:52:55 +00:00
catch (Exception e)
2025-03-14 01:00:31 +00:00
{
2025-04-13 09:26:21 +00:00
_api.LogException(ClassName, $"Exception when create directory <{dir}>", e);
2025-03-14 01:00:31 +00:00
}
2025-03-16 06:52:55 +00:00
}
}
2025-03-16 06:52:55 +00:00
private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate)
2025-03-04 04:46:36 +00:00
{
2025-03-24 14:07:19 +00:00
// Add new resources
if (!Application.Current.Resources.MergedDictionaries.Contains(dictionaryToUpdate))
{
Application.Current.Resources.MergedDictionaries.Add(dictionaryToUpdate);
}
2025-03-24 14:07:19 +00:00
// Remove old resources
if (_oldResource != null && _oldResource != dictionaryToUpdate &&
Application.Current.Resources.MergedDictionaries.Contains(_oldResource))
{
Application.Current.Resources.MergedDictionaries.Remove(_oldResource);
}
2025-03-24 14:07:19 +00:00
2025-03-16 06:52:55 +00:00
_oldResource = dictionaryToUpdate;
2025-03-04 04:46:36 +00:00
}
/// <summary>
/// Updates only the font settings and refreshes the UI.
/// </summary>
public void UpdateFonts()
{
try
{
2025-03-24 15:02:51 +00:00
// Load a ResourceDictionary for the specified theme.
var theme = _settings.Theme;
var dict = GetThemeResourceDictionary(theme);
2025-03-24 15:02:51 +00:00
// Apply font settings to the theme resource.
ApplyFontSettings(dict);
UpdateResourceDictionary(dict);
2025-03-24 15:02:51 +00:00
// Must apply blur and drop shadow effects
_ = RefreshFrameAsync();
}
catch (Exception e)
{
2025-04-13 09:26:21 +00:00
_api.LogException(ClassName, "Error occurred while updating theme fonts", e);
}
}
/// <summary>
/// Loads and applies font settings to the theme resource.
/// </summary>
private void ApplyFontSettings(ResourceDictionary dict)
{
if (dict["QueryBoxStyle"] is Style queryBoxStyle &&
dict["QuerySuggestionBoxStyle"] is Style querySuggestionBoxStyle)
{
var fontFamily = new FontFamily(_settings.QueryBoxFont);
var fontStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.QueryBoxFontStyle);
var fontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.QueryBoxFontWeight);
var fontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.QueryBoxFontStretch);
SetFontProperties(queryBoxStyle, fontFamily, fontStyle, fontWeight, fontStretch, true);
SetFontProperties(querySuggestionBoxStyle, fontFamily, fontStyle, fontWeight, fontStretch, false);
}
if (dict["ItemTitleStyle"] is Style resultItemStyle &&
dict["ItemTitleSelectedStyle"] is Style resultItemSelectedStyle &&
dict["ItemHotkeyStyle"] is Style resultHotkeyItemStyle &&
dict["ItemHotkeySelectedStyle"] is Style resultHotkeyItemSelectedStyle)
{
var fontFamily = new FontFamily(_settings.ResultFont);
var fontStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.ResultFontStyle);
var fontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.ResultFontWeight);
var fontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.ResultFontStretch);
SetFontProperties(resultItemStyle, fontFamily, fontStyle, fontWeight, fontStretch, false);
SetFontProperties(resultItemSelectedStyle, fontFamily, fontStyle, fontWeight, fontStretch, false);
SetFontProperties(resultHotkeyItemStyle, fontFamily, fontStyle, fontWeight, fontStretch, false);
SetFontProperties(resultHotkeyItemSelectedStyle, fontFamily, fontStyle, fontWeight, fontStretch, false);
}
if (dict["ItemSubTitleStyle"] is Style resultSubItemStyle &&
dict["ItemSubTitleSelectedStyle"] is Style resultSubItemSelectedStyle)
{
var fontFamily = new FontFamily(_settings.ResultSubFont);
var fontStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.ResultSubFontStyle);
var fontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.ResultSubFontWeight);
var fontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.ResultSubFontStretch);
SetFontProperties(resultSubItemStyle, fontFamily, fontStyle, fontWeight, fontStretch, false);
SetFontProperties(resultSubItemSelectedStyle, fontFamily, fontStyle, fontWeight, fontStretch, false);
}
}
/// <summary>
/// Applies font properties to a Style.
/// </summary>
2025-03-24 14:07:19 +00:00
private static void SetFontProperties(Style style, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, bool isTextBox)
{
// Remove existing font-related setters
if (isTextBox)
{
// First, find the setters to remove and store them in a list
var settersToRemove = style.Setters
.OfType<Setter>()
.Where(setter =>
2025-03-24 14:07:19 +00:00
setter.Property == Control.FontFamilyProperty ||
setter.Property == Control.FontStyleProperty ||
setter.Property == Control.FontWeightProperty ||
setter.Property == Control.FontStretchProperty)
.ToList();
// Remove each found setter one by one
foreach (var setter in settersToRemove)
{
style.Setters.Remove(setter);
}
// Add New font setter
2025-03-24 14:07:19 +00:00
style.Setters.Add(new Setter(Control.FontFamilyProperty, fontFamily));
style.Setters.Add(new Setter(Control.FontStyleProperty, fontStyle));
style.Setters.Add(new Setter(Control.FontWeightProperty, fontWeight));
style.Setters.Add(new Setter(Control.FontStretchProperty, fontStretch));
// Set caret brush (retain existing logic)
var caretBrushPropertyValue = style.Setters.OfType<Setter>().Any(x => x.Property.Name == "CaretBrush");
var foregroundPropertyValue = style.Setters.OfType<Setter>().Where(x => x.Property.Name == "Foreground")
.Select(x => x.Value).FirstOrDefault();
if (!caretBrushPropertyValue && foregroundPropertyValue != null)
2025-03-24 14:07:19 +00:00
style.Setters.Add(new Setter(TextBoxBase.CaretBrushProperty, foregroundPropertyValue));
}
else
{
var settersToRemove = style.Setters
.OfType<Setter>()
.Where(setter =>
setter.Property == TextBlock.FontFamilyProperty ||
setter.Property == TextBlock.FontStyleProperty ||
setter.Property == TextBlock.FontWeightProperty ||
setter.Property == TextBlock.FontStretchProperty)
.ToList();
foreach (var setter in settersToRemove)
{
style.Setters.Remove(setter);
}
style.Setters.Add(new Setter(TextBlock.FontFamilyProperty, fontFamily));
style.Setters.Add(new Setter(TextBlock.FontStyleProperty, fontStyle));
style.Setters.Add(new Setter(TextBlock.FontWeightProperty, fontWeight));
style.Setters.Add(new Setter(TextBlock.FontStretchProperty, fontStretch));
}
}
2025-03-24 14:07:19 +00:00
2025-03-16 06:52:55 +00:00
private ResourceDictionary GetThemeResourceDictionary(string theme)
2025-03-04 04:46:36 +00:00
{
2025-03-16 06:52:55 +00:00
var uri = GetThemePath(theme);
var dict = new ResourceDictionary
2025-03-04 04:46:36 +00:00
{
2025-03-16 06:52:55 +00:00
Source = new Uri(uri, UriKind.Absolute)
};
2025-03-16 06:52:55 +00:00
return dict;
2025-03-14 01:00:31 +00:00
}
2025-03-16 06:52:55 +00:00
private ResourceDictionary GetResourceDictionary(string theme)
{
2025-03-16 06:52:55 +00:00
var dict = GetThemeResourceDictionary(theme);
if (dict["QueryBoxStyle"] is Style queryBoxStyle &&
dict["QuerySuggestionBoxStyle"] is Style querySuggestionBoxStyle)
2025-03-09 01:16:02 +00:00
{
2025-03-16 06:52:55 +00:00
var fontFamily = new FontFamily(_settings.QueryBoxFont);
var fontStyle = FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.QueryBoxFontStyle);
var fontWeight = FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.QueryBoxFontWeight);
var fontStretch = FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.QueryBoxFontStretch);
2025-03-09 01:16:02 +00:00
2025-03-24 14:07:19 +00:00
queryBoxStyle.Setters.Add(new Setter(Control.FontFamilyProperty, fontFamily));
queryBoxStyle.Setters.Add(new Setter(Control.FontStyleProperty, fontStyle));
queryBoxStyle.Setters.Add(new Setter(Control.FontWeightProperty, fontWeight));
queryBoxStyle.Setters.Add(new Setter(Control.FontStretchProperty, fontStretch));
2025-03-11 03:32:27 +00:00
2025-03-16 06:52:55 +00:00
var caretBrushPropertyValue = queryBoxStyle.Setters.OfType<Setter>().Any(x => x.Property.Name == "CaretBrush");
var foregroundPropertyValue = queryBoxStyle.Setters.OfType<Setter>().Where(x => x.Property.Name == "Foreground")
.Select(x => x.Value).FirstOrDefault();
if (!caretBrushPropertyValue && foregroundPropertyValue != null) //otherwise BaseQueryBoxStyle will handle styling
2025-03-24 14:07:19 +00:00
queryBoxStyle.Setters.Add(new Setter(TextBoxBase.CaretBrushProperty, foregroundPropertyValue));
2025-03-09 01:16:02 +00:00
2025-03-16 06:52:55 +00:00
// Query suggestion box's font style is aligned with query box
2025-03-24 14:07:19 +00:00
querySuggestionBoxStyle.Setters.Add(new Setter(Control.FontFamilyProperty, fontFamily));
querySuggestionBoxStyle.Setters.Add(new Setter(Control.FontStyleProperty, fontStyle));
querySuggestionBoxStyle.Setters.Add(new Setter(Control.FontWeightProperty, fontWeight));
querySuggestionBoxStyle.Setters.Add(new Setter(Control.FontStretchProperty, fontStretch));
2025-03-16 06:52:55 +00:00
}
2025-03-14 01:00:31 +00:00
2025-03-16 06:52:55 +00:00
if (dict["ItemTitleStyle"] is Style resultItemStyle &&
dict["ItemTitleSelectedStyle"] is Style resultItemSelectedStyle &&
dict["ItemHotkeyStyle"] is Style resultHotkeyItemStyle &&
dict["ItemHotkeySelectedStyle"] is Style resultHotkeyItemSelectedStyle)
{
var fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(_settings.ResultFont));
var fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.ResultFontStyle));
var fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.ResultFontWeight));
var fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.ResultFontStretch));
2025-03-16 06:52:55 +00:00
Setter[] setters = { fontFamily, fontStyle, fontWeight, fontStretch };
Array.ForEach(
new[] { resultItemStyle, resultItemSelectedStyle, resultHotkeyItemStyle, resultHotkeyItemSelectedStyle }, o
=> Array.ForEach(setters, p => o.Setters.Add(p)));
}
2025-03-11 03:32:27 +00:00
2025-03-16 06:52:55 +00:00
if (
dict["ItemSubTitleStyle"] is Style resultSubItemStyle &&
dict["ItemSubTitleSelectedStyle"] is Style resultSubItemSelectedStyle)
{
var fontFamily = new Setter(TextBlock.FontFamilyProperty, new FontFamily(_settings.ResultSubFont));
var fontStyle = new Setter(TextBlock.FontStyleProperty, FontHelper.GetFontStyleFromInvariantStringOrNormal(_settings.ResultSubFontStyle));
var fontWeight = new Setter(TextBlock.FontWeightProperty, FontHelper.GetFontWeightFromInvariantStringOrNormal(_settings.ResultSubFontWeight));
var fontStretch = new Setter(TextBlock.FontStretchProperty, FontHelper.GetFontStretchFromInvariantStringOrNormal(_settings.ResultSubFontStretch));
2025-03-16 06:52:55 +00:00
Setter[] setters = { fontFamily, fontStyle, fontWeight, fontStretch };
Array.ForEach(
new[] { resultSubItemStyle, resultSubItemSelectedStyle }, o
=> Array.ForEach(setters, p => o.Setters.Add(p)));
}
/* Ignore Theme Window Width and use setting */
var windowStyle = dict["WindowStyle"] as Style;
var width = _settings.WindowSize;
2025-03-24 14:07:19 +00:00
windowStyle.Setters.Add(new Setter(FrameworkElement.WidthProperty, width));
2025-03-16 06:52:55 +00:00
return dict;
}
2025-04-12 22:37:55 +00:00
public ResourceDictionary GetCurrentResourceDictionary()
2025-02-24 17:42:27 +00:00
{
2025-04-04 12:10:39 +00:00
return GetResourceDictionary(_settings.Theme);
2025-03-16 06:52:55 +00:00
}
2025-02-24 17:42:27 +00:00
2025-03-16 06:52:55 +00:00
private ThemeData GetThemeDataFromPath(string path)
{
using var reader = XmlReader.Create(path);
reader.Read();
2025-02-24 17:42:27 +00:00
2025-03-16 06:52:55 +00:00
var extensionlessName = Path.GetFileNameWithoutExtension(path);
2025-02-24 17:42:27 +00:00
2025-03-16 06:52:55 +00:00
if (reader.NodeType is not XmlNodeType.Comment)
return new ThemeData(extensionlessName, extensionlessName);
var commentLines = reader.Value.Trim().Split('\n').Select(v => v.Trim());
var name = extensionlessName;
bool? isDark = null;
bool? hasBlur = null;
foreach (var line in commentLines)
{
if (line.StartsWith(ThemeMetadataNamePrefix, StringComparison.OrdinalIgnoreCase))
2025-02-24 17:42:27 +00:00
{
2025-03-16 06:52:55 +00:00
name = line[ThemeMetadataNamePrefix.Length..].Trim();
2025-02-24 17:42:27 +00:00
}
2025-03-16 06:52:55 +00:00
else if (line.StartsWith(ThemeMetadataIsDarkPrefix, StringComparison.OrdinalIgnoreCase))
2025-02-24 17:42:27 +00:00
{
2025-03-16 06:52:55 +00:00
isDark = bool.Parse(line[ThemeMetadataIsDarkPrefix.Length..].Trim());
}
else if (line.StartsWith(ThemeMetadataHasBlurPrefix, StringComparison.OrdinalIgnoreCase))
{
hasBlur = bool.Parse(line[ThemeMetadataHasBlurPrefix.Length..].Trim());
2025-02-24 17:42:27 +00:00
}
}
2025-03-16 06:52:55 +00:00
return new ThemeData(extensionlessName, name, isDark, hasBlur);
2025-02-24 17:42:27 +00:00
}
2025-03-16 06:52:55 +00:00
private string GetThemePath(string themeName)
2025-03-13 04:50:09 +00:00
{
2025-03-16 06:52:55 +00:00
foreach (string themeDirectory in _themeDirectories)
2025-03-13 04:50:09 +00:00
{
2025-03-16 06:52:55 +00:00
string path = Path.Combine(themeDirectory, themeName + Extension);
if (File.Exists(path))
2025-03-13 04:50:09 +00:00
{
2025-03-16 06:52:55 +00:00
return path;
2025-03-14 01:00:31 +00:00
}
2025-03-16 06:52:55 +00:00
}
2025-03-13 04:50:09 +00:00
2025-03-16 06:52:55 +00:00
return string.Empty;
}
2025-03-16 06:09:37 +00:00
#endregion
2025-04-04 12:10:39 +00:00
#region Get & Change Theme
public ThemeData GetCurrentTheme()
{
var themes = GetAvailableThemes();
var matchingTheme = themes.FirstOrDefault(t => t.FileNameWithoutExtension == _settings.Theme);
if (matchingTheme == null)
{
2025-04-13 09:26:21 +00:00
_api.LogWarn(ClassName, $"No matching theme found for '{_settings.Theme}'. Falling back to the first available theme.");
}
return matchingTheme ?? themes.FirstOrDefault();
2025-04-04 12:10:39 +00:00
}
2025-03-16 06:52:55 +00:00
2025-04-04 12:10:39 +00:00
public List<ThemeData> GetAvailableThemes()
{
var themes = new List<ThemeData>();
2025-03-16 06:52:55 +00:00
foreach (var themeDirectory in _themeDirectories)
{
2025-03-16 06:52:55 +00:00
var filePaths = Directory
.GetFiles(themeDirectory)
.Where(filePath => filePath.EndsWith(Extension) && !filePath.EndsWith("Base.xaml"))
.Select(GetThemeDataFromPath);
themes.AddRange(filePaths);
}
2025-03-16 06:52:55 +00:00
return themes.OrderBy(o => o.Name).ToList();
}
2025-03-17 01:37:34 +00:00
public bool ChangeTheme(string theme = null)
{
if (string.IsNullOrEmpty(theme)) theme = _settings.Theme;
string path = GetThemePath(theme);
2018-12-22 17:53:13 +00:00
try
{
if (string.IsNullOrEmpty(path))
2025-03-24 15:02:51 +00:00
throw new DirectoryNotFoundException($"Theme path can't be found <{path}>");
2024-06-03 07:25:42 +00:00
// Retrieve theme resource always use the resource with font settings applied.
var resourceDict = GetResourceDictionary(theme);
UpdateResourceDictionary(resourceDict);
2024-06-03 07:25:42 +00:00
2025-02-23 11:48:38 +00:00
_settings.Theme = theme;
// Always allow re-loading default theme, in case of failure of switching to a new theme from default theme
2025-03-17 01:37:34 +00:00
if (_oldTheme != theme || theme == Constant.DefaultTheme)
2017-02-28 00:12:53 +00:00
{
_oldTheme = Path.GetFileNameWithoutExtension(_oldResource.Source.AbsolutePath);
}
2020-05-07 04:30:55 +00:00
// Check if blur is enabled
BlurEnabled = Win32Helper.IsBackdropSupported() && IsThemeBlurEnabled(resourceDict);
// Apply blur and drop shadow effect so that we do not need to call it again
_ = RefreshFrameAsync();
2025-03-24 15:02:51 +00:00
return true;
}
catch (DirectoryNotFoundException)
2018-12-22 17:53:13 +00:00
{
2025-04-13 09:26:21 +00:00
_api.LogError(ClassName, $"Theme <{theme}> path can't be found");
2025-03-17 01:37:34 +00:00
if (theme != Constant.DefaultTheme)
2018-12-22 17:53:13 +00:00
{
_api.ShowMsgBox(Localize.theme_load_failure_path_not_exists(theme));
2025-03-17 01:37:34 +00:00
ChangeTheme(Constant.DefaultTheme);
2018-12-22 17:53:13 +00:00
}
return false;
}
catch (XamlParseException e)
2018-12-22 17:53:13 +00:00
{
_api.LogException(ClassName, $"Theme <{theme}> fail to parse xaml", e);
if (theme != Constant.DefaultTheme)
{
_api.ShowMsgBox(Localize.theme_load_failure_parse_error(theme));
ChangeTheme(Constant.DefaultTheme);
}
return false;
}
catch (Exception e)
{
_api.LogException(ClassName, $"Theme <{theme}> fail to load", e);
2025-03-17 01:37:34 +00:00
if (theme != Constant.DefaultTheme)
2018-12-22 17:53:13 +00:00
{
_api.ShowMsgBox(Localize.theme_load_failure_parse_error(theme));
2025-03-17 01:37:34 +00:00
ChangeTheme(Constant.DefaultTheme);
2018-12-22 17:53:13 +00:00
}
return false;
}
}
2025-03-16 06:52:55 +00:00
#endregion
2020-05-07 04:30:55 +00:00
2025-03-16 06:52:55 +00:00
#region Shadow Effect
2020-05-07 04:30:55 +00:00
2025-03-16 06:52:55 +00:00
public void AddDropShadowEffectToCurrentTheme()
{
2025-03-16 06:52:55 +00:00
var dict = GetCurrentResourceDictionary();
2020-05-07 04:30:55 +00:00
2025-03-16 06:52:55 +00:00
var windowBorderStyle = dict["WindowBorderStyle"] as Style;
2024-06-03 07:25:42 +00:00
2025-03-16 06:52:55 +00:00
var effectSetter = new Setter
{
Property = UIElement.EffectProperty,
2025-03-16 06:52:55 +00:00
Value = new DropShadowEffect
{
Opacity = 0.3,
ShadowDepth = 12,
Direction = 270,
BlurRadius = 30
}
};
if (windowBorderStyle.Setters.FirstOrDefault(setterBase => setterBase is Setter setter && setter.Property == FrameworkElement.MarginProperty) is not Setter marginSetter)
{
2025-03-16 06:52:55 +00:00
var margin = new Thickness(ShadowExtraMargin, 12, ShadowExtraMargin, ShadowExtraMargin);
marginSetter = new Setter()
{
Property = FrameworkElement.MarginProperty,
2025-03-16 06:52:55 +00:00
Value = margin,
};
windowBorderStyle.Setters.Add(marginSetter);
2025-03-16 06:52:55 +00:00
SetResizeBoarderThickness(margin);
}
2025-03-16 06:52:55 +00:00
else
{
2025-03-16 06:52:55 +00:00
var baseMargin = (Thickness)marginSetter.Value;
var newMargin = new Thickness(
baseMargin.Left + ShadowExtraMargin,
baseMargin.Top + ShadowExtraMargin,
baseMargin.Right + ShadowExtraMargin,
baseMargin.Bottom + ShadowExtraMargin);
marginSetter.Value = newMargin;
2025-03-16 06:52:55 +00:00
SetResizeBoarderThickness(newMargin);
}
2025-03-16 06:52:55 +00:00
windowBorderStyle.Setters.Add(effectSetter);
UpdateResourceDictionary(dict);
}
2025-03-16 06:52:55 +00:00
public void RemoveDropShadowEffectFromCurrentTheme()
{
2025-03-16 06:52:55 +00:00
var dict = GetCurrentResourceDictionary();
var windowBorderStyle = dict["WindowBorderStyle"] as Style;
if (windowBorderStyle.Setters.FirstOrDefault(setterBase => setterBase is Setter setter && setter.Property == UIElement.EffectProperty) is Setter effectSetter)
{
2025-03-16 06:52:55 +00:00
windowBorderStyle.Setters.Remove(effectSetter);
}
2024-06-02 12:17:42 +00:00
if (windowBorderStyle.Setters.FirstOrDefault(setterBase => setterBase is Setter setter && setter.Property == FrameworkElement.MarginProperty) is Setter marginSetter)
2025-03-16 06:52:55 +00:00
{
var currentMargin = (Thickness)marginSetter.Value;
var newMargin = new Thickness(
currentMargin.Left - ShadowExtraMargin,
currentMargin.Top - ShadowExtraMargin,
currentMargin.Right - ShadowExtraMargin,
currentMargin.Bottom - ShadowExtraMargin);
marginSetter.Value = newMargin;
}
SetResizeBoarderThickness(null);
UpdateResourceDictionary(dict);
2024-06-02 12:17:42 +00:00
}
public void SetResizeBorderThickness(WindowChrome windowChrome, bool fixedWindowSize)
{
if (fixedWindowSize)
{
windowChrome.ResizeBorderThickness = new Thickness(0);
}
else
{
windowChrome.ResizeBorderThickness = _themeResizeBorderThickness;
}
}
2025-03-16 06:52:55 +00:00
// because adding drop shadow effect will change the margin of the window,
// we need to update the window chrome thickness to correct set the resize border
private void SetResizeBoarderThickness(Thickness? effectMargin)
2024-06-02 12:17:42 +00:00
{
2025-03-16 06:52:55 +00:00
var window = Application.Current.MainWindow;
if (WindowChrome.GetWindowChrome(window) is WindowChrome windowChrome)
{
// Save the theme resize border thickness so that we can restore it if we change ResizeWindow setting
2025-03-16 06:52:55 +00:00
if (effectMargin == null)
{
_themeResizeBorderThickness = SystemParameters.WindowResizeBorderThickness;
2025-03-16 06:52:55 +00:00
}
else
{
_themeResizeBorderThickness = new Thickness(
2025-03-16 06:52:55 +00:00
effectMargin.Value.Left + SystemParameters.WindowResizeBorderThickness.Left,
effectMargin.Value.Top + SystemParameters.WindowResizeBorderThickness.Top,
effectMargin.Value.Right + SystemParameters.WindowResizeBorderThickness.Right,
effectMargin.Value.Bottom + SystemParameters.WindowResizeBorderThickness.Bottom);
}
2024-06-02 12:17:42 +00:00
// Apply the resize border thickness to the window chrome
2025-03-29 08:41:11 +00:00
SetResizeBorderThickness(windowChrome, _settings.KeepMaxResults);
2025-03-16 06:52:55 +00:00
}
}
2024-06-02 12:17:42 +00:00
2025-03-16 06:52:55 +00:00
#endregion
2024-06-02 12:17:42 +00:00
2025-03-16 06:52:55 +00:00
#region Blur Handling
2024-06-03 07:25:42 +00:00
/// <summary>
/// Refreshes the frame to apply the current theme settings.
/// </summary>
public async Task RefreshFrameAsync()
2025-03-16 06:52:55 +00:00
{
2025-03-16 15:55:15 +00:00
await Application.Current.Dispatcher.InvokeAsync(() =>
2024-06-02 12:17:42 +00:00
{
2025-03-16 15:55:15 +00:00
// Get the actual backdrop type and drop shadow effect settings
var (backdropType, useDropShadowEffect) = GetActualValue();
2025-03-16 06:52:55 +00:00
// Remove OS minimizing/maximizing animation
// Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
2025-03-16 15:55:15 +00:00
2025-03-16 06:52:55 +00:00
// The timing of adding the shadow effect should vary depending on whether the theme is transparent.
if (BlurEnabled)
2024-06-02 12:17:42 +00:00
{
2025-03-16 15:55:15 +00:00
AutoDropShadow(useDropShadowEffect);
2024-06-02 12:17:42 +00:00
}
2025-04-04 12:10:39 +00:00
SetBlurForWindow(_settings.Theme, backdropType);
2025-03-16 06:52:55 +00:00
if (!BlurEnabled)
2024-06-02 12:17:42 +00:00
{
2025-03-16 15:55:15 +00:00
AutoDropShadow(useDropShadowEffect);
2024-06-02 12:17:42 +00:00
}
}, DispatcherPriority.Render);
}
2025-03-16 06:52:55 +00:00
/// <summary>
/// Sets the blur for a window via SetWindowCompositionAttribute
/// </summary>
public async Task SetBlurForWindowAsync()
2025-03-16 06:52:55 +00:00
{
2025-03-16 15:55:15 +00:00
await Application.Current.Dispatcher.InvokeAsync(() =>
2025-03-16 06:09:37 +00:00
{
2025-03-16 15:55:15 +00:00
// Get the actual backdrop type and drop shadow effect settings
var (backdropType, _) = GetActualValue();
2025-03-16 06:52:55 +00:00
2025-04-04 12:10:39 +00:00
SetBlurForWindow(_settings.Theme, backdropType);
}, DispatcherPriority.Render);
2025-03-16 15:55:15 +00:00
}
2025-03-16 06:52:55 +00:00
2025-03-16 15:55:15 +00:00
/// <summary>
/// Gets the actual backdrop type and drop shadow effect settings based on the current theme status.
/// </summary>
public (BackdropTypes BackdropType, bool UseDropShadowEffect) GetActualValue()
{
var backdropType = _settings.BackdropType;
var useDropShadowEffect = _settings.UseDropShadowEffect;
2025-03-16 06:52:55 +00:00
2025-03-16 15:55:15 +00:00
// When changed non-blur theme, change to backdrop to none
if (!BlurEnabled)
{
backdropType = BackdropTypes.None;
}
2025-03-04 04:46:36 +00:00
2025-03-16 15:55:15 +00:00
// Dropshadow on and control disabled.(user can't change dropshadow with blur theme)
if (BlurEnabled)
{
useDropShadowEffect = true;
}
2025-03-01 22:51:20 +00:00
2025-03-16 15:55:15 +00:00
return (backdropType, useDropShadowEffect);
}
2025-03-17 01:37:34 +00:00
private void SetBlurForWindow(string theme, BackdropTypes backdropType)
2025-03-16 15:55:15 +00:00
{
2025-03-24 14:07:19 +00:00
var dict = GetResourceDictionary(theme);
if (dict == null) return;
2025-03-16 15:55:15 +00:00
var windowBorderStyle = dict.Contains("WindowBorderStyle") ? dict["WindowBorderStyle"] as Style : null;
2025-03-24 14:07:19 +00:00
if (windowBorderStyle == null) return;
2025-03-16 15:55:15 +00:00
2025-03-24 14:07:19 +00:00
var mainWindow = Application.Current.MainWindow;
if (mainWindow == null) return;
2025-03-16 15:55:15 +00:00
// Check if the theme supports blur
var hasBlur = IsThemeBlurEnabled(dict);
2025-03-16 15:55:15 +00:00
if (BlurEnabled && hasBlur && Win32Helper.IsBackdropSupported())
{
// If the BackdropType is Mica or MicaAlt, set the windowborderstyle's background to transparent
if (backdropType is BackdropTypes.Mica or BackdropTypes.MicaAlt)
2025-03-16 15:55:15 +00:00
{
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property == Control.BackgroundProperty));
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFrozenSolidColorBrush(Color.FromArgb(1, 0, 0, 0))));
2025-03-16 06:52:55 +00:00
}
2025-03-16 15:55:15 +00:00
else if (backdropType == BackdropTypes.Acrylic)
2025-03-16 06:52:55 +00:00
{
windowBorderStyle.Setters.Remove(windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property == Control.BackgroundProperty));
windowBorderStyle.Setters.Add(new Setter(Border.BackgroundProperty, Brushes.Transparent));
2025-03-16 06:52:55 +00:00
}
// For themes with blur enabled, the window border is rendered by the system, so it's treated as a simple rectangle regardless of thickness.
//(This is to avoid issues when the window is forcibly changed to a rectangular shape during snap scenarios.)
var cornerRadiusSetter = windowBorderStyle.Setters.OfType<Setter>().FirstOrDefault(x => x.Property == Border.CornerRadiusProperty);
if (cornerRadiusSetter != null)
cornerRadiusSetter.Value = new CornerRadius(0);
else
windowBorderStyle.Setters.Add(new Setter(Border.CornerRadiusProperty, new CornerRadius(0)));
2025-03-16 15:55:15 +00:00
// Apply the blur effect
Win32Helper.DWMSetBackdropForWindow(mainWindow, backdropType);
2025-03-17 01:37:34 +00:00
ColorizeWindow(theme, backdropType);
2025-03-16 15:55:15 +00:00
}
else
{
// Apply default style when Blur is disabled
Win32Helper.DWMSetBackdropForWindow(mainWindow, BackdropTypes.None);
2025-03-17 01:37:34 +00:00
ColorizeWindow(theme, backdropType);
2025-03-16 15:55:15 +00:00
}
UpdateResourceDictionary(dict);
2020-05-07 04:30:55 +00:00
}
2025-03-16 15:55:15 +00:00
private void AutoDropShadow(bool useDropShadowEffect)
{
2025-03-16 15:55:15 +00:00
if (useDropShadowEffect)
{
if (BlurEnabled && Win32Helper.IsBackdropSupported())
{
// For themes with blur enabled, the window border is rendered by the system,
// so we set corner preference to round and remove drop shadow effect to avoid rendering issues.
SetWindowCornerPreference("Round");
RemoveDropShadowEffectFromCurrentTheme();
}
else
{
// For themes without blur, we set corner preference to default and add drop shadow effect.
SetWindowCornerPreference("Default");
AddDropShadowEffectToCurrentTheme();
}
}
else
{
// When drop shadow effect is disabled, we set corner preference to default and remove drop shadow effect.
SetWindowCornerPreference("Default");
RemoveDropShadowEffectFromCurrentTheme();
}
}
private static void SetWindowCornerPreference(string cornerType)
{
Window mainWindow = Application.Current.MainWindow;
if (mainWindow == null)
return;
Win32Helper.DWMSetCornerPreferenceForWindow(mainWindow, cornerType);
}
2025-03-16 06:52:55 +00:00
// Get Background Color from WindowBorderStyle when there not color for BG.
// for theme has not "LightBG" or "DarkBG" case.
2025-03-17 01:37:34 +00:00
private Color GetWindowBorderStyleBackground(string theme)
2020-05-07 04:30:55 +00:00
{
2025-03-17 01:37:34 +00:00
var Resources = GetThemeResourceDictionary(theme);
2025-03-16 06:52:55 +00:00
var windowBorderStyle = (Style)Resources["WindowBorderStyle"];
2025-03-01 22:51:20 +00:00
2025-03-16 06:52:55 +00:00
var backgroundSetter = windowBorderStyle.Setters
.OfType<Setter>()
.FirstOrDefault(s => s.Property == Border.BackgroundProperty);
if (backgroundSetter != null)
2025-03-04 04:46:36 +00:00
{
2025-03-16 06:52:55 +00:00
// Background's Value is DynamicColor Case
var backgroundValue = backgroundSetter.Value;
if (backgroundValue is SolidColorBrush solidColorBrush)
{
return solidColorBrush.Color; // Return SolidColorBrush's Color
}
else if (backgroundValue is DynamicResourceExtension dynamicResource)
{
// When DynamicResource Extension it is, Key is resource's name.
var resourceKey = backgroundSetter.Value.ToString();
// find key in resource and return color.
if (Resources.Contains(resourceKey))
{
var colorResource = Resources[resourceKey];
if (colorResource is SolidColorBrush colorBrush)
{
return colorBrush.Color;
}
else if (colorResource is Color color)
{
return color;
}
}
}
2025-03-04 04:46:36 +00:00
}
2025-03-16 06:09:37 +00:00
2025-03-16 06:52:55 +00:00
return Colors.Transparent; // Default is transparent
}
private void ApplyPreviewBackground(Color? bgColor = null)
{
if (bgColor == null) return;
// Create a new Style for the preview
var previewStyle = new Style(typeof(Border));
// Get the original WindowBorderStyle
if (Application.Current.Resources.Contains("WindowBorderStyle") &&
Application.Current.Resources["WindowBorderStyle"] is Style originalStyle)
2025-03-04 04:46:36 +00:00
{
// Copy the original style, including the base style if it exists
ThemeHelper.CopyStyle(originalStyle, previewStyle);
}
2020-05-07 04:30:55 +00:00
// Apply background color (remove transparency in color)
var backgroundColor = Color.FromRgb(bgColor.Value.R, bgColor.Value.G, bgColor.Value.B);
previewStyle.Setters.Add(new Setter(Border.BackgroundProperty, ThemeHelper.GetFrozenSolidColorBrush(backgroundColor)));
// The blur theme keeps the corner round fixed (applying DWM code to modify it causes rendering issues).
// The non-blur theme retains the previously set WindowBorderStyle.
if (BlurEnabled)
{
previewStyle.Setters.Add(new Setter(Border.CornerRadiusProperty, new CornerRadius(5)));
previewStyle.Setters.Add(new Setter(Border.BorderThicknessProperty, new Thickness(1)));
}
// Set the new style to the resource
Application.Current.Resources["PreviewWindowBorderStyle"] = previewStyle;
2020-05-07 04:30:55 +00:00
}
2025-03-17 01:37:34 +00:00
private void ColorizeWindow(string theme, BackdropTypes backdropType)
{
2025-03-17 01:37:34 +00:00
var dict = GetThemeResourceDictionary(theme);
if (dict == null) return;
2025-03-16 06:52:55 +00:00
var mainWindow = Application.Current.MainWindow;
if (mainWindow == null) return;
2025-03-16 06:52:55 +00:00
// Check if the theme supports blur
var hasBlur = IsThemeBlurEnabled(dict);
2025-03-16 06:52:55 +00:00
// SystemBG value check (Auto, Light, Dark)
var systemBG = dict.Contains("SystemBG") ? dict["SystemBG"] as string : "Auto"; // 기본값 Auto
2025-03-16 06:52:55 +00:00
// Check the user's ColorScheme setting
var colorScheme = _settings.ColorScheme;
2025-03-16 06:52:55 +00:00
// Final decision on whether to use dark mode
var useDarkMode = false;
2025-03-16 06:52:55 +00:00
// If systemBG is not "Auto", prioritize it over ColorScheme and set the mode based on systemBG value
if (systemBG == "Dark")
{
useDarkMode = true; // Dark
}
else if (systemBG == "Light")
{
useDarkMode = false; // Light
}
else if (systemBG == "Auto")
{
// If systemBG is "Auto", decide based on ColorScheme
if (colorScheme == "Dark")
{
useDarkMode = true;
}
else if (colorScheme == "Light")
{
useDarkMode = false;
}
else
{
// Check system dark mode setting (read AppsUseLightTheme value)
var themeValue = (int)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme", 1);
var isSystemDark = themeValue == 0;
useDarkMode = isSystemDark; // Auto (based on system setting)
}
}
2025-03-16 06:52:55 +00:00
// Apply DWM Dark Mode
Win32Helper.DWMSetDarkModeForWindow(mainWindow, useDarkMode);
2025-03-16 06:52:55 +00:00
Color LightBG;
Color DarkBG;
2025-03-16 06:52:55 +00:00
// Retrieve LightBG value (fallback to WindowBorderStyle background color if not found)
try
{
2025-03-17 01:37:34 +00:00
LightBG = dict.Contains("LightBG") ? (Color)dict["LightBG"] : GetWindowBorderStyleBackground(theme);
}
catch (Exception)
{
2025-03-17 01:37:34 +00:00
LightBG = GetWindowBorderStyleBackground(theme);
}
2025-03-16 06:52:55 +00:00
// Retrieve DarkBG value (fallback to LightBG if not found)
try
{
DarkBG = dict.Contains("DarkBG") ? (Color)dict["DarkBG"] : LightBG;
}
catch (Exception)
{
DarkBG = LightBG;
}
2025-03-16 06:52:55 +00:00
// Select background color based on ColorScheme and SystemBG
Color selectedBG = useDarkMode ? DarkBG : LightBG;
ApplyPreviewBackground(selectedBG);
2025-03-16 06:52:55 +00:00
bool isBlurAvailable = hasBlur && Win32Helper.IsBackdropSupported(); // Windows 11 미만이면 hasBlur를 강제 false
2025-03-16 06:52:55 +00:00
if (!isBlurAvailable)
{
mainWindow.Background = Brushes.Transparent;
}
else
{
// Only set the background to transparent if the theme supports blur
if (backdropType is BackdropTypes.Mica or BackdropTypes.MicaAlt)
2025-03-16 06:52:55 +00:00
{
mainWindow.Background = ThemeHelper.GetFrozenSolidColorBrush(Color.FromArgb(1, 0, 0, 0));
}
else
{
mainWindow.Background = ThemeHelper.GetFrozenSolidColorBrush(selectedBG);
}
}
2025-03-16 06:52:55 +00:00
}
private static bool IsThemeBlurEnabled(ResourceDictionary dict)
2025-03-16 06:52:55 +00:00
{
return dict.Contains("ThemeBlurEnabled") && dict["ThemeBlurEnabled"] is bool enabled && enabled;
}
2025-03-16 06:52:55 +00:00
#endregion
}
}