Class Section

java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.Section

public final class Section extends Entry
A titled group of entries on a MenuScreen. A collapsible section (the default) hides its children until the player expands it by clicking its header; a non-collapsible one is just a heading above always-visible entries.
Section advanced = Section.builder("advanced")
        .title(Text.translatable("mymod.advanced", "Advanced"))
        .expanded(config.advancedOpen)                  // start as the player left it
        .onToggle(open -> config.advancedOpen = open)   // remember it
        .add(Toggle.builder("debug")…build())
        .add(Slider.builder("threads")…build())
        .build();

MenuScreen.builder("main").title(title).add(advanced)…   // adds the section and its children

How it is drawn

Not an entry type of its own for backends: it is drawn as its header, a Button ("▼ title" while expanded, "▶ title" while collapsed, see headerText()) that expands and collapses it, or a Label (the title) if it can't collapse. MenuScreen.Builder.add(Entry) adds its children right after it, and they are Entry.visible only while the section is visible and expanded. The section's own enabled, tooltip and visible options apply to the header; hiding the section hides its children too.

Rules

  • Whether it is expanded lives in the section object (a refresh doesn't change it); onToggle lets the mod remember it across menu openings.
  • Sections don't nest, can't be in the footer, and an entry is in at most one section.
  • Children's ids share the screen's id space like any other entries.
  • A child must not be added to the screen on its own; add the section.
  • Method Details

    • builder

      public static Section.Builder builder(String id)
      Starts building a section.
      Parameters:
      id - the section's id, unique within its screen; also its header's id
      Returns:
      a new builder; Section.Builder.title is required
      Throws:
      IllegalArgumentException - if id is invalid
    • title

      public Text title()
      The section's title, from the mod's supplier, without the ▼/▶ marker.
      Returns:
      the title, without the expand marker
    • headerText

      public Text headerText()
      What the header shows: "▼ title" while expanded, "▶ title" while collapsed (UiTexts.SECTION_EXPANDED, UiTexts.SECTION_COLLAPSED); just the title if the section can't collapse.
      Returns:
      the header text, with the marker, or just the title if the section can't collapse
    • collapsible

      public boolean collapsible()
      Whether the player can expand and collapse it; true unless the builder set collapsible(false).
      Returns:
      true if the player can expand and collapse the section
    • expanded

      public boolean expanded()
      Whether the children are shown (if the section itself is visible); always true for a section that can't collapse.
      Returns:
      true if the children are shown
    • setExpanded

      public void setExpanded(boolean expand)
      Expands or collapses the section and tells onToggle. Nothing happens if it can't collapse or already is in that state. The change shows on the next refresh (automatic after a click on the header). A mod can call it from an action, e.g. to open a section that has an error in it.
      Parameters:
      expand - true to show the children, false to hide them
    • toggle

      public void toggle()
      Flips expanded(), as a click on the header does; see setExpanded(boolean).
    • children

      public List<Entry> children()
      The children in screen order.
      Returns:
      an unmodifiable list
    • accept

      public <R> R accept(EntryVisitor<R> visitor)
      As its header: a Button if collapsible, a Label if not.
      Specified by:
      accept in class Entry
      Type Parameters:
      R - the visitor's result type
      Parameters:
      visitor - the visitor, usually a backend's widget factory
      Returns:
      what the visitor returned