update StreamJsonRPC, use System.Text.Json and apply serialization Option to the formatter; fix empty setting still trigger setting initialization

This commit is contained in:
Hongtao Zhang 2023-12-10 02:26:43 -06:00
parent 15b69a2fac
commit bdc9d02f93
3 changed files with 28 additions and 34 deletions

View file

@ -57,7 +57,7 @@
<PackageReference Include="FSharp.Core" Version="7.0.401" /> <PackageReference Include="FSharp.Core" Version="7.0.401" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" /> <PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" />
<PackageReference Include="squirrel.windows" Version="1.5.2" NoWarn="NU1701" /> <PackageReference Include="squirrel.windows" Version="1.5.2" NoWarn="NU1701" />
<PackageReference Include="StreamJsonRpc" Version="2.16.36" /> <PackageReference Include="StreamJsonRpc" Version="2.17.8" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -15,11 +15,11 @@ namespace Flow.Launcher.Core.Plugin
public required string SettingPath { get; init; } public required string SettingPath { get; init; }
public Dictionary<string, FrameworkElement> SettingControls { get; } = new(); public Dictionary<string, FrameworkElement> SettingControls { get; } = new();
public IReadOnlyDictionary<string, object> Inner => Settings; public IReadOnlyDictionary<string, object> Inner => Settings;
protected ConcurrentDictionary<string, object> Settings { get; set; } protected ConcurrentDictionary<string, object> Settings { get; set; }
public required IPublicAPI API { get; init; } public required IPublicAPI API { get; init; }
private JsonStorage<ConcurrentDictionary<string, object>> _storage; private JsonStorage<ConcurrentDictionary<string, object>> _storage;
// maybe move to resource? // maybe move to resource?
@ -37,18 +37,18 @@ namespace Flow.Launcher.Core.Plugin
_storage = new JsonStorage<ConcurrentDictionary<string, object>>(SettingPath); _storage = new JsonStorage<ConcurrentDictionary<string, object>>(SettingPath);
Settings = await _storage.LoadAsync(); Settings = await _storage.LoadAsync();
if (Settings != null) if (Settings != null || Configuration == null)
{ {
return; return;
} }
foreach (var (type, attributes) in Configuration.Body) foreach (var (type, attributes) in Configuration.Body)
{ {
if (attributes.Name == null) if (attributes.Name == null)
{ {
continue; continue;
} }
if (!Settings.ContainsKey(attributes.Name)) if (!Settings.ContainsKey(attributes.Name))
{ {
Settings[attributes.Name] = attributes.DefaultValue; Settings[attributes.Name] = attributes.DefaultValue;
@ -56,7 +56,7 @@ namespace Flow.Launcher.Core.Plugin
} }
} }
public void UpdateSettings(IReadOnlyDictionary<string, object> settings) public void UpdateSettings(IReadOnlyDictionary<string, object> settings)
{ {
if (settings == null || settings.Count == 0) if (settings == null || settings.Count == 0)
@ -80,34 +80,35 @@ namespace Flow.Launcher.Core.Plugin
comboBox.Dispatcher.Invoke(() => comboBox.SelectedItem = value); comboBox.Dispatcher.Invoke(() => comboBox.SelectedItem = value);
break; break;
case CheckBox checkBox: case CheckBox checkBox:
checkBox.Dispatcher.Invoke(() => checkBox.IsChecked = value is bool isChecked ? isChecked : bool.Parse(value as string ?? string.Empty)); checkBox.Dispatcher.Invoke(() =>
checkBox.IsChecked = value is bool isChecked
? isChecked
: bool.Parse(value as string ?? string.Empty));
break; break;
} }
} }
} }
Save(); Save();
} }
public async Task SaveAsync() public async Task SaveAsync()
{ {
await _storage.SaveAsync(); await _storage.SaveAsync();
} }
public void Save() public void Save()
{ {
_storage.Save(); _storage.Save();
} }
public Control CreateSettingPanel() public Control CreateSettingPanel()
{ {
if (Settings == null) if (Settings == null || Settings.Count == 0)
return new(); return new();
var settingWindow = new UserControl(); var settingWindow = new UserControl();
var mainPanel = new Grid var mainPanel = new Grid { Margin = settingPanelMargin, VerticalAlignment = VerticalAlignment.Center };
{
Margin = settingPanelMargin, VerticalAlignment = VerticalAlignment.Center
};
ColumnDefinition gridCol1 = new ColumnDefinition(); ColumnDefinition gridCol1 = new ColumnDefinition();
ColumnDefinition gridCol2 = new ColumnDefinition(); ColumnDefinition gridCol2 = new ColumnDefinition();
@ -242,10 +243,7 @@ namespace Flow.Launcher.Core.Plugin
Margin = new Thickness(10, 0, 0, 0), Content = "Browse" Margin = new Thickness(10, 0, 0, 0), Content = "Browse"
}; };
var dockPanel = new DockPanel() var dockPanel = new DockPanel() { Margin = settingControlMargin };
{
Margin = settingControlMargin
};
DockPanel.SetDock(Btn, Dock.Right); DockPanel.SetDock(Btn, Dock.Right);
dockPanel.Children.Add(Btn); dockPanel.Children.Add(Btn);
@ -352,7 +350,10 @@ namespace Flow.Launcher.Core.Plugin
case "checkbox": case "checkbox":
var checkBox = new CheckBox var checkBox = new CheckBox
{ {
IsChecked = Settings[attribute.Name] is bool isChecked ? isChecked : bool.Parse(attribute.DefaultValue), IsChecked =
Settings[attribute.Name] is bool isChecked
? isChecked
: bool.Parse(attribute.DefaultValue),
Margin = settingCheckboxMargin, Margin = settingCheckboxMargin,
HorizontalAlignment = System.Windows.HorizontalAlignment.Right, HorizontalAlignment = System.Windows.HorizontalAlignment.Right,
ToolTip = attribute.Description ToolTip = attribute.Description
@ -375,14 +376,12 @@ namespace Flow.Launcher.Core.Plugin
break; break;
case "hyperlink": case "hyperlink":
var hyperlink = new Hyperlink var hyperlink = new Hyperlink { ToolTip = attribute.Description, NavigateUri = attribute.url };
{
ToolTip = attribute.Description, NavigateUri = attribute.url
};
var linkbtn = new System.Windows.Controls.Button var linkbtn = new System.Windows.Controls.Button
{ {
HorizontalAlignment = System.Windows.HorizontalAlignment.Right, Margin = settingControlMargin HorizontalAlignment = System.Windows.HorizontalAlignment.Right,
Margin = settingControlMargin
}; };
linkbtn.Content = attribute.urlLabel; linkbtn.Content = attribute.urlLabel;
@ -408,12 +407,9 @@ namespace Flow.Launcher.Core.Plugin
mainPanel.Children.Add(panel); mainPanel.Children.Add(panel);
mainPanel.Children.Add(contentControl); mainPanel.Children.Add(contentControl);
rowCount++; rowCount++;
} }
return settingWindow; return settingWindow;
} }
} }
} }

View file

@ -91,7 +91,7 @@ namespace Flow.Launcher.Core.Plugin
private void SetupJsonRPC() private void SetupJsonRPC()
{ {
var formatter = new JsonMessageFormatter(); var formatter = new SystemTextJsonFormatter { JsonSerializerOptions = RequestSerializeOption };
var handler = new NewLineDelimitedMessageHandler(ClientPipe, var handler = new NewLineDelimitedMessageHandler(ClientPipe,
formatter); formatter);
@ -100,10 +100,8 @@ namespace Flow.Launcher.Core.Plugin
RPC.AddLocalRpcMethod("UpdateResults", new Action<string, JsonRPCQueryResponseModel>((rawQuery, response) => RPC.AddLocalRpcMethod("UpdateResults", new Action<string, JsonRPCQueryResponseModel>((rawQuery, response) =>
{ {
var results = ParseResults(response); var results = ParseResults(response);
ResultsUpdated?.Invoke(this, new ResultUpdatedEventArgs { Query = new Query() ResultsUpdated?.Invoke(this,
{ new ResultUpdatedEventArgs { Query = new Query() { RawQuery = rawQuery }, Results = results });
RawQuery = rawQuery
}, Results = results });
})); }));
RPC.SynchronizationContext = null; RPC.SynchronizationContext = null;
RPC.StartListening(); RPC.StartListening();