19
19
using System . Text . Json . Serialization ;
20
20
using System . Windows ;
21
21
using System . Windows . Controls ;
22
- using System . Windows . Forms ;
22
+ using YamlDotNet . Serialization ;
23
+ using YamlDotNet . Serialization . NamingConventions ;
23
24
using CheckBox = System . Windows . Controls . CheckBox ;
24
25
using Control = System . Windows . Controls . Control ;
25
26
using Label = System . Windows . Controls . Label ;
@@ -47,6 +48,7 @@ internal abstract class JsonRPCPlugin : IAsyncPlugin, IContextMenu, ISettingProv
47
48
48
49
private static readonly RecyclableMemoryStreamManager BufferManager = new ( ) ;
49
50
51
+ private string SettingConfigurationPath => Path . Combine ( context . CurrentPluginMetadata . PluginDirectory , "SettingConfiguration.yaml" ) ;
50
52
private string SettingPath => Path . Combine ( DataLocation . PluginSettingsDirectory , context . CurrentPluginMetadata . Name , "Setting.json" ) ;
51
53
52
54
public List < Result > LoadContextMenus ( Result selectedResult )
@@ -309,32 +311,22 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
309
311
310
312
public async Task InitSettingAsync ( )
311
313
{
314
+ if ( ! File . Exists ( SettingConfigurationPath ) )
315
+ return ;
316
+
312
317
if ( File . Exists ( SettingPath ) )
313
318
Settings = await JsonSerializer . DeserializeAsync < Dictionary < string , object > > ( File . OpenRead ( SettingPath ) , options ) ;
314
319
315
- var request = new JsonRPCRequestModel ( )
316
- {
317
- Method = "get_setting_template"
318
- } ;
319
- await using var result = await RequestAsync ( request ) ;
320
- if ( result . Length == 0 )
321
- return ;
322
- var settingsTemplate = await JsonSerializer . DeserializeAsync < Dictionary < string , JsonElement > > ( result , options ) ??
323
- new ( ) ;
320
+ var deserializer = new DeserializerBuilder ( ) . WithNamingConvention ( CamelCaseNamingConvention . Instance ) . Build ( ) ;
321
+ _settingsTemplate = deserializer . Deserialize < JsonRpcConfigurationModel > ( await File . ReadAllTextAsync ( SettingConfigurationPath ) ) ;
324
322
325
- Settings ??= new ( ) ;
323
+ Settings ??= new Dictionary < string , object > ( ) ;
326
324
327
- foreach ( var ( key , element ) in settingsTemplate )
325
+ foreach ( var ( type , attribute ) in _settingsTemplate . Body )
328
326
{
329
- if ( ! Settings . ContainsKey ( key ) )
327
+ if ( ! Settings . ContainsKey ( attribute . Name ) )
330
328
{
331
- Settings [ key ] = element . ValueKind switch
332
- {
333
- JsonValueKind . True or JsonValueKind . False => element . GetBoolean ( ) ,
334
- JsonValueKind . String or JsonValueKind . Number => element . GetString ( ) ,
335
- JsonValueKind . Null => throw new ArgumentNullException ( ) ,
336
- _ => throw new ArgumentOutOfRangeException ( )
337
- } ;
329
+ Settings [ attribute . Name ] = attribute . DefaultValue ;
338
330
}
339
331
}
340
332
}
@@ -344,69 +336,148 @@ public virtual async Task InitAsync(PluginInitContext context)
344
336
this . context = context ;
345
337
await InitSettingAsync ( ) ;
346
338
}
347
- private static Thickness settingControlMargin = new ( 10 ) ;
339
+ private static readonly Thickness settingControlMargin = new ( 10 ) ;
340
+ private JsonRpcConfigurationModel _settingsTemplate ;
348
341
public Control CreateSettingPanel ( )
349
342
{
350
343
if ( Settings == null )
351
344
return new ( ) ;
352
345
var settingWindow = new UserControl ( ) ;
353
346
var mainPanel = new StackPanel
354
347
{
355
- Margin = settingControlMargin ,
356
- Orientation = Orientation . Vertical
348
+ Margin = settingControlMargin , Orientation = Orientation . Vertical
357
349
} ;
358
350
settingWindow . Content = mainPanel ;
359
- foreach ( var ( key , value ) in Settings )
351
+
352
+ foreach ( var ( type , attribute ) in _settingsTemplate . Body )
360
353
{
361
354
var panel = new StackPanel
362
355
{
363
356
Orientation = Orientation . Horizontal ,
364
357
Margin = settingControlMargin
365
358
} ;
366
- var name = new Label
359
+ var name = new Label ( )
367
360
{
368
- Content = key ,
369
- VerticalAlignment = VerticalAlignment . Center
361
+ Content = attribute . Label ,
362
+ Margin = settingControlMargin
370
363
} ;
371
- UIElement content = null ;
372
- switch ( value )
364
+
365
+ Control contentControl ;
366
+
367
+ switch ( type )
373
368
{
374
- case int i :
375
- case double d :
376
- throw new TypeAccessException ( ) ;
377
- case string s :
378
- var textBox = new TextBox
369
+ case "Input" :
379
370
{
380
- Text = s ,
381
- Margin = settingControlMargin ,
382
- VerticalAlignment = VerticalAlignment . Center
383
- } ;
384
- textBox . TextChanged += ( _ , _ ) =>
371
+ var textBox = new TextBox ( )
372
+ {
373
+ Width = 300 , Text = Settings [ attribute . Name ] as string ?? string . Empty ,
374
+ Margin = settingControlMargin
375
+ } ;
376
+ textBox . TextChanged += ( _ , _ ) =>
377
+ {
378
+ Settings [ attribute . Name ] = textBox . Text ;
379
+ } ;
380
+ contentControl = textBox ;
381
+ break ;
382
+ }
383
+ case "textarea" :
385
384
{
386
- Settings [ key ] = textBox . Text ;
387
- } ;
388
- content = textBox ;
389
- break ;
390
- case bool b :
385
+ var textBox = new TextBox ( )
386
+ {
387
+ Width = 300 ,
388
+ Height = 100 ,
389
+ Margin = settingControlMargin ,
390
+ TextWrapping = TextWrapping . WrapWithOverflow ,
391
+ Text = Settings [ attribute . Name ] as string ?? string . Empty
392
+ } ;
393
+ textBox . TextChanged += ( sender , _ ) =>
394
+ {
395
+ Settings [ attribute . Name ] = ( ( TextBox ) sender ) . Text ;
396
+ } ;
397
+ contentControl = textBox ;
398
+ break ;
399
+ }
400
+ case "dropdown" :
401
+ {
402
+ var comboBox = new ComboBox ( )
403
+ {
404
+ ItemsSource = attribute . Options , SelectedItem = Settings [ attribute . Name ] ,
405
+ Margin = settingControlMargin
406
+ } ;
407
+ comboBox . SelectionChanged += ( sender , _ ) =>
408
+ {
409
+ Settings [ attribute . Name ] = ( string ) ( ( ComboBox ) sender ) . SelectedItem ;
410
+ } ;
411
+ contentControl = comboBox ;
412
+ break ;
413
+ }
414
+ case "checkbox" :
391
415
var checkBox = new CheckBox
392
416
{
393
- IsChecked = b ,
394
- Margin = settingControlMargin ,
395
- VerticalAlignment = VerticalAlignment . Center
417
+ IsChecked = Settings [ attribute . Name ] is bool isChecked ? isChecked : bool . Parse ( attribute . DefaultValue ) ,
418
+ Margin = settingControlMargin
396
419
} ;
397
420
checkBox . Click += ( _ , _ ) =>
398
421
{
399
- Settings [ key ] = checkBox . IsChecked ;
422
+ Settings [ attribute . Name ] = ! ( ( bool ) Settings [ attribute . Name ] ) ;
400
423
} ;
401
- content = checkBox ;
424
+ contentControl = checkBox ;
402
425
break ;
403
426
default :
404
- throw new ArgumentOutOfRangeException ( ) ;
427
+ continue ;
405
428
}
406
429
panel . Children . Add ( name ) ;
407
- panel . Children . Add ( content ) ;
430
+ panel . Children . Add ( contentControl ) ;
408
431
mainPanel . Children . Add ( panel ) ;
409
432
}
433
+
434
+ // foreach (var (key, value) in Settings)
435
+ // {
436
+ // var panel = new StackPanel
437
+ // {
438
+ // Orientation = Orientation.Horizontal, Margin = settingControlMargin
439
+ // };
440
+ // var name = new Label
441
+ // {
442
+ // Content = key, VerticalAlignment = VerticalAlignment.Center
443
+ // };
444
+ // UIElement content = null;
445
+ // switch (value)
446
+ // {
447
+ // case int i:
448
+ // case double d:
449
+ // throw new TypeAccessException();
450
+ // case string s:
451
+ // var textBox = new TextBox
452
+ // {
453
+ // Text = s,
454
+ // Margin = settingControlMargin,
455
+ // VerticalAlignment = VerticalAlignment.Center
456
+ // };
457
+ // textBox.TextChanged += (_, _) =>
458
+ // {
459
+ // Settings[key] = textBox.Text;
460
+ // };
461
+ // content = textBox;
462
+ // break;
463
+ // case bool b:
464
+ // var checkBox = new CheckBox
465
+ // {
466
+ // IsChecked = b,
467
+ // Margin = settingControlMargin,
468
+ // VerticalAlignment = VerticalAlignment.Center
469
+ // };
470
+ // checkBox.Click += (_, _) =>
471
+ // {
472
+ // Settings[key] = checkBox.IsChecked;
473
+ // };
474
+ // content = checkBox;
475
+ // break;
476
+ // default:
477
+ // throw new ArgumentOutOfRangeException();
478
+ // }
479
+ //
480
+ // }
410
481
return settingWindow ;
411
482
}
412
483
public void Save ( )
0 commit comments