From e1c446128624779d7402a0668b1ecff7378c61e6 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 1 Mar 2025 17:43:45 +0800 Subject: [PATCH] Fix JsonElement parse issue --- .../Plugin/JsonRPCPluginSettings.cs | 63 +++++++++++++++---- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs b/Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs index 1e0e471f7..8282dec4c 100644 --- a/Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs +++ b/Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; using System.Collections.Generic; +using System.Text.Json; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -18,11 +19,11 @@ namespace Flow.Launcher.Core.Plugin public required string SettingPath { get; init; } public Dictionary SettingControls { get; } = new(); - public IReadOnlyDictionary Inner => Settings; - protected ConcurrentDictionary Settings { get; set; } = null!; + public IReadOnlyDictionary Inner => Settings; + protected ConcurrentDictionary Settings { get; set; } = null!; public required IPublicAPI API { get; init; } - private JsonStorage> _storage = null!; + private JsonStorage> _storage = null!; private static readonly Thickness SettingPanelMargin = (Thickness)Application.Current.FindResource("SettingPanelMargin"); private static readonly Thickness SettingPanelItemLeftMargin = (Thickness)Application.Current.FindResource("SettingPanelItemLeftMargin"); @@ -31,15 +32,35 @@ namespace Flow.Launcher.Core.Plugin public async Task InitializeAsync() { - _storage = new JsonStorage>(SettingPath); - Settings = await _storage.LoadAsync(); + if (Settings == null) + { + _storage = new JsonStorage>(SettingPath); + Settings = await _storage.LoadAsync(); + + // Because value type of settings dictionary is object which causes them to be JsonElement when loading from json files, + // we need to convert it to the correct type + foreach (var (key, value) in Settings) + { + if (value is JsonElement jsonElement) + { + Settings[key] = jsonElement.ValueKind switch + { + JsonValueKind.String => jsonElement.GetString() ?? value, + JsonValueKind.True => jsonElement.GetBoolean(), + JsonValueKind.False => jsonElement.GetBoolean(), + JsonValueKind.Null => null, + _ => value + }; + } + } + } if (Configuration == null) { return; } - foreach (var (_, attributes) in Configuration.Body) + foreach (var (type, attributes) in Configuration.Body) { // Skip if the setting does not have attributes or name if (attributes?.Name == null) @@ -47,9 +68,20 @@ namespace Flow.Launcher.Core.Plugin continue; } - if (!Settings.ContainsKey(attributes.Name)) + if (NeedSaveInSettings(type)) { - Settings[attributes.Name] = attributes.DefaultValue; + // If need save in settings, we need to make sure the setting exists in the settings file + if (!Settings.ContainsKey(attributes.Name)) + { + if (type == "checkbox") + { + Settings[attributes.Name] = bool.Parse(attributes.DefaultValue); + } + else + { + Settings[attributes.Name] = attributes.DefaultValue; + } + } } } } @@ -103,8 +135,8 @@ namespace Flow.Launcher.Core.Plugin public Control? CreateSettingPanel() { - // If there are no settings or the settings are empty, return null - if (Configuration == null || Settings == null || Settings.IsEmpty) + // If there are no settings or the settings configuration is empty, return null + if (Configuration == null || Settings == null || Configuration.Body.Count == 0) { return null; } @@ -291,7 +323,7 @@ namespace Flow.Launcher.Core.Plugin { var textBox = new TextBox() { - Height = 150, + MaxHeight = 150, MinWidth = 180, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Center, @@ -449,8 +481,8 @@ namespace Flow.Launcher.Core.Plugin Grid.SetRow(contentControl, rowCount); } - // Add into SettingControls for later use if need - if (type != "textBlock" && type != "seperator") + // Add into SettingControls for settings storage if need + if (NeedSaveInSettings(type)) { SettingControls[attributes.Name] = contentControl; } @@ -464,5 +496,10 @@ namespace Flow.Launcher.Core.Plugin Content = mainPanel }; } + + private static bool NeedSaveInSettings(string type) + { + return type != "textBlock" && type != "seperator" && type != "hyperlink"; + } } }