|
| 1 | +// Copy from https://github.com/sshahine/JFoenix/blob/d427fd801a338f934307ba41ce604eb5c79f0b20/jfoenix/src/main/java/com/jfoenix/skins/JFXListViewSkin.java |
| 2 | + |
| 3 | +/* |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | +package com.jfoenix.skins; |
| 22 | + |
| 23 | +import com.jfoenix.adapters.skins.ListViewSkin; |
| 24 | +import com.jfoenix.controls.JFXListView; |
| 25 | +import com.jfoenix.effects.JFXDepthManager; |
| 26 | +import javafx.scene.control.ListCell; |
| 27 | +import javafx.scene.control.skin.VirtualFlow; |
| 28 | +import javafx.scene.layout.Region; |
| 29 | + |
| 30 | +// https://github.com/HMCL-dev/HMCL/issues/4720 |
| 31 | +public class JFXListViewSkin<T> extends ListViewSkin<T> { |
| 32 | + |
| 33 | + private final VirtualFlow<ListCell<T>> flow; |
| 34 | + |
| 35 | + @SuppressWarnings("unchecked") |
| 36 | + public JFXListViewSkin(final JFXListView<T> listView) { |
| 37 | + super(listView); |
| 38 | + flow = (VirtualFlow<ListCell<T>>) getChildren().get(0); |
| 39 | + JFXDepthManager.setDepth(flow, listView.depthProperty().get()); |
| 40 | + listView.depthProperty().addListener((o, oldVal, newVal) -> JFXDepthManager.setDepth(flow, newVal)); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) { |
| 45 | + return 200; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) { |
| 50 | + final int itemsCount = getSkinnable().getItems().size(); |
| 51 | + if (getSkinnable().maxHeightProperty().isBound() || itemsCount <= 0) { |
| 52 | + return super.computePrefHeight(width, topInset, rightInset, bottomInset, leftInset); |
| 53 | + } |
| 54 | + |
| 55 | + final double fixedCellSize = getSkinnable().getFixedCellSize(); |
| 56 | + double computedHeight = fixedCellSize != Region.USE_COMPUTED_SIZE ? |
| 57 | + fixedCellSize * itemsCount + snapVerticalInsets() : estimateHeight(); |
| 58 | + double height = super.computePrefHeight(width, topInset, rightInset, bottomInset, leftInset); |
| 59 | + if (height > computedHeight) { |
| 60 | + height = computedHeight; |
| 61 | + } |
| 62 | + |
| 63 | + if (getSkinnable().getMaxHeight() > 0 && computedHeight > getSkinnable().getMaxHeight()) { |
| 64 | + return getSkinnable().getMaxHeight(); |
| 65 | + } |
| 66 | + |
| 67 | + return height; |
| 68 | + } |
| 69 | + |
| 70 | + private double estimateHeight() { |
| 71 | + // compute the border/padding for the list |
| 72 | + double borderWidth = snapVerticalInsets(); |
| 73 | + // compute the gap between list cells |
| 74 | + |
| 75 | + JFXListView<T> listview = (JFXListView<T>) getSkinnable(); |
| 76 | + double gap = listview.isExpanded() ? ((JFXListView<T>) getSkinnable()).getVerticalGap() * (getSkinnable().getItems() |
| 77 | + .size()) : 0; |
| 78 | + // compute the height of each list cell |
| 79 | + double cellsHeight = 0; |
| 80 | + for (int i = 0; i < flow.getCellCount(); i++) { |
| 81 | + ListCell<T> cell = flow.getCell(i); |
| 82 | + cellsHeight += cell.getHeight(); |
| 83 | + } |
| 84 | + return cellsHeight + gap + borderWidth; |
| 85 | + } |
| 86 | + |
| 87 | + private double snapVerticalInsets() { |
| 88 | + return getSkinnable().snappedBottomInset() + getSkinnable().snappedTopInset(); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments