|
14 | 14 | package org.eclipse.jface.window; |
15 | 15 |
|
16 | 16 | import java.util.ArrayList; |
| 17 | +import java.util.concurrent.atomic.AtomicReference; |
17 | 18 |
|
18 | 19 | import org.eclipse.core.runtime.Assert; |
19 | 20 | import org.eclipse.jface.resource.JFaceResources; |
@@ -433,6 +434,53 @@ public void create() { |
433 | 434 | //initialize the bounds of the shell to that appropriate for the |
434 | 435 | // contents |
435 | 436 | initializeBounds(); |
| 437 | + hookZoomChangeListenerForDefaultSizedDialogs(); |
| 438 | + } |
| 439 | + |
| 440 | + /* |
| 441 | + * There is no linear relation between font size and containing control size |
| 442 | + * scaled according to some zoom value. In consequence, text with a scaled font |
| 443 | + * size may not fit into a control/shell with a size scaled by the same zoom. To |
| 444 | + * adhere for this in case of Windows using the default computed size, ensure |
| 445 | + * that on zoom change the new proper size is computed and applied. This will |
| 446 | + * not affect Windows that have a custom size, either programmatically or |
| 447 | + * manually applied. |
| 448 | + */ |
| 449 | + private void hookZoomChangeListenerForDefaultSizedDialogs() { |
| 450 | + boolean hasCustomSize = !shell.getSize().equals(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true)); |
| 451 | + boolean isEmpty = shell.getChildren().length == 0; |
| 452 | + if (resizeHasOccurred || hasCustomSize || isEmpty) { |
| 453 | + return; |
| 454 | + } |
| 455 | + |
| 456 | + AtomicReference<Listener> customResizeListener = new AtomicReference<>(); |
| 457 | + Listener adaptDefaultSizeListener = event -> { |
| 458 | + if (shell == null || shell.isDisposed()) { |
| 459 | + return; |
| 460 | + } |
| 461 | + Point size = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); |
| 462 | + shell.setSize(size); |
| 463 | + }; |
| 464 | + AtomicReference<Listener> zoomChangeListener = new AtomicReference<>(); |
| 465 | + zoomChangeListener.set(event -> { |
| 466 | + if (shell == null || shell.isDisposed()) { |
| 467 | + return; |
| 468 | + } |
| 469 | + shell.addListener(SWT.Resize, adaptDefaultSizeListener); |
| 470 | + shell.removeListener(SWT.Resize, customResizeListener.get()); |
| 471 | + shell.getChildren()[0].removeListener(SWT.ZoomChanged, zoomChangeListener.get()); |
| 472 | + }); |
| 473 | + customResizeListener.set(event -> { |
| 474 | + if (shell == null || shell.isDisposed()) { |
| 475 | + return; |
| 476 | + } |
| 477 | + shell.getChildren()[0].removeListener(SWT.ZoomChanged, zoomChangeListener.get()); |
| 478 | + }); |
| 479 | + |
| 480 | + shell.getChildren()[0].addListener(SWT.ZoomChanged, zoomChangeListener.get()); |
| 481 | + // If custom resize happens before first zoom change, the dialog is custom sized |
| 482 | + // and should not be auto-resized on zoom change |
| 483 | + shell.addListener(SWT.Resize, customResizeListener.get()); |
436 | 484 | } |
437 | 485 |
|
438 | 486 | /** |
|
0 commit comments