Skip to content

Commit 5af82c1

Browse files
committed
instead of denying Mode changes, just open a new Editor window (resolves #189)
1 parent 5d62bb5 commit 5af82c1

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

app/src/processing/app/Base.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -940,35 +940,30 @@ public Mode getNextMode() {
940940

941941

942942
/**
943-
* The call has already checked to make sure this sketch is not modified,
944-
* now change the mode.
945-
* @return true if mode is changed.
943+
* @return true if mode is changed within this window (false if new window)
946944
*/
947945
public boolean changeMode(Mode mode) {
948946
Mode oldMode = activeEditor.getMode();
949947
if (oldMode != mode) {
950948
Sketch sketch = activeEditor.getSketch();
951949
nextMode = mode;
952950

953-
if (sketch.isUntitled()) {
951+
if (sketch.isModified()) {
952+
handleNew(); // don't bother with error messages, just switch
953+
return false;
954+
955+
} else if (sketch.isUntitled()) {
954956
// The current sketch is empty, just close and start fresh.
955957
// (Otherwise the editor would lose its 'untitled' status.)
956958
handleClose(activeEditor, true);
957959
handleNew();
958960

959961
} else {
960-
// If the current editor contains file extensions that the new mode can handle, then
961-
// write a sketch.properties file with the new mode specified, and reopen.
962-
boolean newModeCanHandleCurrentSource = true;
963-
for (final SketchCode code : sketch.getCode()) {
964-
if (!mode.validExtension(code.getExtension())) {
965-
newModeCanHandleCurrentSource = false;
966-
break;
967-
}
968-
}
969-
if (!newModeCanHandleCurrentSource) {
970-
return false;
971-
} else {
962+
// If the current sketch contains file extensions that the new mode
963+
// can handle, then write a sketch.properties file with the new mode
964+
// specified, and reopen. (Really only useful for Java <-> Android)
965+
//if (isCompatible(sketch, mode)) {
966+
if (mode.canEdit(sketch)) {
972967
final File props = new File(sketch.getCodeFolder(), "sketch.properties");
973968
saveModeSettings(props, nextMode);
974969
handleClose(activeEditor, true);
@@ -980,13 +975,29 @@ public boolean changeMode(Mode mode) {
980975
handleOpen(sketch.getMainFilePath());
981976
return false;
982977
}
978+
} else {
979+
handleNew(); // create a new window with the new Mode
980+
return false;
983981
}
984982
}
985983
}
984+
// Against all (or at least most) odds, we were able to reassign the Mode
986985
return true;
987986
}
988987

989988

989+
/*
990+
private boolean isCompatible(Sketch sketch, Mode mode) {
991+
for (final SketchCode code : sketch.getCode()) {
992+
if (!mode.validExtension(code.getExtension())) {
993+
return false;
994+
}
995+
}
996+
return true;
997+
}
998+
*/
999+
1000+
9901001
private static class ModeInfo {
9911002
public final String title;
9921003
public final String id;

app/src/processing/app/Mode.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,17 @@ public boolean canEdit(final File f) {
949949
return validExtension(f.getName().substring(dot + 1));
950950
}
951951

952+
953+
public boolean canEdit(Sketch sketch) {
954+
for (final SketchCode code : sketch.getCode()) {
955+
if (!validExtension(code.getExtension())) {
956+
return false;
957+
}
958+
}
959+
return true;
960+
}
961+
962+
952963
/**
953964
* Check this extension (no dots, please) against the list of valid
954965
* extensions.

app/src/processing/app/ui/Editor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ public void rebuildModePopup() {
529529
for (final Mode m : base.getModeList()) {
530530
JRadioButtonMenuItem item = new JRadioButtonMenuItem(m.getTitle());
531531
item.addActionListener(e -> {
532+
/*
532533
if (!sketch.isModified()) {
533534
if (!base.changeMode(m)) {
534535
reselectMode();
@@ -540,6 +541,14 @@ public void rebuildModePopup() {
540541
Messages.showWarning("Save",
541542
"Please save the sketch before changing the mode.");
542543
}
544+
*/
545+
//if (sketch.isModified() || !base.changeMode(m)) {
546+
if (!base.changeMode(m)) {
547+
// Returns false if unable to change the mode in this window
548+
// (which will open a new window with the new Mode), in which case
549+
// re-select the menu item b/c Java changes it automatically.
550+
reselectMode();
551+
}
543552
});
544553
modePopup.add(item);
545554
modeGroup.add(item);

todo.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ X set args for JavaFX and OpenGL for the mouse and key events
1717
X make sure that everything is set properly, also for keys
1818
X move ISSUE_TEMPLATE to .github subfolder
1919
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
20+
o ability to switch mode in p5 w/o saving/closing/etc
21+
X trying to save the user from themselves here is just messier than needed
22+
X opt to open a new editor window rather than weird error messages
23+
X https://github.com/processing/processing4/issues/189
2024

2125
readme
2226
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
170174
_ single file thing is long gone
171175
_ introduce the idea of 'scraps' (ala gist) that are just single page blobs
172176
_ launch/psk files/import from web editor (more details below)
173-
_ ability to switch mode in p5 w/o saving/closing/etc
174-
_ trying to save the user from themselves here is just messier than needed
175-
_ https://github.com/processing/processing4/issues/189
176177
_ cleaning up the temp file handling
177178
_ 'show sketch folder' weird when in temp folder
178179
_ ask to save first (sketch has not been saved yet)

0 commit comments

Comments
 (0)