Documentation
¶
Overview ¶
Package rt is the support library that code written by `oneui generate` calls. It is for generated code only: it is public because generated files live in your package, but it is outside the compatibility promise of the module. Its shape follows what the generator needs, it is versioned together with the generator (RuntimeVersion), and a minor release may change it. Regenerate (go generate) after upgrading; a generated file built against a runtime of another version stops at start-up with an error that says so. Nothing here is meant to be called from application code.
A Scope is a region of a component's UI with its own bindings: the component's top level, each branch of an if block, each row of an each block, the content a parent passes into a slot. A binding reads the component's state, remembers the text, bool or string it last wrote, and writes only when that changes. State changes name the bindings to run by a bit mask, so an update touches only the nodes bound to what changed.
Everything here runs on the goroutine that owns the oneui.Context.
Index ¶
- Constants
- func Attr(s *Scope, n oneui.NodeID, name string, mask uint64, always bool, ...)
- func AttrOpt(s *Scope, n oneui.NodeID, name string, mask uint64, always bool, ...)
- func BindChecked(s *Scope, n oneui.NodeID, mask uint64, always bool, get func() bool, ...)
- func BindValue(s *Scope, n oneui.NodeID, event string, mask uint64, always bool, ...)
- func BoolAttr(s *Scope, n oneui.NodeID, name string, mask uint64, always bool, f func() bool)
- func Class(s *Scope, n oneui.NodeID, name string, mask uint64, always bool, f func() bool)
- func Each[K comparable, T any](s *Scope, parent, before oneui.NodeID, top bool, mask uint64, always bool, ...)
- func Format(v any) string
- func If(s *Scope, parent, before oneui.NodeID, top bool, mask uint64, always bool, ...)
- func Listen(s *Scope, n oneui.NodeID, typ string, h func(*oneui.Event))
- func Note(*Scope, oneui.NodeID, string, int, int)
- func Prop(s *Scope, n oneui.NodeID, name string, mask uint64, always bool, ...)
- func Ref(s *Scope, r *oneui.ElementRef, n oneui.NodeID)
- func RenderChild(h *HTML, child any)
- func Require(version int, file string)
- func SetHTMLSlots(child interface{ ... }, slots HTMLSlots)
- func SetSlots(child interface{ ... }, slots Slots)
- func Style(s *Scope, n oneui.NodeID, prop string, mask uint64, always bool, ...)
- func Text(s *Scope, mask uint64, always bool, f func() string) oneui.NodeID
- func Update(c interface{ ... }, dirty uint64)
- type HTML
- func (h *HTML) Attr(s string)
- func (h *HTML) Bool(b bool)
- func (h *HTML) ClassClose()
- func (h *HTML) ClassOn(name string)
- func (h *HTML) ClassOpen()
- func (h *HTML) ClassValue(s string)
- func (h *HTML) Close() error
- func (h *HTML) Int(n int)
- func (h *HTML) Raw(s string)
- func (h *HTML) Slot(c interface{ ... }, name string, fallback func())
- func (h *HTML) StyleClose()
- func (h *HTML) StyleDecl(prop, value string)
- func (h *HTML) StyleOpen()
- func (h *HTML) Text(s string)
- func (h *HTML) Write(p []byte) (int, error)
- type HTMLSlots
- type Row
- type Scope
- func (s *Scope) Bind(mask uint64, always bool, fn func())
- func (s *Scope) Components() []oneui.Component
- func (s *Scope) Dispose()
- func (s *Scope) DisposeKeeping(keep func(oneui.NodeID) bool)
- func (s *Scope) Keep(n oneui.NodeID)
- func (s *Scope) Mount(parent oneui.NodeID, child oneui.Component, before oneui.NodeID, top bool)
- func (s *Scope) Nodes() []oneui.NodeID
- func (s *Scope) Place(parent, n, before oneui.NodeID)
- func (s *Scope) Run(dirty uint64)
- func (s *Scope) Slot(name string, parent, before oneui.NodeID, top bool, fallback func())
- func (s *Scope) Sub() *Scope
- func (s *Scope) Watch(mask uint64, always bool, fn func())
- type SlotFill
- type Slots
Constants ¶
const RowBit uint64 = 1 << 63
RowBit is the pseudo state bit set while a row of an each block runs after its item or index may have changed. A binding that reads a row variable includes it in its mask. State fields use bits 0 to 62.
const RuntimeVersion = 1
RuntimeVersion is the version of the contract between generated code and this package. The generator writes it into every generated file and the file checks it with Require when the program starts.
Variables ¶
This section is empty.
Functions ¶
func BindChecked ¶
func BindChecked(s *Scope, n oneui.NodeID, mask uint64, always bool, get func() bool, set func(bool))
BindChecked is bind:checked for checkbox and radio inputs.
func BindValue ¶
func BindValue(s *Scope, n oneui.NodeID, event string, mask uint64, always bool, get func() string, set func(string))
BindValue is bind:value: it keeps the value property of n equal to get() and calls set with the control's value on each event of type event.
func Each ¶
func Each[K comparable, T any](s *Scope, parent, before oneui.NodeID, top bool, mask uint64, always bool, items func() []T, key func(T, int) K, build func(*Scope, *Row[T], oneui.NodeID, oneui.NodeID))
Each builds a keyed list at the current position. items returns the list and key the key of an item; rows keep their nodes while their key stays and only rows outside the longest already-ordered run are moved. The list is re-read when a bit of mask is dirty. build creates one row's nodes into the parent before the sibling it is given. An empty text node marks the end of the block.
func Format ¶
Format renders a template value as text with no locale: nil is empty, a bool is "true" or "false", integers are decimal, floats use the shortest representation that round-trips, a fmt.Stringer or error uses its method, anything else uses fmt.Sprint. Generated code uses it for {expr} text.
func If ¶
func If(s *Scope, parent, before oneui.NodeID, top bool, mask uint64, always bool, pick func() int, branches ...func(*Scope, oneui.NodeID, oneui.NodeID))
If builds a conditional block at the current position. pick returns the index of the branch to show, or -1 for none; it is re-evaluated when a bit of mask is dirty. A branch builds its nodes into the parent it is given before the sibling it is given, placing its top-level nodes with its scope. An empty text node marks the end of the block.
func Note ¶
Note records where an element was written in its template. It does nothing unless the program is built with the oneui_reload tag.
func Ref ¶
func Ref(s *Scope, r *oneui.ElementRef, n oneui.NodeID)
Ref binds an element reference to n.
func RenderChild ¶
RenderChild renders a child component into h. A component without a RenderHTML method (a hand-written one) fails the render with oneui.ErrNotRenderable.
func Require ¶
Require stops the program with a clear message when a generated file was written for another runtime version than the one linked in. file names the generated file. Generated files call it from init.
func SetHTMLSlots ¶
SetHTMLSlots stores slot content for the child about to be rendered.
func SetSlots ¶
SetSlots stores the slot content a parent passes to a child component. Call it before the child is mounted.
func Update ¶
Update runs the update scope of a generated component: it is the body of every generated Update. c is the component (any value with the Runtime method of oneui.Base). It panics with a message naming the component when its runtime slot does not hold the scope its Build stored, which happens when application code overwrote it with SetRuntime.
Types ¶
type HTML ¶
type HTML struct {
// contains filtered or unexported fields
}
HTML is the writer behind the RenderHTML method generated for each component. It buffers markup, escapes bound values and keeps the first write error. It is an io.Writer so that a component rendered inside another is handed the same HTML and writes into the same buffer.
An HTML is valid only between the NewHTML that returned it with outer true and the matching HTML.Close; do not keep it, share it between goroutines or wrap it in another writer (a wrapper is not recognised as a nested render and the child would flush on its own). Its buffers come from a pool, so concurrent renders of different components are safe.
func NewHTML ¶
NewHTML returns the HTML to render into w. When w is itself an *HTML (a component rendering a child), that one is returned with outer false, and the caller leaves flushing to whoever made it. Otherwise outer is true and the caller ends with HTML.Close.
func (*HTML) Attr ¶
Attr writes s as the value of an attribute in double quotes: & " < are escaped (the same section, attribute mode), and U+00A0 is written as the character.
func (*HTML) ClassClose ¶
func (h *HTML) ClassClose()
ClassClose writes the class attribute when there is any token.
func (*HTML) ClassOpen ¶
func (h *HTML) ClassOpen()
ClassOpen starts collecting the class tokens of an element.
func (*HTML) ClassValue ¶
ClassValue adds the tokens of a class attribute value, split on ASCII whitespace.
func (*HTML) Close ¶
Close writes what is buffered and returns the first error of the render. The HTML must not be used afterwards.
func (*HTML) Slot ¶
Slot renders the content the parent passed for the named slot, or fallback when it passed none.
func (*HTML) StyleClose ¶
func (h *HTML) StyleClose()
StyleClose ends the style attribute if any declaration opened it.
func (*HTML) StyleOpen ¶
func (h *HTML) StyleOpen()
StyleOpen starts the style attribute of an element; it is written by the first declaration.
type Row ¶
Row is one row of an each block: the current item and index, and the scope holding the row's nodes and bindings. A row's Item and Index are updated in place when the list changes, so closures that read them see current values.
type Scope ¶
type Scope struct {
// Ctx is the context the component is mounted in.
Ctx *oneui.Context
// Owner is the component the scope belongs to.
Owner *oneui.Base
// contains filtered or unexported fields
}
Scope is a region of the UI with its own bindings. Create the root scope of a component with NewScope in its Build.
func NewScope ¶
NewScope returns the root scope of a component that is being built, stores it with the component and arranges for it to be torn down when the component is unmounted. Slot content a parent stored with SetSlots is picked up here.
func (*Scope) Bind ¶
Bind registers fn to run whenever a state bit in mask is dirty, or on every update when always is true, and runs it once now.
func (*Scope) Components ¶
Components lists the child components mounted in the scope and its nested scopes, in order.
func (*Scope) Dispose ¶
func (s *Scope) Dispose()
Dispose unmounts the child components of the scope, runs its cleanups and removes its top-level nodes. It is safe to call more than once.
func (*Scope) DisposeKeeping ¶
DisposeKeeping is Dispose, except that top-level nodes for which keep reports true stay in the document. Template reload uses it to carry the nodes of unchanged elements, with their typed text, focus and scroll position, into the rebuilt tree.
func (*Scope) Keep ¶
Keep records a node that is already in the document as a top-level node of the scope, so that unmounting the scope removes it. Template reload uses it for elements it carries over.
func (*Scope) Mount ¶
Mount mounts a child component into parent before the sibling before. top says the component is at the top of the scope.
func (*Scope) Place ¶
Place inserts a top-level node of the scope. Nodes that are not at the top of a scope are inserted with Context.Insert.
func (*Scope) Run ¶
Run brings the bindings whose mask intersects dirty up to date, then those of the nested scopes. It is the body of a generated Update.
func (*Scope) Slot ¶
Slot renders the content a parent passed for the named slot, or fallback when there is none. top says the slot is at the top of the scope.