WIP components (as built)
This commit is contained in:
71 files changed
+3241
-468
No files matched your search
+11
-6
@@ -7,11 +7,13 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"mujicaui/theme"
|
||||
)
|
||||
|
||||
// CountUpOptions configure a CountUp.
|
||||
type CountUpOptions struct {
|
||||
// Duration of a change; zero means 600 ms.
|
||||
// Duration of a change; zero means StateDuration.
|
||||
Duration time.Duration
|
||||
// Decimals shown; Format overrides it.
|
||||
Decimals int
|
||||
@@ -22,10 +24,9 @@ type CountUpOptions struct {
|
||||
type countUpState struct {
|
||||
from, to float64
|
||||
start time.Time
|
||||
set bool
|
||||
}
|
||||
|
||||
// CountUp counts from the last value to value; reduced motion shows it at once.
|
||||
// CountUp counts from the last value to the new one.
|
||||
func CountUp(c *ui.Context, value float64, opts CountUpOptions) *ui.Element {
|
||||
if math.IsNaN(value) || math.IsInf(value, 0) {
|
||||
panic("mujica: CountUp value must be finite")
|
||||
@@ -35,7 +36,7 @@ func CountUp(c *ui.Context, value float64, opts CountUpOptions) *ui.Element {
|
||||
}
|
||||
d := opts.Duration
|
||||
if d == 0 {
|
||||
d = 600 * time.Millisecond
|
||||
d = theme.StateDuration
|
||||
}
|
||||
format := opts.Format
|
||||
if format == nil {
|
||||
@@ -50,16 +51,20 @@ func CountUp(c *ui.Context, value float64, opts CountUpOptions) *ui.Element {
|
||||
return st.to
|
||||
}
|
||||
p := min(1, float64(now.Sub(st.start))/float64(dur))
|
||||
return st.from + (st.to-st.from)*float64(ui.EaseOut(float32(p)))
|
||||
eased := float64(ui.EaseOut(float32(p)))
|
||||
return st.from*(1-eased) + st.to*eased
|
||||
}
|
||||
if value != st.to {
|
||||
from := at()
|
||||
slog.Debug("mujica: count up", "from", from, "to", value)
|
||||
st.from, st.to, st.start = from, value, now
|
||||
}
|
||||
if dur == 0 {
|
||||
st.from, st.start = st.to, now
|
||||
}
|
||||
shown := at()
|
||||
if shown != st.to {
|
||||
c.AnimationFrame()
|
||||
row.DrawOver(func(*ui.Painter, ui.Rect) { c.AnimationFrame() })
|
||||
}
|
||||
return row.Children(func() { ui.Text(c, format(shown)).FontFeatures("tnum").SingleLine() })
|
||||
}
|
||||
Reference in new issue
Block a user