61
61
* and to make way for future ability to customize.
62
62
*/
63
63
public class Theme {
64
+
65
+ static final String THEME_DIR = "theme/" ;
64
66
65
67
/**
66
68
* Copy of the defaults in case the user mangles a preference.
@@ -73,7 +75,8 @@ public class Theme {
73
75
74
76
static protected void init () {
75
77
try {
76
- table .load (new File (BaseNoGui .getContentFile ("lib" ), "theme/theme.txt" ));
78
+ table .load (new File (BaseNoGui .getContentFile ("lib" ), THEME_DIR + "theme.txt" ));
79
+ table .load (getThemeFile (THEME_DIR + "theme.txt" ));
77
80
} catch (Exception te ) {
78
81
Base .showError (null , tr ("Could not read color theme settings.\n "
79
82
+ "You'll need to reinstall Arduino." ),
@@ -177,6 +180,9 @@ static public Font getFont(String attr) {
177
180
String value = getDefault (attr );
178
181
set (attr , value );
179
182
font = PreferencesHelper .getFont (table , attr );
183
+ if (font == null ) {
184
+ return null ;
185
+ }
180
186
}
181
187
return font .deriveFont ((float ) scale (font .getSize ()));
182
188
}
@@ -245,11 +251,10 @@ public static Map<String, Object> getStyledFont(String what, Font font) {
245
251
*/
246
252
static public Image getLibImage (String filename , Component who , int width ,
247
253
int height ) {
248
- File libFolder = BaseNoGui .getContentFile ("lib" );
249
254
Image image = null ;
250
255
251
256
// Use vector image when available
252
- File vectorFile = new File ( libFolder , filename + ".svg" );
257
+ File vectorFile = getThemeFile ( filename + ".svg" );
253
258
if (vectorFile .exists ()) {
254
259
try {
255
260
image = imageFromSVG (vectorFile .toURI ().toURL (), width , height );
@@ -259,13 +264,16 @@ static public Image getLibImage(String filename, Component who, int width,
259
264
}
260
265
}
261
266
262
- // Otherwise fall-back to PNG bitmaps
263
- if (image == null ) {
264
- File bitmapFile = new File (libFolder , filename + ".png" );
265
- File bitmap2xFile = new File (libFolder , filename + "@2x.png" );
267
+ File bitmapFile = getThemeFile (filename + ".png" );
268
+
269
+ // Otherwise fall-back to PNG bitmaps, allowing user-defined bitmaps to
270
+ // override built-in svgs
271
+ if (image == null || (!isUserThemeFile (vectorFile ) && isUserThemeFile (bitmapFile ))) {
272
+ File bitmap2xFile = getThemeFile (filename + "@2x.png" );
266
273
267
274
File imageFile ;
268
- if ((getScale () > 125 && bitmap2xFile .exists ()) || !bitmapFile .exists ()) {
275
+ if (((getScale () > 125 && bitmap2xFile .exists ()) || !bitmapFile .exists ())
276
+ && isUserThemeFile (bitmapFile ) == isUserThemeFile (bitmap2xFile )) {
269
277
imageFile = bitmap2xFile ;
270
278
} else {
271
279
imageFile = bitmapFile ;
@@ -298,7 +306,7 @@ static public Image getLibImage(String filename, Component who, int width,
298
306
*/
299
307
static public Image getThemeImage (String name , Component who , int width ,
300
308
int height ) {
301
- return getLibImage ("theme/" + name , who , width , height );
309
+ return getLibImage (THEME_DIR + name , who , width , height );
302
310
}
303
311
304
312
private static Image imageFromSVG (URL url , int width , int height )
@@ -324,5 +332,33 @@ static public Graphics2D setupGraphics2D(Graphics graphics) {
324
332
}
325
333
return g ;
326
334
}
335
+
336
+ /**
337
+ * Check whether the specified file is a user-defined theme file
338
+ */
339
+ static public boolean isUserThemeFile (File file ) {
340
+ return file .exists () && file .getAbsolutePath ().startsWith (BaseNoGui .getSketchbookFolder ().getAbsolutePath ());
341
+ }
327
342
343
+ /**
344
+ * @param name
345
+ * @return
346
+ */
347
+ static public File getThemeFile (String name ) {
348
+ File sketchBookThemeFolder = new File (BaseNoGui .getSketchbookFolder (), THEME_DIR );
349
+
350
+ File themeFile = new File (sketchBookThemeFolder , name );
351
+ if (themeFile .exists ()) {
352
+ return themeFile ;
353
+ }
354
+
355
+ if (name .startsWith (THEME_DIR )) {
356
+ themeFile = new File (sketchBookThemeFolder , name .substring (THEME_DIR .length ()));
357
+ if (themeFile .exists ()) {
358
+ return themeFile ;
359
+ }
360
+ }
361
+
362
+ return new File (BaseNoGui .getContentFile ("lib" ), name );
363
+ }
328
364
}
0 commit comments