Skip to content

Commit 2f80023

Browse files
shannahjpage4500claude
authored
fix: DMG background follow-ups to #484 (#489)
* - support custom DMG install backgrounds via package.json and fix the shipped background asset * - update tiff (missed it in first commit) * fix: keep full DMG background visible and reject folder backgrounds - Add title bar height to the Finder window bounds so a background sized to windowSize is not clipped at the bottom - Require the DMG background path to be a file, not just exist Claude-Session: https://claude.ai/code/session_01ScbZRKhQrqgfAaHz1J3eXN --------- Co-authored-by: Joe Page <jpage4500@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 49d3d6a commit 2f80023

5 files changed

Lines changed: 289 additions & 13 deletions

File tree

‎cli/src/main/java/ca/weblite/jdeploy/packaging/PackageService.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import ca.weblite.jdeploy.app.permissions.PermissionRequestService;
1010
import ca.weblite.jdeploy.appbundler.*;
1111
import ca.weblite.jdeploy.appbundler.mac.DmgCreator;
12+
import ca.weblite.jdeploy.appbundler.mac.DmgSettings;
1213
import ca.weblite.jdeploy.environment.Environment;
1314
import ca.weblite.jdeploy.helpers.NpmPackageUtils;
1415
import ca.weblite.jdeploy.helpers.PrereleaseHelper;
@@ -729,18 +730,19 @@ private BundlerResult macArmBundle(
729730
}
730731

731732
private BundlerResult macArmDmg(PackagingContext context, BundlerSettings bundlerSettings, File destDir, String suffix) throws Exception {
732-
return dmg(bundlerSettings, destDir, suffix, BUNDLE_MAC_ARM64_DMG, (bundlerSettings1, bundleDestDir, bundleReleaseDir) -> {
733+
return dmg(context, bundlerSettings, destDir, suffix, BUNDLE_MAC_ARM64_DMG, (bundlerSettings1, bundleDestDir, bundleReleaseDir) -> {
733734
return macArmBundle(context, bundlerSettings1, bundleDestDir, bundleReleaseDir);
734735
});
735736
}
736737

737738
private BundlerResult macIntelDmg(PackagingContext context, BundlerSettings bundlerSettings, File destDir, String suffix) throws Exception {
738-
return dmg(bundlerSettings, destDir, suffix, BUNDLE_MAC_X64_DMG, (bundlerSettings1, bundleDestDir, bundleReleaseDir) -> {
739+
return dmg(context, bundlerSettings, destDir, suffix, BUNDLE_MAC_X64_DMG, (bundlerSettings1, bundleDestDir, bundleReleaseDir) -> {
739740
return macIntelBundle(context, bundlerSettings1, bundleDestDir, bundleReleaseDir);
740741
});
741742
}
742743

743744
private BundlerResult dmg(
745+
PackagingContext context,
744746
BundlerSettings bundlerSettings,
745747
File destDir,
746748
String suffix,
@@ -767,7 +769,8 @@ private BundlerResult dmg(
767769
File dmgFile = new File(destDir, dmgName);
768770
DmgCreator.createDmg(
769771
bundleResult.getOutputFile().getAbsolutePath(),
770-
dmgFile.getAbsolutePath()
772+
dmgFile.getAbsolutePath(),
773+
DmgSettings.fromPackageJson(context.mj(), context.directory)
771774
);
772775
BundlerResult newResult = new BundlerResult(bundleType);
773776
newResult.setOutputFile(dmgFile);

‎shared/src/main/java/ca/weblite/jdeploy/appbundler/mac/DmgCreator.java‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@
1212

1313
public class DmgCreator {
1414

15+
// Finder's window bounds include the title bar, so it is added to the configured
16+
// window size to keep the whole background image visible.
17+
private static final int TITLE_BAR_HEIGHT = 28;
18+
1519
public static void createDmg(String appPath, String dmgPath) throws IOException, InterruptedException {
20+
createDmg(appPath, dmgPath, new DmgSettings());
21+
}
22+
23+
public static void createDmg(
24+
String appPath,
25+
String dmgPath,
26+
DmgSettings settings
27+
) throws IOException, InterruptedException {
1628
File appFile = new File(appPath);
1729
if (!appFile.exists() || !appFile.isDirectory()) {
1830
throw new IllegalArgumentException("The provided app path is not valid.");
@@ -29,7 +41,8 @@ public static void createDmg(String appPath, String dmgPath) throws IOException,
2941
dmgFile = new File(appFile.getParentFile(), dmgName);
3042
}
3143
String tempDir = Files.createTempDirectory("dmg").toString();
32-
String backgroundImg = "background.tiff";
44+
File customBackground = settings.getBackground();
45+
String backgroundImg = customBackground != null ? customBackground.getName() : "background.tiff";
3346

3447
// Create temporary directory structure
3548
Files.createDirectories(Paths.get(tempDir, ".background"));
@@ -40,10 +53,14 @@ public static void createDmg(String appPath, String dmgPath) throws IOException,
4053
// Create alias for /Applications
4154
runCommand("ln", "-s", "/Applications", Paths.get(tempDir, "Applications").toString());
4255

43-
copyResourceToDirectory(
44-
"/com/joshondesign/appbundler/mac/dmg/background.tiff",
45-
Paths.get(tempDir, ".background").toString()
46-
);
56+
if (customBackground != null) {
57+
Files.copy(customBackground.toPath(), Paths.get(tempDir, ".background", backgroundImg));
58+
} else {
59+
copyResourceToDirectory(
60+
"/com/joshondesign/appbundler/mac/dmg/background.tiff",
61+
Paths.get(tempDir, ".background").toString()
62+
);
63+
}
4764

4865
// Create the DMG file
4966
runCommand("hdiutil", "create", "-volname", volumeName, "-srcfolder", tempDir, "-ov", "-format", "UDRW", dmgFile.getPath());
@@ -85,27 +102,36 @@ public static void createDmg(String appPath, String dmgPath) throws IOException,
85102
" log \"Hiding statusbar\"\n" +
86103
" set statusbar visible of container window to false\n" +
87104
" log \"Setting window bounds\"\n" +
88-
" set the bounds of container window to {400, 100, 900, 450}\n" +
105+
" set the bounds of container window to {400, 100, %d, %d}\n" +
89106
" log \"Setting view options\"\n" +
90107
" set viewOptions to the icon view options of container window\n" +
91108
" log \"Setting arrangement to not arranged\"\n" +
92109
" set arrangement of viewOptions to not arranged\n" +
93110
" log \"Setting icon size\"\n" +
94-
" set icon size of viewOptions to 100\n" +
111+
" set icon size of viewOptions to %d\n" +
95112
" log \"Setting background picture\"\n" +
96113
" set background picture of viewOptions to file \".background:%s\"\n" +
97114
" log \"Setting position of the app icon\"\n" +
98-
" set position of item \"%s\" of container window to {100, 150}\n" +
115+
" set position of item \"%s\" of container window to {%d, %d}\n" +
99116
" log \"Setting position of Applications icon\"\n" +
100-
" set position of item \"Applications\" of container window to {400, 150}\n" +
117+
" set position of item \"Applications\" of container window to {%d, %d}\n" +
101118
" log \"Updating window\"\n" +
102119
" update without registering applications\n" +
103120
" log \"Delaying for 5 seconds\"\n" +
104121
" delay 5\n" +
105122
" log \"AppleScript execution completed\"\n" +
106123
" end tell\n" +
107124
"end tell\n",
108-
volumeName, backgroundImg, appName
125+
volumeName,
126+
400 + settings.getWindowWidth(),
127+
100 + settings.getWindowHeight() + TITLE_BAR_HEIGHT,
128+
settings.getIconSize(),
129+
backgroundImg,
130+
appName,
131+
settings.getAppIconX(),
132+
settings.getAppIconY(),
133+
settings.getApplicationsIconX(),
134+
settings.getApplicationsIconY()
109135
);
110136
runCommand("osascript", "-e", appleScript);
111137
} finally {
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package ca.weblite.jdeploy.appbundler.mac;
2+
3+
import java.io.File;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* The appearance of the DMG installer window. The defaults match jDeploy's own bundled
10+
* background image.
11+
*/
12+
public class DmgSettings {
13+
14+
private static final String[] BACKGROUND_FILE_NAMES = {"dmgbackground.tiff", "dmgbackground.png"};
15+
16+
private File background;
17+
18+
private int windowWidth = 500;
19+
20+
private int windowHeight = 350;
21+
22+
private int iconSize = 100;
23+
24+
private int appIconX = 100;
25+
26+
private int appIconY = 150;
27+
28+
private int applicationsIconX = 400;
29+
30+
private int applicationsIconY = 150;
31+
32+
/**
33+
* Reads the settings from the "dmg" object of the jdeploy section of package.json, e.g.
34+
*
35+
* <pre>
36+
* "jdeploy": {
37+
* "dmg": {
38+
* "background": "art/dmg-background.tiff",
39+
* "windowSize": [500, 350],
40+
* "iconSize": 100,
41+
* "appIconPosition": [100, 150],
42+
* "applicationsIconPosition": [400, 150]
43+
* }
44+
* }
45+
* </pre>
46+
*
47+
* If no background is configured there, a dmgbackground.tiff or dmgbackground.png file in
48+
* the project directory is used. Properties that are absent keep their default value.
49+
*
50+
* @param jdeployConfig the jdeploy object of package.json. May be null.
51+
* @param projectDirectory the directory that a relative background path is resolved against.
52+
*/
53+
public static DmgSettings fromPackageJson(Map jdeployConfig, File projectDirectory) {
54+
Object dmg = jdeployConfig == null ? null : jdeployConfig.get("dmg");
55+
Map config = dmg instanceof Map ? (Map) dmg : Collections.emptyMap();
56+
DmgSettings out = new DmgSettings();
57+
out.background = findBackground(config.get("background"), projectDirectory);
58+
int[] windowSize = intPair(config, "windowSize");
59+
if (windowSize != null) {
60+
out.windowWidth = windowSize[0];
61+
out.windowHeight = windowSize[1];
62+
}
63+
if (config.get("iconSize") instanceof Number) {
64+
out.iconSize = ((Number) config.get("iconSize")).intValue();
65+
}
66+
int[] appIconPosition = intPair(config, "appIconPosition");
67+
if (appIconPosition != null) {
68+
out.appIconX = appIconPosition[0];
69+
out.appIconY = appIconPosition[1];
70+
}
71+
int[] applicationsIconPosition = intPair(config, "applicationsIconPosition");
72+
if (applicationsIconPosition != null) {
73+
out.applicationsIconX = applicationsIconPosition[0];
74+
out.applicationsIconY = applicationsIconPosition[1];
75+
}
76+
return out;
77+
}
78+
79+
private static File findBackground(Object configuredPath, File projectDirectory) {
80+
if (configuredPath instanceof String) {
81+
File file = new File((String) configuredPath);
82+
if (!file.isAbsolute()) {
83+
file = new File(projectDirectory, (String) configuredPath);
84+
}
85+
if (!file.isFile()) {
86+
throw new IllegalArgumentException("DMG background image not found: " + file);
87+
}
88+
return file;
89+
}
90+
for (String name : BACKGROUND_FILE_NAMES) {
91+
File file = new File(projectDirectory, name);
92+
if (file.isFile()) {
93+
return file;
94+
}
95+
}
96+
return null;
97+
}
98+
99+
private static int[] intPair(Map config, String property) {
100+
Object value = config.get(property);
101+
if (value == null) {
102+
return null;
103+
}
104+
if (value instanceof List && ((List) value).size() == 2) {
105+
List list = (List) value;
106+
if (list.get(0) instanceof Number && list.get(1) instanceof Number) {
107+
return new int[]{((Number) list.get(0)).intValue(), ((Number) list.get(1)).intValue()};
108+
}
109+
}
110+
throw new IllegalArgumentException(
111+
"jdeploy.dmg." + property + " must be an array of two numbers. E.g. [500, 350]"
112+
);
113+
}
114+
115+
/**
116+
* The background image for the installer window, or null to use jDeploy's own.
117+
*/
118+
public File getBackground() {
119+
return background;
120+
}
121+
122+
public int getWindowWidth() {
123+
return windowWidth;
124+
}
125+
126+
public int getWindowHeight() {
127+
return windowHeight;
128+
}
129+
130+
public int getIconSize() {
131+
return iconSize;
132+
}
133+
134+
public int getAppIconX() {
135+
return appIconX;
136+
}
137+
138+
public int getAppIconY() {
139+
return appIconY;
140+
}
141+
142+
public int getApplicationsIconX() {
143+
return applicationsIconX;
144+
}
145+
146+
public int getApplicationsIconY() {
147+
return applicationsIconY;
148+
}
149+
}
Binary file not shown.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package ca.weblite.jdeploy.appbundler.mac;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.io.TempDir;
5+
6+
import java.io.File;
7+
import java.nio.file.Files;
8+
import java.util.Arrays;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
public class DmgSettingsTest {
15+
16+
@Test
17+
public void testDefaults(@TempDir File projectDirectory) {
18+
DmgSettings settings = DmgSettings.fromPackageJson(new HashMap(), projectDirectory);
19+
assertNull(settings.getBackground());
20+
assertEquals(500, settings.getWindowWidth());
21+
assertEquals(350, settings.getWindowHeight());
22+
assertEquals(100, settings.getIconSize());
23+
assertEquals(100, settings.getAppIconX());
24+
assertEquals(150, settings.getAppIconY());
25+
assertEquals(400, settings.getApplicationsIconX());
26+
assertEquals(150, settings.getApplicationsIconY());
27+
}
28+
29+
@Test
30+
public void testConfiguredBackgroundAndGeometry(@TempDir File projectDirectory) throws Exception {
31+
File background = new File(projectDirectory, "art/background.png");
32+
background.getParentFile().mkdirs();
33+
Files.createFile(background.toPath());
34+
35+
Map dmg = new HashMap();
36+
dmg.put("background", "art/background.png");
37+
dmg.put("windowSize", Arrays.asList(600.0, 400.0));
38+
dmg.put("iconSize", 80.0);
39+
dmg.put("appIconPosition", Arrays.asList(150, 200));
40+
dmg.put("applicationsIconPosition", Arrays.asList(450, 200));
41+
Map jdeploy = new HashMap();
42+
jdeploy.put("dmg", dmg);
43+
44+
DmgSettings settings = DmgSettings.fromPackageJson(jdeploy, projectDirectory);
45+
assertEquals(background, settings.getBackground());
46+
assertEquals(600, settings.getWindowWidth());
47+
assertEquals(400, settings.getWindowHeight());
48+
assertEquals(80, settings.getIconSize());
49+
assertEquals(150, settings.getAppIconX());
50+
assertEquals(200, settings.getAppIconY());
51+
assertEquals(450, settings.getApplicationsIconX());
52+
assertEquals(200, settings.getApplicationsIconY());
53+
}
54+
55+
@Test
56+
public void testBackgroundFileConvention(@TempDir File projectDirectory) throws Exception {
57+
File background = new File(projectDirectory, "dmgbackground.tiff");
58+
Files.createFile(background.toPath());
59+
assertEquals(background, DmgSettings.fromPackageJson(new HashMap(), projectDirectory).getBackground());
60+
}
61+
62+
@Test
63+
public void testMissingBackgroundFails(@TempDir File projectDirectory) {
64+
Map dmg = new HashMap();
65+
dmg.put("background", "nope.tiff");
66+
Map jdeploy = new HashMap();
67+
jdeploy.put("dmg", dmg);
68+
assertThrows(
69+
IllegalArgumentException.class,
70+
() -> DmgSettings.fromPackageJson(jdeploy, projectDirectory)
71+
);
72+
}
73+
74+
@Test
75+
public void testDirectoryBackgroundFails(@TempDir File projectDirectory) {
76+
new File(projectDirectory, "art").mkdirs();
77+
Map dmg = new HashMap();
78+
dmg.put("background", "art");
79+
Map jdeploy = new HashMap();
80+
jdeploy.put("dmg", dmg);
81+
assertThrows(
82+
IllegalArgumentException.class,
83+
() -> DmgSettings.fromPackageJson(jdeploy, projectDirectory)
84+
);
85+
}
86+
87+
@Test
88+
public void testMalformedWindowSizeFails(@TempDir File projectDirectory) {
89+
Map dmg = new HashMap();
90+
dmg.put("windowSize", 500.0);
91+
Map jdeploy = new HashMap();
92+
jdeploy.put("dmg", dmg);
93+
assertThrows(
94+
IllegalArgumentException.class,
95+
() -> DmgSettings.fromPackageJson(jdeploy, projectDirectory)
96+
);
97+
}
98+
}

0 commit comments

Comments
 (0)