import "golang.org/x/exp/shiny/widget"
Package widget provides graphical user interface widgets.
TODO: give an overview and some example code.
flow.go image.go label.go run.go theme.go uniform.go widget.go
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.
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 )
RunWindow creates a new window for s, with the given widget tree, and runs its event loop.
Arity is the number of children a class of nodes can have.
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 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 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(n *Node) Arity
func (ContainerClassEmbed) Layout(n *Node, t *Theme)
func (ContainerClassEmbed) Measure(n *Node, t *Theme)
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.
NewFlow returns a new Flow widget.
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 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.
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)?
Image is a leaf widget that paints an image.Image.
NewImage returns a new Image widget for the part of a source image defined by src and srcRect.
Label is a leaf widget that holds a text label.
NewLabel returns a new Label widget.
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(n *Node) Arity
func (LeafClassEmbed) Layout(n *Node, t *Theme)
func (LeafClassEmbed) Measure(n *Node, t *Theme)
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.
AppendChild adds a node c as a child of n.
It will panic if c already has a parent or siblings.
Arity calls n.Class.Arity with n as its first argument.
Layout calls n.Class.Layout with n as its first argument.
Measure calls n.Class.Measure with n as its first argument.
Paint calls n.Class.Paint with n as its first argument.
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 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 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(n *Node) Arity
func (ShellClassEmbed) Layout(n *Node, t *Theme)
func (ShellClassEmbed) Measure(n *Node, t *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 (t *Theme) AcquireFontFace(o FontFaceOptions) font.Face
AcquireFontFace calls the same method on the result of GetFontFaceCatalog.
Convert implements the unit.Converter interface.
GetDPI returns the theme's DPI, or the default DPI if the field value is zero.
func (t *Theme) GetFontFaceCatalog() FontFaceCatalog
GetFontFaceCatalog returns the theme's font face catalog, or the default catalog if the field value is zero.
GetPalette returns the theme's palette, or the default palette if the field value is zero.
Pixels implements the unit.Converter interface.
func (t *Theme) ReleaseFontFace(o FontFaceOptions, f font.Face)
ReleaseFontFace calls the same method on the result of GetFontFaceCatalog.
Uniform is a leaf widget that paints a uniform color, analogous to an image.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.
Package widget imports 11 packages (graph). Updated about 23 hours ago. Refresh now. Tools for package owners.