From 32ae3a1b67989ae00bb47ccb26579ee82a36f001 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 4 Jun 2025 22:21:08 +0800 Subject: [PATCH 1/4] Improve code quality --- Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs b/Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs index 752c85933d6..7c4c3de79ca 100644 --- a/Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs +++ b/Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs @@ -1,8 +1,9 @@ -using Microsoft.Win32; -using System; +using System; +using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; +using Microsoft.Win32; namespace Flow.Launcher.Plugin.SharedCommands { @@ -13,7 +14,7 @@ public static class SearchWeb { private static string GetDefaultBrowserPath() { - string name = string.Empty; + var name = string.Empty; try { using var regDefault = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice", false); @@ -23,8 +24,7 @@ private static string GetDefaultBrowserPath() name = regKey.GetValue(null).ToString().ToLower().Replace("\"", ""); if (!name.EndsWith("exe")) - name = name.Substring(0, name.LastIndexOf(".exe") + 4); - + name = name[..(name.LastIndexOf(".exe") + 4)]; } catch { @@ -65,7 +65,8 @@ public static void OpenInBrowserWindow(this string url, string browserPath = "", { Process.Start(psi)?.Dispose(); } - catch (System.ComponentModel.Win32Exception) + // This error may be thrown if browser path is incorrect + catch (Win32Exception) { Process.Start(new ProcessStartInfo { @@ -100,7 +101,7 @@ public static void OpenInBrowserTab(this string url, string browserPath = "", bo Process.Start(psi)?.Dispose(); } // This error may be thrown if browser path is incorrect - catch (System.ComponentModel.Win32Exception) + catch (Win32Exception) { Process.Start(new ProcessStartInfo { From 4bc34f64e84f3e8eb306ac1743b6cd4b2725cb32 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 4 Jun 2025 22:22:02 +0800 Subject: [PATCH 2/4] Re-throw the exception if we cannot open the URL in the default browser --- .../SharedCommands/SearchWeb.cs | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs b/Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs index 7c4c3de79ca..ed3e91daf43 100644 --- a/Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs +++ b/Flow.Launcher.Plugin/SharedCommands/SearchWeb.cs @@ -68,10 +68,18 @@ public static void OpenInBrowserWindow(this string url, string browserPath = "", // This error may be thrown if browser path is incorrect catch (Win32Exception) { - Process.Start(new ProcessStartInfo + try { - FileName = url, UseShellExecute = true - }); + Process.Start(new ProcessStartInfo + { + FileName = url, + UseShellExecute = true + }); + } + catch + { + throw; // Re-throw the exception if we cannot open the URL in the default browser + } } } @@ -103,10 +111,18 @@ public static void OpenInBrowserTab(this string url, string browserPath = "", bo // This error may be thrown if browser path is incorrect catch (Win32Exception) { - Process.Start(new ProcessStartInfo + try { - FileName = url, UseShellExecute = true - }); + Process.Start(new ProcessStartInfo + { + FileName = url, + UseShellExecute = true + }); + } + catch + { + throw; // Re-throw the exception if we cannot open the URL in the default browser + } } } } From f6c75c21072b35bbc35e38dbe05adf287a2e164f Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 4 Jun 2025 22:29:45 +0800 Subject: [PATCH 3/4] Handle exception from OpenInBrowserTab & OpenInBrowserWindow --- Flow.Launcher/Languages/en.xaml | 1 + Flow.Launcher/PublicAPIInstance.cs | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index 28673c753b6..ec1d5bc93b4 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -473,6 +473,7 @@ Error An error occurred while opening the folder. {0} + An error occurred while opening the url in browser. Please check your setting in Default Web Browser of general page. Please wait... diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 7f0b03445c7..80d5de53d84 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -399,13 +399,27 @@ private void OpenUri(Uri uri, bool? inPrivate = null) var path = browserInfo.Path == "*" ? "" : browserInfo.Path; - if (browserInfo.OpenInTab) + try { - uri.AbsoluteUri.OpenInBrowserTab(path, inPrivate ?? browserInfo.EnablePrivate, browserInfo.PrivateArg); + if (browserInfo.OpenInTab) + { + uri.AbsoluteUri.OpenInBrowserTab(path, inPrivate ?? browserInfo.EnablePrivate, browserInfo.PrivateArg); + } + else + { + uri.AbsoluteUri.OpenInBrowserWindow(path, inPrivate ?? browserInfo.EnablePrivate, browserInfo.PrivateArg); + } } - else + catch (Exception e) { - uri.AbsoluteUri.OpenInBrowserWindow(path, inPrivate ?? browserInfo.EnablePrivate, browserInfo.PrivateArg); + var tabOrWindow = browserInfo.OpenInTab ? "tab" : "window"; + LogException(ClassName, $"Failed to open URL in browser {tabOrWindow}: {path}, {inPrivate ?? browserInfo.EnablePrivate}, {browserInfo.PrivateArg}", e); + ShowMsgBox( + GetTranslation("browserOpenError"), + GetTranslation("errorTitle"), + MessageBoxButton.OK, + MessageBoxImage.Error + ); } } else From 25a62c8700b8fb4ed14a0f934e742ea979a87757 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 5 Jun 2025 12:46:46 +1000 Subject: [PATCH 4/4] fix wording --- Flow.Launcher/Languages/en.xaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher/Languages/en.xaml b/Flow.Launcher/Languages/en.xaml index ec1d5bc93b4..c9fc3689271 100644 --- a/Flow.Launcher/Languages/en.xaml +++ b/Flow.Launcher/Languages/en.xaml @@ -473,7 +473,7 @@ Error An error occurred while opening the folder. {0} - An error occurred while opening the url in browser. Please check your setting in Default Web Browser of general page. + An error occurred while opening the URL in the browser. Please check your Default Web Browser configuration in the General section of the settings window Please wait...