Skip to content

Commit

Permalink
feat: Add an icon for the GUI application (#1179)
Browse files Browse the repository at this point in the history
* Test run of application icons.

These icons are not final, but meant as a test of the packaging platform.

* Try adding an in-app icon.

* Actually add the image.

* Add defensive null check.

* Update Mac OS icon.

* Add the application icon SVG source.

* Add a helper script for Mac icon creation.

* Update Windows icon + add conversion script.

* Update Mac OS icon.

* Update core icon with better background transparency.

* Update Windows icon, including more intermediate resolutions in the ico file.

* Support different resolution icons within the app JFrame.

* Support JFrame app icon in multiple resolutions.

* Fix typo.

* Add README and script updates.

* Java format.

* Add the --win-console option back in.

Co-authored-by: Brian Ferris <bdferris@google.com>
  • Loading branch information
bdferris-v2 and bdferris authored Jun 3, 2022
1 parent cec914e commit da57dbf
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
Expand Down Expand Up @@ -134,6 +138,7 @@ public void showNewVersionAvailable() {

void constructUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setApplicationIcon();

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
Expand All @@ -155,6 +160,25 @@ void constructUI() {
}
}

private void setApplicationIcon() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
List<String> iconFileNames =
Arrays.asList("icon_16x16.png", "icon_32x32.png", "icon_48x48.png");
List<Image> iconImages = new ArrayList<>();
for (String iconFileName : iconFileNames) {
URL resource = getClass().getResource(iconFileName);
if (resource != null) {
Image image = toolkit.createImage(resource);
iconImages.add(image);
} else {
logger.atWarning().log("Icon image not found: %s", iconFileName);
}
}
if (!iconImages.isEmpty()) {
setIconImages(iconImages);
}
}

private void constructGtfsInputSection(JPanel parent) {
// GTFS Input Section
parent.add(createLabelWithFont(bundle.getString("gtfs_input"), BOLD_FONT));
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 11 additions & 2 deletions app/pkg/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import org.gradle.internal.os.OperatingSystem

plugins {
id 'java'
id "de.jjohannes.extra-java-module-info" version "0.11"
Expand Down Expand Up @@ -96,9 +98,16 @@ jlink {
// Some platforms (e.g. Mac OS) need a pure x.y.z version number.
// So strip any -SNAPSHOT suffix if present.
appVersion = project.version.toString().replace('-SNAPSHOT', '')
if (org.gradle.internal.os.OperatingSystem.current().windows) {
if (OperatingSystem.current().isWindows()) {
installerOptions += ['--win-per-user-install', '--win-dir-chooser', '--win-menu', '--win-shortcut']
imageOptions += ['--win-console']
imageOptions = [
'--icon', "${projectDir}/src/main/icons/Icon.ico", '--win-console'
]
}
if (OperatingSystem.current().isMacOsX()) {
imageOptions = [
'--icon', "${projectDir}/src/main/icons/Icon.icns"
]
}
}
}
Expand Down
Binary file added app/pkg/src/main/icons/Icon.icns
Binary file not shown.
Binary file added app/pkg/src/main/icons/Icon.ico
Binary file not shown.
81 changes: 81 additions & 0 deletions app/pkg/src/main/icons/Icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions app/pkg/src/main/icons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Updating Application Icons

The source file for all application icons is `Icon.svg`. This is used to create three
platform-specific application icons:

* Windows: `Icon.ico`
* Mac OS: `Icon.icns`
* Java In-App

When updating the source icon, you can run the following three scripts to regenerate the
relevant icon files from the SVG source:

* `svg2ico.sh`
* `svg2icsn.sh`
* `svg2jframe_icons.sh`
39 changes: 39 additions & 0 deletions app/pkg/src/main/icons/svg2icns.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
#
# Helper script for constructing a Mac OS icns icon bundle,
# from an intermediate iconset file bundle. Requires install
# of ImageMagik for `convert` command.

set -e

BASEDIR=$(dirname "$0")
SVG=$BASEDIR/Icon.svg
ICONSET_DIR=$BASEDIR/Icon.iconset

CONFIGS="
icon_16x16.png,16x16
icon_16x16@2x.png,32x32
icon_32x32.png,32x32
icon_32x32@2x.png,64x64
icon_128x128.png,128x128
icon_128x128@2x.png,256x256
icon_256x256.png,256x256
icon_256x256@2x.png,512x512
icon_512x512.png,512x512
icon_512x512@2x.png,1024x1024
"

if [ -e $ICONSET_DIR ]; then
rm -rf "$ICONSET_DIR"
fi
mkdir $ICONSET_DIR

for CONFIG in $CONFIGS; do
FILENAME=$(echo $CONFIG | cut -d, -f1)
SIZE=$(echo $CONFIG | cut -d, -f2)
echo $FILENAME
convert -density 1200 -background none -resize $SIZE $SVG $ICONSET_DIR/$FILENAME
done

iconutil -c icns $ICONSET_DIR
rm -rf "$ICONSET_DIR"
34 changes: 34 additions & 0 deletions app/pkg/src/main/icons/svg2ico.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
#
# Helper script for constructing a Windows icon bundle. Requires
# install of ImageMagik for `convert` command.

set -e

BASEDIR=$(dirname "$0")
SVG=$BASEDIR/Icon.svg
ICO_DIR=$BASEDIR/Icon.ico_dir

CONFIGS="
icon_16x16.png,16x16
icon_32x32.png,32x32
icon_48x48.png,48x48
icon_64x64.png,64x64
icon_128x128.png,128x128
icon_256x256.png,256x256
"

if [ -e $ICO_DIR ]; then
rm -rf "$ICO_DIR"
fi
mkdir $ICO_DIR

for CONFIG in $CONFIGS; do
FILENAME=$(echo $CONFIG | cut -d, -f1)
SIZE=$(echo $CONFIG | cut -d, -f2)
echo $FILENAME
convert -density 1200 -background none -resize $SIZE $SVG $ICO_DIR/$FILENAME
done

convert $ICO_DIR/*.png Icon.ico
rm -rf "$ICO_DIR"
25 changes: 25 additions & 0 deletions app/pkg/src/main/icons/svg2jframe_icons.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
#
# Helper script for constructing Java in-app icon resources. Requires
# install of ImageMagik for `convert` command.

set -e

BASEDIR=$(dirname "$0")
SVG=$BASEDIR/Icon.svg

PROJECT_ROOT_DIR=$(git rev-parse --show-toplevel)
RESOURCES_DIR=$PROJECT_ROOT_DIR/app/gui/src/main/resources/org/mobilitydata/gtfsvalidator/app/gui

CONFIGS="
icon_16x16.png,16x16
icon_32x32.png,32x32
icon_48x48.png,48x48
"

for CONFIG in $CONFIGS; do
FILENAME=$(echo $CONFIG | cut -d, -f1)
SIZE=$(echo $CONFIG | cut -d, -f2)
echo $FILENAME
convert -density 1200 -background none -resize $SIZE $SVG $RESOURCES_DIR/$FILENAME
done

0 comments on commit da57dbf

Please sign in to comment.