icons

package
v0.3.23 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 26 Imported by: 2 modules, 0 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.

Documentation

Overview

Icon-theme following (#64): the cache applies live icon-theme switches delivered by the portal monitor (portalsettings), which the application wires in. This is a lookup refresh, never a palette swap — the "toolkit never flips its own theme" rule is untouched (docs/architecture.md "Non-goals"); what changes is which theme's files icon names resolve against.

Package icons implements the freedesktop icon theme spec in pure Go: index.theme parsing with inheritance, the size/threshold directory lookup with hicolor fallback, and a per-(name, size, scale) raster cache with symbolic icon recoloring. It is the lookup half of lys-ui's widget.NewThemeIcon; the SVG/PNG decoding itself lives in render.

Search paths follow the basedir spec: the legacy ~/.icons, $XDG_DATA_HOME/icons, every $XDG_DATA_DIRS entry's icons subdirectory, and the /usr/share/pixmaps fallback. A Cache carries its own paths so tests can point it at a fixture tree; nil restores the environment default.

Index

Constants

View Source
const DefaultTheme = "Adwaita"

DefaultTheme is GTK's default icon theme, the one lookups start on until a setting names another.

View Source
const FileLimit = 8 << 20

FileLimit is the most an icon file or a theme index may hold.

Variables

View Source
var ErrNotFound = errors.New("icons: icon not found")

ErrNotFound reports that no theme in the chain, nor any unthemed fallback directory, holds the requested icon.

Functions

func BuiltinNames

func BuiltinNames() []string

BuiltinNames lists the freedesktop names the bundled set serves, sorted; the Lucide names themselves resolve too.

func DecodeIcon

func DecodeIcon(data []byte, fileName string, size int, frac120 uint32) (*render.Icon, error)

DecodeIcon decodes icon data named by fileName for a size x size logical box on a surface at the 120-based scale frac120: rasterized into the DeviceBox(size, frac120) pixel box and marked with that scale, so it draws one to one there and covers size logical pixels anywhere.

func DecodeImage

func DecodeImage(data []byte, fileName string, w, h int) (*render.Icon, error)

DecodeImage decodes icon data named by fileName into a w x h pixel box. The extension picks the codec: .png, .svg, or .svgz (gzipped SVG); anything else sniffs the PNG magic and falls back to SVG. A currentColor paint decodes as white - pass the result through render.Icon.Tint, Cache.SymbolicIcon, or Cache.Tinted to color it.

func DeviceBox

func DeviceBox(size int, frac120 uint32) int

DeviceBox rounds a logical icon size up to device pixels at the 120-based scale frac120: an icon requested at 16 px on a 1.25x surface rasterizes into a 20 px box.

func IsSymbolic

func IsSymbolic(fileName string, data []byte) bool

IsSymbolic reports whether an icon is symbolic and should follow the theme accent: its file stem carries -symbolic, or the SVG paints with currentColor.

func Lookup

func Lookup(name string, size int, frac120 uint32) (string, error)

Lookup resolves name at logical size and 120-based device scale frac120 and returns the matched file's path. It walks the default cache's theme chain; on a Cache use its Lookup method.

func TintInto

func TintInto(dst, src *image.RGBA, tint render.Color)

TintInto writes src recolored to tint into dst, a raster of its size: the pixels render.Icon.Tint makes, without a raster of their own.

Types

type Cache

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

Cache resolves themed icons and caches the decoded rasters, one per (name, size, scale) key with the scale in 120ths (120 is 1x, 240 is 2x). It is safe for concurrent use.

SetTheme, SetSearchPaths and InvalidateTheme drop every cached raster, bump Generation, and run the OnReset listeners; the application follows the desktop's icon theme through the settings portal and applies each switch with ApplyIconTheme. A consumer watches Generation so its next damage pass picks a change up.

mu is only ever held to look and to store: reads, decodes and index builds run outside it, so a paint's Peek never waits for the disk.

func Default

func Default() *Cache

Default returns the process-wide cache lys-ui's widget.NewThemeIcon resolves through, starting on DefaultTheme, as GTK does.

func New

func New(name string) *Cache

New returns a cache for the theme called name; empty means the default chain alone (Adwaita, then hicolor). Lookups search the standard XDG paths until SetSearchPaths overrides them.

func (*Cache) ApplyIconTheme

func (c *Cache) ApplyIconTheme(name string)

ApplyIconTheme switches the theme lookups resolve against and drops every cached raster (bumping Generation, which live themed widgets watch): the follower's half of a live icon-theme switch. An empty name — a broken or missing setting — keeps the previous theme and logs Warn, the degraded-but-running convention; a name equal to the current theme is a no-op. OnIconThemeChanged listeners hear about every applied switch.

func (*Cache) Cached

func (c *Cache) Cached(name string, size int, frac120 uint32) bool

Cached reports whether a paint of this icon would find it decoded. It never waits: with the lock held elsewhere the answer is no, and the caller asks again.

func (*Cache) FallbackName

func (c *Cache) FallbackName(name string) string

FallbackName is the first of name and its dash-shortened forms that resolves, GThemedIcon's default fallbacks: a missing preferences-system-network tries preferences-system, then preferences. A -symbolic suffix stays on every form. It returns name itself when none resolves. It may read the disk; the answer is kept for PeekFallback until a reset.

func (*Cache) Generation

func (c *Cache) Generation() uint64

Generation counts cache-wide resets: every SetTheme, SetSearchPaths, or InvalidateTheme call bumps it. Widgets compare their resolved generation against it to notice a theme switch. It never waits.

func (*Cache) Icon

func (c *Cache) Icon(name string, size int, frac120 uint32) (*render.Icon, error)

Icon returns the icon called name at logical size in its natural colors, rasterized into a DeviceBox(size, frac120) pixel box. The returned icon is shared between callers and cache entries; treat it as read-only.

func (*Cache) InvalidateTheme

func (c *Cache) InvalidateTheme()

InvalidateTheme drops every cached raster and bumps Generation: the hook for a theme switch or for icon files installed or removed under the search paths, since a miss is cached as firmly as a hit.

func (*Cache) Lookup

func (c *Cache) Lookup(name string, size int, frac120 uint32) (string, error)

Lookup resolves name like the package function, against this cache's theme and search paths, per the icon theme spec's lookup algorithm: for each theme of the inheritance chain (hicolor last), first any directory whose size rule accepts the request, then the nearest existing one, then the unthemed fallback across the base directories, and last the bundled set - whose matches return a "builtin:" path rather than a file.

func (*Cache) OnIconThemeChanged

func (c *Cache) OnIconThemeChanged(fn func(name string))

OnIconThemeChanged registers fn to run with the applied theme name after each applied icon-theme switch (ApplyIconTheme), once the cache has switched and bumped Generation. Callbacks run on the caller's goroutine — with a portal monitor feeding the cache that is the monitor's; bridge into the loop with Application.Invoke, where the usual reaction is requesting a repaint. Registering twice runs both per change.

func (*Cache) OnReset

func (c *Cache) OnReset(fn func()) (unsubscribe func())

OnReset registers fn to run after every reset (SetTheme, SetSearchPaths, InvalidateTheme, an applied ApplyIconTheme), once Generation has moved. Callbacks run on the caller's goroutine; bridge into the loop with Application.Invoke, where the usual reaction is requesting a repaint. The returned unsubscribe takes this registration off the cache again, so what registered it (an application closing) is no longer held by a cache that outlives it; calling it twice does nothing more. A reset another goroutine has already begun may still run fn once after unsubscribe returns. A nil fn registers nothing.

func (*Cache) Peek

func (c *Cache) Peek(name string, size int, frac120 uint32) (Raster, bool)

Peek returns the icon if it is decoded, reading and decoding nothing: what a paint calls.

func (*Cache) PeekFallback

func (c *Cache) PeekFallback(name string) (string, bool)

PeekFallback returns FallbackName's answer if it is known, reading nothing.

func (*Cache) PeekNearest

func (c *Cache) PeekNearest(name string, size int, frac120 uint32) (Raster, bool)

PeekNearest returns the decoded raster of name at size whose scale is nearest frac120, reading nothing: what a paint shows while the raster for its own scale decodes. Failures are not offered.

func (*Cache) Preload

func (c *Cache) Preload(name string, size int, frac120 uint32) error

Preload resolves and decodes an icon into the cache for a later paint. It holds the lock only to look and to store, never across the read and the decode, so a UI loop painting other icons meanwhile does not wait for it. A failure is remembered as a paint's would be, and returned, as it is to a later Preload of the same icon.

func (*Cache) SearchPaths

func (c *Cache) SearchPaths() []string

SearchPaths returns the base directories lookups search: what SetSearchPaths set, else the XDG default.

func (*Cache) SetNamespace

func (c *Cache) SetNamespace(dir string, prefixes []string)

SetNamespace declares names starting with one of prefixes and a dash to be the application's own, kept in dir (relative) below the base directories: a lookup of one finds it there, at any size and scale, without listing the theme chain, which is what costs a first lookup its thousand directories, so a theme cannot override it. One found nowhere there resolves as any other name. Changing it drops every cached raster.

func (*Cache) SetSearchPaths

func (c *Cache) SetSearchPaths(paths []string)

SetSearchPaths overrides the base directories lookups search (the slice is copied); nil restores the XDG default. Changing paths drops every cached raster.

func (*Cache) SetTheme

func (c *Cache) SetTheme(name string)

SetTheme switches the theme icons resolve against (empty means the default chain alone) and drops every cached raster.

func (*Cache) SymbolicIcon

func (c *Cache) SymbolicIcon(name string, size int, frac120 uint32, tint render.Color) (*render.Icon, error)

SymbolicIcon returns the icon called name, mask-recolored to tint when the matched source is symbolic - a -symbolic file or an SVG painting with currentColor - so symbolic glyphs follow the theme accent while ordinary app icons keep their colors.

func (*Cache) Theme

func (c *Cache) Theme() string

Theme returns the theme this cache looks icons up in.

func (*Cache) Tint

func (c *Cache) Tint(r Raster, tint render.Color) *render.Icon

Tint is r, which Peek returned without failure, recolored to tint: a raster every caller with this tint shares.

func (*Cache) Tinted

func (c *Cache) Tinted(name string, size int, frac120 uint32, tint render.Color) (*render.Icon, error)

Tinted returns the icon called name mask-recolored to tint whatever it matched: every pixel keeps its alpha and takes tint's color.

type Dir

type Dir struct {
	// Path is the directory relative to the theme root, as named in
	// the Directories or ScaledDirectories list: "16x16/apps".
	Path      string
	Size      int
	MinSize   int
	MaxSize   int
	Threshold int
	Scale     int
	Type      DirType
}

Dir is one icon subdirectory declared by a theme's index.theme.

type DirType

type DirType uint8

DirType is an icon directory's size-matching rule from index.theme.

const (
	// Threshold matches within Size ± Threshold; it is the spec
	// default when a directory declares no Type.
	Threshold DirType = iota
	// Fixed matches exactly Size.
	Fixed
	// Scalable matches everything between MinSize and MaxSize.
	Scalable
)

type Raster

type Raster struct {
	Icon     *render.Icon
	Symbolic bool
	Err      error
	// contains filtered or unexported fields
}

Raster is one decoded icon: its natural raster, or the failure that stuck in its place. It is shared between callers: read-only.

type Theme

type Theme struct {
	// Name is the display name from the [Icon Theme] section.
	Name string
	// DirName is the theme's directory name - the path component
	// icons live under. It is what lookups join on, not Name.
	DirName  string
	Inherits []string
	Dirs     []Dir
}

Theme is a parsed index.theme: the display name, the themes it inherits, and the icon directories it declares.

Jump to

Keyboard shortcuts

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