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 4.7.9
======================== ========================
* Fix stream resolution detection for Camsoda * Fix stream resolution detection for Camsoda
* Fix bug in settings where panels would be empty
4.7.8 4.7.8
======================== ========================

View File

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

View File

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