Skip to content

Commit 8f0a7d2

Browse files
authored
Add confirmation dialog before resetting work coordinate zero (#3126)
1 parent 88b114a commit 8f0a7d2

11 files changed

Lines changed: 190 additions & 0 deletions

File tree

ugs-core/src/com/willwinder/universalgcodesender/utils/Settings.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public class Settings {
7575
private boolean verboseOutputEnabled = false;
7676
private boolean commandTableEnabled = false;
7777

78+
// Ask for confirmation before resetting the work coordinate zero
79+
private boolean confirmResetZero = true;
80+
7881
// Sender Settings
7982
private WindowSettings mainWindowSettings = new WindowSettings(0, 0, 640, 520);
8083
private WindowSettings visualizerWindowSettings = new WindowSettings(0, 0, 640, 480);
@@ -317,6 +320,15 @@ public void setScrollWindowEnabled(boolean scrollWindowEnabled) {
317320
changed();
318321
}
319322

323+
public boolean isConfirmResetZero() {
324+
return confirmResetZero;
325+
}
326+
327+
public void setConfirmResetZero(boolean confirmResetZero) {
328+
this.confirmResetZero = confirmResetZero;
329+
changed();
330+
}
331+
320332
public boolean isVerboseOutputEnabled() {
321333
return verboseOutputEnabled;
322334
}

ugs-core/src/resources/MessagesBundle_en_US.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ platform.actions.homing.enabled.tooltip = Use this to perform a homing operation
262262
platform.actions.homing.disabled.tooltip = Homing needs to be enabled in the firmware settings
263263
platform.actions.unlock.tooltip = Clear the alarm state and unlock the machine
264264
platform.actions.softreset.tooltip = Reboot the controller
265+
platform.actions.resetZero.confirm.title = Reset work coordinate zero
266+
platform.actions.resetZero.confirm.message = Are you sure you want to reset the work coordinate zero?
267+
platform.actions.resetZero.confirm.dontAsk = Don't ask again
265268
incomplete.localization.title = Not fully translated
266269
incomplete.localization = Universal Gcode Sender is not fully localized for this language. \nPlease visit https://translate.universalgcodesender.com to see how you can help.
267270
incomplete.localization.doNotShowAgain = Do not show this message again

ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/ResetACoordinateToZeroAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public boolean isEnabled() {
8383

8484
@Override
8585
public void actionPerformed(ActionEvent e) {
86+
if (!ResetZeroConfirmation.confirmResetZero(backend)) {
87+
return;
88+
}
8689
try {
8790
backend.resetCoordinateToZero(A);
8891
} catch (Exception ex) {

ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/ResetBCoordinateToZeroAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public boolean isEnabled() {
8383

8484
@Override
8585
public void actionPerformed(ActionEvent e) {
86+
if (!ResetZeroConfirmation.confirmResetZero(backend)) {
87+
return;
88+
}
8689
try {
8790
backend.resetCoordinateToZero(B);
8891
} catch (Exception ex) {

ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/ResetCCoordinateToZeroAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public boolean isEnabled() {
8383

8484
@Override
8585
public void actionPerformed(ActionEvent e) {
86+
if (!ResetZeroConfirmation.confirmResetZero(backend)) {
87+
return;
88+
}
8689
try {
8790
backend.resetCoordinateToZero(C);
8891
} catch (Exception ex) {

ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/ResetCoordinatesToZeroAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public boolean isEnabled() {
8282

8383
@Override
8484
public void actionPerformed(ActionEvent e) {
85+
if (!ResetZeroConfirmation.confirmResetZero(backend)) {
86+
return;
87+
}
8588
try {
8689
backend.resetCoordinatesToZero();
8790
} catch (Exception ex) {

ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/ResetXCoordinateToZeroAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public boolean isEnabled() {
8484

8585
@Override
8686
public void actionPerformed(ActionEvent e) {
87+
if (!ResetZeroConfirmation.confirmResetZero(backend)) {
88+
return;
89+
}
8790
try {
8891
backend.resetCoordinateToZero(X);
8992
} catch (Exception ex) {

ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/ResetYCoordinateToZeroAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public boolean isEnabled() {
8484

8585
@Override
8686
public void actionPerformed(ActionEvent e) {
87+
if (!ResetZeroConfirmation.confirmResetZero(backend)) {
88+
return;
89+
}
8790
try {
8891
backend.resetCoordinateToZero(Y);
8992
} catch (Exception ex) {

ugs-platform/ugs-platform-ugscore/src/main/java/com/willwinder/ugs/nbp/core/actions/ResetZCoordinateToZeroAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public boolean isEnabled() {
8484

8585
@Override
8686
public void actionPerformed(ActionEvent e) {
87+
if (!ResetZeroConfirmation.confirmResetZero(backend)) {
88+
return;
89+
}
8790
try {
8891
backend.resetCoordinateToZero(Z);
8992
} catch (Exception ex) {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2026 Will Winder
3+
4+
This file is part of Universal Gcode Sender (UGS).
5+
6+
UGS is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
UGS is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with UGS. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package com.willwinder.ugs.nbp.core.actions;
20+
21+
import com.willwinder.universalgcodesender.i18n.Localization;
22+
import com.willwinder.universalgcodesender.model.BackendAPI;
23+
import com.willwinder.universalgcodesender.utils.Settings;
24+
import com.willwinder.universalgcodesender.utils.SettingsFactory;
25+
26+
import javax.swing.BoxLayout;
27+
import javax.swing.JCheckBox;
28+
import javax.swing.JLabel;
29+
import javax.swing.JOptionPane;
30+
import javax.swing.JPanel;
31+
32+
/**
33+
* Shared helper that asks the user to confirm before resetting the work
34+
* coordinate zero. The confirmation can be permanently dismissed through a
35+
* "Don't ask again" checkbox which is persisted to the application settings.
36+
* <p>
37+
* The maintainer requested this behavior in
38+
* <a href="https://github.com/winder/Universal-G-Code-Sender/issues/3040">issue #3040</a>:
39+
* a confirmation box with an option to hide future questions, defaulting to ask.
40+
*/
41+
public final class ResetZeroConfirmation {
42+
43+
private ResetZeroConfirmation() {
44+
}
45+
46+
/**
47+
* Returns {@code true} if the reset-zero action should proceed.
48+
* <p>
49+
* When the user has opted out of the confirmation
50+
* ({@link Settings#isConfirmResetZero()} is {@code false}) this returns
51+
* {@code true} immediately without showing a dialog. Otherwise it shows a
52+
* yes/no confirmation dialog with a "Don't ask again" checkbox; checking the
53+
* box persists the opt-out to the settings.
54+
*
55+
* @param backend the active backend, used to read and persist settings
56+
* @return {@code true} if the user confirmed (or confirmation is disabled),
57+
* {@code false} if the user cancelled
58+
*/
59+
public static boolean confirmResetZero(BackendAPI backend) {
60+
Settings settings = backend.getSettings();
61+
if (settings == null || !settings.isConfirmResetZero()) {
62+
return true;
63+
}
64+
65+
JPanel panel = new JPanel();
66+
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
67+
panel.add(new JLabel(Localization.getString("platform.actions.resetZero.confirm.message")));
68+
JCheckBox dontAsk = new JCheckBox(Localization.getString("platform.actions.resetZero.confirm.dontAsk"));
69+
panel.add(dontAsk);
70+
71+
int result = JOptionPane.showConfirmDialog(
72+
null,
73+
panel,
74+
Localization.getString("platform.actions.resetZero.confirm.title"),
75+
JOptionPane.YES_NO_OPTION,
76+
JOptionPane.QUESTION_MESSAGE);
77+
78+
if (dontAsk.isSelected()) {
79+
settings.setConfirmResetZero(false);
80+
SettingsFactory.saveSettings(settings);
81+
}
82+
83+
return result == JOptionPane.YES_OPTION;
84+
}
85+
}

0 commit comments

Comments
 (0)