优化 FXUtils#openLink (#4470)

This commit is contained in:
Glavo
2025-09-13 14:37:53 +08:00
committed by GitHub
parent 6b6276e6e2
commit 72ae46c93d

View File

@@ -545,37 +545,35 @@ public final class FXUtils {
if (link == null) if (link == null)
return; return;
String uri = NetworkUtils.encodeLocation(link);
thread(() -> { thread(() -> {
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) { try {
try { if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
Runtime.getRuntime().exec(new String[]{"rundll32.exe", "url.dll,FileProtocolHandler", link}); Runtime.getRuntime().exec(new String[]{"rundll32.exe", "url.dll,FileProtocolHandler", uri});
return; return;
} catch (Throwable e) { } else if (OperatingSystem.CURRENT_OS == OperatingSystem.MACOS) {
LOG.warning("An exception occurred while calling rundll32", e); Runtime.getRuntime().exec(new String[]{"open", uri});
} return;
} } else {
if (OperatingSystem.CURRENT_OS.isLinuxOrBSD()) { for (String browser : linuxBrowsers) {
for (String browser : linuxBrowsers) { Path path = SystemUtils.which(browser);
Path path = SystemUtils.which(browser); if (path != null) {
if (path != null) { try {
try { Runtime.getRuntime().exec(new String[]{path.toString(), uri});
Runtime.getRuntime().exec(new String[]{path.toString(), link}); return;
return; } catch (Throwable ignored) {
} catch (Throwable ignored) { }
} }
} }
LOG.warning("No known browser found"); LOG.warning("No known browser found");
} }
}
try {
java.awt.Desktop.getDesktop().browse(new URI(link));
} catch (Throwable e) { } catch (Throwable e) {
if (OperatingSystem.CURRENT_OS == OperatingSystem.MACOS) LOG.warning("Failed to open link: " + link + ", fallback to java.awt.Desktop", e);
try { }
Runtime.getRuntime().exec(new String[]{"/usr/bin/open", link});
} catch (IOException ex) { try {
LOG.warning("Unable to open link: " + link, ex); java.awt.Desktop.getDesktop().browse(new URI(uri));
} } catch (Throwable e) {
LOG.warning("Failed to open link: " + link, e); LOG.warning("Failed to open link: " + link, e);
} }
}); });