11using System . Collections . Generic ;
22using System . Linq ;
3- using CommunityToolkit . Mvvm . DependencyInjection ;
4- using Flow . Launcher . Core . Resource ;
53
64namespace Flow . Launcher . Plugin . Sys
75{
@@ -11,61 +9,38 @@ public class ThemeSelector
119
1210 private readonly PluginInitContext _context ;
1311
14- // Do not initialize it in the constructor, because it will cause null reference in
15- // var dicts = Application.Current.Resources.MergedDictionaries; line of Theme
16- private Theme theme = null ;
17- private Theme Theme => theme ??= Ioc . Default . GetRequiredService < Theme > ( ) ;
18-
19- #region Theme Selection
20-
21- // Theme select codes simplified from SettingsPaneThemeViewModel.cs
22-
23- private Theme . ThemeData _selectedTheme ;
24- public Theme . ThemeData SelectedTheme
25- {
26- get => _selectedTheme ??= Themes . Find ( v => v . FileNameWithoutExtension == Theme . GetCurrentTheme ( ) ) ;
27- set
28- {
29- _selectedTheme = value ;
30- Theme . ChangeTheme ( value . FileNameWithoutExtension ) ;
31-
32- _ = Theme . RefreshFrameAsync ( ) ;
33- }
34- }
35-
36- private List < Theme . ThemeData > Themes => Theme . LoadAvailableThemes ( ) ;
37-
38- #endregion
39-
4012 public ThemeSelector ( PluginInitContext context )
4113 {
4214 _context = context ;
4315 }
4416
4517 public List < Result > Query ( Query query )
4618 {
19+ var themes = _context . API . GetAvailableThemes ( ) ;
20+ var selectedTheme = _context . API . GetCurrentTheme ( ) ;
21+
4722 var search = query . SecondToEndSearch ;
4823 if ( string . IsNullOrWhiteSpace ( search ) )
4924 {
50- return Themes . Select ( CreateThemeResult )
25+ return themes . Select ( x => CreateThemeResult ( x , selectedTheme ) )
5126 . OrderBy ( x => x . Title )
5227 . ToList ( ) ;
5328 }
5429
55- return Themes . Select ( theme => ( theme , matchResult : _context . API . FuzzySearch ( search , theme . Name ) ) )
30+ return themes . Select ( theme => ( theme , matchResult : _context . API . FuzzySearch ( search , theme . Name ) ) )
5631 . Where ( x => x . matchResult . IsSearchPrecisionScoreMet ( ) )
57- . Select ( x => CreateThemeResult ( x . theme , x . matchResult . Score , x . matchResult . MatchData ) )
32+ . Select ( x => CreateThemeResult ( x . theme , selectedTheme , x . matchResult . Score , x . matchResult . MatchData ) )
5833 . OrderBy ( x => x . Title )
5934 . ToList ( ) ;
6035 }
6136
62- private Result CreateThemeResult ( Theme . ThemeData theme ) => CreateThemeResult ( theme , 0 , null ) ;
37+ private Result CreateThemeResult ( ThemeData theme , ThemeData selectedTheme ) => CreateThemeResult ( theme , selectedTheme , 0 , null ) ;
6338
64- private Result CreateThemeResult ( Theme . ThemeData theme , int score , IList < int > highlightData )
39+ private Result CreateThemeResult ( ThemeData theme , ThemeData selectedTheme , int score , IList < int > highlightData )
6540 {
66- string themeName = theme . Name ;
41+ var themeName = theme . FileNameWithoutExtension ;
6742 string title ;
68- if ( theme == SelectedTheme )
43+ if ( theme == selectedTheme )
6944 {
7045 title = $ "{ theme . Name } ★";
7146 // Set current theme to the top
@@ -101,7 +76,7 @@ private Result CreateThemeResult(Theme.ThemeData theme, int score, IList<int> hi
10176 Score = score ,
10277 Action = c =>
10378 {
104- SelectedTheme = theme ;
79+ _context . API . SetCurrentTheme ( theme ) ;
10580 _context . API . ReQuery ( ) ;
10681 return false ;
10782 }
0 commit comments