27
27
using Orientation = System . Windows . Controls . Orientation ;
28
28
using TextBox = System . Windows . Controls . TextBox ;
29
29
using UserControl = System . Windows . Controls . UserControl ;
30
+ using System . Windows . Data ;
30
31
31
32
namespace Flow . Launcher . Core . Plugin
32
33
{
@@ -74,8 +75,15 @@ public List<Result> LoadContextMenus(Result selectedResult)
74
75
new JsonObjectConverter ( )
75
76
}
76
77
} ;
78
+
79
+ private static readonly JsonSerializerOptions settingSerializeOption = new ( )
80
+ {
81
+ WriteIndented = true
82
+ } ;
77
83
private Dictionary < string , object > Settings { get ; set ; }
78
84
85
+ private Dictionary < string , Control > _settingControls = new ( ) ;
86
+
79
87
private async Task < List < Result > > DeserializedResultAsync ( Stream output )
80
88
{
81
89
if ( output == Stream . Null ) return null ;
@@ -109,13 +117,7 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
109
117
{
110
118
result . Action = c =>
111
119
{
112
- if ( result . SettingsChange is not null )
113
- {
114
- foreach ( var ( key , value ) in result . SettingsChange )
115
- {
116
- Settings [ key ] = value ;
117
- }
118
- }
120
+ UpdateSettings ( result . SettingsChange ) ;
119
121
120
122
if ( result . JsonRPCAction == null ) return false ;
121
123
@@ -156,13 +158,7 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
156
158
157
159
results . AddRange ( queryResponseModel . Result ) ;
158
160
159
- if ( queryResponseModel . SettingsChange != null )
160
- {
161
- foreach ( var ( key , value ) in queryResponseModel . SettingsChange )
162
- {
163
- Settings [ key ] = value ;
164
- }
165
- }
161
+ UpdateSettings ( queryResponseModel . SettingsChange ) ;
166
162
167
163
return results ;
168
164
}
@@ -384,7 +380,7 @@ public Control CreateSettingPanel()
384
380
385
381
switch ( type )
386
382
{
387
- case "Input " :
383
+ case "input " :
388
384
{
389
385
var textBox = new TextBox ( )
390
386
{
@@ -462,6 +458,7 @@ public Control CreateSettingPanel()
462
458
default :
463
459
continue ;
464
460
}
461
+ _settingControls [ attribute . Name ] = contentControl ;
465
462
panel . Children . Add ( name ) ;
466
463
panel . Children . Add ( contentControl ) ;
467
464
mainPanel . Children . Add ( panel ) ;
@@ -473,7 +470,40 @@ public void Save()
473
470
if ( Settings != null )
474
471
{
475
472
Helper . ValidateDirectory ( Path . Combine ( DataLocation . PluginSettingsDirectory , context . CurrentPluginMetadata . Name ) ) ;
476
- File . WriteAllText ( SettingPath , JsonSerializer . Serialize ( Settings ) ) ;
473
+ File . WriteAllText ( SettingPath , JsonSerializer . Serialize ( Settings , settingSerializeOption ) ) ;
474
+ }
475
+ }
476
+
477
+ public void UpdateSettings ( Dictionary < string , object > settings )
478
+ {
479
+ if ( settings == null || settings . Count == 0 )
480
+ return ;
481
+
482
+ foreach ( var ( key , value ) in settings )
483
+ {
484
+ if ( Settings . ContainsKey ( key ) )
485
+ {
486
+ Settings [ key ] = value ;
487
+ }
488
+ if ( _settingControls . ContainsKey ( key ) )
489
+ {
490
+
491
+ switch ( _settingControls [ key ] )
492
+ {
493
+ case TextBox textBox :
494
+ textBox . Dispatcher . Invoke ( ( ) => textBox . Text = value as string ) ;
495
+ break ;
496
+ case PasswordBox passwordBox :
497
+ passwordBox . Dispatcher . Invoke ( ( ) => passwordBox . Password = value as string ) ;
498
+ break ;
499
+ case ComboBox comboBox :
500
+ comboBox . Dispatcher . Invoke ( ( ) => comboBox . SelectedItem = value ) ;
501
+ break ;
502
+ case CheckBox checkBox :
503
+ checkBox . Dispatcher . Invoke ( ( ) => checkBox . IsChecked = value is bool isChecked ? isChecked : bool . Parse ( value as string ) ) ;
504
+ break ;
505
+ }
506
+ }
477
507
}
478
508
}
479
509
}
0 commit comments