refactor(ui): simplify ThemeManager to single-theme stylesheet utility
This commit is contained in:
+2
-17
@@ -40,7 +40,6 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import br.com.cybercore.signer.ui.CyberCoreDesktopApplication;
|
import br.com.cybercore.signer.ui.CyberCoreDesktopApplication;
|
||||||
import br.com.cybercore.signer.ui.theme.ThemeManager;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@@ -422,8 +421,7 @@ public final class AssetManager {
|
|||||||
Map<String, Paint> defsMap = new HashMap<>();
|
Map<String, Paint> defsMap = new HashMap<>();
|
||||||
parseDefs(svg, defsMap);
|
parseDefs(svg, defsMap);
|
||||||
|
|
||||||
Color effectiveCurrentColor = (tint != null) ? tint :
|
Color effectiveCurrentColor = (tint != null) ? tint : Color.web("#1E293B");
|
||||||
(ThemeManager.isDark() ? Color.web("#F1F5F9") : Color.web("#1E293B"));
|
|
||||||
List<Shape> dynamicStrokeShapes = new ArrayList<>();
|
List<Shape> dynamicStrokeShapes = new ArrayList<>();
|
||||||
List<Shape> dynamicFillShapes = new ArrayList<>();
|
List<Shape> dynamicFillShapes = new ArrayList<>();
|
||||||
RenderContext ctx = new RenderContext(defsMap, effectiveCurrentColor, dynamicStrokeShapes, dynamicFillShapes);
|
RenderContext ctx = new RenderContext(defsMap, effectiveCurrentColor, dynamicStrokeShapes, dynamicFillShapes);
|
||||||
@@ -455,19 +453,6 @@ public final class AssetManager {
|
|||||||
container.setMaxSize(targetWidth, targetHeight);
|
container.setMaxSize(targetWidth, targetHeight);
|
||||||
container.setMinSize(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;
|
return container;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -801,7 +786,7 @@ public final class AssetManager {
|
|||||||
if (ctx != null && ctx.currentColor() != null) {
|
if (ctx != null && ctx.currentColor() != null) {
|
||||||
return ctx.currentColor();
|
return ctx.currentColor();
|
||||||
}
|
}
|
||||||
return ThemeManager.isDark() ? Color.web("#F1F5F9") : Color.web("#1E293B");
|
return Color.web("#1E293B");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return Color.web(trimmed);
|
return Color.web(trimmed);
|
||||||
|
|||||||
+5
-84
@@ -4,70 +4,19 @@ import br.com.cybercore.signer.ui.CyberCoreUiConstants;
|
|||||||
import javafx.scene.Parent;
|
import javafx.scene.Parent;
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gerenciador centralizado de temas visuais do CyberCore Signer.
|
* Utilitário para aplicação da folha de estilo global do CyberCore Signer à Scene.
|
||||||
* Permite alternar dinamicamente entre Tema Claro (Light) e Tema Escuro (Dark)
|
* Gerencia a folha de estilo padrão única do aplicativo.
|
||||||
* aplicando as classes CSS e folhas de estilo correspondentes em qualquer Scene.
|
|
||||||
*/
|
*/
|
||||||
public final class ThemeManager {
|
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() {
|
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
|
* Aplica a folha de estilo padrão única a uma Scene.
|
||||||
* e ajustando a classe CSS raiz (theme-light / theme-dark).
|
|
||||||
*/
|
*/
|
||||||
public static void applyTheme(Scene scene) {
|
public static void applyTheme(Scene scene) {
|
||||||
if (scene == null) {
|
if (scene == null) {
|
||||||
@@ -81,40 +30,12 @@ public final class ThemeManager {
|
|||||||
if (!scene.getStylesheets().contains(cssUrl)) {
|
if (!scene.getStylesheets().contains(cssUrl)) {
|
||||||
scene.getStylesheets().add(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) {
|
public static void applyThemeToNode(Parent node) {
|
||||||
if (node == null) {
|
// No tema padrão único definido em .root, a vinculação da folha na Scene é suficiente.
|
||||||
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) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user