refactor(ui): simplify ThemeManager to single-theme stylesheet utility

This commit is contained in:
Claudecio Martins
2026-10-20 21:25:03 -03:00
parent 14b342c3a2
commit 0658f7007e
2 changed files with 7 additions and 101 deletions
@@ -40,7 +40,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import br.com.cybercore.signer.ui.CyberCoreDesktopApplication;
import br.com.cybercore.signer.ui.theme.ThemeManager;
import java.util.Optional;
import java.util.function.Supplier;
@@ -422,8 +421,7 @@ public final class AssetManager {
Map<String, Paint> defsMap = new HashMap<>();
parseDefs(svg, defsMap);
Color effectiveCurrentColor = (tint != null) ? tint :
(ThemeManager.isDark() ? Color.web("#F1F5F9") : Color.web("#1E293B"));
Color effectiveCurrentColor = (tint != null) ? tint : Color.web("#1E293B");
List<Shape> dynamicStrokeShapes = new ArrayList<>();
List<Shape> dynamicFillShapes = new ArrayList<>();
RenderContext ctx = new RenderContext(defsMap, effectiveCurrentColor, dynamicStrokeShapes, dynamicFillShapes);
@@ -455,19 +453,6 @@ public final class AssetManager {
container.setMaxSize(targetWidth, targetHeight);
container.setMinSize(targetWidth, targetHeight);
// Se for dinâmico (sem tint fixo) e contiver elementos currentColor, atualiza com a mudança de tema
if (tint == null && (!dynamicStrokeShapes.isEmpty() || !dynamicFillShapes.isEmpty())) {
ThemeManager.addListener(theme -> {
Color c = (theme == ThemeManager.AppTheme.DARK) ? Color.web("#F1F5F9") : Color.web("#1E293B");
for (Shape s : dynamicStrokeShapes) {
s.setStroke(c);
}
for (Shape s : dynamicFillShapes) {
s.setFill(c);
}
});
}
return container;
} catch (Exception e) {
@@ -801,7 +786,7 @@ public final class AssetManager {
if (ctx != null && ctx.currentColor() != null) {
return ctx.currentColor();
}
return ThemeManager.isDark() ? Color.web("#F1F5F9") : Color.web("#1E293B");
return Color.web("#1E293B");
}
try {
return Color.web(trimmed);
@@ -4,70 +4,19 @@ import br.com.cybercore.signer.ui.CyberCoreUiConstants;
import javafx.scene.Parent;
import javafx.scene.Scene;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
/**
* Gerenciador centralizado de temas visuais do CyberCore Signer.
* Permite alternar dinamicamente entre Tema Claro (Light) e Tema Escuro (Dark)
* aplicando as classes CSS e folhas de estilo correspondentes em qualquer Scene.
* Utilitário para aplicação da folha de estilo global do CyberCore Signer à Scene.
* Gerencia a folha de estilo padrão única do aplicativo.
*/
public final class ThemeManager {
public enum AppTheme {
LIGHT("theme-light", "Modo Claro"),
DARK("theme-dark", "Modo Escuro");
private final String cssClass;
private final String displayName;
AppTheme(String cssClass, String displayName) {
this.cssClass = cssClass;
this.displayName = displayName;
}
public String getCssClass() {
return cssClass;
}
public String getDisplayName() {
return displayName;
}
}
private static AppTheme currentTheme = AppTheme.LIGHT;
private static final List<Consumer<AppTheme>> listeners = new ArrayList<>();
private ThemeManager() {
}
public static AppTheme getCurrentTheme() {
return currentTheme;
}
public static boolean isDark() {
return currentTheme == AppTheme.DARK;
}
public static void setCurrentTheme(AppTheme theme) {
if (theme == null || theme == currentTheme) {
return;
}
currentTheme = theme;
notifyListeners();
}
public static AppTheme toggleTheme() {
currentTheme = (currentTheme == AppTheme.LIGHT) ? AppTheme.DARK : AppTheme.LIGHT;
notifyListeners();
return currentTheme;
}
/**
* Aplica o tema atualmente ativo a uma Scene, adicionando a folha de estilo global
* e ajustando a classe CSS raiz (theme-light / theme-dark).
* Aplica a folha de estilo padrão única a uma Scene.
*/
public static void applyTheme(Scene scene) {
if (scene == null) {
@@ -81,40 +30,12 @@ public final class ThemeManager {
if (!scene.getStylesheets().contains(cssUrl)) {
scene.getStylesheets().add(cssUrl);
}
Parent root = scene.getRoot();
if (root != null) {
applyThemeToNode(root);
}
}
/**
* Aplica a classe de estilo do tema ativo no nó raiz fornecido.
* Mantido para compatibilidade com chamadas de estilização de nós raiz.
*/
public static void applyThemeToNode(Parent node) {
if (node == null) {
return;
}
node.getStyleClass().removeAll(AppTheme.LIGHT.getCssClass(), AppTheme.DARK.getCssClass());
node.getStyleClass().add(currentTheme.getCssClass());
}
public static void addListener(Consumer<AppTheme> listener) {
if (listener != null && !listeners.contains(listener)) {
listeners.add(listener);
}
}
public static void removeListener(Consumer<AppTheme> listener) {
listeners.remove(listener);
}
private static void notifyListeners() {
for (Consumer<AppTheme> listener : new ArrayList<>(listeners)) {
try {
listener.accept(currentTheme);
} catch (Exception ignored) {
}
}
// No tema padrão único definido em .root, a vinculação da folha na Scene é suficiente.
}
}