WIP components (as built)
This commit is contained in:
102 files changed
+6538
-1
No files matched your search
@@ -0,0 +1,73 @@
|
||||
package mujica
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"mujicaui/icons"
|
||||
"mujicaui/internal"
|
||||
"mujicaui/theme"
|
||||
)
|
||||
|
||||
var issueIDPattern = regexp.MustCompile(`^[A-Z][A-Z0-9]*-[1-9][0-9]*$`)
|
||||
|
||||
// IssueIdBadgeOptions configure an IssueIdBadge.
|
||||
type IssueIdBadgeOptions struct {
|
||||
// Copyable makes a click or Enter copy the key.
|
||||
Copyable bool
|
||||
Disabled bool
|
||||
}
|
||||
|
||||
// issueCopiedFor is how long "Copied" shows after a copy.
|
||||
const issueCopiedFor = 1500 * time.Millisecond
|
||||
|
||||
// IssueIdBadge displays a monospace issue key with optional copying.
|
||||
func IssueIdBadge(c *ui.Context, id string, opts IssueIdBadgeOptions) ChoiceResult {
|
||||
if !issueIDPattern.MatchString(id) {
|
||||
panic(fmt.Sprintf("mujica: IssueIdBadge %q is not a key like ENG-123", id))
|
||||
}
|
||||
k := Tokens(c)
|
||||
d := Density(c)
|
||||
small := FontSize(c, theme.CaptionSize)
|
||||
if !opts.Copyable {
|
||||
e := ui.Row(c).Padding(d.Unit()/2, d.Unit()*1.5).Radius(theme.ControlRadius).Background(k.Background).
|
||||
Border(theme.BorderWidth, k.Border).Shrink(0).Label(id)
|
||||
e.Children(func() { ui.Text(c, id).Font(theme.MonoFont).FontSize(small).TextColor(k.TextMuted).SingleLine() })
|
||||
return ChoiceResult{Element: e}
|
||||
}
|
||||
b := ui.ButtonBase(c).MinHeight(theme.MinHitSize).Gap(d.Unit()).PaddingX(d.Unit()*1.5).Radius(theme.ControlRadius).
|
||||
Background(k.Background).Border(theme.BorderWidth, k.Border).Disabled(opts.Disabled)
|
||||
internal.Ring(b, k.Focus, theme.ControlRadius)
|
||||
copied := ui.Local(b, "issue-copied", func() time.Time { return time.Time{} })
|
||||
changed := false
|
||||
if b.Clicked() && !b.IsDisabled() {
|
||||
c.WriteClipboard(id)
|
||||
c.Announce(fmt.Sprintf(msg(c, "issue_id_badge.copied_label"), id))
|
||||
*copied, changed = c.Now(), true
|
||||
slog.Debug("mujica: issue_id_badge copy", "id", id)
|
||||
}
|
||||
showing := !copied.IsZero() && c.Now().Sub(*copied) < issueCopiedFor
|
||||
if showing {
|
||||
c.After(issueCopiedFor - c.Now().Sub(*copied))
|
||||
}
|
||||
b.Label(fmt.Sprintf(msg(c, "issue_id_badge.copy"), id))
|
||||
if b.Hovered() {
|
||||
b.Background(k.SurfaceHover)
|
||||
}
|
||||
b.Transition(colorFade).Children(func() {
|
||||
ui.Text(c, id).Font(theme.MonoFont).FontSize(small).TextColor(k.Text).SingleLine()
|
||||
icon, col := "copy", k.TextMuted
|
||||
if showing {
|
||||
icon, col = "check", k.Success
|
||||
}
|
||||
ui.Icon(c, icons.Must(icon)).Size(12, 12).TextColor(col)
|
||||
if showing {
|
||||
ui.Text(c, msg(c, "issue_id_badge.copied")).FontSize(small).TextColor(k.Success)
|
||||
}
|
||||
})
|
||||
return ChoiceResult{Element: b, changed: changed}
|
||||
}
|
||||
Reference in new issue
Block a user