icon

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package icon provides a vector path icon system for the gogpu/ui toolkit.

Icons are defined as sequences of path commands (MoveTo, LineTo, CubicTo, Close) within a square viewbox (typically 24x24). The system scales icons to fit any display size while maintaining crisp stroked lines.

Built-in Icons

The package includes common Material-style icons as package-level variables:

Custom Icons

Define custom icons by constructing IconData with a slice of PathOp:

star := icon.IconData{
    Name:    "star",
    ViewBox: 24,
    Ops: []icon.PathOp{
        icon.Move(12, 2),
        icon.Line(15, 9),
        icon.Line(22, 9),
        // ... more ops
        icon.ClosePath(),
    },
}

Widget

Use NewIcon to create a display widget:

w := icon.NewIcon(icon.Check, icon.Size(32), icon.Color(widget.ColorGreen))

Direct Drawing

Use Draw to render an icon directly to a canvas:

icon.Draw(canvas, icon.Settings, bounds, widget.ColorBlack)

Index

Constants

This section is empty.

Variables

View Source
var Add = IconData{
	Name:    "add",
	ViewBox: defaultViewBox,
	Ops: []PathOp{
		Move(12, 5), Line(12, 19),
		Move(5, 12), Line(19, 12),
	},
}

Add is a plus icon.

View Source
var ArrowBack = IconData{
	Name:    "arrow_back",
	ViewBox: defaultViewBox,
	Ops: []PathOp{
		Move(20, 12), Line(4, 12),
		Move(4, 12), Line(10, 6),
		Move(4, 12), Line(10, 18),
	},
}

ArrowBack is a left-pointing arrow.

View Source
var Check = IconData{
	Name:    "check",
	ViewBox: defaultViewBox,
	Ops: []PathOp{
		Move(5, 12), Line(10, 17), Line(19, 7),
	},
}

Check is a checkmark icon.

View Source
var ChevronDown = IconData{
	Name:    "chevron_down",
	ViewBox: defaultViewBox,
	Ops: []PathOp{
		Move(7, 9), Line(12, 14), Line(17, 9),
	},
}

ChevronDown is a downward-pointing chevron.

View Source
var ChevronRight = IconData{
	Name:    "chevron_right",
	ViewBox: defaultViewBox,
	Ops: []PathOp{
		Move(9, 7), Line(14, 12), Line(9, 17),
	},
}

ChevronRight is a right-pointing chevron.

View Source
var Close = IconData{
	Name:    "close",
	ViewBox: defaultViewBox,
	Ops: []PathOp{
		Move(6, 6), Line(18, 18),
		Move(18, 6), Line(6, 18),
	},
}

Close is an X mark icon (two diagonal lines).

View Source
var Delete = IconData{
	Name:    "delete",
	ViewBox: defaultViewBox,
	Ops: []PathOp{

		Move(5, 7), Line(19, 7),
		Move(9, 7), Line(9, 5), Line(15, 5), Line(15, 7),

		Move(7, 7), Line(8, 19), Line(16, 19), Line(17, 7),

		Move(10, 9), Line(10, 17),
		Move(12, 9), Line(12, 17),
		Move(14, 9), Line(14, 17),
	},
}

Delete is a simplified trash can icon.

View Source
var Menu = IconData{
	Name:    "menu",
	ViewBox: defaultViewBox,
	Ops: []PathOp{
		Move(4, 7), Line(20, 7),
		Move(4, 12), Line(20, 12),
		Move(4, 17), Line(20, 17),
	},
}

Menu is a hamburger menu icon (three horizontal lines).

View Source
var Search = IconData{
	Name:    "search",
	ViewBox: defaultViewBox,
	Ops: []PathOp{

		Move(15.5, 10),
		Line(14.9, 12.4), Line(12.9, 14.2), Line(10.5, 14.7),
		Line(8.1, 14.2), Line(6.1, 12.4), Line(5.5, 10),
		Line(6.1, 7.6), Line(8.1, 5.8), Line(10.5, 5.3),
		Line(12.9, 5.8), Line(14.9, 7.6), Line(15.5, 10),

		Move(14, 14), Line(19, 19),
	},
}

Search is a magnifying glass icon.

View Source
var Settings = IconData{
	Name:    "settings",
	ViewBox: defaultViewBox,
	Ops: []PathOp{

		Move(12, 3), Line(14, 5), Line(16, 5), Line(19, 8),
		Line(19, 10), Line(21, 12), Line(19, 14), Line(19, 16),
		Line(16, 19), Line(14, 19), Line(12, 21), Line(10, 19),
		Line(8, 19), Line(5, 16), Line(5, 14), Line(3, 12),
		Line(5, 10), Line(5, 8), Line(8, 5), Line(10, 5),
		ClosePath(),

		Move(14, 12), Line(13.4, 13.4), Line(12, 14),
		Line(10.6, 13.4), Line(10, 12), Line(10.6, 10.6),
		Line(12, 10), Line(13.4, 10.6), Line(14, 12),
	},
}

Settings is a simplified gear icon.

Functions

func Draw

func Draw(canvas widget.Canvas, data IconData, bounds geometry.Rect, color widget.Color)

Draw renders an icon's path operations onto the canvas within the given bounds, using the specified color.

The icon is uniformly scaled to fit the bounds while preserving aspect ratio (the viewbox is square). The stroke width scales proportionally.

Draw can be called directly for custom rendering, or is used internally by IconWidget.

Types

type Command

type Command uint8

Command identifies a path drawing operation within an icon definition.

const (
	// CmdMoveTo moves the current point without drawing. Params: [x, y].
	CmdMoveTo Command = iota

	// CmdLineTo draws a straight line from the current point. Params: [x, y].
	CmdLineTo

	// CmdCubicTo draws a cubic Bezier curve. Params: [cx1, cy1, cx2, cy2, x, y].
	CmdCubicTo

	// CmdClose closes the current sub-path by drawing a line back to the
	// starting point of the sub-path. Params: unused.
	CmdClose
)

func (Command) String

func (c Command) String() string

String returns a human-readable name for the command.

type IconData

type IconData struct {
	// Name is a human-readable identifier for the icon (e.g. "close", "check").
	Name string

	// ViewBox is the side length of the square coordinate space in which the
	// path operations are defined. Typical value is 24 (Material Design).
	ViewBox float32

	// Ops is the ordered sequence of path operations that define the icon shape.
	Ops []PathOp
}

IconData defines a vector icon as a sequence of path operations within a square viewbox.

IconData is a value type: it is safe to copy, compare by name, and store in maps. The Ops slice is shared between copies; callers must not mutate the slice after constructing the IconData.

type IconOption

type IconOption func(*iconConfig)

IconOption configures an IconWidget.

func Color

func Color(color widget.Color) IconOption

Color sets the icon stroke color.

func ColorSignal

func ColorSignal(sig state.ReadonlySignal[widget.Color]) IconOption

ColorSignal binds the icon color to a reactive signal. When set, the signal value takes precedence over Color.

func Label

func Label(text string) IconOption

Label sets the accessibility label for the icon.

func Size

func Size(s float32) IconOption

Size sets the display size of the icon in logical pixels. The icon is rendered as a square of side length s.

type IconWidget

type IconWidget struct {
	widget.WidgetBase
	// contains filtered or unexported fields
}

IconWidget is a display widget that renders a vector icon.

IconWidget implements widget.Widget and a11y.Accessible.

Create an IconWidget with NewIcon.

func NewIcon

func NewIcon(data IconData, opts ...IconOption) *IconWidget

NewIcon creates a new icon widget displaying the given icon data.

w := icon.NewIcon(icon.Check, icon.Size(32), icon.Color(widget.ColorGreen))

func (*IconWidget) AccessibilityActions

func (w *IconWidget) AccessibilityActions() []a11y.Action

AccessibilityActions returns nil. Icons have no actions.

func (*IconWidget) AccessibilityHint

func (w *IconWidget) AccessibilityHint() string

AccessibilityHint returns an empty string.

func (*IconWidget) AccessibilityLabel

func (w *IconWidget) AccessibilityLabel() string

AccessibilityLabel returns the icon's accessibility label. Falls back to the icon name if no label was set.

func (*IconWidget) AccessibilityRole

func (w *IconWidget) AccessibilityRole() a11y.Role

AccessibilityRole returns a11y.RoleImage.

func (*IconWidget) AccessibilityState

func (w *IconWidget) AccessibilityState() a11y.State

AccessibilityState returns the default accessibility state.

func (*IconWidget) AccessibilityValue

func (w *IconWidget) AccessibilityValue() string

AccessibilityValue returns an empty string.

func (*IconWidget) Children

func (w *IconWidget) Children() []widget.Widget

Children returns nil. Icon is a leaf widget.

func (*IconWidget) Data

func (w *IconWidget) Data() IconData

Data returns the icon data being displayed.

func (*IconWidget) Draw

func (w *IconWidget) Draw(_ widget.Context, canvas widget.Canvas)

Draw renders the icon to the canvas.

func (*IconWidget) Event

func (w *IconWidget) Event(_ widget.Context, _ event.Event) bool

Event returns false. Icon widgets do not consume events.

func (*IconWidget) IconColor

func (w *IconWidget) IconColor() widget.Color

IconColor returns the resolved icon color.

Resolution priority: ColorSignal > Color.

func (*IconWidget) IconLabel

func (w *IconWidget) IconLabel() string

IconLabel returns the accessibility label.

func (*IconWidget) IconSize

func (w *IconWidget) IconSize() float32

IconSize returns the display size in logical pixels.

func (*IconWidget) Layout

func (w *IconWidget) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size

Layout returns the icon's preferred size (a square of side [IconSize]).

func (*IconWidget) Mount

func (w *IconWidget) Mount(ctx widget.Context)

Mount creates signal bindings for push-based invalidation.

func (*IconWidget) Unmount

func (w *IconWidget) Unmount()

Unmount is called when the icon widget is removed from the widget tree.

type PathOp

type PathOp struct {
	Cmd    Command
	Params [maxParams]float32
}

PathOp is a single drawing operation in an icon's path definition.

The number of significant parameters depends on the command:

func ClosePath

func ClosePath() PathOp

ClosePath creates a Close path operation.

func Cubic

func Cubic(cx1, cy1, cx2, cy2, x, y float32) PathOp

Cubic creates a CubicTo path operation with control points (cx1, cy1), (cx2, cy2) and endpoint (x, y).

func Line

func Line(x, y float32) PathOp

Line creates a LineTo path operation.

func Move

func Move(x, y float32) PathOp

Move creates a MoveTo path operation.

type Registry

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

Registry is a thread-safe named icon registry.

Use DefaultRegistry to access the global registry, which is pre-populated with all built-in icons. Create a custom registry with NewRegistry for isolated icon sets.

func DefaultRegistry

func DefaultRegistry() *Registry

DefaultRegistry returns the global icon registry.

The default registry is pre-populated with all built-in icons. Additional icons can be registered at any time using Registry.Register.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty icon registry.

func (*Registry) Get

func (r *Registry) Get(name string) (IconData, bool)

Get returns the icon with the given name and true if found, or a zero IconData and false otherwise.

func (*Registry) Len

func (r *Registry) Len() int

Len returns the number of icons in the registry.

func (*Registry) Names

func (r *Registry) Names() []string

Names returns all registered icon names in no particular order.

func (*Registry) Register

func (r *Registry) Register(icon IconData)

Register adds an icon to the registry, keyed by its Name field.

If an icon with the same name already exists, it is replaced.

Jump to

Keyboard shortcuts

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