From 5af82c1989ef0d9ab42c49c020ae1ed0dd45d5cf Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Mon, 5 Jul 2021 21:07:05 -0400 Subject: [PATCH] instead of denying Mode changes, just open a new Editor window (resolves #189) --- app/src/processing/app/Base.java | 43 +++++++++++++++++---------- app/src/processing/app/Mode.java | 11 +++++++ app/src/processing/app/ui/Editor.java | 9 ++++++ todo.txt | 7 +++-- 4 files changed, 51 insertions(+), 19 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 99481d060..d968dcc15 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -940,9 +940,7 @@ public class Base { /** - * The call has already checked to make sure this sketch is not modified, - * now change the mode. - * @return true if mode is changed. + * @return true if mode is changed within this window (false if new window) */ public boolean changeMode(Mode mode) { Mode oldMode = activeEditor.getMode(); @@ -950,25 +948,22 @@ public class Base { Sketch sketch = activeEditor.getSketch(); nextMode = mode; - if (sketch.isUntitled()) { + if (sketch.isModified()) { + handleNew(); // don't bother with error messages, just switch + return false; + + } else if (sketch.isUntitled()) { // The current sketch is empty, just close and start fresh. // (Otherwise the editor would lose its 'untitled' status.) handleClose(activeEditor, true); handleNew(); } else { - // If the current editor contains file extensions that the new mode can handle, then - // write a sketch.properties file with the new mode specified, and reopen. - boolean newModeCanHandleCurrentSource = true; - for (final SketchCode code : sketch.getCode()) { - if (!mode.validExtension(code.getExtension())) { - newModeCanHandleCurrentSource = false; - break; - } - } - if (!newModeCanHandleCurrentSource) { - return false; - } else { + // If the current sketch contains file extensions that the new mode + // can handle, then write a sketch.properties file with the new mode + // specified, and reopen. (Really only useful for Java <-> Android) + //if (isCompatible(sketch, mode)) { + if (mode.canEdit(sketch)) { final File props = new File(sketch.getCodeFolder(), "sketch.properties"); saveModeSettings(props, nextMode); handleClose(activeEditor, true); @@ -980,11 +975,27 @@ public class Base { handleOpen(sketch.getMainFilePath()); return false; } + } else { + handleNew(); // create a new window with the new Mode + return false; } } } + // Against all (or at least most) odds, we were able to reassign the Mode + return true; + } + + + /* + private boolean isCompatible(Sketch sketch, Mode mode) { + for (final SketchCode code : sketch.getCode()) { + if (!mode.validExtension(code.getExtension())) { + return false; + } + } return true; } + */ private static class ModeInfo { diff --git a/app/src/processing/app/Mode.java b/app/src/processing/app/Mode.java index 739caaee0..6afa17c18 100644 --- a/app/src/processing/app/Mode.java +++ b/app/src/processing/app/Mode.java @@ -949,6 +949,17 @@ public abstract class Mode { return validExtension(f.getName().substring(dot + 1)); } + + public boolean canEdit(Sketch sketch) { + for (final SketchCode code : sketch.getCode()) { + if (!validExtension(code.getExtension())) { + return false; + } + } + return true; + } + + /** * Check this extension (no dots, please) against the list of valid * extensions. diff --git a/app/src/processing/app/ui/Editor.java b/app/src/processing/app/ui/Editor.java index 3ed6e4158..9bd34b43c 100644 --- a/app/src/processing/app/ui/Editor.java +++ b/app/src/processing/app/ui/Editor.java @@ -529,6 +529,7 @@ public abstract class Editor extends JFrame implements RunnerListener { for (final Mode m : base.getModeList()) { JRadioButtonMenuItem item = new JRadioButtonMenuItem(m.getTitle()); item.addActionListener(e -> { + /* if (!sketch.isModified()) { if (!base.changeMode(m)) { reselectMode(); @@ -540,6 +541,14 @@ public abstract class Editor extends JFrame implements RunnerListener { Messages.showWarning("Save", "Please save the sketch before changing the mode."); } + */ + //if (sketch.isModified() || !base.changeMode(m)) { + if (!base.changeMode(m)) { + // Returns false if unable to change the mode in this window + // (which will open a new window with the new Mode), in which case + // re-select the menu item b/c Java changes it automatically. + reselectMode(); + } }); modePopup.add(item); modeGroup.add(item); diff --git a/todo.txt b/todo.txt index a654059a2..02a4e8bdb 100755 --- a/todo.txt +++ b/todo.txt @@ -17,6 +17,10 @@ X set args for JavaFX and OpenGL for the mouse and key events X make sure that everything is set properly, also for keys X move ISSUE_TEMPLATE to .github subfolder X https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/manually-creating-a-single-issue-template-for-your-repository +o ability to switch mode in p5 w/o saving/closing/etc +X trying to save the user from themselves here is just messier than needed +X opt to open a new editor window rather than weird error messages +X https://github.com/processing/processing4/issues/189 readme X was fixed in the source for 4.0a5, but may not have been included in the dist @@ -170,9 +174,6 @@ _ better for git, etc _ single file thing is long gone _ introduce the idea of 'scraps' (ala gist) that are just single page blobs _ launch/psk files/import from web editor (more details below) -_ ability to switch mode in p5 w/o saving/closing/etc -_ trying to save the user from themselves here is just messier than needed -_ https://github.com/processing/processing4/issues/189 _ cleaning up the temp file handling _ 'show sketch folder' weird when in temp folder _ ask to save first (sketch has not been saved yet)