-
Notifications
You must be signed in to change notification settings - Fork 221
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
fix: Zoom-in/out using keyboard shortcut not working correctly (#424) #589
base: master
Are you sure you want to change the base?
Changes from all commits
e31fa43
8f82c9d
e9a0f0e
8af8548
d7c6615
409f7f9
0db8640
ac7fa35
7aa46cc
7f494b0
addcd87
fdec9b9
1ccef35
d1e8cc5
92e5663
b143a12
7c651e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,7 @@ | |
import javafx.scene.input.KeyCode; | ||
import javafx.scene.input.KeyCodeCombination; | ||
import javafx.scene.input.KeyCombination; | ||
import javafx.scene.input.KeyEvent; | ||
import javafx.scene.layout.StackPane; | ||
|
||
/** | ||
|
@@ -209,7 +210,11 @@ public class MenuBarController { | |
private MenuItem showSampleControllerMenuItem; | ||
@FXML | ||
private Menu zoomMenu; | ||
|
||
|
||
private ZoomInActionController zoomInController; | ||
|
||
private ZoomOutActionController zoomOutController; | ||
|
||
// Modify | ||
@FXML | ||
private MenuItem fitToParentMenuItem; | ||
|
@@ -1274,22 +1279,36 @@ private void handleOnActionMenu(MenuItem i) { | |
*/ | ||
|
||
final static double[] scalingTable = {0.25, 0.50, 0.75, 1.00, 1.50, 2.0, 4.0}; | ||
|
||
private void updateZoomMenu() { | ||
final double[] scalingTable = {0.25, 0.50, 0.75, 1.00, 1.50, 2.0, 4.0}; | ||
|
||
zoomInController = new ZoomInActionController(); | ||
final MenuItem zoomInMenuItem = new MenuItem(I18N.getString("menu.title.zoom.in")); | ||
zoomInMenuItem.setUserData(new ZoomInActionController()); | ||
zoomInMenuItem.setAccelerator(new KeyCharacterCombination("+", modifier)); //NOI18N | ||
zoomInMenuItem.setUserData(zoomInController); | ||
zoomMenu.getItems().add(zoomInMenuItem); | ||
|
||
|
||
zoomOutController = new ZoomOutActionController(); | ||
final MenuItem zoomOutMenuItem = new MenuItem(I18N.getString("menu.title.zoom.out")); | ||
zoomOutMenuItem.setUserData(new ZoomOutActionController()); | ||
zoomOutMenuItem.setAccelerator(new KeyCharacterCombination("/", modifier)); //NOI18N | ||
zoomOutMenuItem.setUserData(zoomOutController); | ||
zoomMenu.getItems().add(zoomOutMenuItem); | ||
|
||
if (EditorPlatform.IS_MAC) { | ||
zoomInMenuItem.setAccelerator(new KeyCharacterCombination("+", modifier)); // NOI18N | ||
zoomOutMenuItem.setAccelerator(new KeyCharacterCombination("-", modifier)); // NOI18N | ||
// Neither KeyCode.MINUS or KeyCode.SUBTRACT seem to work on macOS. | ||
// No chance to test on keyboard with num key pad yet. | ||
Comment on lines
+1296
to
+1299
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is definitely a strange behavior 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you reproduce this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I can reproduce this on mac. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be this requires a different approach depending on the keyboard type. Are you using a separate keyboard or numpad? I've just an old laptop available. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update 2024 and JavaFX 23.0.1,
works just fine on macOS |
||
} else { | ||
// Accelerators for standard keyboard (not num key pad) | ||
zoomInMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.PLUS, modifier)); // NOI18N | ||
zoomOutMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.MINUS, modifier)); // NOI18N | ||
// Does not work on Windows: | ||
// zoomInMenuItem.setAccelerator(new KeyCharacterCombination("+", modifier)); // NOI18N | ||
// | ||
// Works in Windows: | ||
// zoomOutMenuItem.setAccelerator(new KeyCharacterCombination("-", modifier)); // NOI18N | ||
} | ||
zoomMenu.getItems().add(new SeparatorMenuItem()); | ||
|
||
for (int i = 0; i < scalingTable.length; i++) { | ||
final double scaling = scalingTable[i]; | ||
final String title = String.format("%.0f%%", scaling * 100); //NOI18N | ||
|
@@ -1299,7 +1318,61 @@ private void updateZoomMenu() { | |
} | ||
} | ||
|
||
|
||
private void runActionController(MenuItemController controllerToRun, KeyEvent event) { | ||
if (controllerToRun.canPerform()) { | ||
controllerToRun.perform(); | ||
} | ||
event.consume(); | ||
} | ||
|
||
/** | ||
* Provides handling for additional keyboard accelerators assigned to document view | ||
* zoom in/out operation. Zoom is controllable via [+]/[-] keys on standard keyboard | ||
* and on numerical key pad. | ||
* | ||
* Additionally zoom in via arrow keys (up/right) and zoom out (left/down) is possible. | ||
* | ||
* @param event {@link KeyEvent} If any action is triggered, the event will be consumed. | ||
*/ | ||
public void handleAdditionalZoomAccelerators(KeyEvent event) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// For unknown reasons, on macOS 12.0.1 with Java 17/JavaFX 19 the "-" key code | ||
// is not properly accepted hence this handling here. | ||
if (EditorPlatform.IS_MAC && "-".equals(event.getText())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
runActionController(zoomOutController, event); | ||
return; | ||
} | ||
|
||
if (EditorPlatform.IS_MAC && "+".equals(event.getText())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
runActionController(zoomInController, event); | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is also now special handling of |
||
|
||
if (event.isShiftDown()) { | ||
handleArrowKeysForZoom(event); | ||
} else { | ||
handleNumPadKeys(event); | ||
} | ||
} | ||
|
||
private void handleNumPadKeys(KeyEvent event) { | ||
switch (event.getCode()) { | ||
case ADD -> runActionController(zoomInController, event); | ||
case SUBTRACT -> runActionController(zoomOutController, event); | ||
default -> { /* no action*/ } | ||
} | ||
} | ||
|
||
private void handleArrowKeysForZoom(KeyEvent event) { | ||
switch (event.getCode()) { | ||
case RIGHT -> runActionController(zoomInController, event); | ||
case UP -> runActionController(zoomInController, event); | ||
case LEFT -> runActionController(zoomOutController, event); | ||
case DOWN -> runActionController(zoomOutController, event); | ||
default -> { /* no action*/ } | ||
} | ||
return; | ||
} | ||
|
||
private static int findZoomScaleIndex(double zoomScale) { | ||
int result = -1; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove unneeded if-else,
new KeyCodeCombination(KeyCode.PLUS, modifier)
andnew KeyCodeCombination(KeyCode.MINUS, modifier)
work fine on macOS with JavaFX 23.0.1