Icons
The theme supplies the icons widgets draw: the arrow on a back button, the check mark in a checkbox, the clear button in a search field. You can use the same icons in your own widgets, so they change along with the theme.
Use a theme icon
Each icon has a name, a
bellui.ThemeIconName
constant such as theme.IconNameHome. There are two ways to get it:
// Two ways to get the same icon from the current theme.
home := theme.HomeIcon()
settings := theme.Icon(theme.IconNameSettings)
toolbar := container.NewHBox(
widget.NewButtonWithIcon("Home", home, func() {}),
widget.NewButtonWithIcon("Settings", settings, func() {}),
widget.NewIcon(theme.Icon(theme.IconNameNavigateBack)),
)theme.Icon(name)looks the name up in the current theme.- The shortcut functions, such as
theme.HomeIcon(),theme.SettingsIcon()ortheme.DeleteIcon(), do the same for one icon each.
The theme package reference
lists every IconName… constant. They cover common actions (add, remove,
copy, paste, undo, search), files and folders, media controls, mail,
navigation, visibility, volume, zoom and window controls.
Some icons for common app features, each with a function and an IconName…
constant:
| For | Functions |
|---|---|
| Like and save buttons | FavoriteIcon (a filled heart), FavoriteBorderIcon (an outlined one), BookmarkIcon, BookmarkBorderIcon |
| Notices | NotificationsIcon (an outlined bell) |
| Captions | ClosedCaptionIcon (filled, captions on), ClosedCaptionOffIcon (outlined, captions off) |
| Places and people | PlaceIcon (a map pin), PeopleIcon (a group), ChildIcon (a child’s face) |
| Shopping and purchases | ShoppingCartIcon, AddShoppingCartIcon, StorefrontIcon, ShoppingBagIcon, WorkspacePremiumIcon (a medal, for a premium feature), DiamondIcon, AutorenewIcon (for a subscription that renews), CardMembershipIcon, CreditCardIcon, ReceiptLongIcon, LockIcon |
ZoomFitIcon draws Material’s “fit screen” picture, and ViewRestoreIcon
the “exit full screen” one.
Theme icons are SVG images. Most are drawn in the theme’s foreground colour, so they are dark in the light variant and light in the dark variant.
Directional icons
Some icons point in a reading direction: navigate back and next, the submenu arrow, and mail reply, reply all and forward. When the app’s layout is right-to-left, the theme draws these icons mirrored. Other icons, such as search or settings, are never mirrored. See Right-to-left layout.
Colour your own icons
To make your own SVG icon follow the theme, wrap it in a themed resource. The wrapper replaces the SVG’s fill colours with a theme colour each time it is drawn:
var (
// A 24x24 SVG drawn in black. A themed resource replaces its fill
// colour with a theme colour, so it matches light and dark variants.
starSVG = bellui.NewStaticResource("star.svg", []byte(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">`+
`<path fill="#000000" d="M12 17.3 18.2 21l-1.6-7L22 9.2l-7.2-.6L12 2 9.2 8.6 2 9.2 7.4 14l-1.6 7z"/>`+
`</svg>`))
star = theme.NewThemedResource(starSVG) // foreground colour
primaryStar = theme.NewPrimaryThemedResource(starSVG) // primary colour
errorStar = theme.NewErrorThemedResource(starSVG) // error colour
)| Constructor | Colour |
|---|---|
theme.NewThemedResource | foreground |
theme.NewPrimaryThemedResource | primary |
theme.NewErrorThemedResource | error |
theme.NewSuccessThemedResource | success |
theme.NewWarningThemedResource | warning |
theme.NewDisabledResource | disabled |
theme.NewColoredResource | any colour name you pass |
This works for SVG icons only. For anything that is not SVG, the wrapper logs an error and draws the image with its own colours.
Tint an icon with any colour
A themed resource takes its colour from the theme. For an icon that must keep
one colour of your own, such as a white icon over a photo, use
theme.NewColorizedResource(res, colour).
The colour stays the same when the theme changes. A theme icon is tinted
from its original SVG.
widget.Icon
has a Color field that does the same for the icon it shows. The icon stays
tinted when you change its resource. Leave Color nil, the default, to draw
the resource as it is, so a theme icon keeps following the theme. Call
Refresh after you change the field.
// A fixed colour, whatever the theme: for an icon over artwork.
white := color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
onArtwork := widget.NewIcon(theme.NewColorizedResource(theme.FavoriteBorderIcon(), white))
// The same with the Icon widget's Color field. It stays tinted when
// its resource changes.
like := widget.NewIcon(theme.FavoriteBorderIcon())
like.Color = color.NRGBA{R: 0xd3, G: 0x2f, B: 0x2f, A: 0xff}
like.SetResource(theme.FavoriteIcon()) // still redBoth draw a resource that is not an SVG unchanged.
Replace icons in a custom theme
A custom theme’s Icon method can return a different resource for any icon
name. Widgets that use that icon then draw yours. For an example, see the
Icon method in Custom themes. In a
PXF theme, the icons field maps icon
names to resource URIs:
icons = { play: "file:///path/to/play.svg" }The names are the string values of the IconName… constants. Most are
Material’s icon names: play for IconNameMediaPlay, add for
IconNameContentAdd, fullscreen_exit for IconNameViewRestore. A theme
file that uses an older name, such as mediaPlay or contentAdd, still
works. One name changed meaning: help is now the question mark
(IconNameQuestion), and the help icon (IconNameHelp) is help_outline.
A theme file that replaces help now replaces the question mark.
Return a themed resource from Icon if your replacement should follow the
theme colours like the built-in icons do.
The full program:
// Theme icons: looking them up, using them in widgets, and colouring your
// own SVG icon so it follows the theme.
package main
import (
"image/color"
"github.com/belluikit/bellui"
"github.com/belluikit/bellui/app"
"github.com/belluikit/bellui/canvas"
"github.com/belluikit/bellui/container"
"github.com/belluikit/bellui/theme"
"github.com/belluikit/bellui/widget"
)
var (
// A 24x24 SVG drawn in black. A themed resource replaces its fill
// colour with a theme colour, so it matches light and dark variants.
starSVG = bellui.NewStaticResource("star.svg", []byte(
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">`+
`<path fill="#000000" d="M12 17.3 18.2 21l-1.6-7L22 9.2l-7.2-.6L12 2 9.2 8.6 2 9.2 7.4 14l-1.6 7z"/>`+
`</svg>`))
star = theme.NewThemedResource(starSVG) // foreground colour
primaryStar = theme.NewPrimaryThemedResource(starSVG) // primary colour
errorStar = theme.NewErrorThemedResource(starSVG) // error colour
)
func main() {
a := app.New()
w := a.NewWindow("Icons")
// Two ways to get the same icon from the current theme.
home := theme.HomeIcon()
settings := theme.Icon(theme.IconNameSettings)
toolbar := container.NewHBox(
widget.NewButtonWithIcon("Home", home, func() {}),
widget.NewButtonWithIcon("Settings", settings, func() {}),
widget.NewIcon(theme.Icon(theme.IconNameNavigateBack)),
)
// A fixed colour, whatever the theme: for an icon over artwork.
white := color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
onArtwork := widget.NewIcon(theme.NewColorizedResource(theme.FavoriteBorderIcon(), white))
// The same with the Icon widget's Color field. It stays tinted when
// its resource changes.
like := widget.NewIcon(theme.FavoriteBorderIcon())
like.Color = color.NRGBA{R: 0xd3, G: 0x2f, B: 0x2f, A: 0xff}
like.SetResource(theme.FavoriteIcon()) // still red
w.SetContent(container.NewVBox(
toolbar,
container.NewHBox(widget.NewIcon(star), widget.NewIcon(primaryStar), widget.NewIcon(errorStar)),
container.NewHBox(container.NewStack(canvas.NewRectangle(color.NRGBA{A: 0xff}), onArtwork), like),
))
bellui.ShowAndRun(w)
}