Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8320359: ImageView: add styleable fitWidth, fitHeight, preserveRatio, smooth properties #1293

Closed
Expand Up @@ -1882,11 +1882,34 @@ <h4><a id="imageview">ImageView</a></h4>
</thead>
<tbody>
<tr>
<th class="propertyname" scope="row">-fx-image</th>
<th class="propertyname" scope="row">-fx-fit-height</th>
<td class="value"><a href="#typenumber" class="typelink">&lt;number&gt;</a></td>
<td>0</td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you don't specify a style class for the newly added properties, they will all be left justified. The existing null value for -fx-image specifies class="default" which centers it. I recommend either adding the class="default" to all of the newly added properties, or removing it from the default value of the existing fx-image property.

I'll reapprove if you want to make the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you! I am going to add class="default" as it allows for styling of the default column.

Two notes:

  • There are a few other places where class="default" is omitted (HBox for instance). We may want to fix it in a separate PR.
  • I don't think the center alignment is the right choice here, I would rather see this column left aligned. Perhaps we should fix the .default class as part of the enhancement.

<td>The height of the bounding box within which the source image is resized as necessary to fit.</td>
</tr>
<tr>
<th class="propertyname" scope="row">-fx-fit-width</th>
<td class="value"><a href="#typenumber" class="typelink">&lt;number&gt;</a></td>
<td>0</td>
<td>The width of the bounding box within which the source image is resized as necessary to fit.</td>
</tr>
<th class="propertyname" scope="row">-fx-image</th>
<td class="value"><a href="#typeurl" class="typelink">&lt;uri&gt;</a></td>
<td class="default">null</td>
<td>Relative URLs are resolved against the URL of the stylesheet.</td>
</tr>
<tr>
<th class="propertyname" scope="row">-fx-preserve-ratio</th>
<td class="value"><a href="#typeboolean" class="typelink">&lt;boolean&gt;</a></td>
<td>false</td>
<td>Indicates whether to preserve the aspect ratio of the source image when scaling to fit the image within the fitting bounding box.</td>
</tr>
<tr>
<th class="propertyname" scope="row">-fx-smooth</th>
<td class="value"><a href="#typeboolean" class="typelink">&lt;boolean&gt;</a></td>
<td>Platform-specific</td>
<td>Indicates whether to use a better quality filtering algorithm or a faster one when transforming or scaling the source image to fit.</td>
</tr>
<tr>
<th colspan="4" class="parents" scope="row">Also has all properties of <a href="#node">Node</a></th>
</tr>
Expand Down
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2023, 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.css;

import java.util.List;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
import javafx.scene.Node;
import com.sun.javafx.UnmodifiableArrayList;

/**
* Utility methods for dealing with CSS.
*/
public final class CssUtil {

private CssUtil() {
}

/**
* Utility method which combines {@code CssMetaData} items in one immutable list.
* <p>
* The intended usage is to combine the parent and the child {@code CssMetaData} for
* the purposes of {@code getClassCssMetaData()} method, see for example {@link Node#getClassCssMetaData()}.
* <p>
* Example:
* <pre>{@code
* private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES = CssMetaData.combine(
* <Parent>.getClassCssMetaData(),
* STYLEABLE1,
* STYLEABLE2
* );
* }</pre>
* This method returns an instance of a {@code List} that implements
* {@link java.util.RandomAccess} interface.
*
* @param inheritedFromParent the {@code CssMetaData} items inherited from parent, must not be null
* @param items the additional items
* @return the immutable list containing all of the items
*/
// NOTE: this should be a public utility, see https://bugs.openjdk.org/browse/JDK-8320796
public static List<CssMetaData<? extends Styleable, ?>> combine(
List<CssMetaData<? extends Styleable, ?>> inheritedFromParent,
CssMetaData<? extends Styleable, ?>... items)
{
CssMetaData[] combined = new CssMetaData[inheritedFromParent.size() + items.length];
inheritedFromParent.toArray(combined);
System.arraycopy(items, 0, combined, inheritedFromParent.size(), items.length);
return new UnmodifiableArrayList<>(combined, combined.length);
}
}
167 changes: 119 additions & 48 deletions modules/javafx.graphics/src/main/java/javafx/scene/image/ImageView.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2023, 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
Expand All @@ -25,31 +25,39 @@

package javafx.scene.image;

import com.sun.javafx.beans.event.AbstractNotifyListener;
import com.sun.javafx.css.StyleManager;
import javafx.css.converter.URLConverter;
import com.sun.javafx.geom.BaseBounds;
import com.sun.javafx.geom.transform.BaseTransform;
import com.sun.javafx.scene.DirtyBits;
import com.sun.javafx.scene.ImageViewHelper;
import com.sun.javafx.scene.NodeHelper;
import com.sun.javafx.sg.prism.NGImageView;
import com.sun.javafx.sg.prism.NGNode;
import com.sun.javafx.tk.Toolkit;
import java.util.List;
import javafx.beans.DefaultProperty;
import javafx.beans.Observable;
import javafx.beans.property.*;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.DoublePropertyBase;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ObjectPropertyBase;
import javafx.beans.property.StringProperty;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
import javafx.css.StyleableBooleanProperty;
import javafx.css.StyleableDoubleProperty;
import javafx.css.StyleableProperty;
import javafx.css.StyleableStringProperty;
import javafx.css.converter.BooleanConverter;
import javafx.css.converter.SizeConverter;
import javafx.css.converter.URLConverter;
import javafx.geometry.NodeOrientation;
import javafx.geometry.Rectangle2D;
import javafx.scene.AccessibleRole;
import javafx.scene.Node;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.sun.javafx.beans.event.AbstractNotifyListener;
import com.sun.javafx.css.CssUtil;
import com.sun.javafx.css.StyleManager;
import com.sun.javafx.geom.BaseBounds;
import com.sun.javafx.geom.transform.BaseTransform;
import com.sun.javafx.scene.DirtyBits;
import com.sun.javafx.scene.ImageViewHelper;
import com.sun.javafx.scene.NodeHelper;
import com.sun.javafx.sg.prism.NGImageView;
import com.sun.javafx.sg.prism.NGNode;
import com.sun.javafx.tk.Toolkit;

/**
* The {@code ImageView} is a {@code Node} used for painting images loaded with
Expand Down Expand Up @@ -408,8 +416,7 @@ public String getName() {
*
* @defaultValue 0
*/
private DoubleProperty fitWidth;

private StyleableDoubleProperty fitWidth;

public final void setFitWidth(double value) {
fitWidthProperty().set(value);
Expand All @@ -421,8 +428,7 @@ public final double getFitWidth() {

public final DoubleProperty fitWidthProperty() {
if (fitWidth == null) {
fitWidth = new DoublePropertyBase() {

fitWidth = new StyleableDoubleProperty() {
@Override
protected void invalidated() {
invalidateWidthHeight();
Expand All @@ -439,6 +445,11 @@ public Object getBean() {
public String getName() {
return "fitWidth";
}

@Override
public CssMetaData<? extends Styleable, Number> getCssMetaData() {
return StyleableProperties.FIT_WIDTH;
}
};
}
return fitWidth;
Expand All @@ -456,8 +467,7 @@ public String getName() {
*
* @defaultValue 0
*/
private DoubleProperty fitHeight;

private StyleableDoubleProperty fitHeight;

public final void setFitHeight(double value) {
fitHeightProperty().set(value);
Expand All @@ -469,8 +479,7 @@ public final double getFitHeight() {

public final DoubleProperty fitHeightProperty() {
if (fitHeight == null) {
fitHeight = new DoublePropertyBase() {

fitHeight = new StyleableDoubleProperty() {
@Override
protected void invalidated() {
invalidateWidthHeight();
Expand All @@ -487,6 +496,11 @@ public Object getBean() {
public String getName() {
return "fitHeight";
}

@Override
public CssMetaData<? extends Styleable, Number> getCssMetaData() {
return StyleableProperties.FIT_HEIGHT;
}
};
}
return fitHeight;
Expand Down Expand Up @@ -521,8 +535,7 @@ public String getName() {
*
* @defaultValue false
*/
private BooleanProperty preserveRatio;

private StyleableBooleanProperty preserveRatio;

public final void setPreserveRatio(boolean value) {
preserveRatioProperty().set(value);
Expand All @@ -534,8 +547,7 @@ public final boolean isPreserveRatio() {

public final BooleanProperty preserveRatioProperty() {
if (preserveRatio == null) {
preserveRatio = new BooleanPropertyBase() {

preserveRatio = new StyleableBooleanProperty() {
@Override
protected void invalidated() {
invalidateWidthHeight();
Expand All @@ -552,6 +564,11 @@ public Object getBean() {
public String getName() {
return "preserveRatio";
}

@Override
public CssMetaData<? extends Styleable, Boolean> getCssMetaData() {
return StyleableProperties.PRESERVE_RATIO;
}
};
}
return preserveRatio;
Expand All @@ -573,8 +590,7 @@ public String getName() {
*
* @defaultValue platform-dependent
*/
private BooleanProperty smooth;

private StyleableBooleanProperty smooth;

public final void setSmooth(boolean value) {
smoothProperty().set(value);
Expand All @@ -586,8 +602,7 @@ public final boolean isSmooth() {

public final BooleanProperty smoothProperty() {
if (smooth == null) {
smooth = new BooleanPropertyBase(SMOOTH_DEFAULT) {

smooth = new StyleableBooleanProperty(SMOOTH_DEFAULT) {
@Override
protected void invalidated() {
NodeHelper.markDirty(ImageView.this, DirtyBits.NODE_SMOOTH);
Expand All @@ -602,6 +617,11 @@ public Object getBean() {
public String getName() {
return "smooth";
}

@Override
public CssMetaData<? extends Styleable, Boolean> getCssMetaData() {
return StyleableProperties.SMOOTH;
}
};
}
return smooth;
Expand Down Expand Up @@ -804,16 +824,38 @@ private boolean doComputeContains(double localX, double localY) {

private static final String DEFAULT_STYLE_CLASS = "image-view";

/*
* Super-lazy instantiation pattern from Bill Pugh.
*/
private static class StyleableProperties {
// TODO
// "preserve-ratio","smooth","viewport","fit-width","fit-height"
private static final CssMetaData<ImageView, String> IMAGE =
new CssMetaData<>("-fx-image",
URLConverter.getInstance()) {
private static class StyleableProperties {
private static final CssMetaData<ImageView, Number> FIT_HEIGHT =
new CssMetaData<>("-fx-fit-height", SizeConverter.getInstance(), 0.0)
{
@Override
public boolean isSettable(ImageView n) {
return n.fitHeight == null || !n.fitHeight.isBound();
}

@Override
public StyleableProperty<Number> getStyleableProperty(ImageView n) {
return (StyleableProperty<Number>)n.fitHeightProperty();
}
};

private static final CssMetaData<ImageView, Number> FIT_WIDTH =
new CssMetaData<>("-fx-fit-width", SizeConverter.getInstance(), 0.0)
{
@Override
public boolean isSettable(ImageView n) {
return n.fitWidth == null || !n.fitWidth.isBound();
}

@Override
public StyleableProperty<Number> getStyleableProperty(ImageView n) {
return (StyleableProperty<Number>)n.fitWidthProperty();
}
};

private static final CssMetaData<ImageView, String> IMAGE =
new CssMetaData<>("-fx-image", URLConverter.getInstance())
{
@Override
public boolean isSettable(ImageView n) {
// Note that we care about the image, not imageUrl
Expand All @@ -826,13 +868,42 @@ public StyleableProperty<String> getStyleableProperty(ImageView n) {
}
};

private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
static {
final List<CssMetaData<? extends Styleable, ?>> styleables =
new ArrayList<>(Node.getClassCssMetaData());
styleables.add(IMAGE);
STYLEABLES = Collections.unmodifiableList(styleables);
}
private static final CssMetaData<ImageView, Boolean> PRESERVE_RATIO =
new CssMetaData<>("-fx-preserve-ratio", BooleanConverter.getInstance(), Boolean.FALSE)
{
@Override
public boolean isSettable(ImageView n) {
return n.preserveRatio == null || !n.preserveRatio.isBound();
}

@Override
public StyleableProperty<Boolean> getStyleableProperty(ImageView n) {
return (StyleableProperty<Boolean>)n.preserveRatioProperty();
}
};

private static final CssMetaData<ImageView, Boolean> SMOOTH =
new CssMetaData<>("-fx-smooth", BooleanConverter.getInstance(), SMOOTH_DEFAULT)
{
@Override
public boolean isSettable(ImageView n) {
return n.smooth == null || !n.smooth.isBound();
}

@Override
public StyleableProperty<Boolean> getStyleableProperty(ImageView n) {
return (StyleableProperty<Boolean>)n.smoothProperty();
}
};

private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES = CssUtil.combine(
Node.getClassCssMetaData(),
FIT_HEIGHT,
FIT_WIDTH,
IMAGE,
PRESERVE_RATIO,
SMOOTH
);
}

/**
Expand Down