Skip to content

Add keyboard zoom support (Cmd + +/-) for SWT Browser on macOS to match Windows Ctrl+MouseWheel behavior #3036

@tobiasmelcher

Description

@tobiasmelcher

Zooming inside the SWT Browser control does not work on macOS (e.g., in an “element info popup” or similar contexts). Users expect consistent cross-platform behavior:

On Windows: Ctrl + MouseWheel zooms browser content in/out.
On macOS: No equivalent functionality exists (Cmd + MouseWheel or Cmd + +/- does not zoom).

This inconsistency causes a poor user experience for macOS users, where standard zoom shortcuts like Cmd + + (zoom in) and Cmd + - (zoom out) are expected to work.

Proposed Solution
Add a macOS-specific KeyListener to the Browser to intercept Cmd + + and Cmd + -, then apply a WebKit-compatible CSS transform via JavaScript to scale content if javascript support is enabled.

Could following code snippet be somehow integrated in the macOS specific implementation of the Browser control class?

if (Util.isMac()) {
			browser.addKeyListener(new KeyAdapter() {
				double zoomLevel = 1.0;
 
				public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
					try {
						KeyStroke increaseSizeKeyStroke = org.eclipse.jface.bindings.keys.KeyStroke.getInstance("M1+="); //$NON-NLS-1$
						KeyStroke decreaseSizeKeyStroke = org.eclipse.jface.bindings.keys.KeyStroke.getInstance("M1+-"); //$NON-NLS-1$
						int accelerator = SWTKeySupport.convertEventToUnmodifiedAccelerator(e);
						KeyStroke stroke = SWTKeySupport.convertAcceleratorToKeyStroke(accelerator);
						if (decreaseSizeKeyStroke.equals(stroke)) {
							this.zoomLevel -= 0.1;
							zoom();
							e.doit = false;
						} else if (increaseSizeKeyStroke.equals(stroke)) {
							this.zoomLevel += 0.1;
							zoom();
							e.doit = false;
						}
					} catch (ParseException e1) {
						e1.printStackTrace();
					}
				}
 
				private void zoom() {
					browser.execute("document.body.style.webkitTransform = 'scale(" + this.zoomLevel + ")';" //$NON-NLS-1$ //$NON-NLS-2$
							+ "document.body.style.webkitTransformOrigin = '0 0';"); //$NON-NLS-1$
				}
			});
		}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions