Skip to content

Commit

Permalink
Add support for opening URLs with xdg-open on Linux
Browse files Browse the repository at this point in the history
In environments without GTK, the Swing toolkit isn't available, so
desktop calls don't work. If that's the case, fall back to xdg-open
  • Loading branch information
hedgehog1029 committed Dec 12, 2021
1 parent 0833563 commit 94ae835
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions launcher/src/main/java/com/skcraft/launcher/swing/SwingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.skcraft.launcher.LauncherException;
import com.skcraft.launcher.util.Environment;
import com.skcraft.launcher.util.Platform;
import com.skcraft.launcher.util.SharedLocale;
import com.skcraft.launcher.util.SwingExecutor;
import lombok.NonNull;
Expand Down Expand Up @@ -103,9 +105,19 @@ public static void openURL(@NonNull String url, @NonNull Component parentCompone
public static void openURL(URL url, Component parentComponent) {
try {
Desktop.getDesktop().browse(url.toURI());
} catch (UnsupportedOperationException e) {
if (Environment.detectPlatform() == Platform.LINUX) {
// Try xdg-open instead
try {
Runtime.getRuntime().exec(new String[]{"xdg-open", url.toString()});
} catch (IOException ex) {
showErrorDialog(parentComponent, tr("errors.openUrlError", url.toString()), tr("errorTitle"), ex);
}
}
} catch (IOException e) {
showErrorDialog(parentComponent, tr("errors.openUrlError", url.toString()), SharedLocale.tr("errorTitle"));
} catch (URISyntaxException e) {
log.log(Level.WARNING, "Malformed URL; this is a programming error!", e);
}
}

Expand Down

0 comments on commit 94ae835

Please sign in to comment.