node

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2021 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package node provides the structure for a tree of heterogenous widget nodes.

Most programmers should not need to import this package, only the top-level widget package. Only those that write custom widgets need to explicitly refer to the Node, Embed and related types.

The Node interface is usually implemented by struct types that embed one of LeafEmbed, ShellEmbed or ContainerEmbed (all of which themselves embed an Embed), providing default implementations of all of Node's methods.

The split between an outer wrapper (Node) interface type and an inner wrappee (Embed) struct type enables heterogenous nodes, such as a buttons and labels, in a widget tree where every node contains common fields such as position, size and tree structure links (parent, siblings and children).

In a traditional object-oriented type system, this might be represented by the Button and Label types both subclassing the Node type. Go does not have inheritance, so the outer / inner split is composed explicitly. For example, the concrete Button type is a struct type that embeds an XxxEmbed (such as LeafEmbed), and the NewButton function sets the inner Embed's Wrapper field to point back to the outer value.

There are three layers here (Button embeds LeafEmbed embeds Embed) instead of two. The intermediate layer exists because there needs to be a place to provide default implementations of methods like Measure, but that place shouldn't be the inner-most type (Embed), otherwise it'd be too easy to write subtly incorrect code like:

for c := w.FirstChild; c != nil; c = c.NextSibling {
	c.Measure(etc) // This should instead be c.Wrapper.Measure(etc).
}

In any case, most programmers that want to construct a widget tree should not need to know this detail. It usually suffices to call functions such as widget.NewButton or widget.NewLabel, and then parent.Insert(button, nil).

See the example/gallery program for some example code for a custom widget.

Index

Constants

View Source
const (
	NotHandled = EventHandled(false)
	Handled    = EventHandled(true)
)
View Source
const (
	// MarkNeedsMeasureLayout marks this node as needing Measure and Layout
	// calls.
	MarkNeedsMeasureLayout = Marks(1 << 0)

	// MarkNeedsPaint marks this node as needing a Paint call.
	MarkNeedsPaint = Marks(1 << 1)

	// MarkNeedsPaintBase marks this node as needing a PaintBase call.
	MarkNeedsPaintBase = Marks(1 << 2)
)
View Source
const NoHint = -1

NoHint means that there is no width or height hint in a Measure call.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContainerEmbed

type ContainerEmbed struct{ Embed }

ContainerEmbed is designed to be embedded in struct types for nodes with any number of children.

func (*ContainerEmbed) Insert

func (m *ContainerEmbed) Insert(c, nextSibling Node)

func (*ContainerEmbed) Layout

func (m *ContainerEmbed) Layout(t *theme.Theme)

func (*ContainerEmbed) Measure

func (m *ContainerEmbed) Measure(t *theme.Theme, widthHint, heightHint int)

func (*ContainerEmbed) OnChildMarked

func (m *ContainerEmbed) OnChildMarked(child Node, newMarks Marks)

func (*ContainerEmbed) OnInputEvent

func (m *ContainerEmbed) OnInputEvent(e interface{}, origin image.Point) EventHandled

func (*ContainerEmbed) OnLifecycleEvent

func (m *ContainerEmbed) OnLifecycleEvent(e lifecycle.Event)

func (*ContainerEmbed) Paint

func (m *ContainerEmbed) Paint(ctx *PaintContext, origin image.Point) error

func (*ContainerEmbed) PaintBase

func (m *ContainerEmbed) PaintBase(ctx *PaintBaseContext, origin image.Point) error

func (*ContainerEmbed) Remove

func (m *ContainerEmbed) Remove(c Node)

type Embed

type Embed struct {
	// Wrapper is the outer type that wraps (embeds) this type. It should not
	// be nil.
	Wrapper Node

	// Parent, FirstChild, LastChild, PrevSibling and NextSibling describe the
	// widget tree structure.
	//
	// These fields are exported to enable walking the node tree, but they
	// should not be modified directly. Instead, call the Insert and Remove
	// methods, which keeps the tree structure consistent.
	Parent, FirstChild, LastChild, PrevSibling, NextSibling *Embed

	// LayoutData is layout-specific data for this node. Its type is determined
	// by its parent node's type. For example, each child of a Flow may hold a
	// FlowLayoutData in this field.
	LayoutData interface{}

	// MeasuredSize is the widget's natural size, in pixels, as calculated by
	// the most recent Measure call.
	MeasuredSize image.Point

	// Rect is the widget's position and actual (as opposed to natural) size,
	// in pixels, as calculated by the most recent Layout call on its parent
	// node. A parent may lay out a child at a size different to its natural
	// size in order to satisfy a layout constraint, such as a row of buttons
	// expanding to fill a panel's width.
	//
	// The position (Rectangle.Min) is relative to its parent node. This is not
	// necessarily the same as relative to the screen's, window's or image
	// buffer's origin.
	Rect image.Rectangle

	// Marks are a bitfield of node state, such as whether it needs measure,
	// layout or paint.
	Marks Marks
}

Embed is the common data structure for each node in a widget tree.

func (*Embed) Mark

func (m *Embed) Mark(marks Marks)

func (*Embed) Wrappee

func (m *Embed) Wrappee() *Embed

type EventHandled

type EventHandled bool

EventHandled is whether or not an input event (a key, mouse, touch or gesture event) was handled by a widget. If it was not handled, the event is propagated along the widget tree.

type LeafEmbed

type LeafEmbed struct{ Embed }

LeafEmbed is designed to be embedded in struct types for nodes with no children.

func (*LeafEmbed) Insert

func (m *LeafEmbed) Insert(c, nextSibling Node)

func (*LeafEmbed) Layout

func (m *LeafEmbed) Layout(t *theme.Theme)

func (*LeafEmbed) Measure

func (m *LeafEmbed) Measure(t *theme.Theme, widthHint, heightHint int)

func (*LeafEmbed) OnChildMarked

func (m *LeafEmbed) OnChildMarked(child Node, newMarks Marks)

func (*LeafEmbed) OnInputEvent

func (m *LeafEmbed) OnInputEvent(e interface{}, origin image.Point) EventHandled

func (*LeafEmbed) OnLifecycleEvent

func (m *LeafEmbed) OnLifecycleEvent(e lifecycle.Event)

func (*LeafEmbed) Paint

func (m *LeafEmbed) Paint(ctx *PaintContext, origin image.Point) error

func (*LeafEmbed) PaintBase

func (m *LeafEmbed) PaintBase(ctx *PaintBaseContext, origin image.Point) error

func (*LeafEmbed) Remove

func (m *LeafEmbed) Remove(c Node)

type Marks

type Marks uint32

Marks are a bitfield of node state, such as whether it needs measure, layout or paint.

func (Marks) NeedsMeasureLayout

func (m Marks) NeedsMeasureLayout() bool

func (Marks) NeedsPaint

func (m Marks) NeedsPaint() bool

func (Marks) NeedsPaintBase

func (m Marks) NeedsPaintBase() bool

func (*Marks) UnmarkNeedsMeasureLayout

func (m *Marks) UnmarkNeedsMeasureLayout()

func (*Marks) UnmarkNeedsPaint

func (m *Marks) UnmarkNeedsPaint()

func (*Marks) UnmarkNeedsPaintBase

func (m *Marks) UnmarkNeedsPaintBase()

type Node

type Node interface {
	// Wrappee returns the inner (embedded) type that is wrapped by this type.
	Wrappee() *Embed

	// Insert adds a node c as a child of this node. If nextSibling is nil, c
	// will be inserted at the end of this node's children. Otherwise, c will
	// be inserted such that its next sibling is nextSibling.
	//
	// It will panic if c already has a parent or siblings.
	Insert(c, nextSibling Node)

	// Remove removes a node c that is a child of this node. Afterwards, c will
	// have no parent and no siblings.
	//
	// It will panic if c's parent is not this node.
	Remove(c Node)

	// Measure sets this node's Embed.MeasuredSize to its natural size, taking
	// its children into account.
	//
	// Some nodes' natural height might depend on their imposed width, such as
	// a text widget word-wrapping its contents. The caller may provide hints
	// that the parent can override the child's natural size in the width,
	// height or both directions. A negative value means that there is no hint.
	// For example, a container might lay out its children to all have the same
	// width, and could pass that width as the widthHint argument.
	Measure(t *theme.Theme, widthHint, heightHint int)

	// Layout lays out this node (and its children), setting the Embed.Rect
	// fields of each child. This node's Embed.Rect field should have
	// previously been set during the parent node's layout.
	Layout(t *theme.Theme)

	// Paint paints this node (and its children). Painting is split into two
	// passes: a base pass and an effects pass. The effects pass is often a
	// no-op, and the bulk of the work is typically done in the base pass.
	//
	// The base pass paints onto an *image.RGBA pixel buffer and ancestor nodes
	// may choose to re-use the result. For example, re-painting a text widget
	// after scrolling may copy cached buffers at different offsets, instead of
	// painting the text's glyphs onto a fresh buffer. Similarly, animating the
	// scale and opacity of an overlay can re-use the buffer from a previous
	// base pass.
	//
	// The effects pass paints that part of the widget that can not or should
	// not be cached. For example, the border of a text widget shouldn't move
	// on the screen when that text widget is scrolled. The effects pass does
	// not have a destination RGBA pixel buffer, and is limited to what a
	// screen.Drawer provides: affine-transformed textures and uniform fills.
	//
	// TODO: app-specific OpenGL, if available, should be part of the effects
	// pass. Is that exposed via the screen.Drawer or by another mechanism?
	//
	// The Paint method may create base pass RGBA pixel buffers, by calling
	// ctx.Screen.NewBuffer. Many implementations won't, and instead assume
	// that PaintBase is recursively triggered by an ancestor node such as a
	// widget.Sheet. If it does create those RGBA pixel buffers, it is also
	// responsible for calling PaintBase on this node (and its children). In
	// any case, the Paint method should then paint any effects. Many widgets
	// will neither create their own buffers nor have any effects, so their
	// Paint methods will simply be the default implemention: do nothing except
	// call Paint on its children. As mentioned above, the bulk of the work is
	// typically done in PaintBase.
	//
	// origin is the parent widget's origin with respect to the ctx.Src2Dst
	// transformation matrix; this node's Embed.Rect.Add(origin) will be its
	// position and size in pre-transformed coordinate space.
	Paint(ctx *PaintContext, origin image.Point) error

	// PaintBase paints the base pass of this node (and its children) onto an
	// RGBA pixel buffer.
	//
	// origin is the parent widget's origin with respect to the ctx.Dst image's
	// origin; this node's Embed.Rect.Add(origin) will be its position and size
	// in ctx.Dst's coordinate space.
	PaintBase(ctx *PaintBaseContext, origin image.Point) error

	// Mark adds the given marks to this node. It calls OnChildMarked on its
	// parent if new marks were added.
	Mark(m Marks)

	// OnChildMarked handles a child being given new marks. By default, marks
	// are propagated up the node tree towards the root. For example, a child
	// being marked for needing paint will cause the parent being marked for
	// needing paint.
	OnChildMarked(child Node, newMarks Marks)

	// OnLifecycleEvent propagates a lifecycle event to a node (and its
	// children).
	OnLifecycleEvent(e lifecycle.Event)

	// OnInputEvent handles a key, mouse, touch or gesture event.
	//
	// origin is the parent widget's origin with respect to the event origin;
	// this node's Embed.Rect.Add(origin) will be its position and size in
	// event coordinate space.
	OnInputEvent(e interface{}, origin image.Point) EventHandled
}

Node is a node in the widget tree.

type PaintBaseContext

type PaintBaseContext struct {
	Theme *theme.Theme
	Dst   *image.RGBA
}

PaintBaseContext is the context for the Node.PaintBase method.

type PaintContext

type PaintContext struct {
	Theme   *theme.Theme
	Screen  screen.Screen
	Drawer  screen.Drawer
	Src2Dst f64.Aff3
}

PaintContext is the context for the Node.Paint method.

type ShellEmbed

type ShellEmbed struct{ Embed }

ShellEmbed is designed to be embedded in struct types for nodes with at most one child.

func (*ShellEmbed) Insert

func (m *ShellEmbed) Insert(c, nextSibling Node)

func (*ShellEmbed) Layout

func (m *ShellEmbed) Layout(t *theme.Theme)

func (*ShellEmbed) Measure

func (m *ShellEmbed) Measure(t *theme.Theme, widthHint, heightHint int)

func (*ShellEmbed) OnChildMarked

func (m *ShellEmbed) OnChildMarked(child Node, newMarks Marks)

func (*ShellEmbed) OnInputEvent

func (m *ShellEmbed) OnInputEvent(e interface{}, origin image.Point) EventHandled

func (*ShellEmbed) OnLifecycleEvent

func (m *ShellEmbed) OnLifecycleEvent(e lifecycle.Event)

func (*ShellEmbed) Paint

func (m *ShellEmbed) Paint(ctx *PaintContext, origin image.Point) error

func (*ShellEmbed) PaintBase

func (m *ShellEmbed) PaintBase(ctx *PaintBaseContext, origin image.Point) error

func (*ShellEmbed) Remove

func (m *ShellEmbed) Remove(c Node)

Jump to

Keyboard shortcuts

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