Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ThemeChangeObserver #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

}
Loading