Skip to content

Commit 633d3c7

Browse files
committed
PluginSettingsWindow: Throw exceptions to prevent invalid state, and then catch to LogException and show localized error message, and make OpenPluginSettingsDialog use this to return success boolean
1 parent 93d7d0a commit 633d3c7

5 files changed

Lines changed: 22 additions & 15 deletions

File tree

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ public interface IPublicAPI
167167
/// Open plugin setting window for a specific plugin
168168
/// </summary>
169169
/// <param name="pluginId">ID of the plugin whose settings window should be opened</param>
170-
void OpenPluginSettingsWindow(string pluginId);
170+
/// <returns>True if the plugin settings window was successfully opened; false otherwise</returns>
171+
bool OpenPluginSettingsWindow(string pluginId);
171172

172173
/// <summary>
173174
/// Get translation of current language

Flow.Launcher/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@
487487

488488
<!-- Plugin Settings Window -->
489489
<system:String x:Key="pluginSettingsWindowTitle">Settings: {0}</system:String>
490+
<system:String x:Key="pluginSettingsWindowOpenFailed">Failed to open plugin settings window</system:String>
490491

491492
<!-- Release Notes Window -->
492493
<system:String x:Key="seeMoreReleaseNotes">See more release notes on GitHub</system:String>

Flow.Launcher/PluginSettingsWindow.xaml.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,20 @@ public PluginSettingsWindow(string pluginId)
1717
{
1818
_settings = Ioc.Default.GetRequiredService<Settings>();
1919

20-
if (string.IsNullOrWhiteSpace(pluginId))
21-
{
22-
App.API.ShowMsgError("Plugin settings", "Invalid plugin id.");
23-
return;
20+
if (string.IsNullOrWhiteSpace(pluginId)){
21+
throw new ArgumentException("Plugin ID cannot be null or whitespace.", nameof(pluginId));
2422
}
2523

2624
var pluginPair = PluginManager.GetPluginForId(pluginId);
2725
if (pluginPair == null)
2826
{
29-
App.API.ShowMsgError("Plugin settings", $"Unable to find plugin: {pluginId}");
30-
return;
27+
throw new InvalidOperationException($"Unable to find plugin: {pluginId}");
3128
}
3229

3330
var pluginSettings = _settings.PluginSettings.GetPluginSettings(pluginId);
3431
if (pluginSettings == null)
3532
{
36-
App.API.ShowMsgError("Plugin settings", $"Unable to load settings for plugin: {pluginPair.Metadata.Name}");
37-
return;
33+
throw new InvalidOperationException($"Unable to load settings for plugin: {pluginPair.Metadata.Name}");
3834
}
3935

4036
var pluginViewModel = new PluginViewModel

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,22 @@ public void OpenSettingDialog()
147147
});
148148
}
149149

150-
public void OpenPluginSettingsWindow(string pluginId)
150+
public bool OpenPluginSettingsWindow(string pluginId)
151151
{
152-
Application.Current.Dispatcher.Invoke(() =>
152+
return Application.Current.Dispatcher.Invoke(() =>
153153
{
154-
var window = new PluginSettingsWindow(pluginId);
155-
window.Show();
154+
try
155+
{
156+
var window = new PluginSettingsWindow(pluginId);
157+
window.Show();
158+
return true;
159+
}
160+
catch (Exception e)
161+
{
162+
LogException(ClassName, $"Failed to open plugin settings window for plugin id '{pluginId}'", e);
163+
ShowMsgError(Localize.pluginSettingsWindowOpenFailed());
164+
return false;
165+
}
156166
});
157167
}
158168

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,8 +1841,7 @@ private static Result ContextMenuPluginSettings(Result result)
18411841
PluginDirectory = Constant.ProgramDirectory,
18421842
Action = _ =>
18431843
{
1844-
App.API.OpenPluginSettingsWindow(id);
1845-
return true;
1844+
return App.API.OpenPluginSettingsWindow(id);
18461845
},
18471846
OriginQuery = result.OriginQuery
18481847
};

0 commit comments

Comments
 (0)