From 09712b95967898576fcdefc2c4255ff82d1e781d Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Thu, 4 Aug 2022 08:06:20 -0400 Subject: [PATCH] redo theme handling to reset previous, not store in sketchbook --- .../processing/app/tools/ThemeSelector.java | 68 ++++++------ app/src/processing/app/ui/Theme.java | 102 +++++++++++++----- todo.txt | 29 +++-- 3 files changed, 123 insertions(+), 76 deletions(-) diff --git a/app/src/processing/app/tools/ThemeSelector.java b/app/src/processing/app/tools/ThemeSelector.java index af93dfab0..32d7d8009 100644 --- a/app/src/processing/app/tools/ThemeSelector.java +++ b/app/src/processing/app/tools/ThemeSelector.java @@ -281,17 +281,6 @@ public class ThemeSelector extends JFrame implements Tool { } - private File nextBackupFile() { - int index = 0; - File backupFile; - do { - index++; - backupFile = new File(Base.getSketchbookFolder(), String.format("theme.%03d", index)); - } while (backupFile.exists()); - return backupFile; - } - - private String getCurrentTheme() { if (sketchbookFile.exists()) { return Util.loadFile(sketchbookFile); @@ -331,33 +320,33 @@ public class ThemeSelector extends JFrame implements Tool { private void setCurrentIndex(int index) { currentIndex = index; - try { - if (userModifiedTheme()) { - // If the user has a custom theme they've modified, - // rename it to theme.001, theme.002, etc. as a backup - // to avoid overwriting anything they've created. - File backupFile = nextBackupFile(); - boolean success = sketchbookFile.renameTo(backupFile); - if (!success) { - Messages.showWarning("Could not back up theme", - "Could not save a backup of theme.txt in your sketchbook folder.\n" + - "Rename it manually and try setting the theme again."); - return; - } + + // If there is a theme.txt file in the sketchbook folder, + // archive it and move out of the way. + if (userModifiedTheme()) { + boolean success = Theme.archiveCurrent(); + if (!success) { + Messages.showWarning("Could not back up theme", + "Could not save a backup of theme.txt in your sketchbook folder.\n" + + "Rename it manually and try setting the theme again."); + return; } - - // Save the file and reload the theme. - Util.saveFile(currentSet.get(index), sketchbookFile); - reloadTheme(); - - } catch (IOException e) { - base.getActiveEditor().statusError(e); } + + // No longer saving a new theme.txt when making a selection, just setting a + // preference so that subsequent Processing updates load new theme changes. + //Util.saveFile(currentSet.get(index), sketchbookFile); + Preferences.set("theme", currentSet.getPath(index)); + reloadTheme(); } + /** + * Called when user clicks the 'reload' button, or when the theme + * is changed by clicking on a built-in selection. + */ private void reloadTheme() { - Theme.load(); + Theme.reload(); base.updateTheme(); updateTheme(); } @@ -375,6 +364,7 @@ public class ThemeSelector extends JFrame implements Tool { class ThemeSet { final String name; private int count; + private String[] paths; private String[] themes; private Image[] images; private Map indices; @@ -389,16 +379,18 @@ public class ThemeSelector extends JFrame implements Tool { if (count < lines.length) { System.err.println("Only using the first 16 themes inside " + orderFile); } + paths = new String[count]; themes = new String[count]; images = new Image[count]; indices = new HashMap<>(count); // don't load more than 16 entries for (int i = 0; i < count; i++) { - File file = new File(dir, lines[i] + ".txt"); + String filename = lines[i] + ".txt"; + File file = new File(dir, filename); String theme = Util.loadFile(file); indices.put(theme, i); -// hashes.add(theme.hashCode()); + paths[i] = name + "/" + filename; themes[i] = theme; images[i] = renderImage(file.getName(), theme); } @@ -437,10 +429,14 @@ public class ThemeSelector extends JFrame implements Tool { return Toolkit.svgToImageMult(miniSvgXml, ColorfulPanel.DIM, ColorfulPanel.DIM, replacements); } - String get(int index) { - return themes[index]; + String getPath(int index) { + return paths[index]; } +// String getTheme(int index) { +// return themes[index]; +// } + /** * Return the index for a given theme in this set, * or -1 if not part of this set. diff --git a/app/src/processing/app/ui/Theme.java b/app/src/processing/app/ui/Theme.java index 9cd326578..c24d956cf 100644 --- a/app/src/processing/app/ui/Theme.java +++ b/app/src/processing/app/ui/Theme.java @@ -24,6 +24,7 @@ package processing.app.ui; import processing.app.Base; import processing.app.Messages; +import processing.app.Preferences; import processing.app.Settings; import processing.app.syntax.SyntaxStyle; import processing.core.PApplet; @@ -38,36 +39,41 @@ import java.util.Arrays; public class Theme { + static final String DEFAULT_PATH = "Minerals/kyanite.txt"; static Settings theme; static public void init() { try { - File inputFile = getThemeFile(); -// if (inputFile == null) { -// throw new RuntimeException("Missing required file (theme.txt), you may need to reinstall."); -// } - // First load the default theme data for the whole PDE. - theme = new Settings(inputFile); - - // A spot-check of Modes shows that theme.txt is not being overridden, - // so removing this (questionable, warned against) capability for 4.0a6. - /* - // The mode-specific theme.txt file should only contain additions, - // and in extremely rare cases, it might override entries from the - // main theme. Do not override for style changes unless they are - // objectively necessary for your Mode. - File modeTheme = new File(folder, "theme/theme.txt"); - if (modeTheme.exists()) { - // Override the built-in settings with what the theme provides - theme.load(modeTheme); + File inputFile = getDefaultFile(); + if (!inputFile.exists()) { + System.err.println("Missing required file (theme.txt), please reinstall Processing."); } - */ + // First load the default theme data, in case new parameters were added + // that may not be covered with a custom version found in the sketchbook. + theme = new Settings(inputFile); // other things that have to be set explicitly for the defaults theme.setColor("run.window.bgcolor", SystemColor.control); - // pull in the version from the user's sketchbook folder - load(); + if (Preferences.get("theme") == null) { + // This is not being set in defaults.txt so that we have a way + // to reset the theme after the major changes in 4.0 beta 9. + // This does a one-time archival of the theme.txt file in the + // sketchbook folder, because most people have not customized + // their theme, but they probably made a selection. + // If they customized the theme, they can bring it back by + // renaming the file from theme.001 to theme.txt. + // If they were using a built-in theme, they will need to + // re-select it using the Theme Selector. + Preferences.set("theme", DEFAULT_PATH); + + if (getSketchbookFile().exists()) { + archiveCurrent(); + } + } + + // load sketchbook theme or the one specified in preferences + reload(); } catch (IOException e) { Messages.showError("Problem loading theme.txt", @@ -77,13 +83,37 @@ public class Theme { /** - * Load theme.txt from the user's sketchbook folder. + * Pull in the version from the user's sketchbook folder, + * or if none exists, use the setting from preferences. */ - static public void load() { + static public void reload() { + if (!loadSketchbookFile()) { + String prefTheme = Preferences.get("theme"); + try { + File prefFile = new File(getThemeFolder(), prefTheme); + if (prefFile.exists()) { + theme.load(prefFile); + } + } catch (IOException e) { + Messages.showWarning("Theme Reload Problem", + "Error while reloading the theme. Please report.", e); + } + } + } + + + /** + * Load theme.txt from the user's sketchbook folder. + * The caller is expected to make sure the file exists. + */ + @SuppressWarnings("BooleanMethodIsAlwaysInverted") + static public boolean loadSketchbookFile() { File sketchbookTheme = getSketchbookFile(); if (sketchbookTheme.exists()) { theme.load(sketchbookTheme); + return true; } + return false; } @@ -97,7 +127,10 @@ public class Theme { } - static public File getThemeFile() throws IOException { + /** + * Returns lib/theme/theme.txt in the Processing installation. + */ + static public File getDefaultFile() throws IOException { return new File(getThemeFolder(), "theme.txt"); } @@ -107,6 +140,27 @@ public class Theme { } + /** + * If the user has a custom theme they've modified, rename it to theme.001, + * theme.002, etc. as a backup to avoid overwriting anything they've created. + */ + static public boolean archiveCurrent() { + File backupFile = nextArchiveFile(); + return getSketchbookFile().renameTo(backupFile); + } + + + static private File nextArchiveFile() { + int index = 0; + File backupFile; + do { + index++; + backupFile = new File(Base.getSketchbookFolder(), String.format("theme.%03d", index)); + } while (backupFile.exists()); + return backupFile; + } + + static public void print() { theme.print(); } diff --git a/todo.txt b/todo.txt index 6d4212197..1f40604d0 100755 --- a/todo.txt +++ b/todo.txt @@ -126,7 +126,6 @@ X https://github.com/processing/processing4/pull/513 design X color updates based on changes from Paul (220426) -X if no matching theme selected, was highlighting column -1 o icons for the console items X console.svg and error.svg already done X console scroll bar colors @@ -138,11 +137,6 @@ X look and feel plus fonts X trying out https://www.formdev.com/flatlaf/ to clean things up X using Source Sans Pro as interface font X rewrite language selector to use fallback fonts -X accent color and light/dark mode into the theme, and use with flatlaf -X style the popup menu for Mode using the theme -X console scroll bar colors -X update lib/theme.txt to clean up current Frankenstein status -X just replace with the blue default once that's updated X tweaks to the splash/about screen from Paul X Color Selector buttons misaligned X redo layout for FlatLaf, also tweak further to remove some of the quirkiness @@ -177,7 +171,7 @@ X these go into CompletionPanel.java X also set the color and font with updateTheme() X waiting on final colors from theme -design/selector +design/themes X updated 4x4 for themes, foundation svg icon tweaks X implement 4x4 by auto-generating from svg versions X make drag events work properly @@ -187,6 +181,18 @@ X both sets working, loading from folders X fixed up html wiring for styles X gradients X add a couple with gradients to the selector box? +X if no matching theme selected, was highlighting column -1 +X reset the theme because of significant changes +X move away from writing theme.txt? +X instead store the theme name, for easier updating +X and a version in the sketchbook will always override +X (selecting a new theme will rename that file, but not replace a theme.txt file) +X include in this release b/c of potential for problems +X accent color and light/dark mode into the theme, and use with flatlaf +X style the popup menu for Mode using the theme +X console scroll bar colors +X update lib/theme.txt to clean up current Frankenstein status +X just replace with the blue default once that's updated design/errors X errors table theme @@ -369,15 +375,6 @@ _ https://github.com/processing/processing4/issues/522 _ https://github.com/processing/processing4-javafx/issues/15 -fixes/changes before beta 9 -_ reset the theme because of significant changes -_ move away from writing theme.txt? -_ instead store the theme name, for easier updating -_ and a version in the sketchbook will always override -_ (selecting a new theme will rename that file, but not replace a theme.txt file) -_ include in this release b/c of potential for problems - - can wait until next release _ thicker version of the search icon for the status panel _ copy the icon over from the manager