-
-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flow Launcher Theme Selector plugin #2448
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Flow.Launcher.Core.Resource; | ||
|
||
namespace Flow.Launcher.Plugin.Sys | ||
{ | ||
public class ThemeSelector : IDisposable | ||
{ | ||
public const string Keyword = "fltheme"; | ||
|
||
private readonly PluginInitContext context; | ||
private IEnumerable<string> themes; | ||
|
||
public ThemeSelector(PluginInitContext context) | ||
{ | ||
this.context = context; | ||
context.API.VisibilityChanged += OnVisibilityChanged; | ||
} | ||
|
||
public List<Result> Query(Query query) | ||
{ | ||
if (query.IsReQuery) | ||
{ | ||
LoadThemes(); | ||
} | ||
|
||
string search = query.Search[(query.Search.IndexOf(Keyword, StringComparison.Ordinal) + Keyword.Length + 1)..]; | ||
|
||
if (string.IsNullOrWhiteSpace(search)) | ||
{ | ||
return themes.Select(CreateThemeResult) | ||
.OrderBy(x => x.Title) | ||
.ToList(); | ||
} | ||
|
||
return themes.Select(theme => (theme, matchResult: context.API.FuzzySearch(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(); | ||
} | ||
} | ||
|
||
private void LoadThemes() | ||
=> themes = ThemeManager.Instance.LoadAvailableThemes().Select(Path.GetFileNameWithoutExtension); | ||
Comment on lines
+53
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification The
Analysis chainThe method Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check if ThemeManager handles exceptions during theme loading.
cat Plugins/Flow.Launcher.Plugin.Sys/ThemeManager.cs | grep -A 10 "LoadAvailableThemes"
Length of output: 168 Script: #!/bin/bash
# Locate the file containing the ThemeManager class
fd ThemeManager.cs
# Once located, inspect the LoadAvailableThemes method
ast-grep --lang cs --pattern 'class ThemeManager { $$$ LoadAvailableThemes() { $$$ } $$$ }'
Length of output: 503 Script: #!/bin/bash
# Search for the LoadAvailableThemes method in the ThemeManager.cs file
rg 'LoadAvailableThemes' Flow.Launcher.Core/Resource/ThemeManager.cs -A 20
Length of output: 74 Script: #!/bin/bash
# Search for all method definitions in the ThemeManager.cs file
rg '^\s*(public|private|protected|internal)\s+\w+\s+\w+\s*\(' Flow.Launcher.Core/Resource/ThemeManager.cs
Length of output: 107 Script: #!/bin/bash
# Search for all method definitions across the entire repository to locate the correct method for loading themes
rg '^\s*(public|private|protected|internal)\s+\w+\s+\w+\s*\('
Length of output: 81184 Script: #!/bin/bash
# Search for the LoadAvailableThemes method across the entire repository
rg 'LoadAvailableThemes'
Length of output: 431 Script: #!/bin/bash
# Inspect the LoadAvailableThemes method in Flow.Launcher.Core/Resource/Theme.cs
rg 'LoadAvailableThemes' Flow.Launcher.Core/Resource/Theme.cs -A 20
Length of output: 882 |
||
|
||
private static Result CreateThemeResult(string theme) => CreateThemeResult(theme, 0, null); | ||
|
||
private 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 Dispose() | ||
{ | ||
if (context != null && context.API != null) | ||
{ | ||
context.API.VisibilityChanged -= OnVisibilityChanged; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Query
method handles theme search effectively. However, consider caching the result ofquery.Search.IndexOf(Keyword, StringComparison.Ordinal)
to avoid recalculating it multiple times.Committable suggestion