feat: port the client to MyGo native UI (0.3.0)

Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
This commit is contained in:
OpenChamber committed 2026-11-18 13:22:59 +08:00
1 parent 2fb4a8ea79
commit 6c0ee61750
2538 files changed
+675779 -4792

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
# Notice
The indicators of this package follow those of loading-dev
(https://github.com/jakubkrehel/loading, https://loading.dev): their
shapes, including the SVG path data of Clock and Radar, their keyframes and
their timings. loading-dev's license:
MIT License
Copyright (c) 2026 Jakub Krehel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+27
View File
@@ -0,0 +1,27 @@
# loading
The 29 loading indicators of [loading.dev](https://loading.dev) for
[MyGo](https://github.com/egoist/mygo) apps of native UI: arcs, rings,
dots, bars and blocks that MyGo draws itself, as loading.dev's look and
move.
```go
import "github.com/egoist/mygo/plugins/loading"
loading.Arc(c).Label("Saving")
loading.Ring(c, loading.Options{Size: 32, Color: t.Accent, Easing: loading.EaseInOut})
```
See [the documentation](../../docs/plugins/loading.md), and
`examples/loading`.
The indicators follow loading.dev's SVG and CSS (`loading-dev` 0.3.5,
MIT, © Jakub Krehel; see [NOTICE.md](NOTICE.md)): their shapes in the
same viewBoxes and fractions of their size, their keyframes, staggers and
timing functions, and their poses for reduced motion. Compared with
loading-dev rendered by a browser at the same points of their cycles,
they differ only where the browser snaps borders to pixels (Atom's
rings), in antialiasing, and on macOS in the corners of rounded squares
(Blocks, Gather, Morph, Swirl, Flip), which the renderers draw
continuous, as macOS does, so that they need no mask rasterized every
frame.
+382
View File
@@ -0,0 +1,382 @@
package loading
import (
"math"
"github.com/egoist/mygo/ui"
)
// drawing is an indicator's frame being painted: its square of side s at
// (x, y), how many cycles it has run, and its options.
type drawing struct {
p *ui.Painter
sh *shape
x, y, s float32
cycles float64
color ui.Color
o Options
// still draws the indicator at rest, where the desktop asks for less
// motion.
still bool
}
// at returns how far into its cycle the part step of count is, from 0 to
// 1, the parts staggered as loading.dev's: each a count-th of a cycle
// behind the one before, the first a whole cycle ahead, as a negative
// animation-delay. A count of 0 is the indicator's own cycle.
func (d *drawing) at(step, count int) float64 {
t := d.cycles
if count > 0 {
t += float64(count-step) / float64(count)
}
return t - math.Floor(t)
}
// turn returns how much of a turn the indicator has turned at p of its
// cycle, the other way if reverse, as Options.Easing says.
func (d *drawing) turn(p float64, reverse bool) float64 {
if reverse {
p = 1 - p
}
switch d.o.Easing {
case EaseInOut:
return easeInOut(p)
case Stacked:
return p + easeInOut(p)
}
return p
}
// alpha returns the indicator's color at opacity a.
func (d *drawing) alpha(a float64) ui.Color { return d.color.Alpha(float32(a)) }
// view returns the transform from a viewBox of side n, as loading.dev's
// SVGs draw in, to the indicator's square.
func (d *drawing) view(n float32) affine {
k := d.s / n
return affine{a: k, d: k, e: d.x, f: d.y}
}
// begin empties the shape and has it draw through m.
func (d *drawing) begin(m affine) *shape {
d.sh.Reset()
d.sh.m = m
return d.sh
}
// fill fills the shape in the indicator's color at opacity a.
func (d *drawing) fill(a float64) { d.p.FillPath(&d.sh.Path, d.alpha(a)) }
// fade returns the opacity of part step of count of the indicators whose
// parts fade in turn (loading.dev's fadeCss): from 1 down to dim through
// each cycle, at rest at rest.
func (d *drawing) fade(step, count int, dim, rest float64) float64 {
if d.still {
return rest
}
return 1 + (dim-1)*d.at(step, count)
}
// The timing functions of CSS.
var (
easeInOut = bezier(0.42, 0, 0.58, 1)
easeOut = bezier(0, 0, 0.58, 1)
overshoot = bezier(0.68, -0.75, 0.265, 1.75) // Cascade's
linearEase = func(t float64) float64 { return t }
)
// bezier returns CSS's cubic-bezier(x1, y1, x2, y2).
func bezier(x1, y1, x2, y2 float64) func(float64) float64 {
cx := 3 * x1
bx := 3*(x2-x1) - cx
ax := 1 - cx - bx
cy := 3 * y1
by := 3*(y2-y1) - cy
ay := 1 - cy - by
x := func(s float64) float64 { return ((ax*s+bx)*s + cx) * s }
return func(t float64) float64 {
if t <= 0 || t >= 1 {
return t
}
// Newton's method, then bisection where the slope is flat.
s := t
for range 8 {
e := x(s) - t
if math.Abs(e) < 1e-7 {
return ((ay*s+by)*s + cy) * s
}
dx := (3*ax*s+2*bx)*s + cx
if math.Abs(dx) < 1e-6 {
break
}
s -= e / dx
}
lo, hi := 0.0, 1.0
s = t
for range 40 {
if x(s) < t {
lo = s
} else {
hi = s
}
s = (lo + hi) / 2
}
return ((ay*s+by)*s + cy) * s
}
}
// key is a keyframe: a value at a point of a cycle.
type key struct{ at, v float64 }
// frames returns the value of keyframes ks at p of a cycle, easing each
// stretch between two keyframes, as CSS's timing functions do.
func frames(p float64, ease func(float64) float64, ks ...key) float64 {
for i := 1; i < len(ks); i++ {
a, b := ks[i-1], ks[i]
if p <= b.at {
if b.at <= a.at {
return b.v
}
return a.v + (b.v-a.v)*ease((p-a.at)/(b.at-a.at))
}
}
return ks[len(ks)-1].v
}
// affine maps (x, y) to (a*x + c*y + e, b*x + d*y + f).
type affine struct{ a, b, c, d, e, f float32 }
// apply maps a point.
func (m affine) apply(x, y float32) (float32, float32) {
return m.a*x + m.c*y + m.e, m.b*x + m.d*y + m.f
}
// then returns m after n: n maps first.
func (m affine) then(n affine) affine {
return affine{
a: m.a*n.a + m.c*n.b, b: m.b*n.a + m.d*n.b,
c: m.a*n.c + m.c*n.d, d: m.b*n.c + m.d*n.d,
e: m.a*n.e + m.c*n.f + m.e, f: m.b*n.e + m.d*n.f + m.f,
}
}
// rotate returns m turning what it maps by deg degrees clockwise around
// (cx, cy), as CSS's rotate().
func (m affine) rotate(deg float64, cx, cy float32) affine {
s, c := math.Sincos(deg * math.Pi / 180)
sf, cf := float32(s), float32(c)
return m.then(affine{a: cf, b: sf, c: -sf, d: cf, e: cx - cf*cx + sf*cy, f: cy - sf*cx - cf*cy})
}
// scale returns m scaling what it maps by sx and sy around (cx, cy).
func (m affine) scale(sx, sy, cx, cy float32) affine {
return m.then(affine{a: sx, d: sy, e: cx - sx*cx, f: cy - sy*cy})
}
// move returns m moving what it maps by (dx, dy).
func (m affine) move(dx, dy float32) affine { return m.then(affine{a: 1, d: 1, e: dx, f: dy}) }
// unit returns how many DIPs a length of 1 is through m, for strokes.
func (m affine) unit() float32 {
return float32(math.Sqrt(math.Abs(float64(m.a*m.d - m.b*m.c))))
}
// transform returns m as a ui.Transform, false if it collapses what it
// maps: a scaling, a skew along x, a rotation, then a translation.
func (m affine) transform() (ui.Transform, bool) {
sx := float32(math.Hypot(float64(m.a), float64(m.b)))
det := m.a*m.d - m.b*m.c
if sx == 0 || det == 0 {
return ui.Transform{}, false
}
sy := det / sx
t := ui.Scaling(sx, sy)
if k := (m.a*m.c + m.b*m.d) / sx; k != 0 {
t = t.Then(ui.Skewing(float32(deg(math.Atan(float64(k/sy)))), 0))
}
if m.b != 0 || m.a < 0 {
t = t.Then(ui.Rotation(float32(deg(math.Atan2(float64(m.b), float64(m.a))))))
}
return t.Then(ui.Translation(m.e, m.f)), true
}
// shape is a path drawn through a transform, in the coordinates of a
// viewBox or of the indicator's own.
type shape struct {
ui.Path
m affine
}
func (s *shape) move(x, y float32) *shape { s.MoveTo(s.m.apply(x, y)); return s }
func (s *shape) line(x, y float32) *shape { s.LineTo(s.m.apply(x, y)); return s }
func (s *shape) cube(x1, y1, x2, y2, x, y float32) *shape {
ax, ay := s.m.apply(x1, y1)
bx, by := s.m.apply(x2, y2)
cx, cy := s.m.apply(x, y)
s.CubeTo(ax, ay, bx, by, cx, cy)
return s
}
func (s *shape) close() *shape { s.Close(); return s }
// arc adds the arc of the circle around (cx, cy) of radius r from angle
// a0 to a1, in radians clockwise from the right, as SVG's circles start
// and go: from its start if first, or a line to it.
func (s *shape) arc(cx, cy, r float32, a0, a1 float64, first bool) *shape {
at := func(a float64) (float32, float32) {
sin, cos := math.Sincos(a)
return cx + r*float32(cos), cy + r*float32(sin)
}
if first {
s.move(at(a0))
} else {
s.line(at(a0))
}
n := int(math.Ceil(math.Abs(a1-a0) / (math.Pi / 2)))
h := (a1 - a0) / float64(n)
k := float32(4.0 / 3 * math.Tan(h/4))
for i := range n {
b0, b1 := a0+float64(i)*h, a0+float64(i+1)*h
s0, c0 := math.Sincos(b0)
s1, c1 := math.Sincos(b1)
x0, y0 := cx+r*float32(c0), cy+r*float32(s0)
x1, y1 := cx+r*float32(c1), cy+r*float32(s1)
s.cube(x0-k*r*float32(s0), y0+k*r*float32(c0), x1+k*r*float32(s1), y1-k*r*float32(c1), x1, y1)
}
return s
}
// circle adds a circle, counterclockwise if ccw, as a hole in one.
func (s *shape) circle(cx, cy, r float32, ccw bool) *shape {
if ccw {
return s.arc(cx, cy, r, 0, -2*math.Pi, true).close()
}
return s.arc(cx, cy, r, 0, 2*math.Pi, true).close()
}
// ring adds the ring between radii in and out, as a circle's border.
func (s *shape) ring(cx, cy, out, in float32) *shape {
s.circle(cx, cy, out, false)
if in > 0 {
s.circle(cx, cy, in, true)
}
return s
}
// stroke draws an arc of the circle of radius r around (cx, cy) in the
// shape's coordinates, w wide, from angle a0 to a1 (as arc's), ended as
// Options.Cap says, at opacity a. m maps without turning: the arc is
// drawn from angle 0 and turned to a0 by the renderer, so that an arc of
// the same length is the same mask from frame to frame.
func (d *drawing) stroke(m affine, cx, cy, r, w float32, a0, a1 float64, a float64) {
if a1 <= a0 {
return
}
d.turned(m, cx, cy, a0, func(m affine) {
s := d.begin(m)
if d.o.Cap == FlatCap {
s.arc(cx, cy, r+w/2, 0, a1-a0, true)
s.arc(cx, cy, r-w/2, a1-a0, 0, false).close()
d.fill(a)
return
}
s.arc(cx, cy, r, 0, a1-a0, true)
d.p.StrokePath(&s.Path, w*m.unit(), d.alpha(a))
})
}
// turned draws what fn draws through m turned by angle radians clockwise
// around (cx, cy) in m's coordinates. The renderer turns it: what fn
// draws at rest is the same path from frame to frame, whose mask it
// rasterizes once, where drawing it turned would rasterize another every
// frame. fn draws through the m it is given, which rasterizes the mask
// twice as fine, for the renderer to sample it turned as sharp.
func (d *drawing) turned(m affine, cx, cy float32, angle float64, fn func(m affine)) {
px, py := m.apply(cx, cy)
d.p.Transform(ui.Scaling(0.5, 0.5).Then(ui.Rotation(float32(deg(angle)))), px, py, func() {
fn(identity.scale(2, 2, px, py).then(m))
})
}
// rounded draws the rectangle (x, y, w, h) with corners of radius r in
// m's coordinates, filled, or its border bw wide if bw > 0, at opacity
// a. The renderers draw it themselves, through any transform, as an
// element's background: no path to rasterize into a mask, as moving
// paths need every frame. Its corners are the platform's: circles and
// pills are round, smaller corners continuous on macOS.
func (d *drawing) rounded(m affine, x, y, w, h, r, bw float32, a float64) {
if t, ok := m.move(x, y).transform(); ok {
d.roundedIn(t, w, h, r, bw, a)
}
}
// roundedIn is rounded through t, which maps the rectangle w×h at the
// origin to the indicator.
func (d *drawing) roundedIn(t ui.Transform, w, h, r, bw float32, a float64) {
if w <= 0 || h <= 0 || a <= 0 {
return
}
// Fill and Stroke put a rectangle and its border on whole pixels:
// drawn zoom times as large and scaled back, they keep their sizes to
// a zoom-th of a pixel, as they move and grow.
const zoom = 8
box := ui.Rect{W: w * zoom, H: h * zoom}
c := d.alpha(a)
d.p.Transform(ui.Scaling(1.0/zoom, 1.0/zoom).Then(t), 0, 0, func() {
if bw > 0 {
d.p.Stroke(box, c, r*zoom, bw*zoom)
} else {
d.p.Fill(box, c, r*zoom)
}
})
}
// dot draws the circle of radius r around (cx, cy) in m's coordinates at
// opacity a.
func (d *drawing) dot(m affine, cx, cy, r float32, a float64) {
d.rounded(m, cx-r, cy-r, 2*r, 2*r, r, 0, a)
}
// hoop draws the ring between radii out and in around (cx, cy) in m's
// coordinates at opacity a, as a circle's border.
func (d *drawing) hoop(m affine, cx, cy, out, in float32, a float64) {
d.rounded(m, cx-out, cy-out, 2*out, 2*out, out, max(0, out-in), a)
}
// dashes calls fn with each dash of a pattern of dashes dash long and
// gaps gap long, shifted by offset as SVG's stroke-dashoffset shifts it,
// on a path length long: from and to along it.
func dashes(length, dash, gap, offset float64, fn func(from, to float64)) {
period := dash + gap
if dash <= 0 || period <= 0 {
return
}
// From the last dash starting before the path.
for at := math.Mod(-offset, period) - period; at < length; at += period {
from, to := max(at, 0), min(at+dash, length)
if to > from {
fn(from, to)
}
}
}
// capsule draws the line from (x0, y0) to (x1, y1) in m's coordinates, w
// wide, with round ends, at opacity a.
func (d *drawing) capsule(m affine, x0, y0, x1, y1, w float32, a float64) {
dx, dy := float64(x1-x0), float64(y1-y0)
m = m.rotate(deg(math.Atan2(dy, dx)), x0, y0)
l := float32(math.Hypot(dx, dy))
d.rounded(m, x0-w/2, y0-w/2, l+w, w, w/2, 0, a)
}
// polar returns the point at radius r and angle a (radians clockwise
// from the right) around (cx, cy).
func polar(cx, cy, r float32, a float64) (float32, float32) {
s, c := math.Sincos(a)
return cx + r*float32(c), cy + r*float32(s)
}
// rad converts degrees to radians.
func rad(deg float64) float64 { return deg * math.Pi / 180 }
+208
View File
@@ -0,0 +1,208 @@
// Package loading has the 29 loading indicators of loading.dev
// (https://loading.dev, Jakub Krehel's loading-dev for React) for native
// UI: arcs, dots, bars and blocks that MyGo draws itself, each a ui
// element that moves as long as it shows.
//
// loading.Arc(c)
// loading.Ring(c, loading.Options{Size: 32, Color: t.Accent, Easing: loading.EaseInOut})
// ui.Row(c).Gap(8).Children(func() {
// loading.BouncingDots(c, loading.Options{Size: 12})
// ui.Text(c, "Thinking")
// })
//
// An indicator is Options.Size DIPs high, 20 unless set, and as wide, or
// wider for a row of dots; the element's own size, as Size or Fill set it,
// scales it. It draws in the theme's text color unless Options.Color says
// otherwise, and runs at loading.dev's pace unless Options.Duration says
// otherwise. Where the desktop asks for less motion
// (ui.Preferences.ReduceMotion) it stands still, in loading.dev's pose for
// reduced motion; Options.Paused stops it where it is.
//
// Assistive technology sees a progress indicator of unknown length; name
// it with Label.
package loading
import (
"time"
"github.com/egoist/mygo/ui"
)
// Options are the settings of an indicator, each meaning loading.dev's
// default when zero.
type Options struct {
// Size is the indicator's height in DIPs, 20 if 0. Its width is the
// same, except for BouncingDots's and LinearDots's rows of dots.
Size float32
// Color is what the indicator draws in, the theme's Text if zero.
Color ui.Color
// Duration is the time of one cycle of the indicator's motion, its
// own (as loading.dev's) if 0.
Duration time.Duration
// Paused stops the indicator where it is, and it goes on from there
// once false again, as CSS's paused animation-play-state.
Paused bool
// Easing is how the indicators that turn (Arc, Atom, Clock, Comet,
// Dual, Orbit, Radar, Ring, Snake and Trace) turn: Linear by default.
Easing Easing
// Cap is the end of the strokes of Arc, Cascade, Dual, Ring, Snake
// and Trace: RoundCap by default.
Cap Cap
// Sweep is the order in which Blocks's blocks shrink: Diagonal by
// default.
Sweep Sweep
// Direction is where Ripple's rings go: RippleOut by default.
Direction RippleDirection
// Origin is where Wave's bars grow from: WaveCenter by default.
Origin WaveOrigin
}
// Easing is how an indicator that turns moves through a turn.
type Easing uint8
const (
// Linear turns at one speed.
Linear Easing = iota
// EaseInOut starts and ends each turn slowly, as CSS's ease-in-out.
EaseInOut
// Stacked adds a turn at one speed and one easing in and out: two
// turns a cycle, fast in its middle.
Stacked
)
// Cap is how a stroke ends.
type Cap uint8
const (
// RoundCap ends a stroke in a half circle.
RoundCap Cap = iota
// FlatCap ends a stroke square, where it ends.
FlatCap
)
// Sweep is the order of Blocks's blocks.
type Sweep uint8
const (
// Diagonal sweeps from the top left corner to the bottom right one.
Diagonal Sweep = iota
// Rows sweeps from the top row down.
Rows
// Columns sweeps from the left column right.
Columns
)
// RippleDirection is where Ripple's rings go.
type RippleDirection uint8
const (
// RippleOut spreads the rings out from the center.
RippleOut RippleDirection = iota
// RippleIn draws the rings in to the center.
RippleIn
)
// WaveOrigin is where Wave's bars grow from.
type WaveOrigin uint8
const (
// WaveCenter grows the bars up and down from their middle.
WaveCenter WaveOrigin = iota
// WaveBottom grows the bars up from the bottom.
WaveBottom
)
// DefaultSize is an indicator's height when Options.Size is 0.
const DefaultSize = 20
// A Spinner is one of the indicators, by name, as a gallery lists them.
type Spinner struct {
// Name is the indicator's name, as loading.dev's: "Arc", "Bouncing dots".
Name string
// New creates the indicator, as the function of its name does.
New func(c *ui.Context, o ...Options) ui.Element
}
// All returns the indicators, by name.
func All() []Spinner {
return []Spinner{
{"Arc", Arc}, {"Atom", Atom}, {"Blocks", Blocks}, {"Bouncing dots", BouncingDots},
{"Cascade", Cascade}, {"Circular dots", CircularDots}, {"Classic", Classic},
{"Classic v2", ClassicV2}, {"Clock", Clock}, {"Comet", Comet}, {"Compass", Compass},
{"Dual", Dual}, {"Eclipse", Eclipse}, {"Flip", Flip}, {"Gather", Gather}, {"Leap", Leap},
{"Linear dots", LinearDots}, {"Loading", Loading}, {"Morph", Morph}, {"Orbit", Orbit},
{"Pulse", Pulse}, {"Radar", Radar}, {"Ring", Ring}, {"Ripple", Ripple}, {"Slide", Slide},
{"Snake", Snake}, {"Swirl", Swirl}, {"Trace", Trace}, {"Wave", Wave},
}
}
// spec is what an indicator is: how long its cycle is, how wide it is for
// its height, and how it draws.
type spec struct {
cycle time.Duration
aspect float32
draw func(d *drawing)
}
// timerKey keys an indicator's timer in its element.
type timerKey struct{}
// timer is how long an indicator has run: since start, or held while
// paused.
type timer struct {
start time.Time
held time.Duration
paused bool
// sh is the shape the indicator draws with, its storage kept from
// frame to frame.
sh shape
}
// show creates an indicator of s with the options given, at most one.
func show(c *ui.Context, s *spec, opts []Options) ui.Element {
if len(opts) > 1 {
panic("loading: at most one Options")
}
var o Options
if len(opts) == 1 {
o = opts[0]
}
size := o.Size
if size <= 0 {
size = DefaultSize
}
color := o.Color
if color == (ui.Color{}) {
color = c.Theme().Text
}
cycle := s.cycle
if o.Duration > 0 {
cycle = o.Duration
}
still := c.Preferences().ReduceMotion
// A progress indicator of unknown length, as ui.Spinner.
e := ui.Box(c).Size(size*s.aspect, size).Shrink(0).Role(ui.RoleProgress).Range(0, 1, -1)
now := c.Now()
k := ui.Local(e, timerKey{}, func() timer { return timer{start: now} })
switch {
case o.Paused && !k.paused:
k.held, k.paused = now.Sub(k.start), true
case !o.Paused && k.paused:
k.start, k.paused = now.Add(-k.held), false
}
e.Draw(func(p *ui.Painter, r ui.Rect) {
d := drawing{p: p, sh: &k.sh, color: color, o: o, still: still}
d.s = min(r.H, r.W/s.aspect)
d.x, d.y = r.X+(r.W-d.s*s.aspect)/2, r.Y+(r.H-d.s)/2
if !still {
ran := k.held
if !k.paused {
ran = p.Now().Sub(k.start)
p.AnimationFrame()
}
d.cycles = float64(ran) / float64(cycle)
}
s.draw(&d)
})
return e
}
+224
View File
@@ -0,0 +1,224 @@
package loading
import (
"image"
"testing"
"time"
"github.com/egoist/mygo/ui"
)
// tester shows the indicator s alone, named by its name, with o.
func tester(s Spinner, o *Options) *ui.Tester {
return ui.NewTester(func(c *ui.Context) {
ui.Column(c).Fill().Center().Background(ui.RGB(255, 255, 255)).Children(func() {
s.New(c, *o).Label(s.Name)
})
}, 64, 64)
}
// at returns the tester's frame at its time.
func at(tt *ui.Tester) *image.RGBA {
tt.Frame()
return clone(tt.Image())
}
func clone(img *image.RGBA) *image.RGBA {
c := image.NewRGBA(img.Rect)
copy(c.Pix, img.Pix)
return c
}
// ink counts the pixels not white inside r, and outside it.
func ink(img *image.RGBA, r image.Rectangle) (in, out int) {
b := img.Bounds()
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
p := img.PixOffset(x, y)
if img.Pix[p] > 250 && img.Pix[p+1] > 250 && img.Pix[p+2] > 250 {
continue
}
if image.Pt(x, y).In(r) {
in++
} else {
out++
}
}
}
return in, out
}
// differ reports whether two frames differ by more than rounding.
func differ(a, b *image.RGBA) bool {
for i := range a.Pix {
d := int(a.Pix[i]) - int(b.Pix[i])
if d > 2 || d < -2 {
return true
}
}
return false
}
func TestAll(t *testing.T) {
all := All()
if len(all) != 29 {
t.Fatalf("%d indicators, want loading.dev's 29", len(all))
}
names := map[string]bool{}
for _, s := range all {
if names[s.Name] || s.New == nil {
t.Errorf("indicator %q repeated or without its function", s.Name)
}
names[s.Name] = true
}
}
// Every indicator draws in its box, at every point of its cycle, and
// nothing outside it.
func TestDrawsInItsBox(t *testing.T) {
o := Options{Size: 40, Duration: time.Second}
for _, s := range All() {
tt := tester(s, &o)
r, ok := tt.Find(s.Name)
if !ok {
t.Fatalf("%s: not found", s.Name)
}
var aspect float32 = 1
switch s.Name {
case "Bouncing dots":
aspect = 1.15
case "Linear dots":
aspect = 0.9375
}
if r.H != 40 || r.W != 40*aspect {
t.Errorf("%s: %v×%v, want %v×40", s.Name, r.W, r.H, 40*aspect)
}
box := image.Rect(int(r.X)-1, int(r.Y)-1, int(r.X+r.W)+2, int(r.Y+r.H)+2)
for step := range 8 {
// Flip is edge on a quarter and three quarters into its cycle.
if s.Name == "Flip" && step%4 == 2 {
tt.Advance(time.Second / 8)
continue
}
in, out := ink(at(tt), box)
if in < 20 || out > 0 {
t.Errorf("%s at %d/8: %d pixels drawn in its box, %d outside", s.Name, step, in, out)
}
tt.Advance(time.Second / 8)
}
}
}
// Every indicator moves, asking for frames as it does, and comes back
// where it was after two cycles: Bouncing dots's go up in one and down in
// the next.
func TestMovesInCycles(t *testing.T) {
o := Options{Duration: time.Second}
for _, s := range All() {
tt := tester(s, &o)
start := at(tt)
if !tt.FramePending() {
t.Errorf("%s: asks for no frame", s.Name)
}
tt.Advance(300 * time.Millisecond)
a := at(tt)
tt.Advance(2 * time.Second)
b := at(tt)
if !differ(start, a) {
t.Errorf("%s: the same 0.3 cycles in", s.Name)
}
if differ(a, b) {
t.Errorf("%s: not the same two cycles later", s.Name)
}
}
}
// Where the desktop asks for less motion, an indicator stands still and
// asks for no frames.
func TestReduceMotion(t *testing.T) {
var o Options
for _, s := range All() {
tt := tester(s, &o)
tt.SetPreferences(ui.Preferences{ReduceMotion: true, TextScale: 1})
still := at(tt)
if tt.FramePending() {
t.Errorf("%s: asks for frames at rest", s.Name)
}
tt.Advance(time.Second)
if differ(still, at(tt)) {
t.Errorf("%s: moves at rest", s.Name)
}
if in, _ := ink(still, still.Bounds()); in == 0 {
t.Errorf("%s: draws nothing at rest", s.Name)
}
}
}
// Paused, an indicator stops where it is, and goes on from there.
func TestPaused(t *testing.T) {
o := Options{Duration: time.Second}
for _, s := range All() {
tt := tester(s, &o)
tt.Advance(300 * time.Millisecond)
o.Paused = true
held := at(tt)
if tt.FramePending() {
t.Errorf("%s: asks for frames paused", s.Name)
}
tt.Advance(400 * time.Millisecond)
if differ(held, at(tt)) {
t.Errorf("%s: moves paused", s.Name)
}
o.Paused = false
if differ(held, at(tt)) {
t.Errorf("%s: not where it stopped as it goes on", s.Name)
}
tt.Advance(200 * time.Millisecond)
if !differ(held, at(tt)) {
t.Errorf("%s: does not go on", s.Name)
}
o.Paused = false
}
}
// An indicator draws in the color given, and in the theme's text color
// without one.
func TestColor(t *testing.T) {
red := ui.RGB(255, 0, 0)
for _, color := range []ui.Color{{}, red} {
var text ui.Color
img := ui.Render(func(c *ui.Context) {
text = c.Theme().Text
ui.Column(c).Fill().Center().Children(func() { Morph(c, Options{Size: 40, Color: color}) })
}, 64, 64, 1)
want := color
if want == (ui.Color{}) {
want = text
}
p := img.PixOffset(32, 32)
if got := (ui.Color{R: img.Pix[p], G: img.Pix[p+1], B: img.Pix[p+2], A: 255}); got != want {
t.Errorf("color %v: drawn in %v, want %v", color, got, want)
}
}
}
func TestOneOptions(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("two Options did not panic")
}
}()
ui.Render(func(c *ui.Context) { Arc(c, Options{}, Options{}) }, 32, 32, 1)
}
func TestBezier(t *testing.T) {
for _, c := range []struct{ t, want float64 }{{0, 0}, {0.5, 0.5}, {1, 1}, {0.25, 0.1291}} {
if got := easeInOut(c.t); got < c.want-1e-3 || got > c.want+1e-3 {
t.Errorf("ease-in-out(%v) = %v, want %v", c.t, got, c.want)
}
}
// Cascade's overshoots below 0 and past 1.
if overshoot(0.1) >= 0 || overshoot(0.9) <= 1 {
t.Errorf("overshoot: %v at 0.1, %v at 0.9", overshoot(0.1), overshoot(0.9))
}
}
+651
View File
@@ -0,0 +1,651 @@
package loading
import (
"math"
"time"
"github.com/egoist/mygo/ui"
)
// The indicators follow loading.dev's, drawn in their SVG's viewBox or in
// fractions of their size as its CSS places them, with its keyframes and
// timing functions.
// identity is the transform that maps nothing elsewhere.
var identity = affine{a: 1, d: 1}
// spin returns the angle, in radians, that an indicator turning once a
// cycle has turned, as Options.Easing says, the other way if reverse; at
// rest, rest.
func (d *drawing) spin(reverse bool, rest float64) float64 {
if d.still {
return rest
}
return d.turn(d.at(0, 0), reverse) * 2 * math.Pi
}
// deg converts radians to degrees.
func deg(r float64) float64 { return r * 180 / math.Pi }
// center returns the middle of the indicator's square.
func (d *drawing) center() (float32, float32) { return d.x + d.s/2, d.y + d.s/2 }
var arc = spec{cycle: 800 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
a := d.spin(false, 0)
d.stroke(d.view(24), 12, 12, 10, 2.5, a, a+18.0/10, 1)
}}
// Arc creates a single open stroke turning in a circle.
func Arc(c *ui.Context, o ...Options) ui.Element { return show(c, &arc, o) }
var atom = spec{cycle: time.Second, aspect: 1, draw: func(d *drawing) {
cx, cy := d.center()
r := d.s / 2
d.hoop(identity, cx, cy, r, r-0.055*d.s, 1)
// Each orbit, tilted a third of a half turn more than the one before,
// is a ring turning in 3D around its diameter, seen from the front: an
// ellipse growing and shrinking across it.
for i := range 3 {
a := rad(60)
if !d.still {
a = d.turn(d.at(i, 3), false) * 2 * math.Pi
}
k := float32(math.Cos(a))
if math.Abs(float64(k)) < 1e-4 {
continue
}
d.hoop(identity.rotate(float64(i)*60, cx, cy).scale(k, 1, cx, cy), cx, cy, r, r-0.045*d.s, 1)
}
}}
// Atom creates three rings tumbling inside a circle.
func Atom(c *ui.Context, o ...Options) ui.Element { return show(c, &atom, o) }
var blocks = spec{cycle: 1300 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
cell, gap := d.s*0.8/3, d.s*0.1
for row := range 3 {
for col := range 3 {
place, count := row+col, 5
switch d.o.Sweep {
case Rows:
place, count = row, 3
case Columns:
place, count = col, 3
}
k := 0.8
if !d.still {
k = frames(d.at(place, count), easeInOut, key{0, 1}, key{0.35, 0}, key{0.7, 1}, key{1, 1})
}
if k <= 0 {
continue
}
x, y := d.x+float32(col)*(cell+gap), d.y+float32(row)*(cell+gap)
sk := float32(k)
d.rounded(identity.scale(sk, sk, x+cell/2, y+cell/2), x, y, cell, cell, 0.0625*d.s, 0, 1)
}
}
}}
// Blocks creates nine blocks shrinking and growing in a sweep across a
// grid, in the order Options.Sweep says.
func Blocks(c *ui.Context, o ...Options) ui.Element { return show(c, &blocks, o) }
var bouncingDots = spec{cycle: 500 * time.Millisecond, aspect: 1.15, draw: func(d *drawing) {
dot := 0.25 * d.s
for i := range 3 {
// Up and down on alternate cycles.
y := 0.0
if !d.still {
t := d.cycles + float64(3-i)/3
n := math.Floor(t)
p := t - n
if int64(n)%2 == 1 {
p = 1 - p
}
y = 0.28 - easeInOut(p)
}
cx := d.x + dot/2 + float32(i)*0.45*d.s
d.dot(identity, cx, d.y+d.s/2+float32(y)*dot, dot/2, 1)
}
}}
// BouncingDots creates three staggered dots bouncing up and down, in a
// row 1.15 times as wide as it is high.
func BouncingDots(c *ui.Context, o ...Options) ui.Element { return show(c, &bouncingDots, o) }
var cascade = spec{cycle: 1500 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
for i, r := range [3]float32{10.5, 7, 3.5} {
a := 0.0
if !d.still {
a = overshoot(d.at(i, 24)) * 2 * math.Pi
}
d.stroke(d.view(24), 12, 12, r, 2, a, a+math.Pi/2, 1)
}
}}
// Cascade creates three nested arcs fanning into a spiral, then snapping
// back in line.
func Cascade(c *ui.Context, o ...Options) ui.Element { return show(c, &cascade, o) }
// eightDots are Circular dots' dots, in a viewBox of 16.
var eightDots = [8][2]float32{
{8, 1.5}, {12.5962, 3.4038}, {14.5, 8}, {12.5962, 12.5962},
{8, 14.5}, {3.4038, 12.5962}, {1.5, 8}, {3.4038, 3.4038},
}
var circularDots = spec{cycle: 800 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
for i, at := range eightDots {
d.dot(d.view(16), at[0], at[1], 1.5, d.fade(i, 8, 0.2, 0.6))
}
}}
// CircularDots creates eight dots in a ring, the brightest going around.
func CircularDots(c *ui.Context, o ...Options) ui.Element { return show(c, &circularDots, o) }
var classic = spec{cycle: 1200 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
// The bars are placed as loading.dev's CSS places them: around a point
// a little right of the middle.
cx, cy := d.x+0.52*d.s, d.y+0.501*d.s
w, h := 0.24*d.s, 0.08*d.s
for i := range 12 {
m := identity.rotate(float64(i)*30, cx, cy).move(1.46*w, 0)
d.rounded(m, cx-w/2, cy-h/2, w, h, min(6, h/2), 0, d.fade(i, 12, 0.15, 0.5))
}
}}
// Classic creates twelve fading bars in a radial pattern.
func Classic(c *ui.Context, o ...Options) ui.Element { return show(c, &classic, o) }
var classicV2 = spec{cycle: 800 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
for i := range 8 {
d.capsule(d.view(16).rotate(float64(i)*45, 8, 8), 8, 2.0/3, 8, 10.0/3, 4.0/3, d.fade(i, 8, 0.4, 0.5))
}
}}
// ClassicV2 creates eight ticks in a ring, lit in turn.
func ClassicV2(c *ui.Context, o ...Options) ui.Element { return show(c, &classicV2, o) }
var clock = spec{cycle: 1200 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
v := d.view(16)
d.dot(v, 8, 8, 8, 0.1)
d.turned(v, 8, 8, d.spin(false, 0), d.hand)
}}
// hand draws Clock's hand through v, in its viewBox of 16, pointing up.
func (d *drawing) hand(v affine) {
s := d.begin(v)
s.move(11.1937, 2.92061)
s.cube(10.5206, 2.49739, 9.77304, 2.21397, 8.9954, 2.08314)
s.cube(8.45076, 1.99151, 8, 2.44772, 8, 3)
s.line(8, 7.20324)
s.cube(8, 7.49211, 8.12492, 7.76686, 8.34259, 7.95677)
s.line(11.8999, 11.0603)
s.cube(12.3287, 11.4344, 12.9913, 11.378, 13.2643, 10.8787)
s.cube(13.6356, 10.1998, 13.8736, 9.45248, 13.9617, 8.67713)
s.cube(14.0892, 7.55433, 13.8971, 6.41834, 13.4074, 5.39993)
s.cube(12.9177, 4.38152, 12.1503, 3.5221, 11.1937, 2.92061)
s.close()
d.fill(1)
}
// Clock creates a clock hand sweeping around a faint face.
func Clock(c *ui.Context, o ...Options) ui.Element { return show(c, &clock, o) }
var comet = spec{cycle: 700 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
cx, cy := d.center()
r, w := d.s/2, 0.12*d.s
a := deg(d.spin(false, 0))
// The tail, a ring fading in from transparent around a turn, and the
// head at its end.
s := d.begin(identity).ring(cx, cy, r, r-w)
d.p.FillPathConicGradient(&s.Path, ui.ConicGradient{Angle: float32(a), Stops: []ui.GradientStop{
{Color: d.alpha(0), At: 0}, {Color: d.color, At: 1},
}})
d.dot(identity.rotate(a, cx, cy), cx, d.y+w/2, w/2, 1)
}}
// Comet creates a full ring fading into its tail.
func Comet(c *ui.Context, o ...Options) ui.Element { return show(c, &comet, o) }
var compass = spec{cycle: 500 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
a := 0.0
if !d.still {
a = 90 * easeInOut(d.at(0, 0))
}
for i := range 4 {
d.capsule(d.view(16).rotate(a+float64(i)*90, 8, 8), 8, 2.0/3, 8, 10.0/3, 4.0/3, 1)
}
}}
// Compass creates four ticks turning a quarter turn at a time.
func Compass(c *ui.Context, o ...Options) ui.Element { return show(c, &compass, o) }
var dual = spec{cycle: time.Second, aspect: 1, draw: func(d *drawing) {
v := d.view(24)
a := d.spin(false, 0)
d.stroke(v, 12, 12, 10, 2.5, a, a+18.0/10, 1)
b := d.spin(true, math.Pi)
d.stroke(v, 12, 12, 5.5, 2.5, b, b+10/5.5, 1)
}}
// Dual creates two arcs turning in opposite directions.
func Dual(c *ui.Context, o ...Options) ui.Element { return show(c, &dual, o) }
var eclipse = spec{cycle: 1200 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
cx, cy := d.center()
dot := 0.4 * d.s
for i := range 2 {
// Across and back, nearer (larger) on the way there and farther
// on the way back.
x, a, k := float64(i)*1.5-0.75, 1.0, 1.0
if !d.still {
p := d.at(i, 2)
x = frames(p, easeInOut, key{0, 0.75}, key{0.5, -0.75}, key{1, 0.75})
a = frames(p, easeInOut, key{0, 0.75}, key{0.25, 1}, key{0.5, 0.75}, key{0.75, 0.5}, key{1, 0.75})
k = frames(p, easeInOut, key{0, 1}, key{0.25, 1.3}, key{0.5, 1}, key{0.75, 0.7}, key{1, 1})
}
d.dot(identity, cx+float32(x)*dot, cy, dot/2*float32(k), a)
}
}}
// Eclipse creates two dots trading places, one passing behind the other.
func Eclipse(c *ui.Context, o ...Options) ui.Element { return show(c, &eclipse, o) }
var flip = spec{cycle: 1200 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
cx, cy := d.center()
half, r := 0.32*d.s, 0.06*d.s
ax, ay := 0.0, 0.0
if !d.still {
p := d.at(0, 0)
ax = frames(p, easeInOut, key{0, 0}, key{0.5, -180}, key{1, -180})
ay = frames(p, easeInOut, key{0, 0}, key{0.5, 0}, key{1, -180})
}
// The square turned in 3D, around its horizontal axis and then its
// vertical one (CSS's rotateX(ax) rotateY(ay)), seen in perspective
// from three times the indicator's size.
t := ui.Translation(-half, -half).Then(ui.RotationY(float32(ay))).Then(ui.RotationX(float32(ax))).
Then(ui.Perspective(3 * d.s)).Then(ui.Translation(cx, cy))
d.roundedIn(t, 2*half, 2*half, r, 0, 1)
}}
// Flip creates a square flipping over one axis, then the other.
func Flip(c *ui.Context, o ...Options) ui.Element { return show(c, &flip, o) }
var gather = spec{cycle: 1600 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
cx, cy := d.center()
b, gap := 0.38*d.s, 0.24*d.s
pull, turn := 0.0, 0.0
if !d.still {
p := d.at(0, 0)
pull = frames(p, easeInOut, key{0, 0}, key{0.3, 1}, key{0.6, 1}, key{1, 0})
turn = frames(p, easeInOut, key{0, 0}, key{0.3, 0}, key{0.6, 90}, key{1, 90})
}
// Each block pulls in toward the middle by a fifth of its size.
in := float32(pull) * 0.211 * b
m := identity.rotate(turn, cx, cy)
for i := range 4 {
col, row := float32(i%2), float32(i/2)
x := d.x + col*(b+gap) + (1-2*col)*in
y := d.y + row*(b+gap) + (1-2*row)*in
d.rounded(m, x, y, b, b, 0.14*d.s, 0, 1)
}
}}
// Gather creates four blocks pulling together, turning, and pushing
// apart.
func Gather(c *ui.Context, o ...Options) ui.Element { return show(c, &gather, o) }
var leap = spec{cycle: 1800 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
// Whole DIPs, as loading.dev rounds them to pixels.
round := func(v float32) float32 { return float32(math.Round(float64(v))) }
dot := round(0.22 * d.s)
gap := float32(math.Floor(float64(d.s-dot) / 2))
top := round((d.s - dot) / 2)
left := d.s - dot - 2*gap
// The middle of the stretch a dot leaps over.
mx, my := d.x+left+gap+dot/2, d.y+top+dot/2
for i := range 3 {
// The last dot leaps over the others to the front, as they slide
// back a place each.
x, a := float64(i)*float64(gap), 0.0
if !d.still {
p := d.at(i, 3)
g := float64(gap)
x = frames(p, easeInOut, key{0, 0}, key{0.3333, 0}, key{0.6666, -g}, key{1, -2 * g})
a = frames(p, easeInOut, key{0, 0}, key{0.3333, 180}, key{0.6666, 180}, key{1, 180})
}
px, py := polar(mx+float32(x), my, gap, rad(a+180))
d.dot(identity, px, py, dot/2, 1)
}
}}
// Leap creates three dots in a row, the last one leaping to the front.
func Leap(c *ui.Context, o ...Options) ui.Element { return show(c, &leap, o) }
var linearDots = spec{cycle: 900 * time.Millisecond, aspect: 0.9375, draw: func(d *drawing) {
dot := 0.1875 * d.s
for i := range 3 {
a := 0.75
if !d.still {
a = frames(d.at(i, 3), linearEase, key{0, 1}, key{0.6667, 0.5}, key{1, 1})
}
d.dot(identity, d.x+dot/2+float32(i)*2*dot, d.y+d.s/2, dot/2, a)
}
}}
// LinearDots creates three dots lighting up in turn from left to right,
// in a row nearly as wide as it is high.
func LinearDots(c *ui.Context, o ...Options) ui.Element { return show(c, &linearDots, o) }
// segments are where the Loading mark's segments are, in a viewBox of 15.
var segments = [8][2]float32{{12, 6}, {10, 10}, {6, 12}, {2, 10}, {0, 6}, {2, 2}, {6, 0}, {10, 2}}
var loadingMark = spec{cycle: time.Second, aspect: 1, draw: func(d *drawing) {
v := d.view(15)
for i, at := range segments {
a := d.fade(i, 8, 0.2, 0.6)
for _, b := range [4][2]float32{{0, 0}, {2, 0}, {0, 2}, {2, 2}} {
d.rounded(v, at[0]+b[0], at[1]+b[1], 1, 1, 0, 0, a)
}
}
}}
// Loading creates the loading.dev mark, its brightest segment circling
// the ring.
func Loading(c *ui.Context, o ...Options) ui.Element { return show(c, &loadingMark, o) }
var morph = spec{cycle: 1200 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
cx, cy := d.center()
side := 0.64 * d.s
r, a := 0.08*float64(d.s), 0.0
if !d.still {
p := d.at(0, 0)
r = frames(p, easeInOut, key{0, r}, key{0.5, float64(side) / 2}, key{1, r})
a = frames(p, easeInOut, key{0, 0}, key{0.5, 45}, key{1, 90})
}
d.rounded(identity.rotate(a, cx, cy), cx-side/2, cy-side/2, side, side, float32(r), 0, 1)
}}
// Morph creates a square rounding into a circle and back as it turns.
func Morph(c *ui.Context, o ...Options) ui.Element { return show(c, &morph, o) }
var orbit = spec{cycle: 750 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
cx, cy := d.center()
d.dot(identity, cx, cy, 0.125*d.s, 1)
// Half a ring, from transparent at its start to opaque at its end.
r, w := 0.42*d.s, 0.0625*d.s
d.turned(identity, cx, cy, d.spin(false, 0), func(m affine) {
a0, a1 := math.Pi/2, 3*math.Pi/2
s := d.begin(m).arc(cx, cy, r, a0, a1, true).arc(cx, cy, r-w, a1, a0, false).close()
// Empty subpaths at the corners of the whole ring's box, which the
// gradient turns around the middle of.
s.move(cx-r, cy-r).line(cx-r, cy-r).close().move(cx+r, cy+r).line(cx+r, cy+r).close()
d.p.FillPathConicGradient(&s.Path, ui.ConicGradient{Angle: 180, Stops: []ui.GradientStop{
{Color: d.alpha(0), At: 0}, {Color: d.color, At: 0.5}, {Color: d.color, At: 1},
}})
})
}}
// Orbit creates a fading half ring turning around a dot.
func Orbit(c *ui.Context, o ...Options) ui.Element { return show(c, &orbit, o) }
var pulse = spec{cycle: 1200 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
v := d.view(16)
k, a := 1.0, 0.2
if !d.still {
e := easeOut(d.at(0, 0))
k, a = 0.25+0.75*e, 0.4*(1-e)
}
d.dot(v, 8, 8, 8*float32(k), a)
d.dot(v, 8, 8, 2, 1)
}}
// Pulse creates a ring rippling outward from a dot.
func Pulse(c *ui.Context, o ...Options) ui.Element { return show(c, &pulse, o) }
var radar = spec{cycle: 1500 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
v := d.view(16)
d.dot(v, 8, 8, 8, 0.2)
d.hoop(v, 8, 8, 6, 5, 0.2)
d.turned(v, 8, 8, d.spin(false, 0), d.beam)
d.dot(v, 8, 8, 2, 1)
}}
// beam draws Radar's beam through m, in its viewBox of 16: a sector of
// the ring between radii 3 and 8, from the top to its leading edge,
// opaque inside a third of the radius and fading to transparent at its
// rim, as loading.dev's radial gradient.
func (d *drawing) beam(m affine) {
const in, out, solid = 3, 8, 8 * 0.3334
// The leading edge, from the inner circle to the outer one.
ix, iy, ox, oy := 10.4707-8, 9.69922-8, 14.249-8, 12.9951-8
opacity := func(r float64) float64 { return max(0, min(1, 1-(r-solid)/(out-solid))) }
// Sectors of growing radii over each other, each as opaque as makes
// the ring between its radius and the one before the gradient's
// opacity there: their edges inside the beam meet no other edge, as
// bands side by side would.
n := int(max(6, min(40, math.Ceil(float64(out-in)*float64(m.unit())/1.5))))
band := func(k int) float64 { return in + (out-in)*(float64(k)-0.5)/float64(n) }
for k := 1; k <= n; k++ {
inside, beyond := opacity(band(k)), 0.0
if k < n {
beyond = opacity(band(k + 1))
}
a := 1 - (1-inside)/(1-beyond)
r := in + (out-in)*float64(k)/float64(n)
// Where the leading edge crosses the circle of radius r.
dx, dy := ox-ix, oy-iy
qa, qb, qc := dx*dx+dy*dy, 2*(ix*dx+iy*dy), ix*ix+iy*iy-r*r
u := max(0, min(1, (-qb+math.Sqrt(max(0, qb*qb-4*qa*qc)))/(2*qa)))
end := math.Atan2(iy+u*dy, ix+u*dx)
s := d.begin(m)
s.move(8, 8-in).line(8, 8-float32(r))
s.arc(8, 8, float32(r), -math.Pi/2, end, false)
s.line(float32(8+ix), float32(8+iy))
s.arc(8, 8, in, math.Atan2(iy, ix), -math.Pi/2, false).close()
d.fill(a)
}
}
// Radar creates a fading beam sweeping around a faint dish.
func Radar(c *ui.Context, o ...Options) ui.Element { return show(c, &radar, o) }
var ring = spec{cycle: 800 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
v := d.view(24)
d.hoop(v, 12, 12, 11.25, 8.75, 0.2)
a := d.spin(false, 0)
d.stroke(v, 12, 12, 10, 2.5, a, a+16.0/10, 1)
}}
// Ring creates an arc turning in a faint circle.
func Ring(c *ui.Context, o ...Options) ui.Element { return show(c, &ring, o) }
var ripple = spec{cycle: 1200 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
cx, cy := d.center()
for i := range 3 {
k, a := float64(i+1)/3, 0.4
if !d.still {
p := d.at(i, 3)
if d.o.Direction == RippleIn {
p = 1 - p
}
e := easeOut(p)
k, a = e, 1-e
}
if k <= 0 {
continue
}
r := d.s / 2 * float32(k)
d.hoop(identity, cx, cy, r, r-0.08*d.s*float32(k), a)
}
}}
// Ripple creates three rings spreading out from the center, or drawing in
// to it, as Options.Direction says.
func Ripple(c *ui.Context, o ...Options) ui.Element { return show(c, &ripple, o) }
var slide = spec{cycle: 2400 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
dot := 0.34 * d.s
far := 1.588 * float64(dot)
rest := [3][2]float64{{far, 0}, {0, 0}, {0, far}}
for i := range 3 {
// Around the square's corners, to the one empty, and waiting
// there for the others.
x, y := rest[i][0], rest[i][1]
if !d.still {
p := d.at(i, 3)
x = frames(p, easeInOut, key{0, far}, key{0.0833, far}, key{0.25, far}, key{0.3333, 0}, key{0.5, 0},
key{0.5833, 0}, key{0.75, 0}, key{0.8333, far}, key{1, far})
y = frames(p, easeInOut, key{0, 0}, key{0.0833, far}, key{0.25, far}, key{0.3333, far}, key{0.5, far},
key{0.5833, 0}, key{0.75, 0}, key{0.8333, 0}, key{1, 0})
}
d.dot(identity, d.x+0.06*d.s+dot/2+float32(x), d.y+0.06*d.s+dot/2+float32(y), dot/2, 1)
}
}}
// Slide creates three dots sliding into the empty corner of a square.
func Slide(c *ui.Context, o ...Options) ui.Element { return show(c, &slide, o) }
var snake = spec{cycle: 1400 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
v := d.view(24)
a := d.spin(false, 0)
// A dash stretching to 45 long, then going on at that length, as its
// start catches up with its end.
dash, offset := 18.0, 0.0
if !d.still {
p := d.at(0, 0)
dash = frames(p, easeInOut, key{0, 1}, key{0.5, 45}, key{1, 45})
offset = frames(p, easeInOut, key{0, 0}, key{0.5, -17}, key{1, -62})
}
dashes(2*math.Pi*10, dash, 100, offset, func(from, to float64) {
d.stroke(v, 12, 12, 10, 2.5, a+from/10, a+to/10, 1)
})
}}
// Snake creates an arc stretching and shrinking as it circles.
func Snake(c *ui.Context, o ...Options) ui.Element { return show(c, &snake, o) }
// swirlSteps are the order of Swirl's cells, by rows, around the grid,
// -1 for its empty middle.
var swirlSteps = [9]int{0, 1, 2, 7, -1, 3, 6, 5, 4}
var swirl = spec{cycle: 1200 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
pad, gap := 0.0625*d.s, 0.15625*d.s
cell := (d.s - 2*pad - 2*gap) / 3
for i, step := range swirlSteps {
if step < 0 {
continue
}
x, y := d.x+pad+float32(i%3)*(cell+gap), d.y+pad+float32(i/3)*(cell+gap)
d.rounded(identity, x, y, cell, cell, 0.0625*d.s, 0, d.fade(step, 8, 0.2, 0.6))
}
}}
// Swirl creates a bright cell chasing its trail around a square.
func Swirl(c *ui.Context, o ...Options) ui.Element { return show(c, &swirl, o) }
// Trace's rounded square, in its viewBox of 20, and its dash.
const (
traceAt, traceSide, traceRadius, traceWidth = 1.25, 17.5, 4, 2.5
traceDash = 16
)
// tracePerimeter is the length of Trace's rounded square.
var tracePerimeter = 4*(traceSide-2*traceRadius) + 2*math.Pi*traceRadius
var trace = spec{cycle: 1200 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
v := d.view(20)
const out = traceWidth / 2
d.rounded(v, traceAt-out, traceAt-out, traceSide+2*out, traceSide+2*out, traceRadius+out, traceWidth, 0.2)
at := 0.0
if !d.still {
at = d.turn(d.at(0, 0), false) * tracePerimeter
}
dashes(tracePerimeter, traceDash, tracePerimeter-traceDash, -at, func(from, to float64) {
d.traceDash(v, from, to)
})
}}
// traceDash draws Trace's dash from from to to along its rounded square,
// through v.
func (d *drawing) traceDash(v affine, from, to float64) {
// along returns the point at length u along the square, from the
// start of its top side, as SVG's rect starts, and the direction it
// goes there, in radians.
const straight = traceSide - 2*traceRadius
corner := math.Pi / 2 * traceRadius
along := func(u float64) (x, y, dir float64) {
side := min(3, int(u/(straight+corner)))
u -= float64(side) * (straight + corner)
dir = float64(side) * math.Pi / 2
sin, cos := math.Sincos(dir)
// Where the side starts: the top's after the top left corner.
starts := [4][2]float64{
{traceAt + traceRadius, traceAt}, {traceAt + traceSide, traceAt + traceRadius},
{traceAt + traceSide - traceRadius, traceAt + traceSide}, {traceAt, traceAt + traceSide - traceRadius},
}
x, y = starts[side][0], starts[side][1]
if u <= straight {
return x + u*cos, y + u*sin, dir
}
// Around the corner, whose center is inward of the side's end.
x, y = x+straight*cos-traceRadius*sin, y+straight*sin+traceRadius*cos
a := dir - math.Pi/2 + (u-straight)/traceRadius
sa, ca := math.Sincos(a)
return x + traceRadius*ca, y + traceRadius*sa, a + math.Pi/2
}
n := int(math.Ceil((to-from)/0.4)) + 1
s := d.begin(v)
if d.o.Cap == FlatCap {
// The dash's outline: one edge there, the other back.
for i := range 2 * n {
j, side := i, -1.0
if i >= n {
j, side = 2*n-1-i, 1
}
x, y, dir := along(from + (to-from)*float64(j)/float64(n-1))
sin, cos := math.Sincos(dir)
px, py := float32(x-side*traceWidth/2*sin), float32(y+side*traceWidth/2*cos)
if i == 0 {
s.move(px, py)
} else {
s.line(px, py)
}
}
s.close()
d.fill(1)
return
}
for i := range n {
x, y, _ := along(from + (to-from)*float64(i)/float64(n-1))
if i == 0 {
s.move(float32(x), float32(y))
} else {
s.line(float32(x), float32(y))
}
}
d.p.StrokePath(&s.Path, traceWidth*v.unit(), d.color)
}
// Trace creates a dash tracing the outline of a rounded square.
func Trace(c *ui.Context, o ...Options) ui.Element { return show(c, &trace, o) }
var wave = spec{cycle: 900 * time.Millisecond, aspect: 1, draw: func(d *drawing) {
w := 0.12 * d.s
for i := range 5 {
h := 0.4 + 0.15*float64(i)
if !d.still {
h = frames(d.at(i, 5), easeInOut, key{0, 0.3}, key{0.5, 1}, key{1, 0.3})
}
bh := float32(h) * d.s
y := d.y + (d.s-bh)/2
if d.o.Origin == WaveBottom {
y = d.y + d.s - bh
}
d.rounded(identity, d.x+float32(i)*(d.s-w)/4, y, w, bh, w/2, 0, 1)
}
}}
// Wave creates five bars rising and falling in a wave, from their middle
// or their bottom as Options.Origin says.
func Wave(c *ui.Context, o ...Options) ui.Element { return show(c, &wave, o) }