Split into category packages; module github.com/ZacharyZhang-NY/MujicaUI
This commit is contained in:
1822 files changed
+24568
-21314
No files matched your search
@@ -0,0 +1,92 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/core"
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/icons"
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/internal"
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/theme"
|
||||
)
|
||||
|
||||
// CollapsibleOptions configure a Collapsible.
|
||||
type CollapsibleOptions struct {
|
||||
// Title is the trigger's text; required unless Trigger is set.
|
||||
Title string
|
||||
// Trigger builds custom trigger content after the arrow.
|
||||
Trigger func()
|
||||
Disabled bool
|
||||
}
|
||||
|
||||
// Collapsible shows content below a trigger while *open; closed content is not built.
|
||||
func Collapsible(c *ui.Context, open *bool, opts CollapsibleOptions, content func()) core.ChoiceResult {
|
||||
if opts.Title == "" && opts.Trigger == nil {
|
||||
panic("mujica: Collapsible needs Title or Trigger")
|
||||
}
|
||||
k := core.Tokens(c)
|
||||
u := core.Density(c).Unit()
|
||||
var v core.ChoiceResult
|
||||
v.Element = ui.Column(c).Shrink(0)
|
||||
v.Element.Children(func() {
|
||||
was := *open
|
||||
p := ui.CollapsibleBase(c, open)
|
||||
tr := p.Trigger
|
||||
if opts.Disabled {
|
||||
*open = was
|
||||
}
|
||||
v = core.NewChoiceResult(v.Element, *open != was)
|
||||
tr.Disabled(opts.Disabled).AlignSelf(ui.Start).AlignItems(ui.Center).Gap(u*2).
|
||||
MinHeight(core.Density(c).ControlHeight()).Padding(0, u*3, 0, u*2).Radius(theme.ControlRadius)
|
||||
if opts.Title != "" {
|
||||
tr.Label(opts.Title)
|
||||
}
|
||||
internal.Ring(tr, k.Focus, theme.ControlRadius)
|
||||
if tr.Hovered() && !opts.Disabled {
|
||||
tr.Background(k.SurfaceHover)
|
||||
}
|
||||
prog := tr.Animate("mujica.open", collapsibleTarget(*open), core.Motion(c, theme.StateDuration))
|
||||
tr.Children(func() {
|
||||
ui.Icon(c, icons.Must("chevron-right")).Size(16, 16).Rotate(90 * prog).TextColor(k.TextMuted)
|
||||
if opts.Trigger != nil {
|
||||
opts.Trigger()
|
||||
} else {
|
||||
ui.Text(c, opts.Title).FontWeight(600).SingleLine()
|
||||
}
|
||||
})
|
||||
if panel := collapsiblePanel(c, *open, prog, content); panel != nil {
|
||||
panel.Padding(u*2, u*2, u*2, u*8).Gap(u * 2)
|
||||
}
|
||||
})
|
||||
if v.Changed() {
|
||||
slog.Debug("mujica: collapsible toggle", "open", *open)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func collapsibleTarget(open bool) float32 {
|
||||
if open {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// collapsiblePanel builds fn while open or animating, clipped to its progress.
|
||||
func collapsiblePanel(c *ui.Context, open bool, prog float32, fn func()) *ui.Element {
|
||||
if !open && prog == 0 {
|
||||
return nil
|
||||
}
|
||||
// Closing content takes no input or focus while it shrinks away.
|
||||
clip := ui.Column(c).Shrink(0).Disabled(!open)
|
||||
var inner *ui.Element
|
||||
clip.Children(func() {
|
||||
inner = ui.Column(c).Shrink(0)
|
||||
inner.Children(fn)
|
||||
})
|
||||
if prog < 1 {
|
||||
// The content's height from the last frame, grown by progress.
|
||||
clip.ClipY().Height(prog * inner.Bounds().H)
|
||||
}
|
||||
return inner
|
||||
}
|
||||
Reference in new issue
Block a user