Skip to content

Commit a2c11af

Browse files
authored
Merge pull request #3980 from Flow-Launcher/try_create_setting_panel
Catch exception when creating setting panel
2 parents 89fca85 + 72dae63 commit a2c11af

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

Flow.Launcher/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
<system:String x:Key="failedToUninstallPluginTitle">Fail to uninstall {0}</system:String>
220220
<system:String x:Key="fileNotFoundMessage">Unable to find plugin.json from the extracted zip file, or this path {0} does not exist</system:String>
221221
<system:String x:Key="pluginExistAlreadyMessage">A plugin with the same ID and version already exists, or the version is greater than this downloaded plugin</system:String>
222+
<system:String x:Key="errorCreatingSettingPanel">Error creating setting panel for plugin {0}:{1}{2}</system:String>
222223

223224
<!-- Setting Plugin Store -->
224225
<system:String x:Key="pluginStore">Plugin Store</system:String>

Flow.Launcher/ViewModel/PluginViewModel.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23
using System.Windows;
34
using System.Windows.Controls;
45
using System.Windows.Media;
@@ -14,8 +15,13 @@ namespace Flow.Launcher.ViewModel
1415
{
1516
public partial class PluginViewModel : BaseModel
1617
{
18+
private static readonly string ClassName = nameof(PluginViewModel);
19+
1720
private static readonly Settings Settings = Ioc.Default.GetRequiredService<Settings>();
1821

22+
private static readonly Thickness SettingPanelMargin = (Thickness)Application.Current.FindResource("SettingPanelMargin");
23+
private static readonly Thickness SettingPanelItemTopBottomMargin = (Thickness)Application.Current.FindResource("SettingPanelItemTopBottomMargin");
24+
1925
private readonly PluginPair _pluginPair;
2026
public PluginPair PluginPair
2127
{
@@ -131,11 +137,30 @@ public Control SettingControl
131137
=> IsExpanded
132138
? _settingControl
133139
??= HasSettingControl
134-
? ((ISettingProvider)PluginPair.Plugin).CreateSettingPanel()
140+
? TryCreateSettingPanel(PluginPair)
135141
: null
136142
: null;
137143
private ImageSource _image = ImageLoader.MissingImage;
138144

145+
private static Control TryCreateSettingPanel(PluginPair pair)
146+
{
147+
try
148+
{
149+
// We can safely cast here as we already check this in HasSettingControl
150+
return ((ISettingProvider)pair.Plugin).CreateSettingPanel();
151+
}
152+
catch (Exception e)
153+
{
154+
// Log exception
155+
App.API.LogException(ClassName, $"Failed to create setting panel for {pair.Metadata.Name}", e);
156+
157+
// Show error message in UI
158+
var errorMsg = string.Format(App.API.GetTranslation("errorCreatingSettingPanel"),
159+
pair.Metadata.Name, Environment.NewLine, e.Message);
160+
return CreateErrorSettingPanel(errorMsg);
161+
}
162+
}
163+
139164
public Visibility ActionKeywordsVisibility => PluginPair.Metadata.HideActionKeywordPanel ?
140165
Visibility.Collapsed : Visibility.Visible;
141166
public string InitializeTime => PluginPair.Metadata.InitTime + "ms";
@@ -186,5 +211,28 @@ private void SetActionKeywords()
186211
var changeKeywordsWindow = new ActionKeywords(this);
187212
changeKeywordsWindow.ShowDialog();
188213
}
214+
215+
private static UserControl CreateErrorSettingPanel(string text)
216+
{
217+
var grid = new Grid()
218+
{
219+
Margin = SettingPanelMargin
220+
};
221+
var textBox = new TextBox
222+
{
223+
Text = text,
224+
IsReadOnly = true,
225+
HorizontalAlignment = HorizontalAlignment.Stretch,
226+
VerticalAlignment = VerticalAlignment.Top,
227+
TextWrapping = TextWrapping.Wrap,
228+
Margin = SettingPanelItemTopBottomMargin
229+
};
230+
textBox.SetResourceReference(TextBox.ForegroundProperty, "Color04B");
231+
grid.Children.Add(textBox);
232+
return new UserControl
233+
{
234+
Content = grid
235+
};
236+
}
189237
}
190238
}

0 commit comments

Comments
 (0)