Fix bug in preferences where panels would be empty

This commit is contained in:
0xb00bface 2022-05-28 17:57:35 +02:00
parent e3b54845ff
commit 5dc5083150
3 changed files with 56 additions and 62 deletions

View File

@ -1,6 +1,7 @@
4.7.9
========================
* Fix stream resolution detection for Camsoda
* Fix bug in settings where panels would be empty
4.7.8
========================

View File

@ -1,7 +1,5 @@
package ctbrec.ui.settings.api;
import static java.util.Optional.*;
import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
@ -9,33 +7,36 @@ import javafx.scene.control.TextInputControl;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.Pane;
import static java.util.Optional.ofNullable;
class HighlightingSupport {
private static final String CSS_HIGHLIGHT_CLASS = "setting-highlighted";
private HighlightingSupport() {}
private HighlightingSupport() {
}
static void highlightMatches(Category cat, String filter) {
var node = cat.getGuiOrElse(Label::new);
highlightMatchess(node, filter);
if(cat.hasSubCategories()) {
var node = cat.getGuiOrElse(() -> {
throw new IllegalStateException(cat + " has not been loaded");
});
highlightMatches(node, filter);
if (cat.hasSubCategories()) {
for (Category sub : cat.getSubCategories()) {
highlightMatches(sub, filter);
}
}
}
static void highlightMatchess(Node node, String filter) {
static void highlightMatches(Node node, String filter) {
var contains = false;
if (node instanceof Pane) {
var pane = (Pane) node;
if (node instanceof Pane pane) {
for (Node child : pane.getChildren()) {
highlightMatchess(child, filter);
highlightMatches(child, filter);
}
}
if (node instanceof Label) {
Label lbl = (Label) node;
if (node instanceof Label lbl) {
contains |= lbl.getText().toLowerCase().contains(filter);
contains |= ofNullable(lbl.getTooltip()).map(Tooltip::getText).orElse("").toLowerCase().contains(filter);
contains |= labelControlContains(lbl, filter);
@ -55,11 +56,11 @@ class HighlightingSupport {
if (lbl.labelForProperty().get() != null) {
var labeledNode = lbl.labelForProperty().get();
contains |= labeledNode.toString().toLowerCase().contains(filter);
if (labeledNode instanceof Control) {
contains |= ofNullable(((Control) labeledNode).getTooltip()).map(Tooltip::getText).orElse("").toLowerCase().contains(filter);
if (labeledNode instanceof Control control) {
contains |= ofNullable(control.getTooltip()).map(Tooltip::getText).orElse("").toLowerCase().contains(filter);
}
if (labeledNode instanceof TextInputControl) {
contains |= ofNullable(((TextInputControl) labeledNode).getText()).orElse("").toLowerCase().contains(filter);
if (labeledNode instanceof TextInputControl textInput) {
contains |= ofNullable(textInput.getText()).orElse("").toLowerCase().contains(filter);
}
}
return contains;
@ -68,7 +69,7 @@ class HighlightingSupport {
static void removeHighlights(Category cat) {
var node = cat.getGuiOrElse(Label::new);
removeHighlights(node);
if(cat.hasSubCategories()) {
if (cat.hasSubCategories()) {
for (Category sub : cat.getSubCategories()) {
removeHighlights(sub);
}
@ -77,8 +78,7 @@ class HighlightingSupport {
static void removeHighlights(Node gui) {
if (gui != null) {
if (gui instanceof Pane) {
Pane p = (Pane) gui;
if (gui instanceof Pane p) {
for (Node n : p.getChildren()) {
removeHighlights(n);
}

View File

@ -1,45 +1,34 @@
package ctbrec.ui.settings.api;
import static java.util.Optional.*;
import java.io.IOException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.ui.controls.SearchBox;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.*;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.Tooltip;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.layout.*;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import static java.util.Optional.ofNullable;
@Slf4j
public class Preferences {
private static final Logger LOG = LoggerFactory.getLogger(Preferences.class);
private Category[] categories;
private final Category[] categories;
private final PreferencesStorage preferencesStorage;
private TreeView<Category> categoryTree;
private PreferencesStorage preferencesStorage;
private Runnable restartRequiredCallback = () -> {
};
private Runnable restartRequiredCallback = () -> {};
private Preferences(PreferencesStorage preferencesStorage, Category...categories) {
private Preferences(PreferencesStorage preferencesStorage, Category... categories) {
this.preferencesStorage = preferencesStorage;
this.categories = categories;
for (Category category : categories) {
@ -48,7 +37,7 @@ public class Preferences {
}
private void assignPreferencesStorage(Category cat, PreferencesStorage preferencesStorage) {
if(cat.hasSubCategories()) {
if (cat.hasSubCategories()) {
for (Category sub : cat.getSubCategories()) {
assignPreferencesStorage(sub, preferencesStorage);
}
@ -69,11 +58,10 @@ public class Preferences {
preferencesStorage.save(this);
}
Category[] getCategories() {
return categories;
}
public Region getView(boolean withNavigation) {
for (Category category : categories) {
initializeCategory(category);
}
var search = new SearchBox(true);
search.textProperty().addListener(this::filterTree);
TreeItem<Category> categoryTreeItems = createCategoryTree(categories, new TreeItem<>(), null);
@ -131,6 +119,10 @@ public class Preferences {
}
}
private void initializeCategory(Category category) {
category.getGuiOrElse(() -> createGui(category));
}
private void expandAll(TreeItem<Category> treeItem) {
treeItem.setExpanded(true);
for (TreeItem<Category> child : treeItem.getChildren()) {
@ -142,18 +134,18 @@ public class Preferences {
try {
if (cat.hasSubCategories()) {
return new Label(cat.getName());
} else if(cat.hasGroups()) {
} else if (cat.hasGroups()) {
return createPaneWithGroups(cat);
} else {
return createGrid(cat.getGroups()[0].getSettings());
}
} catch(Exception e) {
LOG.error("Error creating the GUI", e);
} catch (Exception e) {
log.error("Error creating the GUI", e);
return new Label(e.getLocalizedMessage());
}
}
private Node createPaneWithGroups(Category cat) throws Exception {
private Node createPaneWithGroups(Category cat) {
var pane = new VBox();
for (Group grp : cat.getGroups()) {
var groupLabel = new Label(grp.getName());
@ -167,7 +159,7 @@ public class Preferences {
return pane;
}
private Node createGrid(Setting[] settings) throws Exception {
private Node createGrid(Setting[] settings) {
var pane = new GridPane();
pane.setHgap(2);
pane.vgapProperty().bind(pane.hgapProperty());
@ -190,6 +182,7 @@ public class Preferences {
/**
* Creates a tree of the given categories. Filters out categories, which don't match the filter
*
* @param filter may be null
*/
private TreeItem<Category> createCategoryTree(Category[] categories, TreeItem<Category> parent, String filter) {
@ -200,7 +193,7 @@ public class Preferences {
if (!child.getChildren().isEmpty()) {
parent.getChildren().add(child);
}
} else if(category.contains(filter)) {
} else if (category.contains(filter)) {
parent.getChildren().add(child);
}
}
@ -236,9 +229,9 @@ public class Preferences {
return search.getResult();
}
private class SettingSearchVisitor implements Consumer<Setting> {
Optional<Setting> result = Optional.empty();
private String key;
private static class SettingSearchVisitor implements Consumer<Setting> {
Setting result;
private final String key;
public SettingSearchVisitor(String key) {
this.key = key;
@ -247,12 +240,12 @@ public class Preferences {
@Override
public void accept(Setting s) {
if (Objects.equals(key, ofNullable(s.getKey()).orElse(""))) {
result = Optional.of(s);
result = s;
}
}
public Optional<Setting> getResult() {
return result;
return Optional.ofNullable(result);
}
}