Keel v0.5.1 中文 GitHub

Spinner

Live example: a real Keel application compiled to WebAssembly. Interact with it here. The initial download is about 10 MB.Open full screen
Example sourceGo · Copyable

This is the complete source for the gallery example above.examples/components/spinner.go

Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.

package main

import (
	"github.com/dyike/keel/ui/core"
	"github.com/dyike/keel/ui/el"
	"github.com/dyike/keel/ui/kit"
	"github.com/dyike/keel/ui/theme"
	"time"
)

func init() {
	registerSection("spinner", "feedback", func() core.Widget { return el.Embed(spinnerGallery{}) })
}

type spinnerGallery struct{}

func (spinnerGallery) Render(cx *el.Context) el.Element {
	return el.Div().Gap(16).Child(kit.Spinner().Label(demoText("Loading orders 123", "正在加载订单 123")).Render(cx), kit.Spinner().Size(32).Label("").Render(cx), kit.Spinner().Icon(kit.IconSettings).Size(32).Color(theme.Primary).Label(demoText("Custom gear icon", "自定义齿轮图标")).Render(cx), kit.Spinner().Icon(kit.IconClock).Period(2*time.Second).Size(24).Color(theme.Muted).Label(demoText("Waiting · One cycle every two seconds", "等待处理 · 两秒一周")).Render(cx), el.Div().OnClick(func() { theme.SetReducedMotion(!theme.ReducedMotion) }).P(8).Child(el.Text(demoText("Toggle reduced motion", "切换减少动画"))))
}

kit.Spinner().Size(20).Label("Loading") shows indeterminate progress. When Label is empty, only graphics are drawn, and the default name is "Loading". Agent role progressbar, value is indeterminate. No keyboard operation.

The rotation phase is read while painting with el.AmbientPhase, which requests the next 30 Hz tick shared by all ambient indicators; no goroutine is started. A window whose only motion is the spinner repaints those frames without rendering its views (paint-only frames, see the el guide). Remain static after theme.SetReducedMotion(true). Fixed graphic size, text wraps according to parent container constraints.

Verification: go run ./examples/components -section spinner -theme dark, omit theme to see the light theme; example button switching reduces animation. Pixel testing injects different frame times, checking rotation and stillness.

Icon(kit.IconSettings) replaces the ring with a rotating icon, Icon(kit.IconNone) restores the ring; VectorIcon accepts a custom Gio icon, and passes nil to restore the ring. Color(color.NRGBA{...}) sets the graphic color, and the label still uses the theme's Muted color. PrimaryText is used with the theme when no color is set. Custom icons and rings default to one circle per second and remain stationary when animation is reduced; the size is consistent with Agent semantics.

Period(2 * time.Second) sets the time for one rotation. The larger the value, the slower the rotation; 0 returns to the default one second, and negative values are ignored. Period changes immediately recalculate the phase according to the shared frame clock, possibly changing the current angle. Reduced motion takes precedence over period settings. Loading controls such as buttons still use the default cycle.