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.Priority;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.List; import java.util.List;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@ -38,9 +39,11 @@ import java.util.function.Supplier;
public class BrowserChooserComp extends DialogComp { public class BrowserChooserComp extends DialogComp {
private final Stage stage;
private final BrowserFileChooserModel model; private final BrowserFileChooserModel model;
public BrowserChooserComp(BrowserFileChooserModel model) { public BrowserChooserComp(Stage stage, BrowserFileChooserModel model) {
this.stage = stage;
this.model = model; this.model = model;
} }
@ -49,7 +52,7 @@ public class BrowserChooserComp extends DialogComp {
PlatformThread.runLaterIfNeeded(() -> { PlatformThread.runLaterIfNeeded(() -> {
var model = new BrowserFileChooserModel(OpenFileSystemModel.SelectionMode.SINGLE_FILE); var model = new BrowserFileChooserModel(OpenFileSystemModel.SelectionMode.SINGLE_FILE);
DialogComp.showWindow(save ? "saveFileTitle" : "openFileTitle", stage -> { 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)) comp.apply(struc -> struc.get().setPrefSize(1200, 700))
.apply(struc -> AppFont.normal(struc.get())) .apply(struc -> AppFont.normal(struc.get()))
.styleClass("browser") .styleClass("browser")
@ -77,6 +80,7 @@ public class BrowserChooserComp extends DialogComp {
@Override @Override
protected void finish() { protected void finish() {
stage.close();
model.finishChooser(); 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.CompStructure;
import io.xpipe.app.fxcomps.SimpleCompStructure; import io.xpipe.app.fxcomps.SimpleCompStructure;
import io.xpipe.app.fxcomps.augment.GrowAugment; import io.xpipe.app.fxcomps.augment.GrowAugment;
import io.xpipe.app.fxcomps.util.PlatformThread;
import io.xpipe.app.issue.ErrorEvent; import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.app.prefs.AppPrefs; import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.storage.ContextualFileReference; import io.xpipe.app.storage.ContextualFileReference;
@ -144,10 +145,8 @@ public class ContextualFileReferenceChoiceComp extends Comp<CompStructure<HBox>>
} }
private Comp<?> createComboBox() { private Comp<?> createComboBox() {
var combo = new ComboBox<String>(); var items = previousFileReferences.stream().map(previousFileReference -> previousFileReference.getPath().toString()).toList();
combo.setEditable(true); var combo = new ComboTextFieldComp(filePath, items, param -> {
combo.setMaxWidth(2000);
combo.setCellFactory(param -> {
return new ListCell<>() { return new ListCell<>() {
@Override @Override
protected void updateItem(String item, boolean empty) { protected void updateItem(String item, boolean empty) {
@ -165,13 +164,10 @@ public class ContextualFileReferenceChoiceComp extends Comp<CompStructure<HBox>>
} }
}; };
}); });
previousFileReferences.forEach(previousFileReference -> { combo.hgrow();
combo.getItems().add(previousFileReference.getPath().toString()); combo.styleClass(Styles.LEFT_PILL);
}); combo.grow(false, true);
HBox.setHgrow(combo, Priority.ALWAYS); return combo;
combo.getStyleClass().add(Styles.LEFT_PILL);
GrowAugment.create(false, true).augment(combo);
return Comp.of(() -> combo);
} }
private Comp<?> createTextField() { private Comp<?> createTextField() {