diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index f7fd0c8e588..e97bd6cf596 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -219,6 +219,7 @@ Fail to uninstall {0} Unable to find plugin.json from the extracted zip file, or this path {0} does not exist A plugin with the same ID and version already exists, or the version is greater than this downloaded plugin + Error creating setting panel for plugin {0}:{1}{2} Plugin Store diff --git a/Flow.Launcher/ViewModel/PluginViewModel.cs b/Flow.Launcher/ViewModel/PluginViewModel.cs index d889bdd52ab..29f2b9b4370 100644 --- a/Flow.Launcher/ViewModel/PluginViewModel.cs +++ b/Flow.Launcher/ViewModel/PluginViewModel.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; @@ -14,8 +15,13 @@ namespace Flow.Launcher.ViewModel { public partial class PluginViewModel : BaseModel { + private static readonly string ClassName = nameof(PluginViewModel); + private static readonly Settings Settings = Ioc.Default.GetRequiredService(); + private static readonly Thickness SettingPanelMargin = (Thickness)Application.Current.FindResource("SettingPanelMargin"); + private static readonly Thickness SettingPanelItemTopBottomMargin = (Thickness)Application.Current.FindResource("SettingPanelItemTopBottomMargin"); + private readonly PluginPair _pluginPair; public PluginPair PluginPair { @@ -131,11 +137,30 @@ public Control SettingControl => IsExpanded ? _settingControl ??= HasSettingControl - ? ((ISettingProvider)PluginPair.Plugin).CreateSettingPanel() + ? TryCreateSettingPanel(PluginPair) : null : null; private ImageSource _image = ImageLoader.MissingImage; + private static Control TryCreateSettingPanel(PluginPair pair) + { + try + { + // We can safely cast here as we already check this in HasSettingControl + return ((ISettingProvider)pair.Plugin).CreateSettingPanel(); + } + catch (Exception e) + { + // Log exception + App.API.LogException(ClassName, $"Failed to create setting panel for {pair.Metadata.Name}", e); + + // Show error message in UI + var errorMsg = string.Format(App.API.GetTranslation("errorCreatingSettingPanel"), + pair.Metadata.Name, Environment.NewLine, e.Message); + return CreateErrorSettingPanel(errorMsg); + } + } + public Visibility ActionKeywordsVisibility => PluginPair.Metadata.HideActionKeywordPanel ? Visibility.Collapsed : Visibility.Visible; public string InitializeTime => PluginPair.Metadata.InitTime + "ms"; @@ -186,5 +211,28 @@ private void SetActionKeywords() var changeKeywordsWindow = new ActionKeywords(this); changeKeywordsWindow.ShowDialog(); } + + private static UserControl CreateErrorSettingPanel(string text) + { + var grid = new Grid() + { + Margin = SettingPanelMargin + }; + var textBox = new TextBox + { + Text = text, + IsReadOnly = true, + HorizontalAlignment = HorizontalAlignment.Stretch, + VerticalAlignment = VerticalAlignment.Top, + TextWrapping = TextWrapping.Wrap, + Margin = SettingPanelItemTopBottomMargin + }; + textBox.SetResourceReference(TextBox.ForegroundProperty, "Color04B"); + grid.Children.Add(textBox); + return new UserControl + { + Content = grid + }; + } } }