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

Component::applyFallbackStyle helper method #488

Merged
merged 8 commits into from Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Expand Up @@ -46,6 +46,7 @@
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.event.HoverEventSource;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.StyleBuilderApplicable;
import net.kyori.adventure.text.format.StyleGetter;
import net.kyori.adventure.text.format.StyleSetter;
import net.kyori.adventure.text.format.TextColor;
Expand Down Expand Up @@ -1732,6 +1733,35 @@ default void detectCycle(final @NotNull Component that) {
return this.append(builder.build());
}

/**
* Apply a fallback style for this component and its children.
*
* <p>This method can be used to set the "default" style for a component, whilst still allowing children of the component to override the style.</p>
*
* @param style style to be used as a fallback
* @return the styled component
* @since 4.10.0
*/
@Contract(pure = true)
default @NotNull Component applyFallbackStyle(final @NotNull Style style) {
Objects.requireNonNull(style, "style");
return this.style(this.style().merge(style, Style.Merge.Strategy.IF_ABSENT_ON_TARGET));
}

/**
* Apply a fallback style for this component and its children.
*
* <p>This method can be used to set the "default" style for a component, whilst still allowing children of the component to override the style.</p>
*
* @param style style to be used as a fallback
* @return the styled component
* @since 4.10.0
*/
@Contract(pure = true)
default @NotNull Component applyFallbackStyle(final @NotNull StyleBuilderApplicable@NotNull... style) {
return this.applyFallbackStyle(Style.style(style));
}

/**
* Gets the style of this component.
*
Expand Down
13 changes: 13 additions & 0 deletions api/src/test/java/net/kyori/adventure/text/TextComponentTest.java
Expand Up @@ -153,4 +153,17 @@ void testContent() {
void testBuildEmptyIsEmpty() {
assertSame(Component.empty(), Component.text().build());
}

@Test
void testWrapping() {
final Component italic = Component.text("italic").decorate(TextDecoration.ITALIC);
final Component notItalic = Component.text("non-italic");
final Component parent = Component.empty().decoration(TextDecoration.ITALIC, true);

final Component wrappedItalic = parent.append(italic.applyFallbackStyle(TextDecoration.ITALIC.withState(false)));
final Component wrappedNotItalic = parent.append(notItalic.applyFallbackStyle(TextDecoration.ITALIC.withState(false)));

assertEquals(wrappedItalic.compact(), Component.text("italic").decoration(TextDecoration.ITALIC, true));
assertEquals(wrappedNotItalic.compact(), Component.text("non-italic").decoration(TextDecoration.ITALIC, false));
}
}