chore: 全局主题更换

This commit is contained in:
octopus_yan 2024-09-12 01:09:44 +08:00
parent a0e5e16afc
commit 1f6ba2d8cc
9 changed files with 63 additions and 31 deletions

View File

@ -1,6 +1,5 @@
package cn.octopusyan.alistgui;
import atlantafx.base.theme.PrimerLight;
import cn.octopusyan.alistgui.config.Constants;
import cn.octopusyan.alistgui.config.Context;
import cn.octopusyan.alistgui.manager.ConfigManager;
@ -71,7 +70,7 @@ public class Application extends javafx.application.Application {
Context.setLanguage(ConfigManager.language());
// 主题样式
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
Application.setUserAgentStylesheet(ConfigManager.theme().getUserAgentStylesheet());
// 启动主界面
primaryStage.initStyle(StageStyle.TRANSPARENT);

View File

@ -19,7 +19,6 @@ import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;
/**
* 设置页面控制器
@ -38,6 +37,8 @@ public class SetupController extends BaseController<VBox> implements Initializab
@FXML
public ComboBox<Locale> languageComboBox;
@FXML
public ComboBox<String> themeComboBox;
@FXML
public ComboBox<ProxySetup> proxySetupComboBox;
@FXML
public Pane proxySetupPane;
@ -58,8 +59,8 @@ public class SetupController extends BaseController<VBox> implements Initializab
@Override
public void initData() {
languageComboBox.setItems(FXCollections.observableList(Context.SUPPORT_LANGUAGE_LIST));
themeComboBox.setItems(FXCollections.observableList(ConfigManager.THEME_NAME_LIST));
proxySetupComboBox.setItems(FXCollections.observableList(List.of(ProxySetup.values())));
}
@Override
@ -70,6 +71,7 @@ public class SetupController extends BaseController<VBox> implements Initializab
});
languageComboBox.getSelectionModel().select(ConfigManager.language());
themeComboBox.getSelectionModel().select(ConfigManager.themeName());
proxySetupComboBox.getSelectionModel().select(ConfigManager.proxySetup());
}
@ -77,13 +79,14 @@ public class SetupController extends BaseController<VBox> implements Initializab
public void initViewAction() {
autoStartCheckBox.selectedProperty().bindBidirectional(viewModule.autoStartProperty());
silentStartupCheckBox.selectedProperty().bindBidirectional(viewModule.silentStartupProperty());
proxySetupComboBox.getSelectionModel().selectedItemProperty().addListener((_, _, newValue) -> viewModule.proxySetupProperty().set(newValue));
languageComboBox.getSelectionModel().selectedItemProperty()
.subscribe(locale -> viewModule.languageProperty().set(locale));
themeComboBox.getSelectionModel().selectedItemProperty()
.subscribe(theme -> viewModule.themeProperty().set(theme));
proxySetupComboBox.getSelectionModel().selectedItemProperty()
.subscribe((setup) -> viewModule.proxySetupProperty().set(setup));
proxyHost.textProperty().bindBidirectional(viewModule.proxyHostProperty());
proxyPort.textProperty().bindBidirectional(viewModule.proxyPortProperty());
languageComboBox.getSelectionModel().selectedItemProperty().subscribe(locale -> {
viewModule.languageProperty().set(locale);
logger.info("language changed to {}", locale);
});
}
@FXML

View File

@ -1,8 +1,10 @@
package cn.octopusyan.alistgui.manager;
import atlantafx.base.theme.*;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.NumberUtil;
import cn.octopusyan.alistgui.Application;
import cn.octopusyan.alistgui.config.Constants;
import cn.octopusyan.alistgui.enums.ProxySetup;
import cn.octopusyan.alistgui.manager.http.HttpUtil;
@ -20,6 +22,8 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.regex.Matcher;
@ -36,6 +40,20 @@ public class ConfigManager {
private static GuiConfig guiConfig;
private static UpgradeConfig upgradeConfig;
public static final String DEFAULT_THEME = "Primer Light";
public static List<Theme> THEME_LIST = Arrays.asList(
new PrimerLight(), new PrimerDark(),
new NordLight(), new NordDark(),
new CupertinoLight(), new CupertinoDark(),
new Dracula()
);
public static List<String> THEME_NAME_LIST = Arrays.asList(
"Primer Light", "Primer Dark",
"Nord Light", "Nord Dark",
"Cupertino Light", "Cupertino Dark",
"Dracula"
);
static {
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
@ -59,6 +77,24 @@ public class ConfigManager {
return null;
}
// --------------------------------{ 主题 }------------------------------------------
public static String themeName() {
return guiConfig.getTheme();
}
public static Theme theme() {
return THEME_LIST.get(THEME_NAME_LIST.indexOf(themeName()));
}
public static void themeName(String themeName) {
int themeIndex = THEME_NAME_LIST.indexOf(themeName);
if (themeIndex < 0) return;
guiConfig.setTheme(themeName);
Application.setUserAgentStylesheet(theme().getUserAgentStylesheet());
}
public static boolean hasProxy() {
if (guiConfig == null)
return false;

View File

@ -20,5 +20,6 @@ public class GuiConfig {
private ProxyInfo proxyInfo;
private String proxySetup = ProxySetup.NO_PROXY.getName();
private String language = ConfigManager.DEFAULT_LANGUAGE.toString();
private String theme = ConfigManager.DEFAULT_THEME;
private String proxyTestUrl = "http://";
}

View File

@ -1,11 +1,9 @@
package cn.octopusyan.alistgui.viewModel;
import cn.hutool.core.net.NetUtil;
import cn.octopusyan.alistgui.base.BaseTask;
import cn.octopusyan.alistgui.config.Context;
import cn.octopusyan.alistgui.enums.ProxySetup;
import cn.octopusyan.alistgui.manager.ConfigManager;
import cn.octopusyan.alistgui.manager.http.HttpUtil;
import cn.octopusyan.alistgui.task.ProxyCheckTask;
import cn.octopusyan.alistgui.util.alert.AlertUtil;
import javafx.application.Platform;
@ -13,7 +11,6 @@ import javafx.beans.property.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.net.InetSocketAddress;
import java.util.Locale;
/**
@ -25,6 +22,7 @@ import java.util.Locale;
public class SetupViewModel {
private final BooleanProperty autoStart = new SimpleBooleanProperty(ConfigManager.autoStart());
private final BooleanProperty silentStartup = new SimpleBooleanProperty(ConfigManager.silentStartup());
private final StringProperty theme = new SimpleStringProperty(ConfigManager.themeName());
private final StringProperty proxyHost = new SimpleStringProperty(ConfigManager.proxyHost());
private final StringProperty proxyPort = new SimpleStringProperty(ConfigManager.proxyPort());
private final ObjectProperty<Locale> language = new SimpleObjectProperty<>(ConfigManager.language());
@ -33,21 +31,20 @@ public class SetupViewModel {
public SetupViewModel() {
theme.addListener((_, _, newValue) -> ConfigManager.themeName(newValue));
autoStart.addListener((_, _, newValue) -> ConfigManager.autoStart(newValue));
silentStartup.addListener((_, _, newValue) -> ConfigManager.silentStartup(newValue));
proxySetup.addListener((_, _, newValue) -> ConfigManager.proxySetup(newValue));
proxyTestUrl.addListener((_, _, newValue) -> ConfigManager.proxyTestUrl(newValue));
proxyHost.addListener((_, _, newValue) -> {
ConfigManager.proxyHost(newValue);
checkProxy();
});
proxyPort.addListener((_, _, newValue) -> {
ConfigManager.proxyPort(newValue);
checkProxy();
});
proxyHost.addListener((_, _, newValue) -> ConfigManager.proxyHost(newValue));
proxyPort.addListener((_, _, newValue) -> ConfigManager.proxyPort(newValue));
language.addListener((_, _, newValue) -> Context.setLanguage(newValue));
}
public StringProperty themeProperty() {
return theme;
}
public BooleanProperty autoStartProperty() {
return autoStart;
}
@ -85,17 +82,6 @@ public class SetupViewModel {
getProxyCheckTask(checkUrl).execute();
}
private void checkProxy() {
try {
InetSocketAddress address = NetUtil.createAddress(proxyHost.get(), Integer.parseInt(proxyPort.getValue()));
if (NetUtil.isOpen(address, 2000)) {
HttpUtil.getInstance().proxy(proxySetup.get(), ConfigManager.getProxyInfo());
}
} catch (Exception e) {
log.debug(STR."host=\{proxyHost.get()},port=\{proxyPort.get()}");
}
}
private static ProxyCheckTask getProxyCheckTask(String checkUrl) {
var task = new ProxyCheckTask(checkUrl);
final var progress = AlertUtil.progress();

View File

@ -12,6 +12,10 @@
</padding>
<CheckBox fx:id="autoStartCheckBox" text="%setup.auto-start.label"/>
<CheckBox fx:id="silentStartupCheckBox" text="%setup.silent-startup.label"/>
<HBox alignment="CENTER_LEFT" spacing="10">
<Label text="%setup.theme"/>
<ComboBox fx:id="themeComboBox"/>
</HBox>
<HBox alignment="CENTER_LEFT" spacing="10">
<Label text="%setup.language"/>
<ComboBox fx:id="languageComboBox"/>

View File

@ -31,5 +31,6 @@ about.alist.version=AList \u7248\u672C
about.app.version=GUI \u7248\u672C
about.alist.update=\u68C0\u67E5 AList \u7248\u672C
about.app.update=\u68C0\u67E5 GUI \u7248\u672C
setup.theme=\u4E3B\u9898

View File

@ -31,5 +31,6 @@ about.alist.version=AList Version
about.app.version=GUI Version
about.alist.update=Check AList Version
about.app.update=Check GUI Version
setup.theme=Theme

View File

@ -31,5 +31,6 @@ about.alist.version=AList \u7248\u672C
about.app.version=GUI \u7248\u672C
about.alist.update=\u68C0\u67E5 AList \u7248\u672C
about.app.update=\u68C0\u67E5 GUI \u7248\u672C
setup.theme=\u4E3B\u9898