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

fix: Zoom-in/out using keyboard shortcut not working correctly (#424) #589

Open
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

Oliver-Loeffler
Copy link
Collaborator

@Oliver-Loeffler Oliver-Loeffler commented Oct 19, 2022

Zoom-In/Out was supposed to work with keyboard shortcuts.
For Zoom-In MODIFIER++ and for Zoom-Out MODIFIER+/ were defined.
The predefined KeyCombinations were not working, there seems something wrong with how KeyCodes are handled especially on macOS but also on Windows. This might become an issue for OpenJFX which is a separate thing.

I've played a while with suitable settings which work on Win/Mac/Linux and settled with following idea:

Platform(s) Accelerator Action Preview App (with GER keyb. layout)
WIndows, Linux CTRL++ Zoom In ok
WIndows, Linux CTRL+- Zoom Out ok
WIndows, Linux CTRL++ (on num key pad) Zoom In ok
WIndows, Linux CTRL+- (on num key pad) Zoom Out ok
macOS 12.0.1 CMD++ Zoom In ok
macOS 12.0.1 CMD+- Zoom Out ok
macOS 12.0.1 CMD++ (on num key pad) Zoom In ok
macOS 12.0.1 CMD+- (on num key pad) Zoom Out ok
Platform(s) Accelerator Action Preview App (any keyb. layout with cursors)
WIndows, Linux SHIFT+CTRL+up Zoom In ok
WIndows, Linux SHIFT+CTRL+right Zoom In ok
WIndows, Linux SHIFT+CTRL+left Zoom Out ok
WIndows, Linux SHIFT+CTRL+down Zoom Out ok
macOS 12.0.1 SHIFT+CMD+up Zoom In ok
macOS 12.0.1 SHIFT+CMD+right Zoom In ok
macOS 12.0.1 SHIFT+CMD+left Zoom Out ok
macOS 12.0.1 SHIFT+CMD+down Zoom Out ok

The DocumentWindowController offers a mainKeyEventFilter where those double accelerators can be tracked.
All key handling happens in MenuBarController but this is called from mainKeyEventFilter in DocumentWindowController.

ZoomControls_PlusMinus

ZoomControls_Arrows

Issue

Fixes #424

Progress

@abhinayagarwal
Copy link
Collaborator

abhinayagarwal commented Mar 24, 2023

Testing on Mac OS 13.2.1

Preview app

both CMD + = and CMD + SHIFT + = zooms in
both CMD + - and CMD + SHIFT + - zooms out

SceneBuilder app

CMD + = does nothing.
CMD + SHIFT + = zooms in
CMD + - zooms out
CMD + SHIFT + - zooms all out at once

@Oliver-Loeffler
Copy link
Collaborator Author

I will review this and complement the table above with our findings. Actually I've tested the above table on an older x86 Macbook with MacOS 10.12.

Comment on lines +1291 to +1294
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely a strange behavior 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you reproduce this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can reproduce this on mac.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update 2024 and JavaFX 23.0.1,

zoomInMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.PLUS, modifier));
zoomOutMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.MINUS, modifier));

works just fine on macOS

@abhinayagarwal
Copy link
Collaborator

@Oliver-Loeffler If you need any help, feel free to ping me. It would be nice to have this PR merged before we merge #619 and release SB 20.

@Oliver-Loeffler
Copy link
Collaborator Author

Oliver-Loeffler commented Mar 30, 2023 via email

@Oliver-Loeffler
Copy link
Collaborator Author

Oliver-Loeffler commented Mar 30, 2023

Well I just learned that Zooming was also a thing for the Preview. Never thought about this one, just worked on the Zoom function for the editor window. Indeed, I'll update this accordingly.

I have tested the key strokes as described in the table above.
With the keys you have posted, I have no zooming at all for preview.

Testing on Mac OS 12.0.1

  • Preview app

    • both CMD+= and CMD+SHIFT+= zooms in -> cannot reproduce
    • both CMD+- and CMD+SHIFT+- zooms out -> cannot reproduce
  • SceneBuilder app

    • CMD+= does nothing. -> I have no = key -> cannot reproduce
    • CMD+SHIFT+= zooms in -> does nothing
    • CMD+- zooms out -> yes, confirm, can reproduce
    • CMD+SHIFT+- zooms all out at once -> yes, confirm, can reproduce

This behavior was actually the reason why I've implemented SHIFT+CMD+right and SHIFT+CMD+up for Zoom-In and SHIFT+CMD+left and SHIFT+CMD+down for Zoom-Out.

I'll take an image of my keyboard - may be this will help to figure out whats off. Unfortunately I have no numpad for my Mac, so I cannot test this.

if (EditorPlatform.IS_MAC && "+".equals(event.getText())) {
runActionController(zoomInController, event);
return;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also now special handling of CMD++ for MacOS in the MenuBar controller. Could you please try this @abhinayagarwal ?

@Oliver-Loeffler
Copy link
Collaborator Author

Well, the preview window zoom seems to be a different thing, actually I would take care for this in a different PR. I've got this somehow working but not sure if this is desired.

We can couple the preview zoom to the workspace zoom or we can design it a way that preview and workspace can operate with independent zoom levels. Key code / menmoic assignments can stay the same, which part of the program than receives the zoom instruction is the decided by window selection.

@Oliver-Loeffler
Copy link
Collaborator Author

I will not get this one completed before end of next week. As I started digging into this topic I found, that the proposed key combinations using + or - (or even =) are not available for many keyboard layouts. My proposal here is to use more generic combinations which are available on most layouts, such as CMD+up for zoom-in and CMD+down for zoom-out. I've also tested this PR using an external Apple keyboard with numpad and it works as expected - given one has a german key layout. This is the point, pre-configured keys for zoom will break or work depending on the keyboard layout used.

Eventually its even thinkable to make those key combinations configurable. As of now, I have not yet found how to detect which keyboard layout is effectively used for a system. If this would be detectable, one could generate key combinations for certain layouts and a generic combination which fits all.

On the other hand, it seems that zoom is actually not yet implemented for the preview window. If this works in some cases, then by accident. This should be solved by a separate PR.

What do you think about this @abhinayagarwal ?

@abhinayagarwal abhinayagarwal added this to the 22 milestone Sep 25, 2023
@Oliver-Loeffler Oliver-Loeffler modified the milestones: 22, 24, 23 Aug 26, 2024
@Oliver-Loeffler Oliver-Loeffler changed the title fix: Zoom-in/out using keyboard shortcut not working correctly fix: Zoom-in/out using keyboard shortcut not working correctly (#424) Oct 1, 2024
@jperedadnr jperedadnr self-requested a review October 30, 2024 09:46
Copy link
Collaborator

@jperedadnr jperedadnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on macOS, and it looks good. I've left a few comments.

*
* @param event {@link KeyEvent} If any action is triggered, the event will be consumed.
*/
public void handleAdditionalZoomAccelerators(KeyEvent event) {
Copy link
Collaborator

@jperedadnr jperedadnr Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleAdditionalZoomAccelerators is public API, so it should check if the modifier is down (any future use of this API else where will fail otherwise if there is no check):

    public void handleAdditionalZoomAccelerators(KeyEvent event) {
        if (event == null || !(EditorPlatform.IS_MAC ? event.isMetaDown() : event.isControlDown())) {
            return;
        }
        ...
}
``

Comment on lines +1291 to +1294
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update 2024 and JavaFX 23.0.1,

zoomInMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.PLUS, modifier));
zoomOutMenuItem.setAccelerator(new KeyCodeCombination(KeyCode.MINUS, modifier));

works just fine on macOS

zoomMenu.getItems().add(zoomOutMenuItem);

if (EditorPlatform.IS_MAC) {
Copy link
Collaborator

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) and new KeyCodeCombination(KeyCode.MINUS, modifier) work fine on macOS with JavaFX 23.0.1

return;
}

if (EditorPlatform.IS_MAC && "+".equals(event.getText())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KeyCode.PLUS == event.getCode() works fine

public void handleAdditionalZoomAccelerators(KeyEvent event) {
// 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())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KeyCode.MINUS == event.getCode() works fine

@Oliver-Loeffler Oliver-Loeffler modified the milestones: 24, 25 Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Zoom-in/out using keyboard shortcut not working correctly
3 participants