1
1
using System . Collections . Generic ;
2
2
using System . Linq ;
3
- using CommunityToolkit . Mvvm . DependencyInjection ;
4
- using Flow . Launcher . Core . Resource ;
5
3
6
4
namespace Flow . Launcher . Plugin . Sys
7
5
{
@@ -11,61 +9,38 @@ public class ThemeSelector
11
9
12
10
private readonly PluginInitContext _context ;
13
11
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
-
40
12
public ThemeSelector ( PluginInitContext context )
41
13
{
42
14
_context = context ;
43
15
}
44
16
45
17
public List < Result > Query ( Query query )
46
18
{
19
+ var themes = _context . API . GetAvailableThemes ( ) ;
20
+ var selectedTheme = _context . API . GetCurrentTheme ( ) ;
21
+
47
22
var search = query . SecondToEndSearch ;
48
23
if ( string . IsNullOrWhiteSpace ( search ) )
49
24
{
50
- return Themes . Select ( CreateThemeResult )
25
+ return themes . Select ( x => CreateThemeResult ( x , selectedTheme ) )
51
26
. OrderBy ( x => x . Title )
52
27
. ToList ( ) ;
53
28
}
54
29
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 ) ) )
56
31
. 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 ) )
58
33
. OrderBy ( x => x . Title )
59
34
. ToList ( ) ;
60
35
}
61
36
62
- private Result CreateThemeResult ( Theme . ThemeData theme ) => CreateThemeResult ( theme , 0 , null ) ;
37
+ private Result CreateThemeResult ( ThemeData theme , ThemeData selectedTheme ) => CreateThemeResult ( theme , selectedTheme , 0 , null ) ;
63
38
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 )
65
40
{
66
- string themeName = theme . Name ;
41
+ var themeName = theme . FileNameWithoutExtension ;
67
42
string title ;
68
- if ( theme == SelectedTheme )
43
+ if ( theme == selectedTheme )
69
44
{
70
45
title = $ "{ theme . Name } ★";
71
46
// Set current theme to the top
@@ -101,7 +76,7 @@ private Result CreateThemeResult(Theme.ThemeData theme, int score, IList<int> hi
101
76
Score = score ,
102
77
Action = c =>
103
78
{
104
- SelectedTheme = theme ;
79
+ _context . API . SetCurrentTheme ( theme ) ;
105
80
_context . API . ReQuery ( ) ;
106
81
return false ;
107
82
}
0 commit comments