fyne_iconkit

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 19 Imported by: 3 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

fyne_iconkit

CI

Shared runtime and code generator behind the Fyne icon packages

You normally use one of those packages. Use fyne_iconkit directly to build your own icon package. Requires Go 1.21+ and Fyne 2.3+.

Icon sets

package myicons

import (
	"embed"
	"image/color"

	"fyne.io/fyne/v2"
	"github.com/timzifer/fyne_iconkit"
)

//go:generate go run github.com/timzifer/fyne_iconkit/cmd/iconconst -dir icons -type icon -prefix Icon -out icons_gen.go

// icon is unexported and only created by the generated variables (IconHome, ...)
// or Lookup, so callers cannot pass names that do not exist.
type icon struct{ name string }

//go:embed icons
var icons embed.FS

// NewStainableSet for monochrome icons using "currentColor",
// NewSet for multi-colored icons that are served as they are.
var set = fyne_iconkit.NewStainableSet(icons, "icons")

func Icon(i icon, stain ...color.Color) fyne.Resource {
	return set.MustIcon(i.name, stain...)
}

func Lookup(name string) (icon, bool) {
	if !set.Has(name) {
		return icon{}, false
	}
	return icon{name}, true
}

A Set is addressed by plain names; wrapping packages hide that behind their icon type. It offers:

Method Description
Icon(name, stain...) cached fyne.Resource; stainable sets return a StainableResource (default color: theme foreground)
MustIcon(name, stain...) like Icon, logs via fyne.LogError and falls back to theme.ErrorIcon()
Source(name) / StainedSource(name, c) raw or stained SVG bytes
PNG(name, size, stain...) rasterized PNG, size = longer edge in pixels
Names() / Has(name) list / check icons

Unknown icons yield errors wrapping fs.ErrNotExist.

Stroked icons and Fyne's recoloring

Fyne recolors SVG icons by replacing their fill colors, e.g. in disabled buttons and menu items, important buttons, selected tabs and theme.NewThemedResource/NewColoredResource/NewDisabledResource. Stroked icons like Tabler or Lucide do not survive that: strokes lose their attributes and <line>/<polyline> elements are dropped, so icons turn into blobs or vanish. Fyne also draws strokes with a fixed width regardless of the icon size.

Icon therefore serves stainable icons as Outline: strokes are converted into filled shapes once per icon (about 1 ms, then cached). Outlined icons are recolored correctly and scale like Fyne's own icons; only the opacity of translucent parts is lost when Fyne recolors them. Source and StainedSource still return the original SVG, and PNG scales strokes with the requested size.

Generated icon variables

cmd/iconconst writes one variable per *.svg file, e.g. IconArrowUp = icon{"arrow-up"}. Struct values are used instead of string constants because an untyped string literal would still be accepted wherever a string-based type is expected.

Flag Default
-dir icons directory with the SVG files
-out icons_gen.go output file
-pkg $GOPACKAGE package name
-type icon struct type with a single string field, declared in the package
-prefix Icon identifier prefix
-upper false gb-eng → FlagGB_ENG instead of FlagGbEng

Importing icon set releases

Package iconupdate imports releases of an icon set published on npm. An icon package describes its source in a small command and runs it with go generate before iconconst:

// internal/tablerupdate/main.go
func main() {
	iconupdate.Main(iconupdate.Source{
		Name:    "Tabler Icons",
		URL:     "https://tabler.io/icons",
		Package: "@tabler/icons",
		Dirs:    map[string]string{"package/icons/outline/": "", "package/icons/filled/": "-filled"},
		License: "package/LICENSE",
		Aliases: fetchAliases, // renames published by the icon set
		// ... file names, version constant, Go package
	})
}

Each run downloads a release (integrity-checked), replaces the SVGs and the license, and keeps renamed icons as // Deprecated: aliases until the next major release of the icon set. Renames come from the icon set's own alias data, from names (cart-x → shopping-cart-x), from identical looks and from manual hints in the state file. Path data that oksvg cannot parse (compact arc flags) is normalized. Every release adds a section to CHANGELOG.md listing renamed, removed, added and visibly changed icons; -release-notes extracts it for GitHub releases. See fyne_tabler and fyne_lucide.

License

MIT

Documentation

Overview

Package fyne_iconkit provides the shared runtime behind the fyne_* icon packages: loading SVG icons from an fs.FS, caching them as fyne.Resource, staining monochrome icons with an arbitrary color and rasterizing them to PNG.

Icon packages usually wrap a single package-level Set around an embedded directory and generate name constants with cmd/iconconst.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Outline added in v0.3.0

func Outline(src []byte) (out []byte, err error)

Outline converts a monochrome SVG into one that only consists of filled <path> elements with fill="currentColor": strokes become filled outlines. Translucent parts get a fill-opacity.

Fyne recolors SVGs by replacing their fill colors. On the way it drops stroke colors, stroke attributes of the root element and elements like <line> or <polyline>, so stroked icons vanish or turn into blobs in theme.NewThemedResource, NewColoredResource, NewDisabledResource and in widgets that use them (disabled buttons and menu items, important buttons, selected tabs). Outlined icons are recolored correctly, like Fyne's own icons; only the opacity of translucent parts is lost.

Fyne also does not scale stroke widths with the rendered size; outlines scale like any other shape.

Curves are flattened to lines that deviate at most 1/100 of a view box unit from the exact shape.

func Stain

func Stain(src []byte, c color.Color) []byte

Stain replaces every "currentColor" in src with c. Translucent colors add fill-opacity/stroke-opacity attributes, because SVG attributes do not reliably support colors with alpha.

Types

type Set

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

Set is a collection of SVG icons stored in a directory of an fs.FS. All methods are safe for concurrent use.

func NewSet

func NewSet(fsys fs.FS, dir string) *Set

NewSet returns a Set of multi-colored icons (e.g. flags). Icons are served as they are; stain colors passed to Icon are ignored.

func NewStainableSet

func NewStainableSet(fsys fs.FS, dir string) *Set

NewStainableSet returns a Set of monochrome icons that use "currentColor". Icons are always stained before use; without an explicit color the theme's foreground color at the time of the call is used.

func (*Set) Has

func (s *Set) Has(name string) bool

Has reports whether the set contains an icon with the given name.

func (*Set) Icon

func (s *Set) Icon(name string, stainColor ...color.Color) (fyne.Resource, error)

Icon returns the named icon as a cached fyne.Resource.

For stainable sets the result is a StainableResource colored with the first stain color, or with the current theme foreground color if none is given. Its content is the Outline of the icon, so Fyne can recolor it (e.g. in disabled buttons or theme.NewThemedResource) and scales its strokes. For other sets stain colors are ignored.

func (*Set) MustIcon

func (s *Set) MustIcon(name string, stainColor ...color.Color) fyne.Resource

MustIcon is like Icon but logs the error via fyne.LogError and returns theme.ErrorIcon() if the icon cannot be loaded.

func (*Set) Names

func (s *Set) Names() []string

Names returns the sorted names of all icons in the set.

func (*Set) PNG

func (s *Set) PNG(name string, size int, stainColor ...color.Color) ([]byte, error)

PNG rasterizes the named icon to PNG. size is the length of the longer edge in pixels; the aspect ratio of the SVG view box is preserved.

Icons of stainable sets are stained with the first stain color, or black if none is given. For other sets stain colors are ignored.

func (*Set) Source

func (s *Set) Source(name string) ([]byte, error)

Source returns the unmodified SVG content of the named icon. Names are the file names without ".svg". Unknown names yield an error wrapping fs.ErrNotExist.

func (*Set) StainedSource

func (s *Set) StainedSource(name string, c color.Color) ([]byte, error)

StainedSource returns the SVG content of the named icon with every "currentColor" replaced by c.

type StainableResource

type StainableResource interface {
	fyne.Resource

	// StainColor returns the color the resource is currently stained with.
	StainColor() color.Color
	// Stain returns the same icon stained with c.
	Stain(c color.Color) (fyne.Resource, error)
	// MustStain is like Stain but returns theme.ErrorIcon() on failure.
	MustStain(c color.Color) fyne.Resource
}

StainableResource is a monochrome icon resource that can be re-colored.

Directories

Path Synopsis
cmd
iconconst command
Command iconconst generates one Go variable per *.svg icon in a directory.
Command iconconst generates one Go variable per *.svg icon in a directory.
Package iconupdate imports releases of an icon set published on npm into an icon package built on fyne_iconkit, keeping renamed icons usable.
Package iconupdate imports releases of an icon set published on npm into an icon package built on fyne_iconkit, keeping renamed icons usable.
internal
constgen
Package constgen generates Go variables for the icons in a directory.
Package constgen generates Go variables for the icons in a directory.

Jump to

Keyboard shortcuts

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