Skip to content

Commit c677a63

Browse files
committed
Manually Implement UI Binding
1 parent 420d8ea commit c677a63

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using Orientation = System.Windows.Controls.Orientation;
2828
using TextBox = System.Windows.Controls.TextBox;
2929
using UserControl = System.Windows.Controls.UserControl;
30+
using System.Windows.Data;
3031

3132
namespace Flow.Launcher.Core.Plugin
3233
{
@@ -74,8 +75,15 @@ public List<Result> LoadContextMenus(Result selectedResult)
7475
new JsonObjectConverter()
7576
}
7677
};
78+
79+
private static readonly JsonSerializerOptions settingSerializeOption = new()
80+
{
81+
WriteIndented = true
82+
};
7783
private Dictionary<string, object> Settings { get; set; }
7884

85+
private Dictionary<string, Control> _settingControls = new();
86+
7987
private async Task<List<Result>> DeserializedResultAsync(Stream output)
8088
{
8189
if (output == Stream.Null) return null;
@@ -109,13 +117,7 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
109117
{
110118
result.Action = c =>
111119
{
112-
if (result.SettingsChange is not null)
113-
{
114-
foreach (var (key, value) in result.SettingsChange)
115-
{
116-
Settings[key] = value;
117-
}
118-
}
120+
UpdateSettings(result.SettingsChange);
119121

120122
if (result.JsonRPCAction == null) return false;
121123

@@ -156,13 +158,7 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
156158

157159
results.AddRange(queryResponseModel.Result);
158160

159-
if (queryResponseModel.SettingsChange != null)
160-
{
161-
foreach (var (key, value) in queryResponseModel.SettingsChange)
162-
{
163-
Settings[key] = value;
164-
}
165-
}
161+
UpdateSettings(queryResponseModel.SettingsChange);
166162

167163
return results;
168164
}
@@ -384,7 +380,7 @@ public Control CreateSettingPanel()
384380

385381
switch (type)
386382
{
387-
case "Input":
383+
case "input":
388384
{
389385
var textBox = new TextBox()
390386
{
@@ -462,6 +458,7 @@ public Control CreateSettingPanel()
462458
default:
463459
continue;
464460
}
461+
_settingControls[attribute.Name] = contentControl;
465462
panel.Children.Add(name);
466463
panel.Children.Add(contentControl);
467464
mainPanel.Children.Add(panel);
@@ -473,7 +470,40 @@ public void Save()
473470
if (Settings != null)
474471
{
475472
Helper.ValidateDirectory(Path.Combine(DataLocation.PluginSettingsDirectory, context.CurrentPluginMetadata.Name));
476-
File.WriteAllText(SettingPath, JsonSerializer.Serialize(Settings));
473+
File.WriteAllText(SettingPath, JsonSerializer.Serialize(Settings, settingSerializeOption));
474+
}
475+
}
476+
477+
public void UpdateSettings(Dictionary<string, object> settings)
478+
{
479+
if (settings == null || settings.Count == 0)
480+
return;
481+
482+
foreach (var (key, value) in settings)
483+
{
484+
if (Settings.ContainsKey(key))
485+
{
486+
Settings[key] = value;
487+
}
488+
if (_settingControls.ContainsKey(key))
489+
{
490+
491+
switch (_settingControls[key])
492+
{
493+
case TextBox textBox:
494+
textBox.Dispatcher.Invoke(() => textBox.Text = value as string);
495+
break;
496+
case PasswordBox passwordBox:
497+
passwordBox.Dispatcher.Invoke(() => passwordBox.Password = value as string);
498+
break;
499+
case ComboBox comboBox:
500+
comboBox.Dispatcher.Invoke(() => comboBox.SelectedItem = value);
501+
break;
502+
case CheckBox checkBox:
503+
checkBox.Dispatcher.Invoke(() => checkBox.IsChecked = value is bool isChecked ? isChecked : bool.Parse(value as string));
504+
break;
505+
}
506+
}
477507
}
478508
}
479509
}

0 commit comments

Comments
 (0)