Merge pull request #3980 from Flow-Launcher/try_create_setting_panel

Catch exception when creating setting panel
This commit is contained in:
Jack Ye 2025-09-20 07:53:22 +08:00 committed by GitHub
commit a2c11af82b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 51 additions and 2 deletions

View file

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

View file

@ -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<Settings>();
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 @@ namespace Flow.Launcher.ViewModel
=> 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 @@ namespace Flow.Launcher.ViewModel
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
};
}
}
}