Skip to Content
Onevra

Theming

Onevra's light and dark palettes, the system appearance and themes of your own.

Onevra's palettes pair warm white and warm charcoal surfaces with a terracotta accent. They are ordinary MyGo ui.Theme values, so every MyGo widget keeps working with them. Switch these docs between light and dark to see both:

Loading the live example…

theme_palette.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type palette struct{}

// View shows the frame theme's colors, which follow the light or dark
// appearance: switch the site's theme to see the other palette.
func (palette) View(c *ui.Context) {
	t := c.Theme()
	swatches := []struct {
		name  string
		color ui.Color
	}{
		{"Background", t.Background}, {"Surface", t.Surface}, {"Border", t.Border},
		{"Text", t.Text}, {"TextMuted", t.TextMuted}, {"Accent", t.Accent},
		{"Success", t.Success}, {"Warning", t.Warning}, {"Danger", t.Danger},
	}
	ui.Row(c).Wrap().Gap(t.Space(3)).Justify(ui.Center).Children(func() {
		for _, s := range swatches {
			ui.Column(c).Key(s.name).Width(96).Gap(t.Space(1.5)).Children(func() {
				ui.Box(c).FillWidth().Height(48).Radius(t.Radius).
					Background(s.color).Border(1, t.Border)
				components.Text(c, s.name).FontSize(t.Rem(0.8))
			})
		}
	})
}
Source

Install the theme

The application owns the theme. Pass theme.Onevra to a shell:

plain.NewShell(plain.ShellOptions{
	Theme:   theme.Onevra,
	Content: app.view,
})

A window without a shell installs it at the top of its root builder, before creating any element:

func (a *app) view(c *ui.Context) {
	theme.Apply(c)
	// ...
}

Components read the frame's theme and never set it themselves.

Light, dark or system

theme.Onevra follows the window's appearance. Choose that appearance with MyGo, which also covers native chrome such as the title bar:

mygo.Theme.SetSource(mygo.ThemeSystem) // or mygo.ThemeLight, mygo.ThemeDark

Call it before mygo.App.Run to apply a saved preference from the first frame. Like MyGo's default theme, Onevra's follows the system's text size and increased contrast.

Use the theme in your own views

Read colors and spacing from c.Theme() rather than hard-coding them, so custom views match the controls in both appearances:

t := c.Theme()
ui.Column(c).Padding(t.Space(4)).Gap(t.Space(2)).
	Radius(t.Radius).Background(t.Surface).Border(1, t.Border)

t.Space(n) and t.Rem(n) scale with the user's text size.

A theme of your own

Theme takes any function returning a *ui.Theme. Start from Onevra's palette and change what you need:

func brand(c *ui.Context) *ui.Theme {
	t := theme.Onevra(c)
	t.Accent = ui.Hex("#2563eb")
	t.AccentHover = t.Accent.Mix(ui.RGB(0, 0, 0), 0.12)
	return t
}

It runs every frame, so it can follow application state.

API

Onevra

func Onevra(c *ui.Context) *ui.Theme

Onevra returns the palette matching the frame's light or dark appearance, which follows mygo.Theme's source. Like MyGo's default theme, it follows the desktop's text scale and higher contrast; it keeps Onevra's own accent rather than the desktop's.

Its signature fits a Shell's Theme option. Windows a Shell does not create install it with Apply.

pkg.go.dev

Apply

func Apply(c *ui.Context)

Apply installs Onevra for the current frame. Call it at the start of the root content builder, before creating any widgets: SetTheme only affects the window root and the widgets created after the call. MyGo reruns the frame when the appearance or the desktop's preferences change.

pkg.go.dev

Palette

func Palette(dark bool) *ui.Theme

Palette returns Onevra's dark palette when dark is set, and its light palette otherwise. It ignores the desktop's preferences; see Onevra.

pkg.go.dev

LightPalette

func LightPalette() *ui.Theme

LightPalette returns Onevra's light palette.

pkg.go.dev

DarkPalette

func DarkPalette() *ui.Theme

DarkPalette returns Onevra's dark palette. MyGo widgets branch on the Dark flag, so it stays set.

pkg.go.dev
Edit this page on GitHub

On this page