forked from j62/ctbrec
1
0
Fork 0

Add more settings

This commit is contained in:
0xboobface 2020-06-28 12:58:39 +02:00
parent ec1a0826e0
commit 90300473bc
9 changed files with 142 additions and 38 deletions

View File

@ -190,8 +190,8 @@ public class CamrecApplication extends Application {
recordingsTab = new RecordingsTab("Recordings", recorder, config, sites);
tabPane.getTabs().add(recordingsTab);
settingsTab = new SettingsTab(sites, recorder);
tabPane.getTabs().add(settingsTab);
tabPane.getTabs().add(new SettingsTab2(sites, recorder));
tabPane.getTabs().add(settingsTab);
tabPane.getTabs().add(new NewsTab());
tabPane.getTabs().add(new DonateTabFx());
tabPane.getTabs().add(new HelpTab());

View File

@ -1,6 +1,9 @@
package ctbrec.ui.settings;
import java.io.IOException;
import ctbrec.Config;
import ctbrec.ui.controls.Dialogs;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Tooltip;
@ -14,41 +17,49 @@ public class ColorSettingsPane extends HBox {
ColorPicker accentColor = new ColorPicker();
Button reset = new Button("Reset");
Pane foobar = new Pane();
private Config config;
public ColorSettingsPane(SettingsTab settingsTab) {
public ColorSettingsPane(Config config) {
super(5);
this.config = config;
getChildren().add(baseColor);
getChildren().add(accentColor);
getChildren().add(reset);
baseColor.setValue(Color.web(Config.getInstance().getSettings().colorBase));
baseColor.setValue(Color.web(config.getSettings().colorBase));
baseColor.setTooltip(new Tooltip("Base Color"));
baseColor.setMaxSize(44, 25);
accentColor.setValue(Color.web(Config.getInstance().getSettings().colorAccent));
accentColor.setValue(Color.web(config.getSettings().colorAccent));
accentColor.setTooltip(new Tooltip("Accent Color"));
accentColor.setMaxSize(44, 25);
reset.setMinSize(60, 25);
reset.setMaxSize(60, 25);
baseColor.setOnAction(evt -> {
Config.getInstance().getSettings().colorBase = toWeb(baseColor.getValue());
settingsTab.showRestartRequired();
settingsTab.saveConfig();
config.getSettings().colorBase = toWeb(baseColor.getValue());
save();
});
accentColor.setOnAction(evt -> {
Config.getInstance().getSettings().colorAccent = toWeb(accentColor.getValue());
settingsTab.showRestartRequired();
settingsTab.saveConfig();
config.getSettings().colorAccent = toWeb(accentColor.getValue());
save();
});
reset.setOnAction(evt -> {
baseColor.setValue(Color.WHITE);
Config.getInstance().getSettings().colorBase = toWeb(Color.WHITE);
config.getSettings().colorBase = toWeb(Color.WHITE);
accentColor.setValue(Color.WHITE);
Config.getInstance().getSettings().colorAccent = toWeb(Color.WHITE);
settingsTab.showRestartRequired();
settingsTab.saveConfig();
config.getSettings().colorAccent = toWeb(Color.WHITE);
save();
});
}
private void save() {
try {
config.save();
} catch (IOException e) {
Dialogs.showError(getScene(), "Save Settings", "Couldn't save color settings", e);
}
}
private String toWeb(Color value) {
StringBuilder sb = new StringBuilder("#");
sb.append(toHex((int) (value.getRed() * 255)));

View File

@ -2,16 +2,22 @@ package ctbrec.ui.settings;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ctbrec.Config;
import ctbrec.Settings;
import ctbrec.StringUtil;
import ctbrec.ui.settings.api.Preferences;
import ctbrec.ui.settings.api.PreferencesStorage;
import ctbrec.ui.settings.api.SelectionSetting;
import ctbrec.ui.settings.api.Setting;
import javafx.collections.FXCollections;
import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
@ -40,11 +46,14 @@ public class CtbrecPreferencesStorage implements PreferencesStorage {
}
@Override
public Node createGui(String key) throws Exception {
public Node createGui(Setting setting) throws Exception {
String key = setting.getKey();
Field field = Settings.class.getField(key);
Class<?> t = field.getType();
Object value = field.get(settings);
if (t == String.class) {
if(setting instanceof SelectionSetting) {
return createComboBox(key, ((SelectionSetting) setting).getOptions());
} else if (t == String.class) {
return createStringProperty(key, (String) value);
} else if (t == int.class || t == Integer.class) {
return createIntegerProperty(key, (Integer) value);
@ -91,6 +100,21 @@ public class CtbrecPreferencesStorage implements PreferencesStorage {
return ctrl;
}
private Node createComboBox(String key, Object[] options) throws NoSuchFieldException, IllegalAccessException {
@SuppressWarnings({ "rawtypes", "unchecked" })
ComboBox<Object> comboBox = new ComboBox(FXCollections.observableList(Arrays.asList(options)));
Field field = Settings.class.getField(key);
Object value = field.get(settings);
if(StringUtil.isNotBlank(value.toString())) {
comboBox.getSelectionModel().select(value);
}
comboBox.valueProperty().addListener((obs, oldV, newV) -> saveValue(() -> {
field.set(settings, newV);
config.save();
}));
return comboBox;
}
private void saveValue(Exec exe) {
try {
exe.run();

View File

@ -745,7 +745,7 @@ public class SettingsTab extends Tab implements TabSelectionListener {
l = new Label("Colors (Base / Accent)");
layout.add(l, 0, row);
ColorSettingsPane colorSettingsPane = new ColorSettingsPane(this);
ColorSettingsPane colorSettingsPane = new ColorSettingsPane(Config.getInstance());
layout.add(colorSettingsPane, 1, row++);
GridPane.setMargin(l, new Insets(0, 0, 0, 0));
GridPane.setMargin(colorSettingsPane, new Insets(0, 0, 0, CHECKBOX_MARGIN));

View File

@ -1,5 +1,7 @@
package ctbrec.ui.settings;
import static ctbrec.Settings.ProxyType.*;
import java.util.ArrayList;
import java.util.List;
@ -18,13 +20,15 @@ public class SettingsTab2 extends Tab implements TabSelectionListener {
private List<Site> sites;
private Recorder recorder;
private Preferences prefs;
private boolean initialized = false;
public SettingsTab2(List<Site> sites, Recorder recorder) {
this.sites = sites;
this.recorder = recorder;
setText("Settings");
createGui();
setClosable(false);
}
private void createGui() {
@ -33,33 +37,60 @@ public class SettingsTab2 extends Tab implements TabSelectionListener {
siteCategories.add(Category.of(site.getName(), SiteUiFactory.getUi(site).getConfigUI().createConfigPanel()));
}
Preferences prefs = Preferences.of(new CtbrecPreferencesStorage(Config.getInstance()),
prefs = Preferences.of(new CtbrecPreferencesStorage(Config.getInstance()),
Category.of("General",
Group.of("General",
Setting.of("Player", "mediaPlayer"),
Setting.of("Start parameters", "mediaPlayerParams"),
Setting.of("Maximum resolution (0 = unlimited)", "maximumResolutionPlayer", "video height, e.g. 720 or 1080")),
Setting.of("User-Agent", "httpUserAgent"),
Setting.of("User-Agent mobile", "httpUserAgentMobile"),
Setting.of("Update overview interval (seconds)", "overviewUpdateIntervalInSecs", "Update the thumbnail overviews every x seconds"),
Setting.of("Update thumbnails", "updateThumbnails", "The overviews will still be updated, but the thumbnails won't be changed. This is useful for less powerful systems."),
Setting.of("Display stream resolution in overview", "determineResolution"),
Setting.of("Manually select stream quality", "chooseStreamQuality", "Opens a dialog to select the video resolution before recording"),
Setting.of("Enable live previews (experimental)", "livePreviews"),
Setting.of("Start Tab", "startTab", getTabNames()),
Setting.of("Colors (Base / Accent)", null, new ColorSettingsPane(Config.getInstance()))
),
Group.of("Player",
Setting.of("Player", "mediaPlayer"),
Setting.of("Start parameters", "mediaPlayerParams"),
Setting.of("Maximum resolution (0 = unlimited)", "maximumResolutionPlayer", "video height, e.g. 720 or 1080"),
Setting.of("Show \"Player Starting\" Message", "showPlayerStarting"))
Setting.of("Show \"Player Starting\" Message", "showPlayerStarting"),
Setting.of("Start only one player at a time", "singlePlayer")
)
),
Category.of("Sites", siteCategories.toArray(new Category[0]))
Category.of("Sites", siteCategories.toArray(new Category[0])),
Category.of("Proxy",
Group.of("Proxy",
Setting.of("Type", "proxyType", DIRECT, HTTP, SOCKS4, SOCKS5),
Setting.of("Host", "proxyHost"),
Setting.of("Port", "proxyPort"),
Setting.of("Username", "proxyUser"),
Setting.of("Password", "proxyPassword")
)
)
);
setContent(prefs.getView());
}
private Object[] getTabNames() {
List<String> tabNames = new ArrayList<>();
for (Tab tab : getTabPane().getTabs()) {
tabNames.add(tab.getText());
}
return tabNames.toArray(new String[0]);
}
@Override
public void selected() {
// TODO Auto-generated method stub
if (!initialized) {
createGui();
initialized = true;
}
}
@Override
public void deselected() {
// TODO Auto-generated method stub
// TODO maybe save the config ?!?
}

View File

@ -8,6 +8,7 @@ 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.Tooltip;
@ -83,7 +84,7 @@ public class Preferences {
main.setCenter(gui);
}
});
categoryTree.getSelectionModel().select(0);
return main;
}
@ -150,12 +151,14 @@ public class Preferences {
for (Setting setting : settings) {
Node node = setting.getGui();
Label label = new Label(setting.getName());
label.setMinHeight(34);
label.labelForProperty().set(node);
if (setting.getTooltip() != null) {
label.setTooltip(new Tooltip(setting.getTooltip()));
}
label.setTooltip(new Tooltip(setting.getName()));
pane.addRow(row++, label, node);
GridPane.setVgrow(label, Priority.ALWAYS);
GridPane.setValignment(label, VPos.CENTER);
GridPane.setMargin(node, new Insets(5, 0, 5, 10));
GridPane.setValignment(node, VPos.CENTER);
GridPane.setHgrow(node, Priority.ALWAYS);
}
return pane;

View File

@ -9,5 +9,5 @@ public interface PreferencesStorage {
void save(Preferences preferences) throws IOException;
void load(Preferences preferences);
Node createGui(String key) throws Exception;
Node createGui(Setting setting) throws Exception;
}

View File

@ -0,0 +1,15 @@
package ctbrec.ui.settings.api;
public class SelectionSetting extends Setting {
private Object[] options;
public SelectionSetting(String name, String key, Object[] options) {
super(name, key);
this.options = options;
}
public Object[] getOptions() {
return options;
}
}

View File

@ -16,8 +16,9 @@ public class Setting {
private Property<?> property;
private Node gui;
private PreferencesStorage preferencesStorage;
private boolean needsRestart = false;
private Setting(String name, String key) {
protected Setting(String name, String key) {
this.name = name;
this.key = key;
}
@ -32,18 +33,37 @@ public class Setting {
return setting;
}
String getName() {
public static Setting of(String name, String key, Object...options) {
return new SelectionSetting(name, key, options);
}
public static Setting of(String name, String key, Node gui) {
Setting setting = new Setting(name, key);
setting.gui = gui;
return setting;
}
public String getName() {
return name;
}
String getKey() {
public String getKey() {
return key;
}
String getTooltip() {
public String getTooltip() {
return tooltip;
}
public Setting needsRestart() {
needsRestart = true;
return this;
}
public boolean doesNeedRestart() {
return needsRestart;
}
@SuppressWarnings("rawtypes")
Property getProperty() {
return property;
@ -51,7 +71,7 @@ public class Setting {
Node getGui() throws Exception {
if (gui == null) {
gui = preferencesStorage.createGui(key);
gui = preferencesStorage.createGui(this);
if (gui instanceof Control && StringUtil.isNotBlank(tooltip)) {
Control control = (Control) gui;
control.setTooltip(new Tooltip(tooltip));