1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . IO ;
4
3
using System . Linq ;
4
+ using CommunityToolkit . Mvvm . DependencyInjection ;
5
5
using Flow . Launcher . Core . Resource ;
6
+ using Flow . Launcher . Infrastructure . UserSettings ;
6
7
7
8
namespace Flow . Launcher . Plugin . Sys
8
9
{
9
10
public class ThemeSelector : IDisposable
10
11
{
11
12
public const string Keyword = "fltheme" ;
12
13
13
- private readonly PluginInitContext context ;
14
+ private readonly Settings _settings ;
15
+ private readonly Theme _theme ;
16
+ private readonly PluginInitContext _context ;
17
+
14
18
private IEnumerable < string > themes ;
15
19
16
20
public ThemeSelector ( PluginInitContext context )
17
21
{
18
- this . context = context ;
22
+ _context = context ;
23
+ _theme = Ioc . Default . GetRequiredService < Theme > ( ) ;
24
+ _settings = Ioc . Default . GetRequiredService < Settings > ( ) ;
19
25
context . API . VisibilityChanged += OnVisibilityChanged ;
20
26
}
21
27
28
+ ~ ThemeSelector ( )
29
+ {
30
+ Dispose ( false ) ;
31
+ }
32
+
22
33
public List < Result > Query ( Query query )
23
34
{
24
35
if ( query . IsReQuery )
@@ -31,34 +42,34 @@ public List<Result> Query(Query query)
31
42
if ( string . IsNullOrWhiteSpace ( search ) )
32
43
{
33
44
return themes . Select ( CreateThemeResult )
34
- . OrderBy ( x => x . Title )
35
- . ToList ( ) ;
45
+ . OrderBy ( x => x . Title )
46
+ . ToList ( ) ;
36
47
}
37
48
38
- return themes . Select ( theme => ( theme , matchResult : context . API . FuzzySearch ( search , theme ) ) )
39
- . Where ( x => x . matchResult . IsSearchPrecisionScoreMet ( ) )
40
- . Select ( x => CreateThemeResult ( x . theme , x . matchResult . Score , x . matchResult . MatchData ) )
41
- . OrderBy ( x => x . Title )
42
- . ToList ( ) ;
49
+ return themes . Select ( theme => ( theme , matchResult : _context . API . FuzzySearch ( search , theme ) ) )
50
+ . Where ( x => x . matchResult . IsSearchPrecisionScoreMet ( ) )
51
+ . Select ( x => CreateThemeResult ( x . theme , x . matchResult . Score , x . matchResult . MatchData ) )
52
+ . OrderBy ( x => x . Title )
53
+ . ToList ( ) ;
43
54
}
44
55
45
56
private void OnVisibilityChanged ( object sender , VisibilityChangedEventArgs args )
46
57
{
47
- if ( args . IsVisible && ! context . CurrentPluginMetadata . Disabled )
58
+ if ( args . IsVisible && ! _context . CurrentPluginMetadata . Disabled )
48
59
{
49
60
LoadThemes ( ) ;
50
61
}
51
62
}
52
63
53
64
private void LoadThemes ( )
54
- => themes = ThemeManager . Instance . LoadAvailableThemes ( ) . Select ( x => x . FileNameWithoutExtension ) ;
65
+ => themes = _theme . LoadAvailableThemes ( ) . Select ( x => x . FileNameWithoutExtension ) ;
55
66
56
- private static Result CreateThemeResult ( string theme ) => CreateThemeResult ( theme , 0 , null ) ;
67
+ private Result CreateThemeResult ( string theme ) => CreateThemeResult ( theme , 0 , null ) ;
57
68
58
- private static Result CreateThemeResult ( string theme , int score , IList < int > highlightData )
69
+ private Result CreateThemeResult ( string theme , int score , IList < int > highlightData )
59
70
{
60
71
string title ;
61
- if ( theme == ThemeManager . Instance . Settings . Theme )
72
+ if ( theme == _settings . Theme )
62
73
{
63
74
title = $ "{ theme } ★";
64
75
score = 2000 ;
@@ -76,7 +87,7 @@ private static Result CreateThemeResult(string theme, int score, IList<int> high
76
87
Score = score ,
77
88
Action = c =>
78
89
{
79
- ThemeManager . Instance . ChangeTheme ( theme ) ;
90
+ _theme . ChangeTheme ( theme ) ;
80
91
return true ;
81
92
}
82
93
} ;
@@ -90,19 +101,16 @@ protected virtual void Dispose(bool disposing)
90
101
if ( disposing )
91
102
{
92
103
// Dispose managed resources
93
- if ( context ? . API != null )
104
+ if ( _context ? . API != null )
94
105
{
95
- context . API . VisibilityChanged -= OnVisibilityChanged ;
106
+ _context . API . VisibilityChanged -= OnVisibilityChanged ;
96
107
}
97
108
}
98
109
// Free unmanaged resources
99
110
disposed = true ;
100
111
}
101
112
}
102
- ~ ThemeSelector ( )
103
- {
104
- Dispose ( false ) ;
105
- }
113
+
106
114
public void Dispose ( )
107
115
{
108
116
Dispose ( true ) ;
0 commit comments