render

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 10 Imported by: 0 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.

README

Kite Render Package

The render package manages the Visual Tree (Render Tree) for Kite. It is the critical bridge layer that sits between the logical DOM (dom package) and the layout/paint pipelines (layout and paint packages).

Intent and Philosophy

The DOM is an extremely lightweight, semantic data structure that knows nothing about CSS inheritance, physical sizes, or drawing. The layout engine is a strictly mathematical engine that operates on immutable constraints. The render package exists to glue them together.

  1. State Ownership: The render.Object tree owns all "dirty" lifecycle flags (DirtyStyle, DirtyLayout, DirtyPaint).
  2. One-Way Data Flow: The logical DOM "pushes" dirty notifications to the render tree. The render tree never modifies the logical DOM.
  3. Unified Display: In Kite's architecture, there are only two core render objects: render.Box (which handles Block, Flex, and Grid dynamically via layout algorithms) and render.Text. The render object acts as a generic container for physical properties.
  4. Style Caching: The render tree caches the fully-resolved style.Computed output, providing rapid, pre-calculated styling data to the layout and paint phases.

Core Data Structures

  • Object: The fundamental interface for all nodes in the visual tree. It enforces tree navigation, dirty flag manipulation, and access to both logical nodes and computed styles.
  • BaseRender: The unexported struct that implements the vast majority of the Object interface. Specific render node types embed BaseRender to inherit tree-walking, fragment caching, and dirty-state logic without duplicating boilerplate.
  • Box: The universal render container for elements. It holds children, background/border styles, and delegates to the appropriate LayoutNG algorithm based on its computed Display property.
  • Text: A specialized render object for textual content. It cannot have children and is managed by the Inline layout algorithms.
  • RenderView: The absolute root of the render tree. It represents the physical dimensions of the terminal viewport and coordinates global overlays.
  • DirtyFlag: A bitmask system (e.g., DirtyStyle, ChildNeedsLayout) used to isolate and optimize the engine's phases, preventing unnecessary full-tree traversals.

The Synchronization Lifecycle

The render tree must perfectly mirror the visible elements of the logical DOM. This synchronization happens seamlessly during the engine's frame loop through a "Push/Pull" model.

1. The Push (DOM Mutations)

When a developer interacts with the logical DOM (e.g., el.SetStyle(...) or el.AppendChild(...)), the DOM node does not instantiate render objects directly. Instead, it locates its currently attached render.Object and pushes a dirty flag.

  • If a style changes, it calls MarkDirty(DirtyStyle).
  • If a child is added or removed, the parent calls MarkDirty(DirtyStructure).

These flags instantly bubble up the render tree to the RenderView as "relay flags" (e.g., ChildNeedsStyle), establishing a highly optimized path for the engine to follow later.

2. The Pull (On-the-Fly Attachment)

During the engine's Style Phase (style.ResolveTree), the resolver walks top-down, skipping any subtrees that don't have ChildNeedsStyle or ChildNeedsStructure.

When it hits a node requiring structural work (a DOM node was added/removed), the walker performs an On-the-Fly Sync:

  1. Creation: If a logical DOM node has no attached render.Object, the walker immediately instantiates a render.Box (or Text), attaches it, and pulls the raw style from the DOM.
  2. Destruction (Display: None): If the computed style resolves to display: none, the walker unlinks and destroys the existing render.Object. The subtree is ignored.
  3. Pruning: If the DOM node was removed entirely, the walker detects the orphaned render.Boxes and completely unlinks that visual subtree.
3. Layout & Paint Handoff

Once the render tree is structurally sound and fully styled, the layout engine takes over. The render.Object provides the layout.Node interface required by LayoutNG.

When layout completes, the engine calls renderObject.SetCachedLayout(frag). If the new Fragment pointer differs from the previous one (meaning physical sizes actually changed), the render package automatically flags DirtyPaint, queuing it for the final terminal draw.

Documentation

Overview

Package render provides the core engine for maintaining the render tree. It bridges the logical DOM tree with the Layout and Style engines via render objects that track dirty state and computed styles.

Render objects do not own sparse style state (author styles or element defaults); they act as stateless proxies that query their underlying logical DOM node dynamically.

RenderView and Overlays

The RenderView is the root of the render tree and represents the viewport. It manages the main document flow and a list of overlay render roots that are painted on top of the main flow. LayoutPhase handles both the main tree and all active overlays in a single pass.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LayoutPhase

func LayoutPhase(ctx *layout.Context, root Object, available geom.Size)

LayoutPhase runs the layout process for the given subtree using the LayoutNG-inspired architecture.

func LayoutSubtree

func LayoutSubtree(ctx *layout.Context, root Object, space layout.ConstraintSpace) *layout.Fragment

LayoutSubtree lays out a render subtree with the given constraint space.

func Unlink(obj Object)

Unlink removes obj from its parent.

Types

type BaseRender

type BaseRender struct {
	// contains filtered or unexported fields
}

BaseRender provides a default implementation for many render.Object methods.

func (*BaseRender) CachedBlockSize

func (b *BaseRender) CachedBlockSize(width int) (int, bool)

func (*BaseRender) CachedLayout

func (b *BaseRender) CachedLayout(space layout.ConstraintSpace) *layout.Fragment

func (*BaseRender) CachedMinMaxSizes

func (b *BaseRender) CachedMinMaxSizes() (layout.MinMaxSizes, bool)

func (*BaseRender) Children

func (b *BaseRender) Children() iter.Seq[Object]

func (*BaseRender) ClearDirty

func (b *BaseRender) ClearDirty(f DirtyFlag)

func (*BaseRender) ClearDirtyLayout

func (b *BaseRender) ClearDirtyLayout()

func (*BaseRender) ClearDirtyRecursive

func (b *BaseRender) ClearDirtyRecursive(f DirtyFlag)

func (*BaseRender) ComputedStyle

func (b *BaseRender) ComputedStyle() *style.Computed

func (*BaseRender) EventTarget

func (b *BaseRender) EventTarget() event.EventTarget

func (*BaseRender) FirstChild

func (b *BaseRender) FirstChild() Object

func (*BaseRender) FirstLayoutChild

func (b *BaseRender) FirstLayoutChild() layout.Node

func (*BaseRender) Flags

func (b *BaseRender) Flags() DirtyFlag

func (*BaseRender) Fragment

func (b *BaseRender) Fragment() *layout.Fragment

func (*BaseRender) HasChildNeedsPaint

func (b *BaseRender) HasChildNeedsPaint() bool

func (*BaseRender) Init

func (b *BaseRender) Init(self Object, logicalNode dom.Node, target event.EventTarget)

Init sets the self-pointer and logical identity for the BaseRender.

func (*BaseRender) InsertChild

func (b *BaseRender) InsertChild(child, before Object)

func (*BaseRender) IsAnonymous

func (b *BaseRender) IsAnonymous() bool

func (*BaseRender) IsDetached

func (b *BaseRender) IsDetached() bool

func (*BaseRender) IsDirtyLayout

func (b *BaseRender) IsDirtyLayout() bool

func (*BaseRender) IsDirtyPaint

func (b *BaseRender) IsDirtyPaint() bool

func (*BaseRender) LastChild

func (b *BaseRender) LastChild() Object

func (*BaseRender) LogicalNode

func (b *BaseRender) LogicalNode() dom.Node

func (*BaseRender) MarkChildrenDirty

func (b *BaseRender) MarkChildrenDirty()

func (*BaseRender) MarkDirty

func (b *BaseRender) MarkDirty(f DirtyFlag)

func (*BaseRender) MaxScroll

func (b *BaseRender) MaxScroll() (x, y int)

func (*BaseRender) NextLayoutSibling

func (b *BaseRender) NextLayoutSibling(child layout.Node) layout.Node

func (*BaseRender) NextSibling

func (b *BaseRender) NextSibling() Object

func (*BaseRender) Offset

func (b *BaseRender) Offset() geom.Point

func (*BaseRender) Parent

func (b *BaseRender) Parent() Object

func (*BaseRender) PreviousSibling

func (b *BaseRender) PreviousSibling() Object

func (*BaseRender) RemoveChild

func (b *BaseRender) RemoveChild(child Object)

func (*BaseRender) SetCachedBlockSize

func (b *BaseRender) SetCachedBlockSize(width, height int)

func (*BaseRender) SetCachedLayout

func (b *BaseRender) SetCachedLayout(space layout.ConstraintSpace, frag *layout.Fragment)

func (*BaseRender) SetCachedMinMaxSizes

func (b *BaseRender) SetCachedMinMaxSizes(sizes layout.MinMaxSizes)

func (*BaseRender) SetComputedStyle

func (b *BaseRender) SetComputedStyle(c *style.Computed)

func (*BaseRender) SetOffset

func (b *BaseRender) SetOffset(p geom.Point)

func (*BaseRender) Style

func (b *BaseRender) Style() *style.Computed

type Block

type Block struct {
	BaseRender
}

Block represents a standard block-level formatting object.

func NewBlock

func NewBlock(logicalNode dom.Node, target event.EventTarget) *Block

NewBlock creates a new Block render object.

type Box

type Box struct {
	BaseRender
}

func NewBox

func NewBox(logicalNode dom.Node, target event.EventTarget) *Box

type CustomObjectProvider

type CustomObjectProvider interface {
	CreateRenderObject() Object
}

CustomObjectProvider is an interface that logical nodes can implement to provide a custom render object instead of the default render.Box.

type DirtyFlag

type DirtyFlag uint16

DirtyFlag is a bitmask that describes what work a render object (or its subtree) needs on the next frame.

All mutations to flags must happen on the main goroutine; this package provides no atomic operations.

const (
	// Clean is the zero value; no work is pending.
	Clean DirtyFlag = 0

	// DirtyStyle means the computed style is stale and must be re-resolved
	// before layout.
	DirtyStyle DirtyFlag = 1 << 0

	// DirtyLayout means the box geometry is stale and must be re-measured.
	DirtyLayout DirtyFlag = 1 << 1

	// DirtyPaint means visible output is stale and the object must be redrawn.
	DirtyPaint DirtyFlag = 1 << 2

	// DirtyScroll means the scroll offset changed. Style and layout caches are
	// preserved; only the paint walk is needed. The paint phase clears
	// DirtyScroll together with DirtyPaint.
	DirtyScroll DirtyFlag = 1 << 3

	// ChildNeedsStyle means some descendant has DirtyStyle set.
	ChildNeedsStyle DirtyFlag = 1 << 5

	// ChildNeedsLayout means some descendant has DirtyLayout.
	ChildNeedsLayout DirtyFlag = 1 << 6

	// ChildNeedsPaint means some descendant has DirtyPaint or DirtyScroll.
	ChildNeedsPaint DirtyFlag = 1 << 7
)

type NodeOrder

type NodeOrder struct {
	First int
	Last  int
}

NodeOrder tracks the pre-order traversal boundaries of a node's subtree.

type Object

type Object interface {
	// EventTarget returns the event target associated with this render object.
	// This is typically the logical DOM node.
	EventTarget() event.EventTarget

	Parent() Object
	FirstChild() Object
	LastChild() Object
	NextSibling() Object
	PreviousSibling() Object
	Children() iter.Seq[Object]

	InsertChild(child, before Object)
	RemoveChild(child Object)

	ComputedStyle() *style.Computed
	SetComputedStyle(*style.Computed)
	Flags() DirtyFlag
	MarkDirty(DirtyFlag)
	ClearDirty(DirtyFlag)
	MarkChildrenDirty()
	ClearDirtyRecursive(DirtyFlag)

	IsDetached() bool

	// layout.Node implementation (Task 05)
	Style() *style.Computed
	FirstLayoutChild() layout.Node
	NextLayoutSibling(layout.Node) layout.Node
	IsDirtyLayout() bool
	IsDirtyPaint() bool
	HasChildNeedsPaint() bool
	ClearDirtyLayout()
	Fragment() *layout.Fragment
	CachedLayout(layout.ConstraintSpace) *layout.Fragment
	SetCachedLayout(layout.ConstraintSpace, *layout.Fragment)
	CachedMinMaxSizes() (layout.MinMaxSizes, bool)
	SetCachedMinMaxSizes(layout.MinMaxSizes)
	CachedBlockSize(width int) (int, bool)
	SetCachedBlockSize(width, height int)
	LogicalNode() dom.Node

	// Offset returns the physical offset of this object relative to its parent.
	// For most objects, this is managed by the parent's layout algorithm.
	// For overlays, this is calculated by the overlay's own layout algorithm.
	Offset() geom.Point
	// SetOffset updates the physical offset of this object.
	SetOffset(geom.Point)

	// IsAnonymous reports whether this object is a virtual layout-only node.
	IsAnonymous() bool

	// MaxScroll returns the maximum horizontal and vertical scroll offsets
	// based on the current layout fragment and content extent.
	MaxScroll() (x, y int)
}

Object is the interface for all renderable objects that sit in the render tree.

type Overlay

type Overlay struct {
	Box
}

Overlay represents an anchored overlay render object.

func NewOverlay

func NewOverlay(logicalNode dom.Node, target event.EventTarget) *Overlay

NewOverlay creates a new Overlay render object.

type OverlaySelection

type OverlaySelection struct {
	Element  dom.Element
	Fragment *layout.Fragment
	Offset   geom.Point
}

type RenderObjectHook

type RenderObjectHook interface {
	OnRenderObjectCreated(ro Object)
}

RenderObjectHook is an optional interface that logical nodes can implement to be notified when their render object is created or replaced.

type RenderView

type RenderView struct {
	BaseRender
	// contains filtered or unexported fields
}

RenderView is the root of a render tree. It represents the viewport.

func NewRenderView

func NewRenderView() *RenderView

NewRenderView creates a new RenderView.

func (*RenderView) ComputedStyle

func (v *RenderView) ComputedStyle() *style.Computed

func (*RenderView) Inline

func (v *RenderView) Inline() bool

Inline reports whether inline mode is enabled.

func (*RenderView) InvalidateMediaStyles

func (v *RenderView) InvalidateMediaStyles()

func (*RenderView) IsDetached

func (v *RenderView) IsDetached() bool

func (*RenderView) Overlays

func (v *RenderView) Overlays() []Object

Overlays returns the list of overlay render trees.

func (*RenderView) SetComputedStyle

func (v *RenderView) SetComputedStyle(*style.Computed)

func (*RenderView) SetInline

func (v *RenderView) SetInline(inline bool)

SetInline configures the render view for inline mode.

func (*RenderView) SetLogicalNode

func (v *RenderView) SetLogicalNode(n dom.Node)

func (*RenderView) SetOverlays

func (v *RenderView) SetOverlays(overlays []Object)

SetOverlays replaces the list of overlay render trees.

func (*RenderView) SetViewportSize

func (v *RenderView) SetViewportSize(sz geom.Size)

SetViewportSize updates the viewport dimensions.

func (*RenderView) Style

func (v *RenderView) Style() *style.Computed

func (*RenderView) ViewportSize

func (v *RenderView) ViewportSize() geom.Size

ViewportSize returns the current viewport dimensions.

type SelectionRange

type SelectionRange interface {
	StartContainerAny() any
	EndContainerAny() any
	StartOffset() int
	EndOffset() int
	IsCollapsed() bool
	StartIndex() int
	EndIndex() int
}

SelectionRange is an interface for a selection range.

type SelectionRect

type SelectionRect struct {
	Rect geom.Rect
	Fg   color.Color
	Bg   color.Color
}

SelectionRect represents a physical rectangle of selected content.

func ResolveSelection

func ResolveSelection(
	root *layout.Fragment,
	overlays []OverlaySelection,
	sel SelectionSource,
	nodeOrder map[any]NodeOrder,
) []SelectionRect

ResolveSelection maps the active selection to physical screen rectangles.

type SelectionSource

type SelectionSource interface {
	RangeCount() int
	GetRangeAt(index int) SelectionRange
}

SelectionSource is an interface that provides access to selection ranges.

type Text

type Text struct {
	BaseRender
	// contains filtered or unexported fields
}

Text represents a text-level render object.

func NewText

func NewText(logicalNode dom.Node, target event.EventTarget) *Text

NewText creates a new Text render object.

func (*Text) CachedClusters

func (t *Text) CachedClusters() []text.Cluster

func (*Text) CachedText

func (t *Text) CachedText() string

func (*Text) Data

func (t *Text) Data() string

Data returns the text content from the logical node.

func (*Text) IsInlineLevel

func (t *Text) IsInlineLevel() bool

func (*Text) LayoutChildren

func (t *Text) LayoutChildren() iter.Seq[layout.Node]

func (*Text) SetCachedClusters

func (t *Text) SetCachedClusters(txt string, clusters []text.Cluster)

Jump to

Keyboard shortcuts

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