diff options
| author | 2021-04-06 14:58:54 +0200 | |
|---|---|---|
| committer | 2021-04-06 14:59:54 +0200 | |
| commit | 41f009e6787a374a8f0c786099fd2c5aa5be8cb8 (patch) | |
| tree | 4b3cf4562ad189d4aad075012c18bcab31959eed | |
| parent | Make progress dialog thread-safe. (diff) | |
| download | enigma-41f009e6787a374a8f0c786099fd2c5aa5be8cb8.tar.gz enigma-41f009e6787a374a8f0c786099fd2c5aa5be8cb8.tar.xz enigma-41f009e6787a374a8f0c786099fd2c5aa5be8cb8.zip | |
Fix progress bar being very tall.
Closes #366.
| -rw-r--r-- | enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java index 3773b8a0..7110fc86 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java | |||
| @@ -45,35 +45,52 @@ public class ProgressDialog implements ProgressListener, AutoCloseable { | |||
| 45 | .fill(GridBagConstraints.BOTH) | 45 | .fill(GridBagConstraints.BOTH) |
| 46 | .weight(1.0, 0.0); | 46 | .weight(1.0, 0.0); |
| 47 | 47 | ||
| 48 | pane.add(this.labelTitle, cb.pos(0, 0).build()); | ||
| 49 | pane.add(this.labelText, cb.pos(0, 1).build()); | ||
| 50 | pane.add(this.progress, cb.pos(0, 2).weight(1.0, 1.0).build()); | ||
| 51 | |||
| 48 | // Set label text since otherwise the label height is 0, which makes the | 52 | // Set label text since otherwise the label height is 0, which makes the |
| 49 | // window size get set incorrectly | 53 | // window size get set incorrectly |
| 50 | this.labelTitle.setText("Idle"); | 54 | this.labelTitle.setText("Idle"); |
| 51 | this.labelText.setText("Idle"); | 55 | this.labelText.setText("Idle"); |
| 52 | this.progress.setPreferredSize(ScaleUtil.getDimension(0, 30)); | 56 | this.progress.setPreferredSize(ScaleUtil.getDimension(0, 20)); |
| 53 | |||
| 54 | pane.add(this.labelTitle, cb.pos(0, 0).build()); | ||
| 55 | pane.add(this.labelText, cb.pos(0, 1).build()); | ||
| 56 | // set padding because otherwise the progress bar gets cut off when | ||
| 57 | // using the Darkula theme | ||
| 58 | pane.add(this.progress, cb.pos(0, 2).weight(1.0, 1.0).padding(10).build()); | ||
| 59 | 57 | ||
| 60 | // show the frame | 58 | // show the frame |
| 59 | this.dialog.setResizable(false); | ||
| 60 | this.reposition(); | ||
| 61 | this.dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); | ||
| 62 | } | ||
| 63 | |||
| 64 | // This tries to set the window size to the smallest it can be vertically, | ||
| 65 | // and 400 units in width. | ||
| 66 | // Gets called twice, including after the window opens to try to fix the | ||
| 67 | // window size (more specifically, the progress bar size) being smaller when | ||
| 68 | // the dialog opens for the very first time compared to afterwards. (#366) | ||
| 69 | private void reposition() { | ||
| 61 | this.dialog.pack(); | 70 | this.dialog.pack(); |
| 62 | Dimension size = this.dialog.getSize(); | 71 | Dimension size = this.dialog.getSize(); |
| 63 | this.dialog.setMinimumSize(size); | 72 | this.dialog.setMinimumSize(size); |
| 64 | size.width = ScaleUtil.scale(400); | 73 | size.width = ScaleUtil.scale(400); |
| 65 | this.dialog.setSize(size); | 74 | this.dialog.setSize(size); |
| 66 | 75 | ||
| 67 | this.dialog.setResizable(false); | 76 | this.dialog.setLocationRelativeTo(this.dialog.getParent()); |
| 68 | this.dialog.setLocationRelativeTo(parent); | ||
| 69 | this.dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); | ||
| 70 | this.dialog.setVisible(true); | ||
| 71 | } | 77 | } |
| 72 | 78 | ||
| 73 | public static CompletableFuture<Void> runOffThread(final JFrame parent, final ProgressRunnable runnable) { | 79 | public static CompletableFuture<Void> runOffThread(final JFrame parent, final ProgressRunnable runnable) { |
| 74 | return CompletableFuture.supplyAsync(() -> { | 80 | return CompletableFuture.supplyAsync(() -> { |
| 75 | ProgressDialog progress = new ProgressDialog(parent); | 81 | ProgressDialog progress = new ProgressDialog(parent); |
| 82 | |||
| 83 | // Somehow opening the dialog, disposing it, then reopening it | ||
| 84 | // and then repositioning it fixes the size issues detailed above | ||
| 85 | // most of the time. | ||
| 86 | // Using setVisible(false) instead of dispose() does not work as | ||
| 87 | // well. | ||
| 88 | // Don't ask me why. | ||
| 89 | progress.dialog.setVisible(true); | ||
| 90 | progress.dialog.dispose(); | ||
| 76 | progress.dialog.setVisible(true); | 91 | progress.dialog.setVisible(true); |
| 92 | progress.reposition(); | ||
| 93 | |||
| 77 | return progress; | 94 | return progress; |
| 78 | }, SwingUtilities::invokeLater).thenAcceptAsync(progress -> { | 95 | }, SwingUtilities::invokeLater).thenAcceptAsync(progress -> { |
| 79 | // TODO use "try (progress)" with Java 9 | 96 | // TODO use "try (progress)" with Java 9 |