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 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
- Variables
- func BuiltinNames() []string
- func DecodeIcon(data []byte, fileName string, size int, frac120 uint32) (*render.Icon, error)
- func DecodeImage(data []byte, fileName string, w, h int) (*render.Icon, error)
- func DeviceBox(size int, frac120 uint32) int
- func IsSymbolic(fileName string, data []byte) bool
- func Lookup(name string, size int, frac120 uint32) (string, error)
- func TintInto(dst, src *image.RGBA, tint render.Color)
- type Cache
- func (c *Cache) ApplyIconTheme(name string)
- func (c *Cache) Cached(name string, size int, frac120 uint32) bool
- func (c *Cache) FallbackName(name string) string
- func (c *Cache) Generation() uint64
- func (c *Cache) Icon(name string, size int, frac120 uint32) (*render.Icon, error)
- func (c *Cache) InvalidateTheme()
- func (c *Cache) Lookup(name string, size int, frac120 uint32) (string, error)
- func (c *Cache) OnIconThemeChanged(fn func(name string))
- func (c *Cache) OnReset(fn func())
- func (c *Cache) Peek(name string, size int, frac120 uint32) (Raster, bool)
- func (c *Cache) PeekFallback(name string) (string, bool)
- func (c *Cache) PeekNearest(name string, size int, frac120 uint32) (Raster, bool)
- func (c *Cache) Preload(name string, size int, frac120 uint32) error
- func (c *Cache) SearchPaths() []string
- func (c *Cache) SetNamespace(dir string, prefixes []string)
- func (c *Cache) SetSearchPaths(paths []string)
- func (c *Cache) SetTheme(name string)
- func (c *Cache) SymbolicIcon(name string, size int, frac120 uint32, tint render.Color) (*render.Icon, error)
- func (c *Cache) Theme() string
- func (c *Cache) Tint(r Raster, tint render.Color) *render.Icon
- func (c *Cache) Tinted(name string, size int, frac120 uint32, tint render.Color) (*render.Icon, error)
- type Dir
- type DirType
- type Raster
- type Theme
Constants ¶
const DefaultTheme = "Adwaita"
DefaultTheme is GTK's default icon theme, the one lookups start on until a setting names another.
const FileLimit = 8 << 20
FileLimit is the most an icon file or a theme index may hold.
Variables ¶
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 ¶
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 ¶
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 ¶
DeviceBox rounds a logical icon size up to device pixels at the 120-based scale frac120, the same convention as internal/scale.DeviceSize: an icon requested at 16 px on a 1.25x surface rasterizes into a 20 px box.
func IsSymbolic ¶
IsSymbolic reports whether an icon is symbolic and should follow the theme accent: its file stem carries -symbolic, or the SVG paints with currentColor.
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 the 120-based convention of internal/scale (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. widget.Icon 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 widget.NewThemeIcon resolves through, starting on DefaultTheme, as GTK does.
func New ¶
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 ¶
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 ¶ added in v0.2.1
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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())
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.
func (*Cache) Peek ¶ added in v0.2.5
Peek returns the icon if it is decoded, reading and decoding nothing: what a paint calls.
func (*Cache) PeekFallback ¶ added in v0.2.5
PeekFallback returns FallbackName's answer if it is known, reading nothing.
func (*Cache) PeekNearest ¶ added in v0.2.5
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 ¶ added in v0.2.1
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 ¶
SearchPaths returns the base directories lookups search: what SetSearchPaths set, else the XDG default.
func (*Cache) SetNamespace ¶
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 ¶
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 ¶
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.
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.
type Raster ¶ added in v0.2.5
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.