Flow.Launcher/Plugins/Flow.Launcher.Plugin.Sys/ThemeSelector.cs
2025-03-31 21:53:33 +08:00

111 lines
3.6 KiB
C#

using System.Collections.Generic;
using System.Linq;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Core.Resource;
namespace Flow.Launcher.Plugin.Sys
{
public class ThemeSelector
{
public const string Keyword = "fltheme";
private readonly PluginInitContext _context;
// Do not initialize it in the constructor, because it will cause null reference in
// var dicts = Application.Current.Resources.MergedDictionaries; line of Theme
private Theme theme = null;
private Theme Theme => theme ??= Ioc.Default.GetRequiredService<Theme>();
#region Theme Selection
// Theme select codes simplified from SettingsPaneThemeViewModel.cs
private Theme.ThemeData _selectedTheme;
public Theme.ThemeData SelectedTheme
{
get => _selectedTheme ??= Themes.Find(v => v.FileNameWithoutExtension == Theme.GetCurrentTheme());
set
{
_selectedTheme = value;
Theme.ChangeTheme(value.FileNameWithoutExtension);
_ = Theme.RefreshFrameAsync();
}
}
private List<Theme.ThemeData> Themes => Theme.LoadAvailableThemes();
#endregion
public ThemeSelector(PluginInitContext context)
{
_context = context;
}
public List<Result> Query(Query query)
{
var search = query.SecondToEndSearch;
if (string.IsNullOrWhiteSpace(search))
{
return Themes.Select(CreateThemeResult)
.OrderBy(x => x.Title)
.ToList();
}
return Themes.Select(theme => (theme, matchResult: _context.API.FuzzySearch(search, theme.Name)))
.Where(x => x.matchResult.IsSearchPrecisionScoreMet())
.Select(x => CreateThemeResult(x.theme, x.matchResult.Score, x.matchResult.MatchData))
.OrderBy(x => x.Title)
.ToList();
}
private Result CreateThemeResult(Theme.ThemeData theme) => CreateThemeResult(theme, 0, null);
private Result CreateThemeResult(Theme.ThemeData theme, int score, IList<int> highlightData)
{
string themeName = theme.Name;
string title;
if (theme == SelectedTheme)
{
title = $"{theme.Name} ★";
// Set current theme to the top
score = 2000;
}
else
{
title = theme.Name;
// Set them to 1000 so that they are higher than other non-theme records
score = 1000;
}
string description = string.Empty;
if (theme.IsDark == true)
{
description += _context.API.GetTranslation("TypeIsDarkToolTip");
}
if (theme.HasBlur == true)
{
if (!string.IsNullOrEmpty(description))
description += " ";
description += _context.API.GetTranslation("TypeHasBlurToolTip");
}
return new Result
{
Title = title,
TitleHighlightData = highlightData,
SubTitle = description,
IcoPath = "Images\\theme_selector.png",
Glyph = new GlyphInfo("/Resources/#Segoe Fluent Icons", "\ue790"),
Score = score,
Action = c =>
{
SelectedTheme = theme;
_context.API.ReQuery();
return false;
}
};
}
}
}