On Thu, 15 Jan 2026 03:11:11 GMT, Michael Strauß <[email protected]> wrote:
> In the following program, resizing the window to be smaller than 300px should
> start a transition for `-fx-scale-x` from 2 to 1, but instead the value
> sometimes snaps back to 1 instantly:
>
>
> public class InterruptedTransitionBug extends Application {
> @Override
> public void start(Stage stage) throws Exception {
> var root = new StackPane(new Button("My Button"));
> var scene = new Scene(root, 600, 200);
> scene.getStylesheets().add("data:text/css;base64," +
> Base64.getEncoder().encodeToString("""
> .button {
> transition: -fx-scale-x 2s;
> }
>
> @media (width > 300px) {
> .button {
> -fx-scale-x: 2;
> }
> }
> """.getBytes(StandardCharsets.UTF_8)));
> stage.setScene(scene);
> stage.show();
> }
> }
>
>
> ### Problem
> First, let's look at how `CssStyleHelper` works when the cascading style map
> for a node changes:
> 1. When the style map for a node has changed,
> `StyleHelper.createStyleHelper(Node)` determines that the style helper is no
> longer any good, and a new `CssStyleHelper` for the new style map is created.
> 2. Before the old style helper is discarded, it calls
> `CssStyleHelper.resetToInitialValues(Styleable)` and resets all properties
> that were set by this style helper back to their initial values. This ensures
> that the new style helper has a "clean slate" to work from.
> 3. The next `Node.applyCss()` pass will now set the properties that are
> specified in the new style map to their new values.
>
> However, when transitions come into play, this mechanism can break:
> 1. In our example, the `Button` starts with a style map that contains two
> properties:
> * `transition: -fx-scale-x 2s`
> * `-fx-scale-x: 2`
> 2. Due to a changing media query, this is replaced with a style map that only
> contains a single entry:
> * `transition: -fx-scale-x 2s`
> 3. Before the new style map is applied, all properties affected by the old
> style map are reset to their initial values. That means:
> * `-fx-scale-x` is reset to `1`
> * `transition` is reset to `null`
>
> This is where it breaks: if `transition` is reset before `-fx-scale-x`, the
> latter will see no transition when its value is set back to `1`.
>
> ### Solution
> Simply resetting all properties back to their initial values when a style map
> is changed is not necessary. We only need to reset the properties that are
> set by the old style map, but are no longer set by the new style map. In our
> example, si...
Without this PR, the behavior is even stranger: if the window contains two
buttons, to reducing the width below the threshold causes the first button to
snap to the original size, but the second button continues the transition:
<img width="396" height="612" alt="Screenshot 2026-01-15 at 10 59 16"
src="https://github.com/user-attachments/assets/6e40af3d-f662-4491-8935-0d569203fe92"
/>
-------------
PR Comment: https://git.openjdk.org/jfx/pull/2038#issuecomment-3756403388