Skip to content

Commit b14cd24

Browse files
committed
SNTUI: Fix inconsistent handling of UI states
1 parent 21adf17 commit b14cd24

4 files changed

Lines changed: 46 additions & 23 deletions

File tree

‎src/main/java/sc/fiji/snt/SNT.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,24 +1040,25 @@ protected synchronized void saveFill() {
10401040
fillerSet.clear();
10411041
pathAndFillManager.getLoadedFills().clear();
10421042
fillerThreadPool = null;
1043-
changeUIState(SNTUI.WAITING_TO_START_PATH);
1043+
changeUIState(tracingHalted ? SNTUI.TRACING_PAUSED : SNTUI.WAITING_TO_START_PATH);
10441044
if (getUI() != null)
10451045
getUI().getFillManager().changeState(FillManagerUI.State.READY);
10461046
}
10471047

10481048
protected synchronized void discardFill() {
10491049
if (fillerSet.isEmpty()) {
10501050
SNTUtils.log("No Fill(s) to discard...");
1051+
return;
10511052
}
10521053
for (FillerThread filler : fillerSet) {
10531054
removeThreadToDraw(filler);
10541055
}
10551056
fillerSet.clear();
10561057
pathAndFillManager.getLoadedFills().clear();
10571058
fillerThreadPool = null;
1058-
changeUIState(SNTUI.WAITING_TO_START_PATH);
1059-
if (getUI() != null)
1060-
getUI().getFillManager().changeState(FillManagerUI.State.READY);
1059+
changeUIState(tracingHalted ? SNTUI.TRACING_PAUSED : SNTUI.WAITING_TO_START_PATH);
1060+
if (getUI() != null)
1061+
getUI().getFillManager().changeState(FillManagerUI.State.READY);
10611062
}
10621063

10631064
protected synchronized void stopFilling() {

‎src/main/java/sc/fiji/snt/SNTUI.java‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,8 @@ private void updateRebuildCanvasButton() {
915915
private interface UIState {
916916
void enter();
917917
int getStateId();
918-
}
918+
default void exit() {}
919+
}
919920

920921
// State registry
921922
private final Map<Integer, UIState> states = new HashMap<>();
@@ -950,9 +951,15 @@ private void initializeStates() {
950951
*/
951952
public void changeState(final int newState) {
952953
if (newState == currentState || plugin == null) return; // plugin may be null when exiting
954+
955+
// Call exit() on current state
956+
UIState oldState = states.get(currentState);
957+
if (oldState != null) oldState.exit();
958+
953959
currentState = newState;
954960

955961
SwingUtilities.invokeLater(() -> {
962+
if (currentState != newState) return; // Verify state hasn't changed since we queued this
956963
UIState state = states.get(newState);
957964
if (state != null) {
958965
state.enter();
@@ -1087,11 +1094,37 @@ public int getStateId() {
10871094
}
10881095

10891096
private class RunningCmdState implements UIState {
1097+
private static final String[] BRAILLE = {"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"};
1098+
private static final String[] SPINNER_FRAMES;
1099+
static {
1100+
SPINNER_FRAMES = new String[BRAILLE.length];
1101+
for (int i = 0; i < BRAILLE.length; i++) {
1102+
SPINNER_FRAMES[i] = "<html><strong>Running Command... <span style='font-size:1.1em'>"
1103+
+ BRAILLE[i] + " " + BRAILLE[i] + " " + BRAILLE[i] + "</span></strong></html>";
1104+
}
1105+
}
1106+
Timer runningAnimationTimer;
1107+
private int spinnerIndex = 0;
1108+
10901109
@Override
10911110
public void enter() {
10921111
updateStatusText("Running Command...");
10931112
disableEverything();
1094-
}
1113+
spinnerIndex = 0;
1114+
runningAnimationTimer = new Timer(150, e -> {
1115+
spinnerIndex = (spinnerIndex + 1) % SPINNER_FRAMES.length;
1116+
statusText.setText(SPINNER_FRAMES[spinnerIndex]);
1117+
});
1118+
runningAnimationTimer.start();
1119+
}
1120+
1121+
@Override
1122+
public void exit() {
1123+
if (runningAnimationTimer != null) {
1124+
runningAnimationTimer.stop();
1125+
runningAnimationTimer = null;
1126+
}
1127+
}
10951128

10961129
@Override
10971130
public int getStateId() {
@@ -4284,7 +4317,7 @@ protected void abortCurrentOperation() {// FIXME: MOVE TO SNT?
42844317
}
42854318
}
42864319
if (activeWorker != null && !activeWorker.isDone()) activeWorker.kill();
4287-
changeState(WAITING_TO_START_PATH);
4320+
changeState(plugin.tracingHalted ? TRACING_PAUSED : WAITING_TO_START_PATH);
42884321
}
42894322

42904323
protected void launchSigmaPaletteAround(final int x, final int y) {

‎src/main/java/sc/fiji/snt/gui/DemoRunner.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,11 @@ private void error(final Throwable ex) {
448448
}
449449

450450
private void exit() {
451-
ui.changeState(priorUIState);
452-
ui.showStatus(null, true);
451+
if (priorUIState == SNTUI.TRACING_PAUSED && ui.getState() != SNTUI.TRACING_PAUSED) {
452+
return; // Don't restore TRACING_PAUSED if we're no longer in that state
453+
}
454+
ui.changeState(priorUIState);
455+
ui.showStatus(null, true);
453456
}
454457

455458
public Demo getChoice() {

‎src/main/java/sc/fiji/snt/gui/GuiUtils.java‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -434,20 +434,6 @@ public int getNextMatch(final String prefix, final int startIndex, final Positio
434434
});
435435
list.setSelectedIndex(defIdx);
436436
list.ensureIndexIsVisible(defIdx);
437-
list.setCellRenderer(new DefaultListCellRenderer() {
438-
439-
private static final long serialVersionUID = -810009340376431810L;
440-
441-
@Override
442-
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
443-
boolean cellHasFocus) {
444-
final Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
445-
if (isSelected)
446-
c.setFont(c.getFont().deriveFont(Font.BOLD));
447-
return c;
448-
}
449-
} );
450-
451437
ta.setText(descriptions[defIdx]);
452438
ta.setCaretPosition(0);
453439
ta.setBackground(list.getBackground());

0 commit comments

Comments
 (0)