Skip to content

Commit add4779

Browse files
committed
Rewrite of the Base.getSettingsFolder() and Platform.getSettingsFolder()
Rewrote both function so they fit into a single file, negating the need for hopping around when looking into what this functionality does. Also rewrote it so it is no longer generates random awt windows through the `Messages` class
1 parent 11f1a1e commit add4779

File tree

10 files changed

+244
-244
lines changed

10 files changed

+244
-244
lines changed

app/src/processing/app/Base.java

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,29 @@
2323

2424
package processing.app;
2525

26-
import java.awt.*;
27-
import java.awt.event.ActionListener;
28-
import java.io.*;
29-
import java.lang.reflect.InvocationTargetException;
30-
import java.util.*;
31-
import java.util.List;
32-
import java.util.Map.Entry;
33-
34-
import javax.swing.*;
35-
import javax.swing.tree.DefaultMutableTreeNode;
36-
3726
import com.formdev.flatlaf.FlatDarkLaf;
3827
import com.formdev.flatlaf.FlatLaf;
3928
import com.formdev.flatlaf.FlatLightLaf;
4029
import processing.app.contrib.*;
4130
import processing.app.tools.Tool;
4231
import processing.app.ui.*;
4332
import processing.app.ui.Toolkit;
44-
import processing.core.*;
33+
import processing.core.PApplet;
4534
import processing.data.StringList;
4635

36+
import javax.swing.*;
37+
import javax.swing.tree.DefaultMutableTreeNode;
38+
import java.awt.*;
39+
import java.awt.event.ActionListener;
40+
import java.io.File;
41+
import java.io.FileInputStream;
42+
import java.io.IOException;
43+
import java.io.InputStream;
44+
import java.lang.reflect.InvocationTargetException;
45+
import java.util.*;
46+
import java.util.List;
47+
import java.util.Map.Entry;
48+
4749
/**
4850
* The base class for the main processing application.
4951
* Primary role of this class is for platform identification and
@@ -2212,29 +2214,50 @@ static public InputStream getLibStream(String filename) throws IOException {
22122214
* something similar on Windows, a dot folder on Linux.) Removed this as a
22132215
* preference for 3.0a3 because we need this to be stable, but adding back
22142216
* for 4.0 beta 4 so that folks can do 'portable' versions again.
2217+
*
2218+
* @deprecated use processing.utils.Settings.getFolder() instead, this method will invoke AWT
22152219
*/
22162220
static public File getSettingsFolder() {
2217-
File settingsFolder = null;
2218-
2219-
try {
2220-
settingsFolder = Platform.getSettingsFolder();
2221-
2222-
// create the folder if it doesn't exist already
2223-
if (!settingsFolder.exists()) {
2224-
if (!settingsFolder.mkdirs()) {
2225-
Messages.showError("Settings issues",
2226-
"Processing cannot run because it could not\n" +
2227-
"create a folder to store your settings at\n" +
2228-
settingsFolder, null);
2229-
}
2221+
try {
2222+
return processing.utils.Settings.getFolder();
2223+
} catch (processing.utils.Settings.SettingsFolderException e) {
2224+
switch (e.getType()) {
2225+
case COULD_NOT_CREATE_FOLDER -> Messages.showError("Settings issues",
2226+
"""
2227+
Processing cannot run because it could not
2228+
create a folder to store your settings at
2229+
""" + e.getMessage(), null);
2230+
case WINDOWS_APPDATA_NOT_FOUND -> Messages.showError("Settings issues",
2231+
"""
2232+
Processing cannot run because it could not
2233+
find the AppData or LocalAppData folder on your system.
2234+
""", null);
2235+
case MACOS_LIBRARY_FOLDER_NOT_FOUND -> Messages.showError("Settings issues",
2236+
"""
2237+
Processing cannot run because it could not
2238+
find the Library folder on your system.
2239+
""", null);
2240+
case LINUX_CONFIG_FOLDER_NOT_FOUND -> Messages.showError("Settings issues",
2241+
"""
2242+
Processing cannot run because either your
2243+
XDG_CONFIG_HOME or SNAP_USER_COMMON is set
2244+
but the folder does not exist.
2245+
""", null);
2246+
case LINUX_SUDO_USER_ERROR -> Messages.showError("Settings issues",
2247+
"""
2248+
Processing cannot run because it was started
2249+
with sudo and Processing could not resolve
2250+
the original users home directory.
2251+
""", null);
2252+
default -> Messages.showTrace("An rare and unknowable thing happened",
2253+
"""
2254+
Could not get the settings folder. Please report:
2255+
http://github.com/processing/processing4/issues/new
2256+
""",
2257+
e, true);
2258+
}
22302259
}
2231-
} catch (Exception e) {
2232-
Messages.showTrace("An rare and unknowable thing happened",
2233-
"Could not get the settings folder. Please report:\n" +
2234-
"http://github.com/processing/processing/issues/new",
2235-
e, true);
2236-
}
2237-
return settingsFolder;
2260+
throw new RuntimeException("Unreachable code in Base.getSettingsFolder()");
22382261
}
22392262

22402263

app/src/processing/app/Platform.java

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,24 @@
2323

2424
package processing.app;
2525

26-
import java.io.File;
27-
import java.io.IOException;
28-
import java.lang.management.ManagementFactory;
29-
import java.net.URISyntaxException;
30-
import java.net.URL;
31-
import java.util.*;
32-
3326
import com.sun.jna.platform.FileUtils;
34-
3527
import processing.app.platform.DefaultPlatform;
3628
import processing.core.PApplet;
3729
import processing.core.PConstants;
3830
import processing.data.StringDict;
3931

32+
import java.io.File;
33+
import java.io.IOException;
34+
import java.lang.management.ManagementFactory;
35+
import java.net.URISyntaxException;
36+
import java.net.URL;
37+
import java.util.ArrayList;
38+
import java.util.HashMap;
39+
import java.util.List;
40+
import java.util.Map;
41+
4042

41-
public class Platform {
43+
public class Platform extends processing.utils.Platform {
4244
static DefaultPlatform inst;
4345

4446
/*
@@ -131,12 +133,7 @@ static public float getSystemZoom() {
131133
}
132134

133135

134-
static public File getSettingsFolder() throws Exception {
135-
return inst.getSettingsFolder();
136-
}
137-
138-
139-
static public File getDefaultSketchbookFolder() throws Exception {
136+
static public File getDefaultSketchbookFolder() throws Exception {
140137
return inst.getDefaultSketchbookFolder();
141138
}
142139

@@ -298,28 +295,7 @@ static public int getIndex(String platformName) {
298295
// the MACOSX constant would instead read as the LINUX constant.
299296

300297

301-
/**
302-
* returns true if Processing is running on a Mac OS X machine.
303-
*/
304-
static public boolean isMacOS() {
305-
return System.getProperty("os.name").contains("Mac"); //$NON-NLS-1$ //$NON-NLS-2$
306-
}
307-
308-
309-
/**
310-
* returns true if running on windows.
311-
*/
312-
static public boolean isWindows() {
313-
return System.getProperty("os.name").contains("Windows"); //$NON-NLS-1$ //$NON-NLS-2$
314-
}
315-
316298

317-
/**
318-
* true if running on linux.
319-
*/
320-
static public boolean isLinux() {
321-
return System.getProperty("os.name").contains("Linux"); //$NON-NLS-1$ //$NON-NLS-2$
322-
}
323299

324300

325301
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

app/src/processing/app/Preferences.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package processing.app
33
import androidx.compose.runtime.*
44
import kotlinx.coroutines.Dispatchers
55
import kotlinx.coroutines.launch
6+
import processing.utils.Settings
67
import java.io.File
78
import java.io.InputStream
8-
import java.nio.file.*
9-
import java.util.Properties
9+
import java.nio.file.FileSystems
10+
import java.nio.file.StandardWatchEventKinds
11+
import java.nio.file.WatchEvent
12+
import java.util.*
1013

1114

1215
const val PREFERENCES_FILE_NAME = "preferences.txt"
@@ -20,7 +23,7 @@ fun PlatformStart(){
2023
fun loadPreferences(): Properties{
2124
PlatformStart()
2225

23-
val settingsFolder = Platform.getSettingsFolder()
26+
val settingsFolder = Settings.getFolder()
2427
val preferencesFile = settingsFolder.resolve(PREFERENCES_FILE_NAME)
2528

2629
if(!preferencesFile.exists()){

app/src/processing/app/platform/DefaultPlatform.java

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,19 @@
2323

2424
package processing.app.platform;
2525

26-
import java.awt.Desktop;
27-
import java.awt.Font;
28-
import java.io.File;
29-
30-
import javax.swing.UIManager;
31-
import javax.swing.border.EmptyBorder;
32-
3326
import com.formdev.flatlaf.FlatLaf;
3427
import com.formdev.flatlaf.FlatLightLaf;
35-
import com.sun.jna.Library;
36-
import com.sun.jna.Native;
37-
3828
import processing.app.Base;
3929
import processing.app.Preferences;
4030
import processing.app.ui.Toolkit;
4131
import processing.awt.ShimAWT;
4232
import processing.core.PApplet;
4333

34+
import javax.swing.*;
35+
import javax.swing.border.EmptyBorder;
36+
import java.awt.*;
37+
import java.io.File;
38+
4439

4540
/**
4641
* Used by Base for platform-specific tweaking, for instance finding the
@@ -206,24 +201,7 @@ public void setInterfaceZoom() throws Exception {
206201
public void saveLanguage(String languageCode) { }
207202

208203

209-
/**
210-
* This function should throw an exception or return a value.
211-
* Do not return null.
212-
*/
213-
public File getSettingsFolder() throws Exception {
214-
File override = Base.getSettingsOverride();
215-
if (override != null) {
216-
return override;
217-
}
218-
219-
// If no subclass has a behavior, default to making a
220-
// ".processing" directory in the user's home directory.
221-
File home = new File(System.getProperty("user.home"));
222-
return new File(home, ".processing");
223-
}
224-
225-
226-
/**
204+
/**
227205
* @return if not overridden, a folder named "sketchbook" in user.home.
228206
* @throws Exception so that subclasses can throw a fit
229207
*/

app/src/processing/app/platform/LinuxPlatform.java

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@
2222

2323
package processing.app.platform;
2424

25-
import java.io.File;
26-
import java.awt.Desktop;
27-
import java.awt.Toolkit;
28-
2925
import processing.app.Base;
30-
import processing.app.Messages;
3126
import processing.app.Preferences;
3227
import processing.core.PApplet;
3328

3429
import javax.swing.*;
30+
import java.awt.*;
31+
import java.io.File;
3532

3633

3734
public class LinuxPlatform extends DefaultPlatform {
@@ -90,40 +87,7 @@ static public String getHomeDir(String user) throws Exception {
9087
}
9188

9289

93-
@Override
94-
public File getSettingsFolder() throws Exception {
95-
File override = Base.getSettingsOverride();
96-
if (override != null) {
97-
return override;
98-
}
99-
100-
// https://github.com/processing/processing4/issues/203
101-
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
102-
103-
File configHome = null;
104-
105-
// Check to see if the user has set a different location for their config
106-
String configHomeEnv = System.getenv("XDG_CONFIG_HOME");
107-
if (configHomeEnv != null && !configHomeEnv.isBlank()) {
108-
configHome = new File(configHomeEnv);
109-
if (!configHome.exists()) {
110-
Messages.err("XDG_CONFIG_HOME is set to " + configHomeEnv + " but does not exist.");
111-
configHome = null; // don't use non-existent folder
112-
}
113-
}
114-
String snapUserCommon = System.getenv("SNAP_USER_COMMON");
115-
if (snapUserCommon != null && !snapUserCommon.isBlank()) {
116-
configHome = new File(snapUserCommon);
117-
}
118-
// If not set properly, use the default
119-
if (configHome == null) {
120-
configHome = new File(getHomeDir(), ".config");
121-
}
122-
return new File(configHome, "processing");
123-
}
124-
125-
126-
@Override
90+
@Override
12791
public File getDefaultSketchbookFolder() throws Exception {
12892
return new File(getHomeDir(), "sketchbook");
12993
}

0 commit comments

Comments
 (0)