Settings
A searchable settings surface of categories, groups and rows, built from definitions.
Loading the live example…
import (
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
"github.com/oneo-me/onevra/source/icons/lucide"
)
type preferences struct {
state components.SettingsState
sounds, banners bool
launch bool
scale float64
name string
}
func (p *preferences) View(c *ui.Context) {
categories := []components.SettingsCategory{
{ID: "general", Title: "General", Icon: lucide.Settings(), Groups: []components.SettingsGroup{
{ID: "startup", Title: "Startup", Items: []components.SettingsItem{
{ID: "launch", Title: "Open at login", Description: "Start when you sign in.",
Build: func(c *ui.Context) {
components.Switch(c, &p.launch, "Open at login", components.SwitchOptions{HideLabel: true})
}},
}},
{ID: "profile", Title: "Profile", Items: []components.SettingsItem{
{ID: "name", Title: "Display name", Stacked: true,
Build: func(c *ui.Context) {
components.TextBox(c, &p.name, components.TextBoxOptions{}).Label("Display name").FillWidth()
}},
}},
}},
{ID: "notifications", Title: "Notifications", Icon: lucide.Bell(), Groups: []components.SettingsGroup{
{ID: "alerts", Items: []components.SettingsItem{
{ID: "sounds", Title: "Play sounds", Keywords: []string{"audio"},
Build: func(c *ui.Context) {
components.Switch(c, &p.sounds, "Play sounds", components.SwitchOptions{HideLabel: true})
}},
{ID: "banners", Title: "Show banners",
Build: func(c *ui.Context) {
components.Switch(c, &p.banners, "Show banners", components.SwitchOptions{HideLabel: true})
}},
}},
}},
{ID: "appearance", Title: "Appearance", Icon: lucide.Palette(), Groups: []components.SettingsGroup{
{ID: "text", Items: []components.SettingsItem{
{ID: "scale", Title: "Text size", Keywords: []string{"font", "zoom"}, Stacked: true,
Build: func(c *ui.Context) {
components.Slider(c, &p.scale, 80, 140, components.SliderOptions{Step: 10}).Label("Text size").FillWidth()
}},
}},
}},
}
// The host chooses the narrow layout from the space it gives Settings.
width, _ := c.Size()
components.Settings(c, &p.state, categories, components.SettingsOptions{Narrow: width < 560})
}Describe settings as categories of groups of items, each with a stable ID, a title and a builder for its control. Settings provides the navigation, search and row layout; the application keeps the values, persists them and decides when they take effect. Try searching for "audio" or "font": search matches titles, descriptions and keywords without building hidden controls.
Place Settings in a dialog, a shell or its own window; it fills a bounded parent. Narrow stacks rows and replaces the sidebar with a selector; choose it from the space Settings is given. Stacked places a wide control under its text, and Content builds a whole row.
API
Settings
func Settings(c *ui.Context, state *SettingsState, categories []SettingsCategory, opts SettingsOptions) ui.ElementSettings builds a bounded, scrollable settings surface using application-owned state and definitions. Place it in a parent with a finite height. It fills its parent, and can be embedded in Dialog, Shell content, or a standalone window. The host supplies the background or material; navigation adds a subtle tint. Use a keyed parent to distinguish multiple instances. state must not be nil. Navigation clears Query; clearing Query alone restores the selected category.
pkg.go.devSettingsOptions
SettingsOptions controls presentation without imposing a window or dialog. Empty text fields use Onevra's catalog in the window's language. Narrow is supplied by the host from its allocated width (not the window width); it replaces the sidebar with a category selector and stacks each row's control below its text.
Prop
Type
SettingsCategory
SettingsCategory describes a navigation destination. IDs must be nonempty and unique among siblings; keep them stable across localization and rebuilds. Image, such as a project logo, replaces Icon in the navigation when set.
Prop
Type
SettingsGroup
SettingsGroup groups related items. Title may be empty for an unheaded group. Actions optionally builds trailing controls on the group's header row, such as a refresh button; like Build, it runs on the UI thread only when the header is visible and must not start background work during rendering. Actions are not searched.
Prop
Type
SettingsItem
SettingsItem describes a searchable row. Build runs synchronously on the UI thread only when visible. Applications own values, accessible control labels, persistence and effects; Build must not start background work during rendering. IDs must be nonempty and unique within the group.
DescriptionContent optionally replaces Description's text with inline text elements, such as Text and a ButtonLink, built into the description's paragraph and styled like it. Only text elements belong in it; like Build, it runs only when visible. Description is still what search matches, so give it the same words.
Prop
Type
SettingsState
SettingsState is application-owned presentation state, independent of setting values. An empty or unknown Category selects the first category. Searching preserves Category.
Prop
Type
SearchSettings
func SearchSettings(categories []SettingsCategory, query string) []SettingsResultSearchSettings searches every category using case-insensitive, whitespace- separated terms. Every term must occur in the combined category, group, title, description or keywords. An empty query returns all items. No builders run.
pkg.go.dev