Skip to content

Commit afb38db

Browse files
committed
Fix possible null reference exception
1 parent 43bf634 commit afb38db

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public async Task InitializeAsync()
7878
{
7979
if (type == "checkbox")
8080
{
81-
Settings[attributes.Name] = bool.Parse(attributes.DefaultValue);
81+
// If can parse the default value to bool, use it, otherwise use false
82+
Settings[attributes.Name] = bool.TryParse(attributes.DefaultValue, out var value) && value;
8283
}
8384
else
8485
{
@@ -105,19 +106,24 @@ public void UpdateSettings(IReadOnlyDictionary<string, object> settings)
105106
switch (control)
106107
{
107108
case TextBox textBox:
108-
textBox.Dispatcher.Invoke(() => textBox.Text = value as string ?? string.Empty);
109+
var text = value as string ?? string.Empty;
110+
textBox.Dispatcher.Invoke(() => textBox.Text = text);
109111
break;
110112
case PasswordBox passwordBox:
111-
passwordBox.Dispatcher.Invoke(() => passwordBox.Password = value as string ?? string.Empty);
113+
var password = value as string ?? string.Empty;
114+
passwordBox.Dispatcher.Invoke(() => passwordBox.Password = password);
112115
break;
113116
case ComboBox comboBox:
114117
comboBox.Dispatcher.Invoke(() => comboBox.SelectedItem = value);
115118
break;
116119
case CheckBox checkBox:
120+
var isChecked = value is bool boolValue
121+
? boolValue
122+
// If can parse the default value to bool, use it, otherwise use false
123+
: value is string stringValue && bool.TryParse(stringValue, out var boolValueFromString)
124+
&& boolValueFromString;
117125
checkBox.Dispatcher.Invoke(() =>
118-
checkBox.IsChecked = value is bool isChecked
119-
? isChecked
120-
: bool.Parse(value as string ?? string.Empty));
126+
checkBox.IsChecked = isChecked);
121127
break;
122128
}
123129
}
@@ -145,6 +151,7 @@ public bool NeedCreateSettingPanel()
145151
public Control CreateSettingPanel()
146152
{
147153
// No need to check if NeedCreateSettingPanel is true because CreateSettingPanel will only be called if it's true
154+
// if (!NeedCreateSettingPanel()) return null;
148155

149156
// Create main grid with two columns (Column 1: Auto, Column 2: *)
150157
var mainPanel = new Grid { Margin = SettingPanelMargin, VerticalAlignment = VerticalAlignment.Center };
@@ -159,7 +166,7 @@ public Control CreateSettingPanel()
159166

160167
// Iterate over each setting and create one row for it
161168
var rowCount = 0;
162-
foreach (var (type, attributes) in Configuration.Body)
169+
foreach (var (type, attributes) in Configuration!.Body)
163170
{
164171
// Skip if the setting does not have attributes or name
165172
if (attributes?.Name == null)
@@ -389,12 +396,14 @@ public Control CreateSettingPanel()
389396
}
390397
case "checkbox":
391398
{
399+
// If can parse the default value to bool, use it, otherwise use false
400+
var defaultValue = bool.TryParse(attributes.DefaultValue, out var value) && value;
392401
var checkBox = new CheckBox
393402
{
394403
IsChecked =
395404
Settings[attributes.Name] is bool isChecked
396405
? isChecked
397-
: bool.Parse(attributes.DefaultValue),
406+
: defaultValue,
398407
HorizontalAlignment = HorizontalAlignment.Left,
399408
VerticalAlignment = VerticalAlignment.Center,
400409
Margin = SettingPanelItemTopBottomMargin,
@@ -404,7 +413,7 @@ Settings[attributes.Name] is bool isChecked
404413

405414
checkBox.Click += (sender, _) =>
406415
{
407-
Settings[attributes.Name] = ((CheckBox)sender).IsChecked!;
416+
Settings[attributes.Name] = ((CheckBox)sender).IsChecked ?? defaultValue;
408417
};
409418

410419
contentControl = checkBox;

0 commit comments

Comments
 (0)