Skip to content

Commit 054d165

Browse files
committed
Use Yaml as configuration file
1 parent 67027eb commit 054d165

File tree

2 files changed

+164
-51
lines changed

2 files changed

+164
-51
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Collections.Generic;
2+
3+
namespace Flow.Launcher.Core.Plugin
4+
{
5+
public class JsonRpcConfigurationModel
6+
{
7+
public List<SettingField> Body { get; set; }
8+
public void Deconstruct(out List<SettingField> Body)
9+
{
10+
Body = this.Body;
11+
}
12+
}
13+
14+
public class SettingField
15+
{
16+
public string Type { get; set; }
17+
public FieldAttributes Attributes { get; set; }
18+
public void Deconstruct(out string Type, out FieldAttributes attributes)
19+
{
20+
Type = this.Type;
21+
attributes = this.Attributes;
22+
}
23+
}
24+
public class FieldAttributes
25+
{
26+
public string Name { get; set; }
27+
public string Label { get; set; }
28+
public string Description { get; set; }
29+
public bool Validation { get; set; }
30+
public List<string> Options { get; set; }
31+
public string DefaultValue { get; set; }
32+
public void Deconstruct(out string Name, out string Label, out string Description, out bool Validation, out List<string> Options, out string DefaultValue)
33+
{
34+
Name = this.Name;
35+
Label = this.Label;
36+
Description = this.Description;
37+
Validation = this.Validation;
38+
Options = this.Options;
39+
DefaultValue = this.DefaultValue;
40+
}
41+
}
42+
}

Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs

Lines changed: 122 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
using System.Text.Json.Serialization;
2020
using System.Windows;
2121
using System.Windows.Controls;
22-
using System.Windows.Forms;
22+
using YamlDotNet.Serialization;
23+
using YamlDotNet.Serialization.NamingConventions;
2324
using CheckBox = System.Windows.Controls.CheckBox;
2425
using Control = System.Windows.Controls.Control;
2526
using Label = System.Windows.Controls.Label;
@@ -47,6 +48,7 @@ internal abstract class JsonRPCPlugin : IAsyncPlugin, IContextMenu, ISettingProv
4748

4849
private static readonly RecyclableMemoryStreamManager BufferManager = new();
4950

51+
private string SettingConfigurationPath => Path.Combine(context.CurrentPluginMetadata.PluginDirectory, "SettingConfiguration.yaml");
5052
private string SettingPath => Path.Combine(DataLocation.PluginSettingsDirectory, context.CurrentPluginMetadata.Name, "Setting.json");
5153

5254
public List<Result> LoadContextMenus(Result selectedResult)
@@ -309,32 +311,22 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
309311

310312
public async Task InitSettingAsync()
311313
{
314+
if (!File.Exists(SettingConfigurationPath))
315+
return;
316+
312317
if (File.Exists(SettingPath))
313318
Settings = await JsonSerializer.DeserializeAsync<Dictionary<string, object>>(File.OpenRead(SettingPath), options);
314319

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));
324322

325-
Settings ??= new();
323+
Settings ??= new Dictionary<string, object>();
326324

327-
foreach (var (key, element) in settingsTemplate)
325+
foreach (var (type, attribute) in _settingsTemplate.Body)
328326
{
329-
if (!Settings.ContainsKey(key))
327+
if (!Settings.ContainsKey(attribute.Name))
330328
{
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;
338330
}
339331
}
340332
}
@@ -344,69 +336,148 @@ public virtual async Task InitAsync(PluginInitContext context)
344336
this.context = context;
345337
await InitSettingAsync();
346338
}
347-
private static Thickness settingControlMargin = new(10);
339+
private static readonly Thickness settingControlMargin = new(10);
340+
private JsonRpcConfigurationModel _settingsTemplate;
348341
public Control CreateSettingPanel()
349342
{
350343
if (Settings == null)
351344
return new();
352345
var settingWindow = new UserControl();
353346
var mainPanel = new StackPanel
354347
{
355-
Margin = settingControlMargin,
356-
Orientation = Orientation.Vertical
348+
Margin = settingControlMargin, Orientation = Orientation.Vertical
357349
};
358350
settingWindow.Content = mainPanel;
359-
foreach (var (key, value) in Settings)
351+
352+
foreach (var (type, attribute) in _settingsTemplate.Body)
360353
{
361354
var panel = new StackPanel
362355
{
363356
Orientation = Orientation.Horizontal,
364357
Margin = settingControlMargin
365358
};
366-
var name = new Label
359+
var name = new Label()
367360
{
368-
Content = key,
369-
VerticalAlignment = VerticalAlignment.Center
361+
Content = attribute.Label,
362+
Margin = settingControlMargin
370363
};
371-
UIElement content = null;
372-
switch (value)
364+
365+
Control contentControl;
366+
367+
switch (type)
373368
{
374-
case int i:
375-
case double d:
376-
throw new TypeAccessException();
377-
case string s:
378-
var textBox = new TextBox
369+
case "Input":
379370
{
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":
385384
{
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":
391415
var checkBox = new CheckBox
392416
{
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
396419
};
397420
checkBox.Click += (_, _) =>
398421
{
399-
Settings[key] = checkBox.IsChecked;
422+
Settings[attribute.Name] = !((bool)Settings[attribute.Name]);
400423
};
401-
content = checkBox;
424+
contentControl = checkBox;
402425
break;
403426
default:
404-
throw new ArgumentOutOfRangeException();
427+
continue;
405428
}
406429
panel.Children.Add(name);
407-
panel.Children.Add(content);
430+
panel.Children.Add(contentControl);
408431
mainPanel.Children.Add(panel);
409432
}
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+
// }
410481
return settingWindow;
411482
}
412483
public void Save()

0 commit comments

Comments
 (0)