diff --git a/build.gradle b/build.gradle index 51761fc0a..537fd3cca 100644 --- a/build.gradle +++ b/build.gradle @@ -213,6 +213,14 @@ def jvmArgsLinux = '--enable-native-access=javafx.graphics', //JDK24 - suppress restricted java.lang.System calls '--enable-native-access=ALL-UNNAMED'] //Suppress warnings for FFM API native calls (tuner libraries) +/** + * Startup splash screen image. The JVM shows it before any application code runs and closes it when the main window + * opens. The release launch scripts load it from the runtime image lib folder (see the runtime task below) and the + * gradle run task loads it from the resources folder. The JVM picks up the @2x image on HiDPI displays. + */ +def splashImages = ['sdrtrunk-splash.png', 'sdrtrunk-splash@2x.png'] +def splashResourceDir = 'src/main/resources/images/sdrtrunk' + application { mainClass = "io.github.dsheirer.gui.SDRTrunk" @@ -224,6 +232,10 @@ application { } } +tasks.named('run') { + jvmArgs "-splash:${file(splashResourceDir + '/' + splashImages[0]).absolutePath}" +} + test { if(os.isWindows()) { jvmArgs = jvmArgsWindows @@ -288,8 +300,8 @@ def jdk_download_windows_aarch64 = jdk_download_base + "windows-aarch64-full.zip * @return configured task. */ def configure(RuntimeZipTask rt, List jvmArgs) { - //Use the correct set of JVM arguments - rt.extension.launcherData.get().jvmArgs = jvmArgs + //Use the correct set of JVM arguments, plus the startup splash screen that the runtime task copies to lib + rt.extension.launcherData.get().jvmArgs = jvmArgs + ['-splash:{{BIN_DIR}}/../lib/sdrtrunk-splash.png'] //Additional modules to include with auto-detected modules. //jdk.charsets - NXDN BIG5 encoding support @@ -388,6 +400,22 @@ tasks.named('runtime') {rt -> } } +/** + * Copies the startup splash screen images into the lib folder of each runtime target image, where the launch scripts + * load them from (-splash:{{BIN_DIR}}/../lib/...). + */ +tasks.named('runtime') {rt -> + rt.doLast { + rt.targetPlatforms.get().keySet().each { target -> + def lib = new File(rt.imageDir.asFile, "${project.name}-${target}/lib") + splashImages.each { name -> + java.nio.file.Files.copy(file(splashResourceDir + '/' + name).toPath(), new File(lib, name).toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING) + } + } + } +} + /** * Verifies that each runtime target image contains every module that the sdrtrunk jar requires, as reported by jdeps. * This guards against the runtime plugin's module auto-detection silently missing modules (e.g. when it can't parse diff --git a/src/main/java/io/github/dsheirer/gui/ApplicationIcons.java b/src/main/java/io/github/dsheirer/gui/ApplicationIcons.java new file mode 100644 index 000000000..06c6ae1a3 --- /dev/null +++ b/src/main/java/io/github/dsheirer/gui/ApplicationIcons.java @@ -0,0 +1,255 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ + +package io.github.dsheirer.gui; + +import java.awt.AWTEvent; +import java.awt.Dialog; +import java.awt.Frame; +import java.awt.Image; +import java.awt.Taskbar; +import java.awt.Toolkit; +import java.awt.event.WindowEvent; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javafx.application.Platform; +import javafx.collections.ListChangeListener; +import javafx.stage.Stage; +import javafx.stage.Window; +import javax.imageio.ImageIO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * sdrtrunk application icon for Swing frames and dialogs, JavaFX stages and the macOS Dock. + * + * Call installSwing() once at startup and installJavaFx() once the JavaFX toolkit is running. After that, each + * window that opens without its own icon gets the sdrtrunk icon, so individual windows don't have to set it. + */ +public final class ApplicationIcons +{ + private static final Logger mLog = LoggerFactory.getLogger(ApplicationIcons.class); + private static final String RESOURCE_PATTERN = "/images/sdrtrunk/sdrtrunk-icon-%d.png"; + private static final int[] SIZES = {16, 24, 32, 48, 64, 128, 256, 512}; + private static final int TASKBAR_SIZE = 512; + + private static List sAwtImages; + private static List sFxImages; + private static boolean sSwingInstalled; + private static boolean sJavaFxInstalled; + + private ApplicationIcons() + { + } + + /** + * Application icon in each available size, smallest first, for Swing/AWT windows. + * @return images, or an empty list if the icon resources can't be loaded. + */ + public static synchronized List getAwtImages() + { + if(sAwtImages == null) + { + List images = new ArrayList<>(); + + for(int size: SIZES) + { + String path = String.format(RESOURCE_PATTERN, size); + + try(InputStream in = ApplicationIcons.class.getResourceAsStream(path)) + { + if(in != null) + { + Image image = ImageIO.read(in); + + if(image != null) + { + images.add(image); + } + } + else + { + mLog.warn("Application icon resource not found [" + path + "]"); + } + } + catch(IOException ioe) + { + mLog.warn("Unable to load application icon [" + path + "]", ioe); + } + } + + sAwtImages = Collections.unmodifiableList(images); + } + + return sAwtImages; + } + + /** + * Application icon in each available size, smallest first, for JavaFX stages. + * @return images, or an empty list if the icon resources can't be loaded. + */ + public static synchronized List getFxImages() + { + if(sFxImages == null) + { + List images = new ArrayList<>(); + + for(int size: SIZES) + { + String path = String.format(RESOURCE_PATTERN, size); + + try(InputStream in = ApplicationIcons.class.getResourceAsStream(path)) + { + if(in != null) + { + javafx.scene.image.Image image = new javafx.scene.image.Image(in); + + if(!image.isError()) + { + images.add(image); + } + } + } + catch(IOException ioe) + { + mLog.warn("Unable to load application icon [" + path + "]", ioe); + } + } + + sFxImages = Collections.unmodifiableList(images); + } + + return sFxImages; + } + + /** + * Sets the application icon on the Swing/AWT window if it doesn't already have one. + */ + public static void apply(java.awt.Window window) + { + if(window != null && window.getIconImages().isEmpty() && !getAwtImages().isEmpty()) + { + window.setIconImages(getAwtImages()); + } + } + + /** + * Sets the application icon on the JavaFX stage if it doesn't already have one. Call on the FX thread. + */ + public static void apply(Stage stage) + { + if(stage != null && stage.getIcons().isEmpty() && !getFxImages().isEmpty()) + { + stage.getIcons().setAll(getFxImages()); + } + } + + /** + * Sets the macOS Dock icon and gives each Swing frame and dialog that opens without an icon the application + * icon. Safe to call more than once. Does nothing in a headless environment. + */ + public static synchronized void installSwing() + { + if(sSwingInstalled || java.awt.GraphicsEnvironment.isHeadless()) + { + return; + } + + sSwingInstalled = true; + + Toolkit.getDefaultToolkit().addAWTEventListener(event -> { + if(event.getID() == WindowEvent.WINDOW_OPENED && + (event.getSource() instanceof Frame || event.getSource() instanceof Dialog)) + { + apply((java.awt.Window)event.getSource()); + } + }, AWTEvent.WINDOW_EVENT_MASK); + + //The Dock icon on macOS. Windows and Linux take the icon from each window instead. + try + { + if(Taskbar.isTaskbarSupported() && Taskbar.getTaskbar().isSupported(Taskbar.Feature.ICON_IMAGE)) + { + List images = getAwtImages(); + + for(int x = 0; x < SIZES.length && x < images.size(); x++) + { + if(SIZES[x] == TASKBAR_SIZE) + { + Taskbar.getTaskbar().setIconImage(images.get(x)); + } + } + } + } + catch(Exception e) + { + mLog.debug("Unable to set taskbar icon", e); + } + } + + /** + * Gives each JavaFX stage that shows without an icon the application icon, and closes the startup splash screen + * if a JavaFX window opens before the main window does. Safe to call more than once. The JavaFX toolkit must + * already be running. + */ + public static synchronized void installJavaFx() + { + if(sJavaFxInstalled) + { + return; + } + + sJavaFxInstalled = true; + + Platform.runLater(() -> { + for(Window window: Window.getWindows()) + { + onFxWindowShown(window); + } + + Window.getWindows().addListener((ListChangeListener)change -> { + while(change.next()) + { + for(Window window: change.getAddedSubList()) + { + onFxWindowShown(window); + } + } + }); + }); + } + + /** + * Handles a JavaFX window that is showing. + */ + private static void onFxWindowShown(Window window) + { + //Only stages are top-level windows. JFXPanel content and popups (menus, tooltips) are other Window types. + if(window instanceof Stage stage) + { + apply(stage); + + //A JavaFX window (e.g. an error dialog) that shows before the main window would sit behind the splash. + Splash.close(); + } + } +} diff --git a/src/main/java/io/github/dsheirer/gui/JavaFxWindowManager.java b/src/main/java/io/github/dsheirer/gui/JavaFxWindowManager.java index 8d44606d4..3801fb4c2 100644 --- a/src/main/java/io/github/dsheirer/gui/JavaFxWindowManager.java +++ b/src/main/java/io/github/dsheirer/gui/JavaFxWindowManager.java @@ -181,6 +181,7 @@ public class JavaFxWindowManager extends Application { mJFXPanel = new JFXPanel(); Platform.setImplicitExit(false); + ApplicationIcons.installJavaFx(); } } diff --git a/src/main/java/io/github/dsheirer/gui/SDRTrunk.java b/src/main/java/io/github/dsheirer/gui/SDRTrunk.java index 28eb07c7f..ceb7498ab 100644 --- a/src/main/java/io/github/dsheirer/gui/SDRTrunk.java +++ b/src/main/java/io/github/dsheirer/gui/SDRTrunk.java @@ -144,12 +144,28 @@ public class SDRTrunk implements Listener private String mTitle; public SDRTrunk() + { + try + { + initialize(); + } + catch(Throwable t) + { + startupFailed(t); + } + } + + /** + * Creates and starts the application components and shows the main window. + */ + private void initialize() { if(!GraphicsEnvironment.isHeadless()) { //Initialize the theme manager before any Swing components are realized so the chosen //look-and-feel applies to the very first window that is created. ThemeManager.getInstance().initialize(mUserPreferences); + ApplicationIcons.installSwing(); mMainGui = new JFrame(); } @@ -273,6 +289,9 @@ public class SDRTrunk implements Listener if(!GraphicsEnvironment.isHeadless()) { mMainGui.setVisible(true); + + //The JVM closes the splash when the first window shows. Close it here as well in case it didn't. + Splash.close(); Tuner tuner = tunerSpectralDisplayManager.showFirstTuner(); if(tuner != null) @@ -311,7 +330,15 @@ public class SDRTrunk implements Listener } catch(Exception e) { - e.printStackTrace(); + if(mMainGui != null && !mMainGui.isVisible()) + { + //The main window didn't open, so the user has no way to close the application. + startupFailed(e); + } + else + { + mLog.error("Error while starting the application", e); + } } }); } @@ -361,6 +388,7 @@ public class SDRTrunk implements Listener */ mTitle = SystemProperties.getInstance().getApplicationName(); mMainGui.setTitle(mTitle); + ApplicationIcons.apply(mMainGui); Point location = mUserPreferences.getSwingPreference().getLocation(WINDOW_FRAME_IDENTIFIER); if(location != null) @@ -967,11 +995,117 @@ public class SDRTrunk implements Listener JOptionPane.showMessageDialog(mMainGui, message, "Duplicate Library Versions", JOptionPane.WARNING_MESSAGE); } + /** + * Handles an error that stops the application from starting. Stops the tuners that already started, so that they + * are released for the next start, then reports the error and exits. Without this, the threads that the + * application started before the error keep the JVM running with no main window and the splash screen showing. + * @param t error + */ + private void startupFailed(Throwable t) + { + mLog.error("sdrtrunk failed to start", t); + + if(mTunerManager != null) + { + try + { + mTunerManager.stop(); + } + catch(Throwable t2) + { + mLog.error("Error stopping tuners after the startup error", t2); + } + } + + String logsDirectory = null; + + try + { + logsDirectory = mUserPreferences.getDirectoryPreference().getDirectoryApplicationLog().toString(); + } + catch(Throwable t2) + { + //Not available - the dialog leaves out the log location + } + + if(mApplicationLog != null) + { + mApplicationLog.stop(); + } + + exitAfterStartupError(t, logsDirectory); + } + + /** + * Closes the splash screen, shows the startup error to the user and exits the JVM with a non-zero status. + * @param t error + * @param logsDirectory where the application log is, or null if not known. + */ + private static void exitAfterStartupError(Throwable t, String logsDirectory) + { + //The application log may not be running yet, so also write the error to the console. + System.err.println("sdrtrunk failed to start"); + t.printStackTrace(); + + Splash.close(); + + if(!GraphicsEnvironment.isHeadless()) + { + String message = "" + + "

sdrtrunk couldn't start because of this error:


" + + "

" + escapeHtml(String.valueOf(t)) + "


" + + (logsDirectory != null ? "

The application log has the details:

" + + escapeHtml(logsDirectory) + "


" : "") + + "

See Help / Support on the sdrtrunk wiki for help.

" + + ""; + + try + { + Runnable dialog = () -> JOptionPane.showMessageDialog(null, message, "sdrtrunk Startup Error", + JOptionPane.ERROR_MESSAGE); + + if(EventQueue.isDispatchThread()) + { + dialog.run(); + } + else + { + EventQueue.invokeAndWait(dialog); + } + } + catch(Throwable t2) + { + t2.printStackTrace(); + } + } + + System.exit(1); + } + + /** + * Escapes text for display in a Swing HTML label. + */ + private static String escapeHtml(String text) + { + return text.replace("&", "&").replace("<", "<").replace(">", ">"); + } + /** * Launch the application. */ public static void main(String[] args) { - new SDRTrunk(); + String version = SDRTrunk.class.getPackage().getImplementationVersion(); + Splash.showVersion(version != null ? "v" + version : null); + + try + { + new SDRTrunk(); + } + catch(Throwable t) + { + //An error creating the SDRTrunk fields, before the constructor's own startup error handling runs. + exitAfterStartupError(t, null); + } } } diff --git a/src/main/java/io/github/dsheirer/gui/Splash.java b/src/main/java/io/github/dsheirer/gui/Splash.java new file mode 100644 index 000000000..348c1cd48 --- /dev/null +++ b/src/main/java/io/github/dsheirer/gui/Splash.java @@ -0,0 +1,117 @@ +/* + * ***************************************************************************** + * Copyright (C) 2014-2026 Dennis Sheirer + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + * **************************************************************************** + */ + +package io.github.dsheirer.gui; + +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.GraphicsEnvironment; +import java.awt.RenderingHints; +import java.awt.SplashScreen; + +/** + * Startup splash screen. + * + * The JVM shows the splash image named by the -splash:path option (set by the launch scripts) before any application + * code runs, and closes it when the main window becomes visible. This class draws the version on the splash and lets + * the application close the splash early, for example when a dialog has to show before the main window. + * + * Headless operation: the java launcher doesn't show the splash when -Djava.awt.headless=true is on the command line + * (for example in JAVA_OPTS or SDR_TRUNK_OPTS), and there is nothing to show it on without a display. This class does + * nothing when headless. + */ +public final class Splash +{ + //Splash image layout, in pixels of the 640 x 360 image. The version goes under the tagline. + private static final double IMAGE_WIDTH = 640.0; + private static final double VERSION_X = 214.0; + private static final double VERSION_Y = 232.0; + private static final double VERSION_FONT_SIZE = 13.0; + private static final Color VERSION_COLOR = new Color(0x2E, 0xE6, 0xA6); + + private Splash() + { + } + + /** + * Draws the version text on the splash screen, if a splash screen is showing. + * @param version to show, for example "v1.0.1", or null to leave the splash unchanged. + */ + public static void showVersion(String version) + { + if(version == null || version.isBlank() || GraphicsEnvironment.isHeadless()) + { + return; + } + + try + { + SplashScreen splash = SplashScreen.getSplashScreen(); + + if(splash == null || !splash.isVisible()) + { + return; + } + + //Overlay coordinates are in the splash's size, which can differ from the image (HiDPI @2x image). + Dimension size = splash.getSize(); + double scale = size.width / IMAGE_WIDTH; + + Graphics2D g = splash.createGraphics(); + g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setColor(VERSION_COLOR); + g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, (int)Math.round(VERSION_FONT_SIZE * scale))); + g.drawString(version, (float)(VERSION_X * scale), (float)(VERSION_Y * scale)); + g.dispose(); + splash.update(); + } + catch(Exception e) + { + //The splash is cosmetic. Never let it stop the application from starting. + } + } + + /** + * Closes the splash screen if it is still showing. + */ + public static void close() + { + if(GraphicsEnvironment.isHeadless()) + { + return; + } + + try + { + SplashScreen splash = SplashScreen.getSplashScreen(); + + if(splash != null && splash.isVisible()) + { + splash.close(); + } + } + catch(Exception e) + { + //The splash is cosmetic. Never let it stop the application from starting. + } + } +} diff --git a/src/main/resources/images/sdrtrunk/sdrtrunk-icon-128.png b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-128.png new file mode 100644 index 000000000..dee7d071b Binary files /dev/null and b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-128.png differ diff --git a/src/main/resources/images/sdrtrunk/sdrtrunk-icon-16.png b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-16.png new file mode 100644 index 000000000..56e02babe Binary files /dev/null and b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-16.png differ diff --git a/src/main/resources/images/sdrtrunk/sdrtrunk-icon-24.png b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-24.png new file mode 100644 index 000000000..c49f08cb6 Binary files /dev/null and b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-24.png differ diff --git a/src/main/resources/images/sdrtrunk/sdrtrunk-icon-256.png b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-256.png new file mode 100644 index 000000000..174aea7b8 Binary files /dev/null and b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-256.png differ diff --git a/src/main/resources/images/sdrtrunk/sdrtrunk-icon-32.png b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-32.png new file mode 100644 index 000000000..a726f935d Binary files /dev/null and b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-32.png differ diff --git a/src/main/resources/images/sdrtrunk/sdrtrunk-icon-48.png b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-48.png new file mode 100644 index 000000000..24f960465 Binary files /dev/null and b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-48.png differ diff --git a/src/main/resources/images/sdrtrunk/sdrtrunk-icon-512.png b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-512.png new file mode 100644 index 000000000..25b8d1979 Binary files /dev/null and b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-512.png differ diff --git a/src/main/resources/images/sdrtrunk/sdrtrunk-icon-64.png b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-64.png new file mode 100644 index 000000000..7c9794484 Binary files /dev/null and b/src/main/resources/images/sdrtrunk/sdrtrunk-icon-64.png differ diff --git a/src/main/resources/images/sdrtrunk/sdrtrunk-splash.png b/src/main/resources/images/sdrtrunk/sdrtrunk-splash.png new file mode 100644 index 000000000..e1d699d3c Binary files /dev/null and b/src/main/resources/images/sdrtrunk/sdrtrunk-splash.png differ diff --git a/src/main/resources/images/sdrtrunk/sdrtrunk-splash@2x.png b/src/main/resources/images/sdrtrunk/sdrtrunk-splash@2x.png new file mode 100644 index 000000000..068e16474 Binary files /dev/null and b/src/main/resources/images/sdrtrunk/sdrtrunk-splash@2x.png differ