exp: golang.org/x/exp/shiny/widget Index | Files

package widget

import "golang.org/x/exp/shiny/widget"

Package widget provides graphical user interface widgets.

TODO: give an overview and some example code.

Index

Package Files

flow.go image.go label.go run.go theme.go uniform.go widget.go

Constants

const (
    Leaf      = Arity(0) // Leaf nodes have no children.
    Shell     = Arity(1) // Shell nodes have at most one child.
    Container = Arity(2) // Container nodes can have any number of children.
)
const (
    AxisNone       = Axis(0)
    AxisHorizontal = Axis(1)
    AxisVertical   = Axis(2)
    AxisBoth       = Axis(3) // AxisBoth equals AxisHorizontal | AxisVertical.
)
const DefaultDPI = 72.0

DefaultDPI is the fallback value of a theme's DPI, if the underlying context does not provide a DPI value.

Variables

var (
    // DefaultFontFaceCatalog is a catalog for a basic font face.
    DefaultFontFaceCatalog FontFaceCatalog = defaultFontFaceCatalog{}

    // DefaultPalette is the default theme's palette.
    DefaultPalette = Palette{
        Light:      &image.Uniform{C: color.RGBA{0xf5, 0xf5, 0xf5, 0xff}},
        Neutral:    &image.Uniform{C: color.RGBA{0xee, 0xee, 0xee, 0xff}},
        Dark:       &image.Uniform{C: color.RGBA{0xe0, 0xe0, 0xe0, 0xff}},
        Accent:     &image.Uniform{C: color.RGBA{0x21, 0x96, 0xf3, 0xff}},
        Foreground: &image.Uniform{C: color.RGBA{0x00, 0x00, 0x00, 0xff}},
        Background: &image.Uniform{C: color.RGBA{0xff, 0xff, 0xff, 0xff}},
    }

    // DefaultTheme uses the default DPI, FontFaceCatalog and Palette.
    //
    // The nil-valued pointer is a valid receiver for a Theme's methods.
    DefaultTheme *Theme
)

func RunWindow

func RunWindow(s screen.Screen, root *Node) error

RunWindow creates a new window for s, with the given widget tree, and runs its event loop.

type Arity

type Arity uint8

Arity is the number of children a class of nodes can have.

type Axis

type Axis uint8

Axis is zero, one or both of the horizontal and vertical axes. For example, a widget may be scrollable in one of the four AxisXxx values.

type Class

type Class interface {
    // Arity returns the number of children a specific node can have.
    Arity(n *Node) Arity

    // Measure sets n.MeasuredSize to the natural size, in pixels, of a
    // specific node (and its children) of this class.
    Measure(n *Node, t *Theme)

    // Layout lays out a specific node (and its children) of this class,
    // setting the Node.Rect fields of each child. The n.Rect field should have
    // previously been set during the parent node's layout.
    Layout(n *Node, t *Theme)

    // Paint paints a specific node (and its children) of this class onto a
    // destination image. origin is the parent widget's origin with respect to
    // the dst image's origin; n.Rect.Add(origin) will be n's position and size
    // in dst's coordinate space.
    //
    // TODO: add a clip rectangle? Or rely on the RGBA.SubImage method to pass
    // smaller dst images?
    Paint(n *Node, t *Theme, dst *image.RGBA, origin image.Point)
}

Class is a class of nodes. For example, all button widgets would be Nodes whose Class values have the buttonClass type.

type ContainerClassEmbed

type ContainerClassEmbed struct{}

ContainerClassEmbed is designed to be embedded in struct types that implement the Class interface and have Container arity. It provides default implementations of the Class interface's methods.

func (ContainerClassEmbed) Arity

func (ContainerClassEmbed) Arity(n *Node) Arity

func (ContainerClassEmbed) Layout

func (ContainerClassEmbed) Layout(n *Node, t *Theme)

func (ContainerClassEmbed) Measure

func (ContainerClassEmbed) Measure(n *Node, t *Theme)

func (ContainerClassEmbed) Paint

func (ContainerClassEmbed) Paint(n *Node, t *Theme, dst *image.RGBA, origin image.Point)

type Flow

type Flow struct{ *Node }

Flow is a container widget that lays out its children in sequence along an axis, either horizontally or vertically. The children's laid out size may differ from their natural size, along or across that axis, if a child's LayoutData is a FlowLayoutData.

func NewFlow

func NewFlow(a Axis) Flow

NewFlow returns a new Flow widget.

func (Flow) Axis

func (o Flow) Axis() Axis

func (Flow) SetAxis

func (o Flow) SetAxis(v Axis)

type FlowLayoutData

type FlowLayoutData struct {
    // ExpandAlongWeight is the relative weight for distributing any excess
    // space along the Flow's axis. For example, if an AxisHorizontal Flow's
    // Rect width was 100 pixels greater than the sum of its children's natural
    // widths, and three children had non-zero FlowLayoutData.ExpandAlongWeight
    // values 6, 3 and 1, then those children's laid out widths would be larger
    // than their natural widths by 60, 30 and 10 pixels.
    ExpandAlongWeight int

    // ExpandAcross is whether the child's laid out size should expand to fill
    // the Flow's cross-axis. For example, if an AxisHorizontal Flow's Rect
    // height was 80 pixels, any child whose FlowLayoutData.ExpandAcross was
    // true would also be laid out with at least an 80 pixel height.
    ExpandAcross bool
}

FlowLayoutData is the Node.LayoutData type for a Flow's children.

type FontFaceCatalog

type FontFaceCatalog interface {
    AcquireFontFace(FontFaceOptions) font.Face
    ReleaseFontFace(FontFaceOptions, font.Face)
}

FontFaceCatalog provides a theme's font faces.

AcquireFontFace returns a font.Face. ReleaseFontFace should be called, with the same options, once a widget's measure, layout or paint is done with the font.Face returned.

A FontFaceCatalog is safe for use by multiple goroutines simultaneously, but in general, a font.Face is not safe for concurrent use, as its methods may re-use implementation-specific caches and mask image buffers.

type FontFaceOptions

type FontFaceOptions struct {
    Style  font.Style
    Weight font.Weight
}

FontFaceOptions allows asking for font face variants, such as style (e.g. italic) or weight (e.g. bold).

TODO: include font.Hinting and font.Stretch typed fields?

TODO: include font size? If so, directly as "12pt" or indirectly as an enum (Heading1, Heading2, Body, etc)?

type Image

type Image struct{ *Node }

Image is a leaf widget that paints an image.Image.

func NewImage

func NewImage(src image.Image, srcRect image.Rectangle) Image

NewImage returns a new Image widget for the part of a source image defined by src and srcRect.

func (Image) SetSrc

func (o Image) SetSrc(v image.Image)

func (Image) SetSrcRect

func (o Image) SetSrcRect(v image.Rectangle)

func (Image) Src

func (o Image) Src() image.Image

func (Image) SrcRect

func (o Image) SrcRect() image.Rectangle

type Label

type Label struct{ *Node }

Label is a leaf widget that holds a text label.

func NewLabel

func NewLabel(text string) Label

NewLabel returns a new Label widget.

func (Label) SetText

func (o Label) SetText(v string)

func (Label) Text

func (o Label) Text() string

type LeafClassEmbed

type LeafClassEmbed struct{}

LeafClassEmbed is designed to be embedded in struct types that implement the Class interface and have Leaf arity. It provides default implementations of the Class interface's methods.

func (LeafClassEmbed) Arity

func (LeafClassEmbed) Arity(n *Node) Arity

func (LeafClassEmbed) Layout

func (LeafClassEmbed) Layout(n *Node, t *Theme)

func (LeafClassEmbed) Measure

func (LeafClassEmbed) Measure(n *Node, t *Theme)

func (LeafClassEmbed) Paint

func (LeafClassEmbed) Paint(n *Node, t *Theme, dst *image.RGBA, origin image.Point)

type Node

type Node struct {
    // 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 AppendChild and
    // RemoveChild methods, which keeps the tree structure consistent.
    Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node

    // Class is class-specific data and behavior for this node. For example, a
    // buttonClass-typed value may store an image and some text in this field.
    // A progressBarClass-typed value may store a numerical percentage.
    // Different class types would paint their nodes differently.
    Class Class

    // LayoutData is layout-specific data for this node. Its type is determined
    // by its parent node's class. 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 Class.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 Class.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
}

Node is an element of a widget tree.

Every element of a widget tree is a node, but nodes can be of different classes. For example, a Flow node (i.e. one whose Class is FlowClass) can contain two Button nodes and an Image node.

func (*Node) AppendChild

func (n *Node) AppendChild(c *Node)

AppendChild adds a node c as a child of n.

It will panic if c already has a parent or siblings.

func (*Node) Arity

func (n *Node) Arity() Arity

Arity calls n.Class.Arity with n as its first argument.

func (*Node) Layout

func (n *Node) Layout(t *Theme)

Layout calls n.Class.Layout with n as its first argument.

func (*Node) Measure

func (n *Node) Measure(t *Theme)

Measure calls n.Class.Measure with n as its first argument.

func (*Node) Paint

func (n *Node) Paint(t *Theme, dst *image.RGBA, origin image.Point)

Paint calls n.Class.Paint with n as its first argument.

func (*Node) RemoveChild

func (n *Node) RemoveChild(c *Node)

RemoveChild removes a node c that is a child of n. Afterwards, c will have no parent and no siblings.

It will panic if c's parent is not n.

type Palette

type Palette struct {
    // Light, Neutral and Dark are three color tones used to fill in widgets
    // such as buttons, menu bars and panels.
    Light   *image.Uniform
    Neutral *image.Uniform
    Dark    *image.Uniform

    // Accent is the color used to accentuate selections or suggestions.
    Accent *image.Uniform

    // Foreground is the color used for text, dividers and icons.
    Foreground *image.Uniform

    // Background is the color used behind large blocks of text. Short,
    // non-editable label text will typically be on the Neutral color.
    Background *image.Uniform
}

Palette provides a theme's color palette.

The colors are expressed as *image.Uniform values so that they can be easily passed as the src argument to image/draw functions.

type ShellClassEmbed

type ShellClassEmbed struct{}

ShellClassEmbed is designed to be embedded in struct types that implement the Class interface and have Shell arity. It provides default implementations of the Class interface's methods.

func (ShellClassEmbed) Arity

func (ShellClassEmbed) Arity(n *Node) Arity

func (ShellClassEmbed) Layout

func (ShellClassEmbed) Layout(n *Node, t *Theme)

func (ShellClassEmbed) Measure

func (ShellClassEmbed) Measure(n *Node, t *Theme)

func (ShellClassEmbed) Paint

func (ShellClassEmbed) Paint(n *Node, t *Theme, dst *image.RGBA, origin image.Point)

type Theme

type Theme struct {
    // DPI is the screen resolution, in dots (i.e. pixels) per inch.
    //
    // A zero value means to use the DefaultDPI.
    DPI float64

    // FontFaceCatalog provides a theme's font faces.
    //
    // A zero value means to use the DefaultFontFaceCatalog.
    FontFaceCatalog FontFaceCatalog

    // Palette provides a theme's color palette.
    //
    // A zero value means to use the DefaultPalette.
    Palette *Palette
}

Theme is used for measuring, laying out and painting widgets. It consists of a screen DPI resolution, a set of font faces and colors.

func (*Theme) AcquireFontFace

func (t *Theme) AcquireFontFace(o FontFaceOptions) font.Face

AcquireFontFace calls the same method on the result of GetFontFaceCatalog.

func (*Theme) Convert

func (t *Theme) Convert(v unit.Value, to unit.Unit) unit.Value

Convert implements the unit.Converter interface.

func (*Theme) GetDPI

func (t *Theme) GetDPI() float64

GetDPI returns the theme's DPI, or the default DPI if the field value is zero.

func (*Theme) GetFontFaceCatalog

func (t *Theme) GetFontFaceCatalog() FontFaceCatalog

GetFontFaceCatalog returns the theme's font face catalog, or the default catalog if the field value is zero.

func (*Theme) GetPalette

func (t *Theme) GetPalette() *Palette

GetPalette returns the theme's palette, or the default palette if the field value is zero.

func (*Theme) Pixels

func (t *Theme) Pixels(v unit.Value) fixed.Int26_6

Pixels implements the unit.Converter interface.

func (*Theme) ReleaseFontFace

func (t *Theme) ReleaseFontFace(o FontFaceOptions, f font.Face)

ReleaseFontFace calls the same method on the result of GetFontFaceCatalog.

type Uniform

type Uniform struct{ *Node }

Uniform is a leaf widget that paints a uniform color, analogous to an image.Uniform.

func NewUniform

func NewUniform(c color.Color, naturalWidth, naturalHeight unit.Value) Uniform

NewUniform returns a new Uniform widget of the given color and natural size. Its parent widget may lay it out at a different size than its natural size, such as expanding to fill a panel's width.

func (Uniform) Color

func (o Uniform) Color() color.Color

func (Uniform) NaturalHeight

func (o Uniform) NaturalHeight() unit.Value

func (Uniform) NaturalWidth

func (o Uniform) NaturalWidth() unit.Value

func (Uniform) SetColor

func (o Uniform) SetColor(v color.Color)

func (Uniform) SetNaturalHeight

func (o Uniform) SetNaturalHeight(v unit.Value)

func (Uniform) SetNaturalWidth

func (o Uniform) SetNaturalWidth(v unit.Value)

Package widget imports 11 packages (graph). Updated about 23 hours ago. Refresh now. Tools for package owners.