1
1
using System . Collections . Concurrent ;
2
2
using System . Collections . Generic ;
3
+ using System . Text . Json ;
3
4
using System . Threading . Tasks ;
4
5
using System . Windows ;
5
6
using System . Windows . Controls ;
@@ -18,11 +19,11 @@ public class JsonRPCPluginSettings
18
19
public required string SettingPath { get ; init ; }
19
20
public Dictionary < string , FrameworkElement > SettingControls { get ; } = new ( ) ;
20
21
21
- public IReadOnlyDictionary < string , object > Inner => Settings ;
22
- protected ConcurrentDictionary < string , object > Settings { get ; set ; } = null ! ;
22
+ public IReadOnlyDictionary < string , object ? > Inner => Settings ;
23
+ protected ConcurrentDictionary < string , object ? > Settings { get ; set ; } = null ! ;
23
24
public required IPublicAPI API { get ; init ; }
24
25
25
- private JsonStorage < ConcurrentDictionary < string , object > > _storage = null ! ;
26
+ private JsonStorage < ConcurrentDictionary < string , object ? > > _storage = null ! ;
26
27
27
28
private static readonly Thickness SettingPanelMargin = ( Thickness ) Application . Current . FindResource ( "SettingPanelMargin" ) ;
28
29
private static readonly Thickness SettingPanelItemLeftMargin = ( Thickness ) Application . Current . FindResource ( "SettingPanelItemLeftMargin" ) ;
@@ -31,25 +32,56 @@ public class JsonRPCPluginSettings
31
32
32
33
public async Task InitializeAsync ( )
33
34
{
34
- _storage = new JsonStorage < ConcurrentDictionary < string , object > > ( SettingPath ) ;
35
- Settings = await _storage . LoadAsync ( ) ;
35
+ if ( Settings == null )
36
+ {
37
+ _storage = new JsonStorage < ConcurrentDictionary < string , object ? > > ( SettingPath ) ;
38
+ Settings = await _storage . LoadAsync ( ) ;
39
+
40
+ // Because value type of settings dictionary is object which causes them to be JsonElement when loading from json files,
41
+ // we need to convert it to the correct type
42
+ foreach ( var ( key , value ) in Settings )
43
+ {
44
+ if ( value is JsonElement jsonElement )
45
+ {
46
+ Settings [ key ] = jsonElement . ValueKind switch
47
+ {
48
+ JsonValueKind . String => jsonElement . GetString ( ) ?? value ,
49
+ JsonValueKind . True => jsonElement . GetBoolean ( ) ,
50
+ JsonValueKind . False => jsonElement . GetBoolean ( ) ,
51
+ JsonValueKind . Null => null ,
52
+ _ => value
53
+ } ;
54
+ }
55
+ }
56
+ }
36
57
37
58
if ( Configuration == null )
38
59
{
39
60
return ;
40
61
}
41
62
42
- foreach ( var ( _ , attributes ) in Configuration . Body )
63
+ foreach ( var ( type , attributes ) in Configuration . Body )
43
64
{
44
65
// Skip if the setting does not have attributes or name
45
66
if ( attributes ? . Name == null )
46
67
{
47
68
continue ;
48
69
}
49
70
50
- if ( ! Settings . ContainsKey ( attributes . Name ) )
71
+ if ( NeedSaveInSettings ( type ) )
51
72
{
52
- Settings [ attributes . Name ] = attributes . DefaultValue ;
73
+ // If need save in settings, we need to make sure the setting exists in the settings file
74
+ if ( ! Settings . ContainsKey ( attributes . Name ) )
75
+ {
76
+ if ( type == "checkbox" )
77
+ {
78
+ Settings [ attributes . Name ] = bool . Parse ( attributes . DefaultValue ) ;
79
+ }
80
+ else
81
+ {
82
+ Settings [ attributes . Name ] = attributes . DefaultValue ;
83
+ }
84
+ }
53
85
}
54
86
}
55
87
}
@@ -103,8 +135,8 @@ public void Save()
103
135
104
136
public Control ? CreateSettingPanel ( )
105
137
{
106
- // If there are no settings or the settings are empty, return null
107
- if ( Configuration == null || Settings == null || Settings . IsEmpty )
138
+ // If there are no settings or the settings configuration is empty, return null
139
+ if ( Configuration == null || Settings == null || Configuration . Body . Count == 0 )
108
140
{
109
141
return null ;
110
142
}
@@ -291,7 +323,7 @@ public void Save()
291
323
{
292
324
var textBox = new TextBox ( )
293
325
{
294
- Height = 150 ,
326
+ MaxHeight = 150 ,
295
327
MinWidth = 180 ,
296
328
HorizontalAlignment = HorizontalAlignment . Left ,
297
329
VerticalAlignment = VerticalAlignment . Center ,
@@ -449,8 +481,8 @@ Settings[attributes.Name] is bool isChecked
449
481
Grid . SetRow ( contentControl , rowCount ) ;
450
482
}
451
483
452
- // Add into SettingControls for later use if need
453
- if ( type != "textBlock" && type != "seperator" )
484
+ // Add into SettingControls for settings storage if need
485
+ if ( NeedSaveInSettings ( type ) )
454
486
{
455
487
SettingControls [ attributes . Name ] = contentControl ;
456
488
}
@@ -464,5 +496,10 @@ Settings[attributes.Name] is bool isChecked
464
496
Content = mainPanel
465
497
} ;
466
498
}
499
+
500
+ private static bool NeedSaveInSettings ( string type )
501
+ {
502
+ return type != "textBlock" && type != "seperator" && type != "hyperlink" ;
503
+ }
467
504
}
468
505
}
0 commit comments