Co-authored-by: denny <dennis.sheirer@gmail.com>
255
src/main/java/io/github/dsheirer/gui/ApplicationIcons.java
Normal file
@@ -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 <http://www.gnu.org/licenses/>
|
||||
* ****************************************************************************
|
||||
*/
|
||||
|
||||
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<Image> sAwtImages;
|
||||
private static List<javafx.scene.image.Image> 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<Image> getAwtImages()
|
||||
{
|
||||
if(sAwtImages == null)
|
||||
{
|
||||
List<Image> 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<javafx.scene.image.Image> getFxImages()
|
||||
{
|
||||
if(sFxImages == null)
|
||||
{
|
||||
List<javafx.scene.image.Image> 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<Image> 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<Window>)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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,6 +181,7 @@ public class JavaFxWindowManager extends Application
|
||||
{
|
||||
mJFXPanel = new JFXPanel();
|
||||
Platform.setImplicitExit(false);
|
||||
ApplicationIcons.installJavaFx();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -144,12 +144,28 @@ public class SDRTrunk implements Listener<TunerEvent>
|
||||
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<TunerEvent>
|
||||
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<TunerEvent>
|
||||
}
|
||||
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<TunerEvent>
|
||||
*/
|
||||
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<TunerEvent>
|
||||
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 = "<html><body style='width: 450px'>" +
|
||||
"<p>sdrtrunk couldn't start because of this error:</p><br>" +
|
||||
"<p><b>" + escapeHtml(String.valueOf(t)) + "</b></p><br>" +
|
||||
(logsDirectory != null ? "<p>The application log has the details:</p><p>" +
|
||||
escapeHtml(logsDirectory) + "</p><br>" : "") +
|
||||
"<p>See Help / Support on the sdrtrunk wiki for help.</p>" +
|
||||
"</body></html>";
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
117
src/main/java/io/github/dsheirer/gui/Splash.java
Normal file
@@ -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 <http://www.gnu.org/licenses/>
|
||||
* ****************************************************************************
|
||||
*/
|
||||
|
||||
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.
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
src/main/resources/images/sdrtrunk/sdrtrunk-icon-128.png
Normal file
|
After Width: | Height: | Size: 8.6 KiB |
BIN
src/main/resources/images/sdrtrunk/sdrtrunk-icon-16.png
Normal file
|
After Width: | Height: | Size: 663 B |
BIN
src/main/resources/images/sdrtrunk/sdrtrunk-icon-24.png
Normal file
|
After Width: | Height: | Size: 1.1 KiB |
BIN
src/main/resources/images/sdrtrunk/sdrtrunk-icon-256.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
src/main/resources/images/sdrtrunk/sdrtrunk-icon-32.png
Normal file
|
After Width: | Height: | Size: 1.5 KiB |
BIN
src/main/resources/images/sdrtrunk/sdrtrunk-icon-48.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
BIN
src/main/resources/images/sdrtrunk/sdrtrunk-icon-512.png
Normal file
|
After Width: | Height: | Size: 59 KiB |
BIN
src/main/resources/images/sdrtrunk/sdrtrunk-icon-64.png
Normal file
|
After Width: | Height: | Size: 3.7 KiB |
BIN
src/main/resources/images/sdrtrunk/sdrtrunk-splash.png
Normal file
|
After Width: | Height: | Size: 107 KiB |
BIN
src/main/resources/images/sdrtrunk/sdrtrunk-splash@2x.png
Normal file
|
After Width: | Height: | Size: 323 KiB |