rt

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2026 License: Apache-2.0, MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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 Attr

func Attr(s *Scope, n oneui.NodeID, name string, mask uint64, always bool, f func() string)

Attr binds attribute name of n to f; the attribute is always present.

func AttrOpt

func AttrOpt(s *Scope, n oneui.NodeID, name string, mask uint64, always bool, f func() string)

AttrOpt binds attribute name of n to f; an empty value removes the attribute.

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 BoolAttr

func BoolAttr(s *Scope, n oneui.NodeID, name string, mask uint64, always bool, f func() bool)

BoolAttr binds the presence of attribute name of n to f.

func Class

func Class(s *Scope, n oneui.NodeID, name string, mask uint64, always bool, f func() bool)

Class binds whether n has class name to f.

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

func Format(v any) string

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 Listen

func Listen(s *Scope, n oneui.NodeID, typ string, h func(*oneui.Event))

Listen adds a handler for DOM events of type typ on n.

func Note

func Note(*Scope, oneui.NodeID, string, int, int)

Note records where an element was written in its template. It does nothing unless the program is built with the oneui_reload tag.

func Prop

func Prop(s *Scope, n oneui.NodeID, name string, mask uint64, always bool, f func() string)

Prop binds DOM property name ("value", "checked") of n to f.

func Ref

func Ref(s *Scope, r *oneui.ElementRef, n oneui.NodeID)

Ref binds an element reference to n.

func RenderChild

func RenderChild(h *HTML, child any)

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

func Require(version int, file string)

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

func SetHTMLSlots(child interface{ SetHTMLSlots(any) }, slots HTMLSlots)

SetHTMLSlots stores slot content for the child about to be rendered.

func SetSlots

func SetSlots(child interface{ SetRuntime(any) }, slots Slots)

SetSlots stores the slot content a parent passes to a child component. Call it before the child is mounted.

func Style

func Style(s *Scope, n oneui.NodeID, prop string, mask uint64, always bool, f func() string)

Style binds inline style property prop of n to f; an empty value removes it.

func Text

func Text(s *Scope, mask uint64, always bool, f func() string) oneui.NodeID

Text creates a text node bound to f.

func Update

func Update(c interface{ Runtime() any }, dirty uint64)

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

func NewHTML(w io.Writer) (h *HTML, outer bool)

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

func (h *HTML) Attr(s string)

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) Bool

func (h *HTML) Bool(b bool)

Bool writes "true" or "false".

func (*HTML) ClassClose

func (h *HTML) ClassClose()

ClassClose writes the class attribute when there is any token.

func (*HTML) ClassOn

func (h *HTML) ClassOn(name string)

ClassOn adds a class unless the element has it.

func (*HTML) ClassOpen

func (h *HTML) ClassOpen()

ClassOpen starts collecting the class tokens of an element.

func (*HTML) ClassValue

func (h *HTML) ClassValue(s string)

ClassValue adds the tokens of a class attribute value, split on ASCII whitespace.

func (*HTML) Close

func (h *HTML) Close() error

Close writes what is buffered and returns the first error of the render. The HTML must not be used afterwards.

func (*HTML) Int

func (h *HTML) Int(n int)

Int writes n in decimal.

func (*HTML) Raw

func (h *HTML) Raw(s string)

Raw writes trusted markup: literal template text the generator already escaped.

func (*HTML) Slot

func (h *HTML) Slot(c interface{ HTMLSlots() any }, name string, fallback func())

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) StyleDecl

func (h *HTML) StyleDecl(prop, value string)

StyleDecl writes one declaration, in the order the generator sorted them.

func (*HTML) StyleOpen

func (h *HTML) StyleOpen()

StyleOpen starts the style attribute of an element; it is written by the first declaration.

func (*HTML) Text

func (h *HTML) Text(s string)

Text writes s as character data: & < > are escaped (HTML Standard, "Serializing HTML fragments", escaping a string), except that U+00A0 is written as the character and not as &nbsp;.

func (*HTML) Write

func (h *HTML) Write(p []byte) (int, error)

Write implements io.Writer for nested rendering: p is trusted markup. It is for the generated RenderHTML of a child component; see the lifetime rules on HTML.

type HTMLSlots

type HTMLSlots map[string]func(*HTML)

HTMLSlots is the content a parent passes into the slots of a child it renders.

type Row

type Row[T any] struct {
	Item  T
	Index int
	Scope *Scope
}

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

func NewScope(ctx *oneui.Context, owner *oneui.Base) *Scope

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 ScopeOf

func ScopeOf(c oneui.Component) *Scope

ScopeOf returns the root scope of a mounted generated component, or nil.

func (*Scope) Bind

func (s *Scope) Bind(mask uint64, always bool, fn func())

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

func (s *Scope) Components() []oneui.Component

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

func (s *Scope) DisposeKeeping(keep func(oneui.NodeID) bool)

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

func (s *Scope) Keep(n oneui.NodeID)

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

func (s *Scope) Mount(parent oneui.NodeID, child oneui.Component, before oneui.NodeID, top bool)

Mount mounts a child component into parent before the sibling before. top says the component is at the top of the scope.

func (*Scope) Nodes

func (s *Scope) Nodes() []oneui.NodeID

Nodes lists the top-level nodes the scope owns now, in order.

func (*Scope) Place

func (s *Scope) Place(parent, n, before oneui.NodeID)

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

func (s *Scope) Run(dirty uint64)

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

func (s *Scope) Slot(name string, parent, before oneui.NodeID, top bool, fallback func())

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.

func (*Scope) Sub

func (s *Scope) Sub() *Scope

Sub returns a nested scope, run and torn down with s.

func (*Scope) Watch

func (s *Scope) Watch(mask uint64, always bool, fn func())

Watch registers fn like Bind, without running it now.

type SlotFill

type SlotFill func(parent, before oneui.NodeID) *Scope

SlotFill builds the content a parent passes into a slot. It creates the nodes in parent before the sibling before, in a scope of the declaring component, and returns that scope.

type Slots

type Slots map[string]SlotFill

Slots maps slot names to their content; the default slot is "".

Directories

Path Synopsis
Package reload is the part of template reload that generated code calls in programs built with the oneui_reload tag.
Package reload is the part of template reload that generated code calls in programs built with the oneui_reload tag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL