Create Flow Launcher Theme Selector plugin

This commit is contained in:
Odotocodot 2023-11-30 14:37:05 +00:00
parent 2c39cf7501
commit 1b31e9c968
4 changed files with 140 additions and 0 deletions

View file

@ -0,0 +1,37 @@

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\..\Output\Debug\Plugins\Flow.Launcher.Plugin.FlowThemeSelector</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>..\..\Output\Release\Plugins\Flow.Launcher.Plugin.FlowThemeSelector</OutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
<ProjectReference Include="..\..\Flow.Launcher.Core\Flow.Launcher.Core.csproj" />
<ProjectReference Include="..\..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="plugin.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="icon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View file

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Flow.Launcher.Core.Resource;
namespace Flow.Launcher.Plugin.FlowThemeSelector
{
public class FlowThemeSelector : IPlugin, IReloadable, IDisposable
{
private PluginInitContext context;
private IEnumerable<string> themes;
public void Init(PluginInitContext context)
{
this.context = context;
context.API.VisibilityChanged += OnVisibilityChanged;
}
public List<Result> Query(Query query)
{
if (query.IsReQuery)
{
LoadThemes();
}
if (string.IsNullOrWhiteSpace(query.Search))
{
return themes.Select(CreateThemeResult)
.OrderBy(x => x.Title)
.ToList();
}
return themes.Select(theme => (theme, matchResult: context.API.FuzzySearch(query.Search, theme)))
.Where(x => x.matchResult.IsSearchPrecisionScoreMet())
.Select(x => CreateThemeResult(x.theme, x.matchResult.Score, x.matchResult.MatchData))
.OrderBy(x => x.Title)
.ToList();
}
private void OnVisibilityChanged(object sender, VisibilityChangedEventArgs args)
{
if (args.IsVisible && !context.CurrentPluginMetadata.Disabled)
{
LoadThemes();
}
}
public void LoadThemes() => themes = ThemeManager.Instance.LoadAvailableThemes().Select(Path.GetFileNameWithoutExtension);
public static Result CreateThemeResult(string theme) => CreateThemeResult(theme, 0, null);
public static Result CreateThemeResult(string theme, int score, IList<int> highlightData)
{
string title;
if (theme == ThemeManager.Instance.Settings.Theme)
{
title = $"{theme} ★";
score = 2000;
}
else
{
title = theme;
}
return new Result
{
Title = title,
TitleHighlightData = highlightData,
Glyph = new GlyphInfo("/Resources/#Segoe Fluent Icons", "\ue790"),
Score = score,
Action = c =>
{
ThemeManager.Instance.ChangeTheme(theme);
return true;
}
};
}
public void ReloadData() => LoadThemes();
public void Dispose()
{
if (context != null && context.API != null)
{
context.API.VisibilityChanged -= OnVisibilityChanged;
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

View file

@ -0,0 +1,12 @@
{
"ID": "4DFA743E1086414BAB0AA3071D561D75",
"ActionKeyword": "flowtheme",
"Name": "Flow Launcher Theme Selector",
"Description": "Quickly switch your Flow Launcher theme.",
"Author": "Odotocodot",
"Version": "1.0.0",
"Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"IcoPath": "icon.png",
"ExecuteFileName": "Flow.Launcher.Plugin.FlowThemeSelector.dll"
}