Move theme data and functions into api

This commit is contained in:
Jack251970 2025-04-04 20:10:39 +08:00
parent 67cc1e2c77
commit 358b1fd7c8
4 changed files with 116 additions and 18 deletions

View file

@ -81,11 +81,6 @@ namespace Flow.Launcher.Core.Resource
#region Theme Resources
public string GetCurrentTheme()
{
return _settings.Theme;
}
private void MakeSureThemeDirectoriesExist()
{
foreach (var dir in _themeDirectories.Where(dir => !Directory.Exists(dir)))
@ -127,7 +122,7 @@ namespace Flow.Launcher.Core.Resource
try
{
// Load a ResourceDictionary for the specified theme.
var themeName = GetCurrentTheme();
var themeName = _settings.Theme;
var dict = GetThemeResourceDictionary(themeName);
// Apply font settings to the theme resource.
@ -330,7 +325,7 @@ namespace Flow.Launcher.Core.Resource
private ResourceDictionary GetCurrentResourceDictionary()
{
return GetResourceDictionary(GetCurrentTheme());
return GetResourceDictionary(_settings.Theme);
}
private ThemeData GetThemeDataFromPath(string path)
@ -383,9 +378,15 @@ namespace Flow.Launcher.Core.Resource
#endregion
#region Load & Change
#region Get & Change Theme
public List<ThemeData> LoadAvailableThemes()
public ThemeData GetCurrentTheme()
{
var themes = GetAvailableThemes();
return themes.FirstOrDefault(t => t.FileNameWithoutExtension == _settings.Theme) ?? themes.FirstOrDefault();
}
public List<ThemeData> GetAvailableThemes()
{
List<ThemeData> themes = new List<ThemeData>();
foreach (var themeDirectory in _themeDirectories)
@ -403,7 +404,7 @@ namespace Flow.Launcher.Core.Resource
public bool ChangeTheme(string theme = null)
{
if (string.IsNullOrEmpty(theme))
theme = GetCurrentTheme();
theme = _settings.Theme;
string path = GetThemePath(theme);
try
@ -591,7 +592,7 @@ namespace Flow.Launcher.Core.Resource
{
AutoDropShadow(useDropShadowEffect);
}
SetBlurForWindow(GetCurrentTheme(), backdropType);
SetBlurForWindow(_settings.Theme, backdropType);
if (!BlurEnabled)
{
@ -610,7 +611,7 @@ namespace Flow.Launcher.Core.Resource
// Get the actual backdrop type and drop shadow effect settings
var (backdropType, _) = GetActualValue();
SetBlurForWindow(GetCurrentTheme(), backdropType);
SetBlurForWindow(_settings.Theme, backdropType);
}, DispatcherPriority.Render);
}
@ -898,11 +899,5 @@ namespace Flow.Launcher.Core.Resource
}
#endregion
#region Classes
public record ThemeData(string FileNameWithoutExtension, string Name, bool? IsDark = null, bool? HasBlur = null);
#endregion
}
}

View file

@ -344,5 +344,24 @@ namespace Flow.Launcher.Plugin
/// Stop the loading bar in main window
/// </summary>
public void StopLoadingBar();
/// <summary>
/// Get all available themes
/// </summary>
/// <returns></returns>
public List<ThemeData> GetAvailableThemes();
/// <summary>
/// Get the current theme
/// </summary>
/// <returns></returns>
public ThemeData GetCurrentTheme();
/// <summary>
/// Set the current theme
/// </summary>
/// <param name="theme"></param>
/// <returns></returns>
public void SetCurrentTheme(ThemeData theme);
}
}

View file

@ -0,0 +1,71 @@
namespace Flow.Launcher.Plugin;
/// <summary>
/// Theme data model
/// </summary>
public class ThemeData
{
/// <summary>
/// Theme file name without extension
/// </summary>
public string FileNameWithoutExtension { get; private init; }
/// <summary>
/// Theme name
/// </summary>
public string Name { get; private init; }
/// <summary>
/// Theme file path
/// </summary>
public bool? IsDark { get; private init; }
/// <summary>
/// Theme file path
/// </summary>
public bool? HasBlur { get; private init; }
/// <summary>
/// Theme data constructor
/// </summary>
public ThemeData(string fileNameWithoutExtension, string name, bool? isDark = null, bool? hasBlur = null)
{
FileNameWithoutExtension = fileNameWithoutExtension;
Name = name;
IsDark = isDark;
HasBlur = hasBlur;
}
/// <inheritdoc />
public static bool operator ==(ThemeData left, ThemeData right)
{
return left.Equals(right);
}
/// <inheritdoc />
public static bool operator !=(ThemeData left, ThemeData right)
{
return !(left == right);
}
/// <inheritdoc />
public override bool Equals(object obj)
{
if (obj is not ThemeData other)
return false;
return FileNameWithoutExtension == other.FileNameWithoutExtension &&
Name == other.Name;
}
/// <inheritdoc />
public override int GetHashCode()
{
return Name?.GetHashCode() ?? 0;
}
/// <inheritdoc />
public override string ToString()
{
return Name;
}
}

View file

@ -37,6 +37,9 @@ namespace Flow.Launcher
private readonly Internationalization _translater;
private readonly MainViewModel _mainVM;
private Theme _theme;
private Theme Theme => _theme ??= Ioc.Default.GetRequiredService<Theme>();
private readonly object _saveSettingsLock = new();
#region Constructor
@ -354,6 +357,16 @@ namespace Flow.Launcher
public Task ShowProgressBoxAsync(string caption, Func<Action<double>, Task> reportProgressAsync, Action cancelProgress = null) => ProgressBoxEx.ShowAsync(caption, reportProgressAsync, cancelProgress);
public List<ThemeData> GetAvailableThemes() => Theme.GetAvailableThemes();
public ThemeData GetCurrentTheme() => Theme.GetCurrentTheme();
public void SetCurrentTheme(ThemeData theme)
{
Theme.ChangeTheme(theme.FileNameWithoutExtension);
_ = _theme.RefreshFrameAsync();
}
#endregion
#region Private Methods