Flow.Launcher/Plugins/Flow.Launcher.Plugin.Sys/ThemeSelector.cs

89 lines
2.9 KiB
C#
Raw Normal View History

2025-03-12 13:50:39 +00:00
using System.Collections.Generic;
using System.Linq;
2025-04-08 07:56:54 +00:00
using Flow.Launcher.Plugin.SharedModels;
2024-06-15 11:16:36 +00:00
namespace Flow.Launcher.Plugin.Sys
{
2025-03-12 13:50:39 +00:00
public class ThemeSelector
{
2024-06-15 11:16:36 +00:00
public const string Keyword = "fltheme";
2025-03-12 10:39:14 +00:00
private readonly PluginInitContext _context;
2024-06-15 11:16:36 +00:00
public ThemeSelector(PluginInitContext context)
{
2025-03-12 10:39:14 +00:00
_context = context;
}
public List<Result> Query(Query query)
{
var themes = _context.API.GetAvailableThemes();
var selectedTheme = _context.API.GetCurrentTheme();
var search = query.SecondToEndSearch;
2024-06-15 11:16:36 +00:00
if (string.IsNullOrWhiteSpace(search))
{
return themes.Select(x => CreateThemeResult(x, selectedTheme))
2025-03-12 10:39:14 +00:00
.OrderBy(x => x.Title)
.ToList();
}
return themes.Select(theme => (theme, matchResult: _context.API.FuzzySearch(search, theme.Name)))
2025-03-12 10:39:14 +00:00
.Where(x => x.matchResult.IsSearchPrecisionScoreMet())
.Select(x => CreateThemeResult(x.theme, selectedTheme, x.matchResult.Score, x.matchResult.MatchData))
2025-03-12 10:39:14 +00:00
.OrderBy(x => x.Title)
.ToList();
}
private Result CreateThemeResult(ThemeData theme, ThemeData selectedTheme) => CreateThemeResult(theme, selectedTheme, 0, null);
private Result CreateThemeResult(ThemeData theme, ThemeData selectedTheme, int score, IList<int> highlightData)
{
string title;
if (theme == selectedTheme)
{
title = $"{theme.Name} ★";
2025-03-12 13:50:39 +00:00
// Set current theme to the top
score = 2000;
}
else
{
title = theme.Name;
2025-03-12 10:53:00 +00:00
// 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");
}
2025-03-12 16:05:50 +00:00
if (theme.HasBlur == true)
{
2025-03-12 16:05:50 +00:00
if (!string.IsNullOrEmpty(description))
description += " ";
description += _context.API.GetTranslation("TypeHasBlurToolTip");
}
return new Result
{
Title = title,
TitleHighlightData = highlightData,
SubTitle = description,
2025-03-12 10:54:43 +00:00
IcoPath = "Images\\theme_selector.png",
Glyph = new GlyphInfo("/Resources/#Segoe Fluent Icons", "\ue790"),
Score = score,
Action = c =>
{
2025-04-08 08:49:29 +00:00
if (_context.API.SetCurrentTheme(theme))
{
_context.API.ReQuery();
}
2025-03-12 13:59:04 +00:00
return false;
}
};
}
}
}