Skip to content

Commit d20d6d0

Browse files
akoch-yattaHeikoKlare
authored andcommitted
Extend HiDPI docu for Rectangle & Point.OfFloat
This commit extends the documentation about HiDPI support for the win32 implementation with a section about Point.OfFloat or Rectangle.OfFloat.
1 parent a1cfcd4 commit d20d6d0

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

docs/hidpi-support-for-windows-dev-guide.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,38 @@ The `MultiZoomCoordinateSystemMapper` addresses coordinate ambiguity in dynamic
416416
![MultiZoomCoordinateSystem](images/coordinate_system_with_coordinate_system_mapper.png)
417417
Fig: The representation of the pixel coordinates space and points coordinates space using coordinate mappers
418418

419+
### Usage of Rectangle.OfFloat & Point.OfFloat
420+
421+
Since both the SWT API and the Windows API predominantly use integer values for positions, sizes, and bounds, rounding is generally involved when values are converted to or from the operating system.
422+
If API return values (for example, `Control.getLocation`) are used in calculations and later passed to other API methods (such as `Control.setBounds`), multiple destructive rounding steps can occur. These accumulated roundings may result in final pixel values that are larger or smaller than expected.
423+
To mitigate this effect, most API methods in Windows return internal subclasses `Rectangle.OfFloat` or `Point.OfFloat`, which preserve fractional residuals so that a correct rounding can be applied at a later stage.
424+
To maintain this behavior, it is important to keep and reuse the returned `Point`/`Rectangle` instances instead of replacing them with new ones. The static utility methods provided by `Rectangle` and `Point` help retain as much precision as possible throughout these operations.
425+
426+
**Example**
427+
428+
In the following example, the parent control is resized based on the current size of a child. One possible implementation is to use the `Control.setBounds(int, int, int, int)` API, which converts any potential `Point.OfFloat` inputs and immediately applies rounded integer values.
429+
430+
```java
431+
void resize(Composite parent, Control, child) {
432+
Point childSize = child.getSize();
433+
parent.setBounds(5, 5, childSize.x, childSize.y);
434+
}
435+
```
436+
437+
A better alternative is to use the `Control.setBounds(Rectangle)` API and create the bounds using `Rectangle.of(Point, Point)`. On Windows, this will automatically create a `Rectangle.OfFloat` if any of the provided points is a `Point.OfFloat`, preserving fractional values and improving the precision of subsequent conversions.
438+
439+
```java
440+
void resize(Composite parent, Control, child) {
441+
Point childSize = child.getSize();
442+
Rectangle newBounds = Rectangle.of(new Point(5, 5), childSize);
443+
parent.setBounds(newBounds);
444+
}
445+
```
446+
447+
As a rule of thumb, avoid API methods that take raw integer values when an equivalent overload accepting `Rectangle` or `Point` is available, as these retain precision and reduce cumulative rounding errors.
448+
449+
Be aware that `Rectangle.OfFloat` or `Point.OfFloat` are internal classes which you should not instantiate outside of SWT. There are static utility methods `Rectangle.of` and `Point.of` that will create precise version if necessary as well as implementations of `Rectangle.clone` and `Point.clone` to create precise copies if the original one was precise.
450+
419451
### Process & Thread DPI Awareness
420452

421453
Windows differentiates between process and thread DPI awareness.

0 commit comments

Comments
 (0)