From ffd09d3d6322fe7f128095dc3d086bdf26644d6f Mon Sep 17 00:00:00 2001
From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com>
Date: Mon, 23 Dec 2024 14:36:13 -0300
Subject: [PATCH] feat: add ThemeChangeObserver
Close #110
---
pom.xml | 2 +-
.../flowingcode/vaadin/addons/demo/TabbedDemo.java | 10 ++++++++++
.../vaadin/addons/demo/ThemeChangeObserver.java | 12 ++++++++++++
3 files changed, 23 insertions(+), 1 deletion(-)
create mode 100644 src/main/java/com/flowingcode/vaadin/addons/demo/ThemeChangeObserver.java
diff --git a/pom.xml b/pom.xml
index cbe0e5e..ca3dcf4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
com.flowingcode.vaadin.addons.demo
commons-demo
- 4.1.1-SNAPSHOT
+ 4.2.0-SNAPSHOT
Commons Demo
Common classes for add-ons demo
diff --git a/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java b/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java
index da1bd00..5d7731c 100644
--- a/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java
+++ b/src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java
@@ -43,6 +43,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
+import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -340,6 +341,15 @@ public void setOrientation(Orientation orientation) {
public static void applyTheme(Element element, boolean useDarkTheme) {
String theme = useDarkTheme ? "dark" : "";
element.executeJs("document.documentElement.setAttribute('theme', $0);", theme);
+
+ Component c = element.getComponent().get();
+ collectThemeChangeObservers(c).forEach(observer -> observer.onThemeChange(theme));
+ }
+
+ private static Stream collectThemeChangeObservers(Component c) {
+ Stream children =
+ c.getChildren().flatMap(child -> collectThemeChangeObservers(child));
+ return c instanceof ThemeChangeObserver o ? Stream.concat(Stream.of(o), children) : children;
}
private void updateFooterButtonsVisibility() {
diff --git a/src/main/java/com/flowingcode/vaadin/addons/demo/ThemeChangeObserver.java b/src/main/java/com/flowingcode/vaadin/addons/demo/ThemeChangeObserver.java
new file mode 100644
index 0000000..bc75a51
--- /dev/null
+++ b/src/main/java/com/flowingcode/vaadin/addons/demo/ThemeChangeObserver.java
@@ -0,0 +1,12 @@
+package com.flowingcode.vaadin.addons.demo;
+
+/**
+ * Any attached component implementing this interface will receive an event when a new theme is
+ * applied.
+ */
+@FunctionalInterface
+public interface ThemeChangeObserver {
+
+ void onThemeChange(String themeName);
+
+}