Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/SVG.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum SVG {
ARROW_FORWARD("M16.175 13H4V11H16.175L10.575 5.4 12 4 20 12 12 20 10.575 18.6 16.175 13Z"),
BETA_CIRCLE("M15,10.5C15,11.3 14.3,12 13.5,12C14.3,12 15,12.7 15,13.5V15A2,2 0 0,1 13,17H9V7H13A2,2 0 0,1 15,9V10.5M13,15V13H11V15H13M13,11V9H11V11H13M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4Z"), // Not Material
CANCEL("M8.4 17 12 13.4 15.6 17 17 15.6 13.4 12 17 8.4 15.6 7 12 10.6 8.4 7 7 8.4 10.6 12 7 15.6 8.4 17ZM12 22Q9.925 22 8.1 21.2125T4.925 19.075Q3.575 17.725 2.7875 15.9T2 12Q2 9.925 2.7875 8.1T4.925 4.925Q6.275 3.575 8.1 2.7875T12 2Q14.075 2 15.9 2.7875T19.075 4.925Q20.425 6.275 21.2125 8.1T22 12Q22 14.075 21.2125 15.9T19.075 19.075Q17.725 20.425 15.9 21.2125T12 22ZM12 20Q15.35 20 17.675 17.675T20 12Q20 8.65 17.675 6.325T12 4Q8.65 4 6.325 6.325T4 12Q4 15.35 6.325 17.675T12 20ZM12 12Z"),
CATEGORY("M6.5 11L12 2l5.5 9zm11 11q-1.875 0-3.187-1.312T13 17.5t1.313-3.187T17.5 13t3.188 1.313T22 17.5t-1.312 3.188T17.5 22M3 21.5v-8h8v8zM17.5 20q1.05 0 1.775-.725T20 17.5t-.725-1.775T17.5 15t-1.775.725T15 17.5t.725 1.775T17.5 20M5 19.5h4v-4H5zM10.05 9h3.9L12 5.85zm7.45 8.5"),
CHAT("M6 14H14V12H6V14ZM6 11H18V9H6V11ZM6 8H18V6H6V8ZM2 22V4Q2 3.175 2.5875 2.5875T4 2H20Q20.825 2 21.4125 2.5875T22 4V16Q22 16.825 21.4125 17.4125T20 18H6L2 22ZM5.15 16H20V4H4V17.125L5.15 16ZM4 16V4 16Z"),
CHECK("M9.55 18 3.85 12.3 5.275 10.875 9.55 15.15 18.725 5.975 20.15 7.4 9.55 18Z"),
CHECKROOM("M3 20Q2.575 20 2.2875 19.7125T2 19Q2 18.75 2.1 18.5375T2.4 18.2L11 11.75V10Q11 9.575 11.3 9.2875T12.025 9Q12.65 9 13.075 8.55T13.5 7.475Q13.5 6.85 13.0625 6.425T12 6Q11.375 6 10.9375 6.4375T10.5 7.5H8.5Q8.5 6.05 9.525 5.025T12 4Q13.45 4 14.475 5.0125T15.5 7.475Q15.5 8.65 14.8125 9.575T13 10.85V11.75L21.6 18.2Q21.8 18.325 21.9 18.5375T22 19Q22 19.425 21.7125 19.7125T21 20H3ZM6 18H18L12 13.5 6 18Z"),
Expand Down
143 changes: 33 additions & 110 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/construct/LineSelectButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,30 @@
*/
package org.jackhuang.hmcl.ui.construct;

import com.jfoenix.controls.JFXPopup;
import javafx.beans.InvalidationListener;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.collections.ObservableList;
import javafx.css.PseudoClass;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.*;
import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.ui.SVG;
import org.jackhuang.hmcl.util.javafx.MappedObservableList;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;

import static org.jackhuang.hmcl.ui.FXUtils.determineOptimalPopupPosition;

/// @author Glavo
public final class LineSelectButton<T> extends LineButton {

private static final String DEFAULT_STYLE_CLASS = "line-select-button";
private static final PseudoClass SELECTED_PSEUDO_CLASS = PseudoClass.getPseudoClass("selected");

private JFXPopup popup;
private final SelectPopup<T> selectPopup;

public LineSelectButton() {
this.getStyleClass().add(DEFAULT_STYLE_CLASS);

this.selectPopup = new SelectPopup<>();

InvalidationListener updateTrailingText = observable -> {
T value = getValue();
if (value != null) {
Expand All @@ -67,150 +57,83 @@ public LineSelectButton() {

ripplerContainer.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
if (event.getButton() == MouseButton.SECONDARY) {
if (popup != null)
popup.hide();
if (selectPopup.isShowing()) {
selectPopup.hide();
}
event.consume();
}
});

ripplerContainer.addEventFilter(ScrollEvent.ANY, ignored -> selectPopup.hide());

selectPopup.showingProperty().addListener((observable, oldValue, newValue) ->
ripplerContainer.getRippler().setRipplerDisabled(newValue));
}

@Override
public void fire() {
super.fire();
if (popup == null) {
PopupMenu popupMenu = new PopupMenu();
this.popup = new JFXPopup(popupMenu);

ripplerContainer.addEventFilter(ScrollEvent.ANY, ignored -> popup.hide());

Bindings.bindContent(popupMenu.getContent(), MappedObservableList.create(itemsProperty(), item -> {
VBox vbox = new VBox();

var itemTitleLabel = new Label();
itemTitleLabel.getStyleClass().add("title-label");
itemTitleLabel.textProperty().bind(Bindings.createStringBinding(() -> {
if (item == null)
return "";

Function<T, String> converter = getConverter();
return converter != null ? converter.apply(item) : Objects.toString(item, "");
}, converterProperty()));

var itemSubtitleLabel = new Label();
itemSubtitleLabel.getStyleClass().add("subtitle-label");
itemSubtitleLabel.textProperty().bind(Bindings.createStringBinding(() -> {
Function<T, String> descriptionConverter = getDescriptionConverter();
return descriptionConverter != null ? descriptionConverter.apply(item) : "";
}, descriptionConverterProperty()));

FXUtils.onChangeAndOperate(itemSubtitleLabel.textProperty(), text -> {
if (text == null || text.isEmpty()) {
vbox.getChildren().setAll(itemTitleLabel);
} else {
vbox.getChildren().setAll(itemTitleLabel, itemSubtitleLabel);
}
});

var wrapper = new StackPane(vbox);
wrapper.setAlignment(Pos.CENTER_LEFT);
wrapper.getStyleClass().add("menu-container");
wrapper.setMouseTransparent(true);
RipplerContainer ripplerContainer = new RipplerContainer(wrapper);
FXUtils.onClicked(ripplerContainer, () -> {
setValue(item);
popup.hide();
});

FXUtils.onChangeAndOperate(valueProperty(),
value -> wrapper.pseudoClassStateChanged(SELECTED_PSEUDO_CLASS, Objects.equals(value, item)));

return ripplerContainer;
}));

popup.showingProperty().addListener((observable, oldValue, newValue) ->
ripplerContainer.getRippler().setRipplerDisabled(newValue));
}

if (popup.isShowing()) {
popup.hide();
if (selectPopup.isShowing()) {
selectPopup.hide();
} else {
JFXPopup.PopupVPosition vPosition = determineOptimalPopupPosition(this, popup);
popup.show(this, vPosition, JFXPopup.PopupHPosition.RIGHT,
0,
vPosition == JFXPopup.PopupVPosition.TOP ? this.getHeight() : -this.getHeight(),
true);
selectPopup.showRelativeTo(this);
}
}

private final ObjectProperty<T> value = new SimpleObjectProperty<>(this, "value");

public ObjectProperty<T> valueProperty() {
return value;
return selectPopup.valueProperty();
}

public T getValue() {
return valueProperty().get();
return selectPopup.getValue();
}

public void setValue(T value) {
valueProperty().set(value);
selectPopup.setValue(value);
}

private final ObjectProperty<Function<T, String>> converter = new SimpleObjectProperty<>(this, "converter");

public ObjectProperty<Function<T, String>> converterProperty() {
return converter;
return selectPopup.converterProperty();
}

public Function<T, String> getConverter() {
return converterProperty().get();
return selectPopup.getConverter();
}

public void setConverter(Function<T, String> value) {
converterProperty().set(value);
selectPopup.setConverter(value);
}

private ObjectProperty<Function<T, String>> descriptionConverter;

public ObjectProperty<Function<T, String>> descriptionConverterProperty() {
if (descriptionConverter == null)
descriptionConverter = new SimpleObjectProperty<>(this, "descriptionConverter");
return descriptionConverter;
return selectPopup.descriptionConverterProperty();
}

public Function<T, String> getDescriptionConverter() {
return descriptionConverterProperty().get();
return selectPopup.getDescriptionConverter();
}

public void setDescriptionConverter(Function<T, String> value) {
descriptionConverterProperty().set(value);
selectPopup.setDescriptionConverter(value);
}

private final ListProperty<T> items = new SimpleListProperty<>(this, "items", FXCollections.emptyObservableList());

public ListProperty<T> itemsProperty() {
return items;
return selectPopup.itemsProperty();
}

public ObservableList<T> getItems() {
return selectPopup.getItems();
}

public void setItems(ObservableList<T> value) {
itemsProperty().set(value);
selectPopup.setItems(value);
}

public void setItems(Collection<T> value) {
if (value instanceof ObservableList<T> observableList) {
this.setItems(observableList);
} else {
this.setItems(FXCollections.observableArrayList(value));
}
selectPopup.setItems(value);
}

@SafeVarargs
public final void setItems(T... values) {
this.setItems(FXCollections.observableArrayList(values));
selectPopup.setItems(values);
}

public ObservableList<T> getItems() {
return items.get();
}

}
Loading