1
1
using System . Runtime . InteropServices ;
2
2
using System . Diagnostics ;
3
3
using System . Windows ;
4
+ using System . Windows . Controls ;
4
5
using System . Text ;
5
6
using System . Collections . Generic ;
7
+ using Flow . Launcher . Plugin . FendCalculator . ViewModels ;
8
+ using Flow . Launcher . Plugin . FendCalculator . Views ;
6
9
7
10
namespace Flow . Launcher . Plugin . FendCalculator
8
11
{
9
- public class FendCalculator : IPlugin
12
+ public class FendCalculator : IPlugin , ISettingProvider
10
13
{
11
14
private PluginInitContext _context ;
15
+ private Settings _settings ;
16
+ private static SettingsViewModel _viewModel ;
12
17
13
18
public void Init ( PluginInitContext context )
14
19
{
15
20
_context = context ;
21
+ _settings = context . API . LoadSettingJsonStorage < Settings > ( ) ;
22
+ _viewModel = new SettingsViewModel ( _settings ) ;
23
+ if ( string . IsNullOrEmpty ( _settings . FendCommand ) )
24
+ {
25
+ _settings . FendCommand = "fend" ;
26
+ }
16
27
}
17
28
18
29
public List < Result > Query ( Query query )
@@ -31,39 +42,56 @@ public List<Result> Query(Query query)
31
42
UseShellExecute = false ,
32
43
RedirectStandardOutput = true ,
33
44
StandardOutputEncoding = Encoding . UTF8 ,
34
- FileName = "fend ",
45
+ FileName = $ " { _settings . FendCommand } ",
35
46
Arguments = $ "\" { query . Search } \" "
36
47
} ;
37
- Process process = Process . Start ( startInfo ) ;
38
-
39
- string output = process . StandardOutput . ReadToEnd ( ) . TrimEnd ( ) ;
40
- if ( process . ExitCode == 0 && ! string . IsNullOrEmpty ( output ) )
48
+ try
41
49
{
42
- var result = new Result
50
+ Process process = Process . Start ( startInfo ) ;
51
+ string output = process . StandardOutput . ReadToEnd ( ) . TrimEnd ( ) ;
52
+ if ( process . ExitCode == 0 && ! string . IsNullOrEmpty ( output ) )
43
53
{
44
- Title = output ,
45
- SubTitle = "Copy result to clipboard" ,
46
- IcoPath = "Images/calculator.png" ,
47
- Score = 300 ,
48
- CopyText = output ,
49
- Action = c =>
54
+ var result = new Result
50
55
{
51
- try
56
+ Title = output ,
57
+ SubTitle = "Copy result to clipboard" ,
58
+ IcoPath = "Images/calculator.png" ,
59
+ Score = 300 ,
60
+ CopyText = output ,
61
+ Action = c =>
52
62
{
53
- Clipboard . SetDataObject ( output ) ;
54
- return true ;
63
+ try
64
+ {
65
+ Clipboard . SetDataObject ( output ) ;
66
+ return true ;
67
+ }
68
+ catch ( ExternalException )
69
+ {
70
+ MessageBox . Show ( "Copy failed, please try later" ) ;
71
+ return false ;
72
+ }
55
73
}
56
- catch ( ExternalException )
57
- {
58
- MessageBox . Show ( "Copy failed, please try later" ) ;
59
- return false ;
60
- }
61
- }
62
- } ;
63
- results . Add ( result ) ;
74
+ } ;
75
+ results . Add ( result ) ;
76
+ }
77
+ }
78
+ catch ( ExternalException )
79
+ {
80
+ results . Add ( new Result
81
+ {
82
+ Title = "Error" ,
83
+ SubTitle = "fend command error. Check installation path or fend config file" ,
84
+ IcoPath = "Images/calculator.png" ,
85
+ Score = 300
86
+ } ) ;
87
+ return results ;
64
88
}
65
89
66
90
return results ;
67
91
}
92
+ public Control CreateSettingPanel ( )
93
+ {
94
+ return new FendCalculatorSettings ( _viewModel ) ;
95
+ }
68
96
}
69
97
}
0 commit comments