Skip to content

Commit 9ed22de

Browse files
Jack251970TBM13
authored andcommitted
Merge pull request Flow-Launcher#3980 from Flow-Launcher/try_create_setting_panel
Catch exception when creating setting panel
1 parent aecc8ca commit 9ed22de

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
@@ -215,6 +215,7 @@
215215
<system:String x:Key="failedToUninstallPluginTitle">Fail to uninstall {0}</system:String>
216216
<system:String x:Key="fileNotFoundMessage">Unable to find plugin.json from the extracted zip file, or this path {0} does not exist</system:String>
217217
<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>
218+
<system:String x:Key="errorCreatingSettingPanel">Error creating setting panel for plugin {0}:{1}{2}</system:String>
218219

219220
<!-- Setting Plugin Store -->
220221
<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
{
@@ -111,11 +117,30 @@ public Control SettingControl
111117
=> IsExpanded
112118
? _settingControl
113119
??= HasSettingControl
114-
? ((ISettingProvider)PluginPair.Plugin).CreateSettingPanel()
120+
? TryCreateSettingPanel(PluginPair)
115121
: null
116122
: null;
117123
private ImageSource _image = ImageLoader.MissingImage;
118124

125+
private static Control TryCreateSettingPanel(PluginPair pair)
126+
{
127+
try
128+
{
129+
// We can safely cast here as we already check this in HasSettingControl
130+
return ((ISettingProvider)pair.Plugin).CreateSettingPanel();
131+
}
132+
catch (Exception e)
133+
{
134+
// Log exception
135+
App.API.LogException(ClassName, $"Failed to create setting panel for {pair.Metadata.Name}", e);
136+
137+
// Show error message in UI
138+
var errorMsg = string.Format(App.API.GetTranslation("errorCreatingSettingPanel"),
139+
pair.Metadata.Name, Environment.NewLine, e.Message);
140+
return CreateErrorSettingPanel(errorMsg);
141+
}
142+
}
143+
119144
public Visibility ActionKeywordsVisibility => PluginPair.Metadata.HideActionKeywordPanel ?
120145
Visibility.Collapsed : Visibility.Visible;
121146
public string InitializeTime => PluginPair.Metadata.InitTime + "ms";
@@ -155,5 +180,28 @@ private void SetActionKeywords()
155180
var changeKeywordsWindow = new ActionKeywords(this);
156181
changeKeywordsWindow.ShowDialog();
157182
}
183+
184+
private static UserControl CreateErrorSettingPanel(string text)
185+
{
186+
var grid = new Grid()
187+
{
188+
Margin = SettingPanelMargin
189+
};
190+
var textBox = new TextBox
191+
{
192+
Text = text,
193+
IsReadOnly = true,
194+
HorizontalAlignment = HorizontalAlignment.Stretch,
195+
VerticalAlignment = VerticalAlignment.Top,
196+
TextWrapping = TextWrapping.Wrap,
197+
Margin = SettingPanelItemTopBottomMargin
198+
};
199+
textBox.SetResourceReference(TextBox.ForegroundProperty, "Color04B");
200+
grid.Children.Add(textBox);
201+
return new UserControl
202+
{
203+
Content = grid
204+
};
205+
}
158206
}
159207
}

0 commit comments

Comments
 (0)