focustense / StardewUI

UI/widget library for Stardew modding
MIT License
5 stars 1 forks source link

Inline templates #39

Closed focustense closed 3 days ago

focustense commented 2 weeks ago

Looking at what mods like AFS do as an example, there is probably a good case to make for lightweight "templates" that are not as sophisticated as custom views but can help promote reuse within complex views and reduce repetition.

For example, there is the problem that FormBuilder tries to solve in code. We have a pretty regularized form with:

This doesn't work so well with *repeat because it just isn't a single repeating data source, it's an entire context with different properties (of different types) and each property corresponds to a row. They just happen to be formatted in a uniform way for usability or familiarity purposes.

Templates would help with this. A hypothetical grammar would involve a <template> element with attribute replacement and <outlet> sub-elements where additional content could be injected. Similar to normal layout views, could support both default and named outlets.

Example:

<template name="form-heading">
    <banner margin="0, 0, 0, 16" text={&text} />
</template>

<template name="form-row">
    <lane layout="stretch content" padding="8, 4" vertical-content-alignment="middle">
        <label layout="400px content" text={&title} />
        <outlet />
    </lane>
</template>

<lane orientation="vertical">
    <label font="dialogue" text={#Settings.Title} />
    <form-heading text="General Settings" />
    <form-row title="Tile Radius">
        <slider min="1" max="20" interval="1" value={TileRadius} />
    </form-row>
    <form-row title="More Pirates">
        <checkbox checked={EnableMorePirates} />
    </form-row>
</lane>

With a long form, this becomes vastly easier to read and organize than the non-templated equivalent repeating the same margins, layouts, paddings, etc. over and over again.

Some rules probably have to be established for sanity: