RFR: 8358450: Viewport characteristics media features [v20]
Markus Mack
mmack at openjdk.org
Fri Jan 9 10:34:58 UTC 2026
On Fri, 9 Jan 2026 02:51:10 GMT, Michael Strauß <mstrauss at openjdk.org> wrote:
>> Implementation of [viewport characteristics media features](https://www.w3.org/TR/mediaqueries-5/#mf-viewport-characteristics):
>> * `width`
>> * `height`
>> * `aspect-ratio`: width / height
>> * `orientation`: `portrait`, `landscape`
>> * `display-mode`: `fullscreen`, `standalone` (note: `browser` and `minimal-ui` are not supported in JavaFX)
>>
>> Here's a small example how the new media features can be used:
>>
>> @Override
>> public void start(Stage stage) {
>> var button = new Button("Toggle full-screen");
>> button.setOnAction(_ -> stage.setFullScreen(!stage.isFullScreen()));
>> var label = new Label();
>> var root = new BorderPane(button, null, null, label, null);
>> var scene = new Scene(root, 650, 200);
>> BorderPane.setAlignment(label, Pos.CENTER);
>> label.textProperty().bind(scene.widthProperty().map(v -> String.format("Width: %.2f", v.doubleValue())));
>> scene.getStylesheets().add("data:text/css;base64," + Base64.getEncoder().encodeToString("""
>> @media (max-width: 500) {
>> .button {
>> -fx-background-color: red;
>> }
>> }
>>
>> @media (600 < width <= 700) {
>> .button {
>> -fx-background-color: green;
>> }
>> }
>>
>> @media (min-width: 800) {
>> .button {
>> -fx-background-color: yellow;
>> }
>> }
>>
>> @media (display-mode: fullscreen) {
>> .button {
>> -fx-background-color: black !important;
>> }
>> }
>> """.getBytes(StandardCharsets.UTF_8)));
>>
>> stage.initStyle(StageStyle.DECORATED);
>> stage.setScene(scene);
>> stage.show();
>> }
>
> Michael Strauß has updated the pull request incrementally with one additional commit since the last revision:
>
> update copyright years
I reviewed this PR's code and left some minor comments. The largest part is an implementation of complex media query expressions, which looks very solid and well-maintainable.
Manual testing on Windows 11 looks good as well as long as there are no CSS transitions.
In combination with CSS transitions I see some irregularities, e.g. consider this in a resizable window with two buttons:
.button {
transition: -fx-scale-x 2s;
}
@media (width > 300px) {
.button {
-fx-scale-x: 2;
}
}
When horizontally resizing the window around the width threshold, sometimes the transition jumps or even affects one of the buttons differently than the other.
Is such a configuration supposed to be supported?
modules/javafx.graphics/src/main/docs/javafx/scene/doc-files/cssref.html line 880:
> 878: operators and two values. For example:<br>
> 879: <code>
> 880: (400px >= width >= 600px)<br>
This should be `(400px <= width <= 600px)`
modules/javafx.graphics/src/main/java/com/sun/javafx/css/media/MediaQuery.java line 55:
> 53: * @return the context awareness flags
> 54: */
> 55: int getContextAwareness();
Have you considered using an EnumSet here for stronger typing?
-------------
PR Review: https://git.openjdk.org/jfx/pull/1844#pullrequestreview-3643179694
PR Review Comment: https://git.openjdk.org/jfx/pull/1844#discussion_r2675525845
PR Review Comment: https://git.openjdk.org/jfx/pull/1844#discussion_r2675527995
More information about the openjfx-dev
mailing list