-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add support for dynamic theme in iframes #150
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,7 +139,10 @@ public TabbedDemo() { | |
| themeSelect.setItems(DynamicTheme.values()); | ||
| themeSelect.setValue(DynamicTheme.getCurrent()); | ||
| themeSelect.setWidth("85px"); | ||
| themeSelect.addValueChangeListener(ev -> ev.getValue().apply(this)); | ||
| themeSelect.addValueChangeListener(ev -> { | ||
| ev.getValue().apply(this); | ||
| observeThemeChange(this); | ||
| }); | ||
| footer.add(themeSelect); | ||
| } | ||
|
|
||
|
|
@@ -466,8 +469,19 @@ public static void setColorScheme(Component component, @NonNull ColorScheme colo | |
| element.executeJs(script, theme); | ||
|
|
||
| collectThemeChangeObservers(component).forEach(observer -> observer.onThemeChange(theme)); | ||
| observeThemeChange(component); | ||
| } | ||
|
|
||
| private static void observeThemeChange(Component source) { | ||
| DynamicTheme dynamicTheme = null; | ||
| if (DynamicTheme.isFeatureSupported()) { | ||
| dynamicTheme = DynamicTheme.getCurrent(); | ||
| } | ||
| ThemeChangeEvent event = new ThemeChangeEvent(source, false, getColorScheme(), dynamicTheme); | ||
| collectThemeChangeObservers(source).forEach(observer -> observer.onThemeChange(event)); | ||
| } | ||
|
Comment on lines
+475
to
+482
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: fd 'DynamicTheme.java$' --exec cat -n {}Repository: FlowingCode/CommonsDemo Length of output: 10202 🏁 Script executed: fd 'TabbedDemo.java$' --exec cat -n {} | sed -n '130,140p'Repository: FlowingCode/CommonsDemo Length of output: 558 Use Line 477 checks
Since 🤖 Prompt for AI Agents
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai I disagree. Since getCurrent() returns null if the feature has not been initialized, I only have to check if the feature is supported (in order to avoid the exception). Note that isFeatureInitialized already checks getCurrent()!=null, thus modifying this block to call isFeatureInitialized would be equivalent to the following redundant code: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: Length of output: 804
As you noted, substituting ✏️ Learnings added
🧠 Learnings used |
||
|
|
||
|
|
||
| private static Stream<ThemeChangeObserver> collectThemeChangeObservers(Component c) { | ||
| Stream<ThemeChangeObserver> children = | ||
| c.getChildren().flatMap(child -> collectThemeChangeObservers(child)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /*- | ||
| * #%L | ||
| * Commons Demo | ||
| * %% | ||
| * Copyright (C) 2020 - 2026 Flowing Code | ||
| * %% | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * #L% | ||
| */ | ||
| package com.flowingcode.vaadin.addons.demo; | ||
|
|
||
| import com.vaadin.flow.component.Component; | ||
| import com.vaadin.flow.component.ComponentEvent; | ||
| import java.util.Optional; | ||
| import lombok.Getter; | ||
|
|
||
paodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Event fired when a theme change occurs in the application. It contains information about the new | ||
| * {@link ColorScheme} and {@link DynamicTheme}. | ||
| */ | ||
| @SuppressWarnings("serial") | ||
| public class ThemeChangeEvent extends ComponentEvent<Component> { | ||
|
|
||
| @Getter | ||
| private final ColorScheme colorScheme; | ||
|
|
||
| private final DynamicTheme dynamicTheme; | ||
|
|
||
| /** | ||
| * Constructs a new {@code ThemeChangeEvent}. | ||
| * | ||
| * @param source the source component of the event | ||
| * @param fromClient true if the event originated from the client side, false otherwise | ||
| * @param colorScheme the new color scheme applied | ||
| * @param dynamicTheme the new dynamic theme applied (may be null) | ||
| */ | ||
| public ThemeChangeEvent(Component source, boolean fromClient, ColorScheme colorScheme, DynamicTheme dynamicTheme) { | ||
| super(source, fromClient); | ||
| this.colorScheme = colorScheme; | ||
| this.dynamicTheme = dynamicTheme; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the dynamic theme applied, if any. | ||
| * | ||
| * @return an {@code Optional} containing the dynamic theme, or empty if dynamic theming is not | ||
| * initialized. | ||
| */ | ||
| public Optional<DynamicTheme> getDynamicTheme() { | ||
| return Optional.ofNullable(dynamicTheme); | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.