Skip to content

Commit

Permalink
8252863: Spinner keeps spinning if removed from Scene
Browse files Browse the repository at this point in the history
Reviewed-by: angorya, aghaisas
  • Loading branch information
karthikpandelu authored and aghaisas committed Dec 20, 2022
1 parent bac8ee8 commit 9c52605
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
Expand Up @@ -53,7 +53,8 @@ public class SpinnerBehavior<T> extends BehaviorBase<Spinner<T>> {

private boolean isIncrementing = false;

private Timeline timeline;
/* Package-private for testing purposes */
Timeline timeline;

final EventHandler<ActionEvent> spinningKeyFrameEventHandler = event -> {
final SpinnerValueFactory<T> valueFactory = getNode().getValueFactory();
Expand Down Expand Up @@ -158,4 +159,4 @@ public boolean arrowsAreVertical() {
styleClass.contains(Spinner.STYLE_CLASS_ARROWS_ON_RIGHT_HORIZONTAL) ||
styleClass.contains(Spinner.STYLE_CLASS_SPLIT_ARROWS_HORIZONTAL));
}
}
}
Expand Up @@ -81,7 +81,8 @@ public class SpinnerSkin<T> extends SkinBase<Spinner<T>> {
private static final int SPLIT_ARROWS_HORIZONTAL = 5;

private int layoutMode = 0;
private final SpinnerBehavior behavior;
/* Package-private for testing purposes */
final SpinnerBehavior behavior;


/* *************************************************************************
Expand Down Expand Up @@ -249,6 +250,11 @@ public void executeAccessibleAction(AccessibleAction action, Object... parameter
return null;
}
}));

lh.addChangeListener(control.sceneProperty(), (op) -> {
// Stop spinning when sceneProperty is modified
behavior.stopSpinning();
});
}

private boolean isIncDecKeyEvent(KeyEvent ke) {
Expand Down
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package com.sun.javafx.scene.control.behavior;

import javafx.animation.Timeline;

import com.sun.javafx.scene.control.behavior.SpinnerBehavior;

public class SpinnerBehaviorShim {

public static Timeline getTimeline(SpinnerBehavior spinnerBehavior) {
return spinnerBehavior.timeline;
}

}
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package javafx.scene.control.skin;

import javafx.scene.control.skin.SpinnerSkin;

import com.sun.javafx.scene.control.behavior.SpinnerBehavior;

public class SpinnerSkinShim {

public static SpinnerBehavior getSpinnerBehavior(SpinnerSkin spinnerSkin) {
return spinnerSkin.behavior;
}

}
Expand Up @@ -30,11 +30,20 @@
import org.junit.Before;
import org.junit.Test;

import javafx.animation.Animation.Status;
import javafx.geometry.BoundingBox;
import javafx.geometry.Insets;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerShim;
import javafx.scene.control.skin.SpinnerSkin;
import javafx.scene.control.skin.SpinnerSkinShim;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.Scene;
import javafx.stage.Stage;

import com.sun.javafx.scene.control.behavior.SpinnerBehavior;
import com.sun.javafx.scene.control.behavior.SpinnerBehaviorShim;

/**
* Tests for SpinnerSkin
Expand Down Expand Up @@ -149,4 +158,25 @@ public void testSpinnerSkin() {
spinner.setSkin(new SpinnerSkin<>(spinner));
spinner.setSkin(new SpinnerSkin<>(spinner));
}

@Test
public void testSpinnerIncrementOnRemovingFromScene() {
spinner = new Spinner<>(0, 1000, 0);
spinner.setSkin(new SpinnerSkin<>(spinner));

HBox root = new HBox(spinner);
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();

SpinnerBehavior behavior = SpinnerSkinShim.getSpinnerBehavior((SpinnerSkin)spinner.getSkin());
behavior.startSpinning(true);

assertEquals(Status.RUNNING, SpinnerBehaviorShim.getTimeline(behavior).getStatus());
root.getChildren().clear();
assertEquals(Status.STOPPED, SpinnerBehaviorShim.getTimeline(behavior).getStatus());
root.getChildren().setAll(spinner);
assertEquals(Status.STOPPED, SpinnerBehaviorShim.getTimeline(behavior).getStatus());
}
}

0 comments on commit 9c52605

Please sign in to comment.