Skip to content

Commit

Permalink
feat: add ThemeChangeObserver
Browse files Browse the repository at this point in the history
Close #110
  • Loading branch information
javier-godoy committed Dec 23, 2024
1 parent 9114a44 commit ffd09d3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.flowingcode.vaadin.addons.demo</groupId>
<artifactId>commons-demo</artifactId>
<version>4.1.1-SNAPSHOT</version>
<version>4.2.0-SNAPSHOT</version>

<name>Commons Demo</name>
<description>Common classes for add-ons demo</description>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/flowingcode/vaadin/addons/demo/TabbedDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<ThemeChangeObserver> collectThemeChangeObservers(Component c) {
Stream<ThemeChangeObserver> children =
c.getChildren().flatMap(child -> collectThemeChangeObservers(child));
return c instanceof ThemeChangeObserver o ? Stream.concat(Stream.of(o), children) : children;
}

private void updateFooterButtonsVisibility() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}

0 comments on commit ffd09d3

Please sign in to comment.