Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close windows with keyboard shortcuts #34

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## v0.1.5
- Improve behavior of `FXUtils.installSelectAllOrNoneMenu` (https://github.com/qupath/qupath/issues/1498)
- New `FXUtils.addCloseWindowShortcuts` methods to enable closing windows from the keyboard

## v0.1.4
- Make yes/no/cancel dialog non-resizable, for consistency (https://github.com/qupath/qupath-fxtras/issues/26)
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/qupath/fx/utils/FXUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,25 @@
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.stage.WindowEvent;
import org.controlsfx.control.CheckComboBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -556,6 +562,42 @@ public static void simplifyTitledPane(TitledPane pane, boolean boldTitle) {
}
}


/**
* Add shortcuts to close a window using the standard shortcuts (Shortcut+W, Esc).
* @param stage
* @see #addCloseWindowShortcuts(Stage, Collection)
*/
public static void addCloseWindowShortcuts(Stage stage) {
addCloseWindowShortcuts(stage, Arrays.asList(
new KeyCodeCombination(KeyCode.W, KeyCodeCombination.SHORTCUT_DOWN),
new KeyCodeCombination(KeyCode.ESCAPE)
));
}

/**
* Add shortcuts to close a window using the specified key combinations.
* These are applied as key released events, so they will not interfere with text input.
* @param stage
* @param keyCombinations
* @see #addCloseWindowShortcuts(Stage)
* @implSpec this only fires a window close request; any handler may still choose to consume the event quietly
*/
public static void addCloseWindowShortcuts(Stage stage, Collection<? extends KeyCombination> keyCombinations) {
if (keyCombinations.isEmpty()) {
logger.warn("Empty list of close window shortcuts - ignoring request");
return;
}
stage.addEventHandler(KeyEvent.KEY_RELEASED, e -> {
if (e.isConsumed())
return;
if (keyCombinations.stream().anyMatch(kc -> kc.match(e))) {
stage.fireEvent(new WindowEvent(stage, WindowEvent.WINDOW_CLOSE_REQUEST));
}
});
}


/**
* Enable an undecorated stage to be moved by clicking and dragging within it.
* Requires the scene to be set. Note that this will set mouse event listeners.
Expand Down
Loading