Custom widgets

Retained components, low-level drawing, and the API for building text widgets of your own.

Most screens are functions that return ui.View. For a retained component, implement ui.View:

func (p *Page) Layout(c *ui.Ctx) *layout.Element {
	return ui.Column(/* … */).Grow(1).Layout(c)
}

Chapar-style pages return ui.View from a helper also named Layout and are composed by the shell — that helper is not the ui.View interface (which must return *layout.Element).

Low-level drawing: layout.New(layout.Box().…, children…), attach Paint / OnMouse, wrap with ui.Raw. Hit-test with e.Frame.Contains(m.X, m.Y).

Building text widgets#

Text widgets outside package ui can behave like TextInput:

  • textedit holds the editing rules: WordLeft / WordRight, character classes, DoubleClickInterval and MultiClickSlop, and the undo grouping (Merge, UndoMergeGap, StartsWord).
  • syntax holds token classes, the Highlighter interface and the registry behind syntax.ForPath and syntax.ByName. Themes map the classes to colors (theme.Syntax), and Markdown code blocks use the registry. An engine registers its highlighters with syntax.RegisterDefaults; an app overrides an extension with syntax.Register.
  • Frame resources for painting outside the layout tree: ui.FrameText(), ui.FrameIcons(), ui.FrameClipboard(), and the drawing helpers ui.DrawElevationShadow, ui.PaintFocusRingInset and ui.ClampToViewport.
  • The standard edit menu: ui.EditMenuItems(t, ui.EditMenuFlags{Editable: true, Copyable: true}) for any ui.TextEditing, shown with a ui.EditMenu.
  • Hosted text inputs: a TextInput can live inside another widget's painting, as the code editor's find bar does. The host calls Embed, PaintIn, HandleMouseIn and LayoutHosted.
  • Scrollbars for your own scroll areas: ui.NewScrollbar, with Host, Gutter, Strip, Hit and Tick.

The code editor is built entirely on this API.

Edit this page on GitHub
esc