File choice fixes

This commit is contained in:
crschnick 2024-10-02 18:54:33 +00:00
parent 1924bd1644
commit 48523690b7
3 changed files with 77 additions and 13 deletions

View file

@ -29,6 +29,7 @@ import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.List;
import java.util.function.BiConsumer;
@ -38,9 +39,11 @@ import java.util.function.Supplier;
public class BrowserChooserComp extends DialogComp {
private final Stage stage;
private final BrowserFileChooserModel model;
public BrowserChooserComp(BrowserFileChooserModel model) {
public BrowserChooserComp(Stage stage, BrowserFileChooserModel model) {
this.stage = stage;
this.model = model;
}
@ -49,7 +52,7 @@ public class BrowserChooserComp extends DialogComp {
PlatformThread.runLaterIfNeeded(() -> {
var model = new BrowserFileChooserModel(OpenFileSystemModel.SelectionMode.SINGLE_FILE);
DialogComp.showWindow(save ? "saveFileTitle" : "openFileTitle", stage -> {
var comp = new BrowserChooserComp(model);
var comp = new BrowserChooserComp(stage, model);
comp.apply(struc -> struc.get().setPrefSize(1200, 700))
.apply(struc -> AppFont.normal(struc.get()))
.styleClass("browser")
@ -77,6 +80,7 @@ public class BrowserChooserComp extends DialogComp {
@Override
protected void finish() {
stage.close();
model.finishChooser();
}

View file

@ -0,0 +1,64 @@
package io.xpipe.app.fxcomps.impl;
import io.xpipe.app.fxcomps.Comp;
import io.xpipe.app.fxcomps.CompStructure;
import io.xpipe.app.fxcomps.SimpleCompStructure;
import io.xpipe.app.fxcomps.util.PlatformThread;
import javafx.beans.property.Property;
import javafx.collections.FXCollections;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.util.Callback;
import java.util.List;
import java.util.Objects;
public class ComboTextFieldComp extends Comp<CompStructure<ComboBox<String>>> {
private final Property<String> value;
private final List<String> predefinedValues;
private final Callback<ListView<String>, ListCell<String>> customCellFactory;
public ComboTextFieldComp(Property<String> value, List<String> predefinedValues, Callback<ListView<String>, ListCell<String>> customCellFactory) {
this.value = value;
this.predefinedValues = predefinedValues;
this.customCellFactory = customCellFactory;
}
@Override
public CompStructure<ComboBox<String>> createBase() {
var text = new ComboBox<>(FXCollections.observableList(predefinedValues));
text.setEditable(true);
text.setMaxWidth(2000);
text.setValue(value.getValue() != null ? value.getValue() : null);
text.valueProperty().addListener((c, o, n) -> {
value.setValue(n != null && n.length() > 0 ? n : null);
});
value.addListener((c, o, n) -> {
PlatformThread.runLaterIfNeeded(() -> {
// Check if control value is the same. Then don't set it as that might cause bugs
if (Objects.equals(text.getValue(), n) || (n == null && text.getValue().isEmpty())) {
return;
}
text.setValue(n);
});
});
if (customCellFactory != null) {
text.setCellFactory(customCellFactory);
}
text.setOnKeyPressed(ke -> {
if (ke.getCode().equals(KeyCode.ENTER)) {
text.getScene().getRoot().requestFocus();
}
ke.consume();
});
return new SimpleCompStructure<>(text);
}
}

View file

@ -8,6 +8,7 @@ import io.xpipe.app.fxcomps.Comp;
import io.xpipe.app.fxcomps.CompStructure;
import io.xpipe.app.fxcomps.SimpleCompStructure;
import io.xpipe.app.fxcomps.augment.GrowAugment;
import io.xpipe.app.fxcomps.util.PlatformThread;
import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.storage.ContextualFileReference;
@ -144,10 +145,8 @@ public class ContextualFileReferenceChoiceComp extends Comp<CompStructure<HBox>>
}
private Comp<?> createComboBox() {
var combo = new ComboBox<String>();
combo.setEditable(true);
combo.setMaxWidth(2000);
combo.setCellFactory(param -> {
var items = previousFileReferences.stream().map(previousFileReference -> previousFileReference.getPath().toString()).toList();
var combo = new ComboTextFieldComp(filePath, items, param -> {
return new ListCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
@ -165,13 +164,10 @@ public class ContextualFileReferenceChoiceComp extends Comp<CompStructure<HBox>>
}
};
});
previousFileReferences.forEach(previousFileReference -> {
combo.getItems().add(previousFileReference.getPath().toString());
});
HBox.setHgrow(combo, Priority.ALWAYS);
combo.getStyleClass().add(Styles.LEFT_PILL);
GrowAugment.create(false, true).augment(combo);
return Comp.of(() -> combo);
combo.hgrow();
combo.styleClass(Styles.LEFT_PILL);
combo.grow(false, true);
return combo;
}
private Comp<?> createTextField() {