Documentation
¶
Overview ¶
Package appicon reads a running application's own icon, as pixels, from pure Go with CGO_ENABLED=0.
It exists because an interface that offers a person a list of what is running — a switcher, a gallery, a picker for which window goes where — is a list of NAMES until it has the icons, and a name is not how anybody recognises an application. Every system that shows one shows the icon: the Dock, ⌘-Tab, Mission Control.
The icon belongs to whoever wrote the application. This package does not draw one, guess one, or ship artwork: it asks the window server for the icon that application is already showing in the Dock, and hands over the pixels.
px, err := appicon.ForPID(pid, 64)
if err == nil {
cell.Image = toolkit.NewImageRGBA(px.Pix, px.W, px.H)
}
It needs no permission of any kind: NSRunningApplication's icon is public information about a running process, unlike its windows.
Index ¶
Constants ¶
const ( MinSize = 8 MaxSize = 1024 )
The sizes an icon may be asked for.
The floor is where an icon stops being recognisable and the ceiling is what macOS itself stores: an .icns holds 1024 at the largest, and asking for more scales a picture up rather than giving a better one.
Variables ¶
var ( // ErrUnsupported is what every entry point answers away from macOS. ErrUnsupported = errors.New("appicon: unsupported on this platform (macOS only)") // ErrNoApp means no running application owns that process id. A shell, a // daemon and a process that has already exited all look like this. ErrNoApp = errors.New("appicon: no running application with that process id") // ErrNoIcon means the application has one but it could not be rasterised — // an icon that is entirely a vector at a size nothing has drawn yet, or a // bitmap context the window server would not give. ErrNoIcon = errors.New("appicon: the application's icon could not be read") // ErrSize means a size outside [MinSize, MaxSize] was asked for. ErrSize = errors.New("appicon: unusable size") )
Errors this package returns.
var ErrNoSymbol = errors.New("appicon: no such system symbol")
ErrNoSymbol means this system has no symbol of that name, or the name is not one.
Functions ¶
This section is empty.
Types ¶
type Pixels ¶
Pixels is a rasterised icon: straight RGBA, 8 bits a channel, W*H*4 bytes, rows top to bottom with no padding.
Straight and not premultiplied, because that is what a toolkit's image widget takes and what a PNG holds; the conversion is done here, once, rather than left as a surprise in whatever draws it.
func ForPID ¶
ForPID returns the icon of the application owning pid, rasterised square at size pixels a side.
A size is asked for rather than inferred because an icon is a family of representations, not a picture: asking for 64 gets the 64 the designer drew, where taking "the" icon and scaling it gets a blurred 512.
func Symbol ¶ added in v0.2.0
Symbol renders one of the system's own symbols, at its own shape, no bigger than size pixels on its longer side.
It is here rather than in a package of its own because the hard half is already here: turning an NSImage into straight RGBA without cgo. An SF Symbol IS an NSImage; only the way of asking for one differs.
It exists because a menu-bar icon has to be legible at about 22 points, and a glyph drawn as an outline by a cross-platform toolkit is not the same thing as one the system draws for its own bar. MEASURED, in the item's own strip of a real menu bar: an emoji title puts 79 pixels of ink there, a toolkit's glasses outline 140 at twice the size, and the system's own "visionpro" 182.
The name is an SF Symbol's, such as "eyeglasses" or "display". A name the system does not have reports ErrNoSymbol rather than a blank picture, because a menu bar with a hole in it is worse than one with nothing in it.