MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:
- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
elements expire and Context methods are inert: read IsDisabled into a
local during the build, keep a ui.Handle for Focus, use c.Services()
for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
content builds, so same-frame autofocus lost focus restore on close.
It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
split button tooltip no longer reorders its open menu; disabled
menubar rows pass presses to the panel; Form defers blur-started
async checks so the growing message no longer moves Submit away
from the release; Combobox drops its disabled copy now that MyGo
blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
time.Sleep/time.Now and read element state inside the view. The web
mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.
Snapshot goldens are darwin-only and were not re-verified here.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
96 lines
3.4 KiB
Go
96 lines
3.4 KiB
Go
package account
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"time"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/data"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
|
|
)
|
|
|
|
// QuotaItem is one metered resource: Used of Limit, in Unit.
|
|
type QuotaItem struct {
|
|
Label string
|
|
Used, Limit float64
|
|
Unit string
|
|
}
|
|
|
|
// UsageQuotaOptions configure a UsageQuota.
|
|
type UsageQuotaOptions struct {
|
|
// ResetAt provides the reset timestamp; zero hides it.
|
|
ResetAt time.Time
|
|
// Warn sets the warning threshold; zero means 0.8.
|
|
Warn float64
|
|
}
|
|
|
|
// UsageQuota displays usage bars and reset time.
|
|
func UsageQuota(c *ui.Context, items []QuotaItem, opts UsageQuotaOptions) ui.Element {
|
|
warn := opts.Warn
|
|
if warn == 0 {
|
|
warn = 0.8
|
|
}
|
|
if warn < 0 || warn > 1 || math.IsNaN(warn) {
|
|
panic("mujica: UsageQuota Warn must be between 0 and 1")
|
|
}
|
|
k := core.Tokens(c)
|
|
d := core.Density(c)
|
|
e := ui.Column(c).Gap(d.Unit() * 4).MinWidth(320)
|
|
e.Children(func() {
|
|
for i, it := range items {
|
|
if it.Label == "" || !(it.Limit > 0) || it.Used < 0 || math.IsNaN(it.Used) || math.IsInf(it.Used, 0) || math.IsInf(it.Limit, 0) {
|
|
panic(fmt.Sprintf("mujica: QuotaItem %q needs a Label, a positive Limit and Used of zero or more", it.Label))
|
|
}
|
|
share := it.Used / it.Limit
|
|
col, icon, note := k.AccentText, "", ""
|
|
switch {
|
|
case share >= 1:
|
|
col, icon, note = k.Danger, "circle-x", core.Msg(c, "usage_quota.over")
|
|
case share >= warn:
|
|
col, icon, note = k.Warning, "triangle-alert", core.Msg(c, "usage_quota.near")
|
|
}
|
|
amount := fmt.Sprintf(core.Msg(c, "usage_quota.amount"), data.StatisticFormat(it.Used, 0), data.StatisticFormat(it.Limit, 0), it.Unit)
|
|
ui.Column(c.Key(i)).Gap(d.Unit()).Role(ui.RoleMeter).Label(it.Label).
|
|
Description(fmt.Sprintf("%s, %s%% %s", amount, data.StatisticFormat(share*100, 0), note)).Children(func() {
|
|
ui.Row(c).Gap(d.Unit() * 2).AlignItems(ui.Center).Children(func() {
|
|
ui.Text(c, it.Label).FontWeight(500).Grow(1)
|
|
if icon != "" {
|
|
ui.Row(c).Gap(d.Unit() / 2).AlignItems(ui.Center).TextColor(col).Children(func() {
|
|
ui.Icon(c, icons.Must(icon)).Size(12, 12)
|
|
ui.Text(c, note).FontSize(core.FontSize(c, theme.CaptionSize)).FontWeight(600)
|
|
})
|
|
}
|
|
ui.Text(c, amount).TextColor(k.TextMuted).FontSize(core.FontSize(c, theme.CaptionSize)).FontFeatures("tnum")
|
|
})
|
|
ui.Box(c).Height(6).FillWidth().Draw(func(p *ui.Painter, r ui.Rect) {
|
|
p.Fill(r, k.SurfaceHover, theme.ControlRadius)
|
|
p.Fill(ui.Rect{X: r.X, Y: r.Y, W: r.W * float32(math.Min(1, share)), H: r.H}, col, theme.ControlRadius)
|
|
})
|
|
})
|
|
}
|
|
if !opts.ResetAt.IsZero() {
|
|
ui.Row(c).Gap(d.Unit()).AlignItems(ui.Center).TextColor(k.TextMuted).Children(func() {
|
|
ui.Icon(c, icons.Must("clock")).Size(12, 12)
|
|
ui.Text(c, usageQuotaReset(c, opts.ResetAt)).FontSize(core.FontSize(c, theme.CaptionSize))
|
|
})
|
|
}
|
|
})
|
|
return e
|
|
}
|
|
|
|
// usageQuotaReset formats hours or the reset date.
|
|
func usageQuotaReset(c *ui.Context, at time.Time) string {
|
|
left := at.Sub(c.Now())
|
|
switch {
|
|
case left <= 0:
|
|
return core.Msg(c, "usage_quota.reset_due")
|
|
case left < 24*time.Hour:
|
|
return fmt.Sprintf(core.Msg(c, "usage_quota.reset_hours"), int(math.Ceil(left.Hours())))
|
|
}
|
|
return fmt.Sprintf(core.Msg(c, "usage_quota.reset_on"), at.Format("2006-01-02"))
|
|
}
|