Skip to content

Commit

Permalink
Fix VariantTooltip when one variant has count=1
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSturgeon committed Apr 21, 2024
1 parent c987f32 commit b93a4b3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* Will try to use translations defined for the current build variant (e.g. {@code @ModrinthTooltip}), but will
* fall back to using the default {@code @Tooltip} translations if variant-specific ones are not defined.
* <p>
* If <strong>any</strong> variant has a {@code count} greater than 1, all variants must use the indexed syntax,
* e.g. {@code @Tooltip[0]}.
* <p>
* Can be declared multiple times on the same field.
*
* @see ConfigEntry.Gui.Tooltip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ private static void applyVariantTooltip(List<AbstractConfigListEntry> guis, List
})
.orElseThrow(() -> new IllegalStateException("%s: No variants matching \"%s\" declared on \"%s\".".formatted(VariantTooltip.class.getSimpleName(), variant, i18n)));

// True if _any_ variant has multiple lines
boolean multiline = tooltipVariants.stream()
.mapToInt(VariantTooltip::count)
.anyMatch(c -> c > 1);

// Add tooltip to each of the fields guis that support tooltips
// (except text entries)
guis.stream()
.filter(gui -> !(gui instanceof TextListEntry))
.filter(TooltipListEntry.class::isInstance)
.map(gui -> (TooltipListEntry<?>) gui)
.forEach(gui -> gui.setTooltipSupplier(getVariantTooltip(variant, i18n, count)));
.forEach(gui -> gui.setTooltipSupplier(getVariantTooltip(variant, i18n, count, multiline)));
}

/**
Expand All @@ -83,23 +88,20 @@ private static void applyVariantTooltip(List<AbstractConfigListEntry> guis, List
* @param variant the current build variant.
* @param i18n the config entry's translation key.
* @param count the number of lines in the tooltip.
* @param indexed whether the tooltip should use indexed syntax.
* @return A tooltip supplier accepted by {@link TooltipListEntry#setTooltipSupplier(Supplier)}.
* @see TooltipListEntry
*/
private static Supplier<Optional<Component[]>> getVariantTooltip(String variant, String i18n, int count) {
private static Supplier<Optional<Component[]>> getVariantTooltip(String variant, String i18n, int count, boolean indexed) {
if (count == 0) {
return Optional::empty;
}

// We can cache the tooltip since language can't change while config GUI is open.
Optional<Component[]> tooltip;
if (count == 1) {
tooltip = Optional.of(new Component[] { getVariantTooltipLine(variant, i18n, -1) });
} else {
tooltip = Optional.of(IntStream.range(0, count)
.mapToObj(i -> getVariantTooltipLine(variant, i18n, i))
.toArray(Component[]::new));
}
Optional<Component[]> tooltip = Optional.of(IntStream.range(0, count)
.mapToObj(i -> getVariantTooltipLine(variant, i18n, i, indexed))
.toArray(Component[]::new));

return () -> tooltip;
}

Expand All @@ -110,13 +112,14 @@ private static Supplier<Optional<Component[]>> getVariantTooltip(String variant,
*
* @param variant the current build variant.
* @param i18n the config entry's translation key.
* @param index the line's index (or {@code -1}).
* @param index the line's index.
* @param indexed whether to use indexed syntax.
* @return A line of {@link Component text} to be included in a wider tooltip.
* @see #getVariantTooltip(String, String, int)
* @see #getVariantTooltip(String, String, int, boolean)
*/
private static Component getVariantTooltipLine(String variant, String i18n, int index) {
private static Component getVariantTooltipLine(String variant, String i18n, int index, boolean indexed) {
String key = "%s.@%sTooltip".formatted(i18n, StringUtils.capitalize(variant));
if (index > -1) {
if (indexed) {
key += "[%d]".formatted(index);
}
// FIXME how will this behave for untranslated languages?
Expand All @@ -127,6 +130,6 @@ private static Component getVariantTooltipLine(String variant, String i18n, int
return Component.empty();
}
// Fallback to default "@Tooltip" translation
return getVariantTooltipLine("", i18n, index);
return getVariantTooltipLine("", i18n, index, indexed);
}
}

0 comments on commit b93a4b3

Please sign in to comment.