diff --git a/native/gockup-lich/app.go b/native/gockup-lich/app.go new file mode 100644 index 000000000..a4d0e5b21 --- /dev/null +++ b/native/gockup-lich/app.go @@ -0,0 +1,169 @@ +package lich + +import ( + "image" + "image/color" + + "gioui.org/layout" + "gioui.org/op" + "gioui.org/op/clip" + "gioui.org/op/paint" + "gioui.org/unit" + + "github.com/lichdotdev/gockup" + + "github.com/lichdotdev/lich/native/grimoire" + "github.com/lichdotdev/lich/native/ui" + "github.com/lichdotdev/lich/native/ui/markdown" +) + +// app registers the few drawings of cmd/lich-native a fragment cannot compose. +// That is a main package, so they are copies: each names the file it follows. +func (k kit) app() { + k.add("ProviderMark", "a provider's pixel mark in its brand color, as a session's row and header lead with it", func(c *gockup.Ctx, th *ui.Theme) D { + return providerMark(c.Gtx, th, c.Enum("provider"), c.Dp("box")) + }, enum("provider", "", "claude", "codex", "antigravity", "opencode", "omp", "crush", "cursor", "kiro"), num("box", 22, "dp: 22 in a row, 14 in a header")) + k.add("SessionStatus", "the corner of a session's row: working, waiting on the person, done and unread, or nothing; pending is a task not begun", func(c *gockup.Ctx, th *ui.Theme) D { + return sessionStatus(c.Gtx, th, c.Enum("state")) + }, enum("state", "", "idle", "working", "waiting", "done", "pending")) + k.holder("Composer", "the chat's message box: children are its foot, side by side, the attach button first and Send last after a weight=1 Spacer; the first head of them stack over the text instead, full width: the limit's notice, the attached files, the quotes", composer, + str("text", ""), str("placeholder", ""), num("head", 0, "how many of the first children stand over the text"), + flag("shell", "the line runs as a command"), flag("drag", "a file is dragged over the session")) +} + +// markColors and markHalf are sidebar_mark.go's. +var markColors = map[string]color.NRGBA{ + "claude": {R: 0xd9, G: 0x77, B: 0x57, A: 0xff}, + "codex": {R: 0x6e, G: 0x7b, B: 0xff, A: 0xff}, + "antigravity": {R: 0x42, G: 0x85, B: 0xf4, A: 0xff}, + "kiro": {R: 0x90, G: 0x46, B: 0xff, A: 0xff}, + "crush": {R: 0xff, G: 0x5f, B: 0xd2, A: 0xff}, +} + +const markHalf = 0.35 + +var marks = map[string]grimoire.Grid{} + +func providerMark(gtx C, th *ui.Theme, provider string, box unit.Dp) D { + grid, ok := marks[provider] + if !ok { + grid = grimoire.Mark(provider) + marks[provider] = grid + } + size := image.Pt(gtx.Dp(box), gtx.Dp(box)) + if len(grid) == 0 { + return D{Size: size} + } + ink, ok := markColors[provider] + if !ok { + ink = th.Foreground + } + px := max(1, gtx.Dp(1)) + defer op.Offset(image.Pt((size.X-len(grid[0])*px)/2, (size.Y-len(grid)*px)/2)).Push(gtx.Ops).Pop() + for y, row := range grid { + for x, cell := range row { + if cell == '.' { + continue + } + fill := ink + if cell == '2' { + fill = ui.Alpha(ink, markHalf) + } + at := image.Pt(x*px, y*px) + paint.FillShape(gtx.Ops, fill, clip.Rect{Min: at, Max: at.Add(image.Pt(px, px))}.Op()) + } + } + return D{Size: size} +} + +// The corner is sidebar_corner.go's 3x3, on a still step of its motion: the +// snake of a working session, the ring of a waiting one, the whole block. +var ( + statusBox = ui.Space(5.5) + statusSide = unit.Dp(16) + statusCells = map[string][9]float64{ + "working": {1, 0.10, 0.10, 0.65, 0.35, 0.10, 0.10, 0.10, 0.10}, + "waiting": {0.35, 0.65, 1, 0.10, 0.10, 0.10, 0.10, 0.10, 0.10}, + "done": {1, 1, 1, 1, 1, 1, 1, 1, 1}, + "pending": {0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22}, + } +) + +// statusSprite is the corner's side in sprite pixels: three cells of three +// with a pixel of gap. +const statusSprite = 11 + +func sessionStatus(gtx C, th *ui.Theme, state string) D { + size := image.Pt(gtx.Dp(statusBox), gtx.Dp(statusBox)) + cells, ok := statusCells[state] + if !ok { + return D{Size: size} + } + tone := map[string]color.NRGBA{"working": th.Primary, "waiting": th.ToneWait, "done": th.TonePass, "pending": th.MutedForeground}[state] + side := max(statusSprite, gtx.Dp(statusSide)) + edge := func(i int) int { return (i*side + statusSprite/2) / statusSprite } + defer op.Offset(size.Sub(image.Pt(side, side)).Div(2)).Push(gtx.Ops).Pop() + for i, alpha := range cells { + x, y := i%3*4, i/3*4 + paint.FillShape(gtx.Ops, ui.MixOKLab(th.Surface, tone, alpha), clip.Rect(image.Rect(edge(x), edge(y), edge(x+3), edge(y+3))).Op()) + } + return D{Size: size} +} + +// What chat_view.go and chat_footer.go dress the composer's textarea in. +const ( + composerEdgeTint = 0.45 + composerFillTint = 0.08 +) + +var ( + composerMax = ui.Space(48) + composerMin = ui.Space(24) + composerPadX = ui.Space(4) + composerPadY = ui.Space(3.5) + composerFootPad = ui.Space(1.5) +) + +func composer(c *gockup.Ctx, th *ui.Theme) D { + lean := th.Primary + if c.Bool("shell") { + lean = th.ToneWait + } + tinted := *th + tinted.Palette.Input = ui.MixOKLab(th.Palette.Input, lean, composerFillTint) + ta := tinted.Textarea(editor(c), c.String("placeholder")) + ta.MaxHeight, ta.MinHeight, ta.Radius, ta.Steady = composerMax, composerMin, ui.RadiusLG, true + ta.Size, ta.Leading, ta.PadX, ta.PadY = markdown.BodySize, markdown.BodySize*markdown.LineHeightScale, composerPadX, composerPadY + c.Provide(surfaceKey{}, th.On(tinted.InputFill())) + nodes := c.Children() + n := min(max(int(c.Number("head")), 0), len(nodes)) + if n > 0 { + m := op.Record(c.Gtx.Ops) + head := stacked(nodes[:n], layout.Vertical, 0)(spanning(c.Gtx)) + call := m.Stop() + ta.MaxHeight += c.Gtx.Metric.PxToDp(head.Size.Y) + ta.Head = func(gtx C) D { call.Add(gtx.Ops); return head } + } + foot := stacked(nodes[n:], layout.Horizontal, 1) + ta.Foot = func(gtx C) D { + return layout.Inset{Left: composerFootPad, Right: composerFootPad, Bottom: composerFootPad}.Layout(gtx, func(gtx C) D { return foot(spanning(gtx)) }) + } + d := ta.Layout(c.Gtx) + if c.Bool("drag") { + dashed(c.Gtx, d.Size, ui.RadiusLG, th.Foreground) + return d + } + ui.InsetBorder(c.Gtx, d.Size, c.Gtx.Dp(ui.RadiusLG), th.Over(ui.MixOKLab(th.Palette.Input, lean, composerEdgeTint))) + return d +} + +// dashed is chat_attach.go's dragRing. +func dashed(gtx C, size image.Point, radius unit.Dp, ink color.NRGBA) { + ui.DashedRing(gtx, image.Rectangle{Max: size}.Inset(gtx.Dp(1)), radius, dashedWidth, ink, color.NRGBA{}) +} + +const ( + dashedWidth = unit.Dp(1.5) + // dashedOpacity is chat_attach.go's chatGhostOpacity. + dashedOpacity = 0.55 +) diff --git a/native/gockup-lich/controls.go b/native/gockup-lich/controls.go new file mode 100644 index 000000000..2d3ae3b4a --- /dev/null +++ b/native/gockup-lich/controls.go @@ -0,0 +1,270 @@ +package lich + +import ( + "strings" + + "gioui.org/layout" + "gioui.org/unit" + "gioui.org/widget" + + "github.com/lichdotdev/gockup" + + "github.com/lichdotdev/lich/native/ui" +) + +var ( + variantProp = enum("variant", "default is the view's one filled button", "default", "outline", "secondary", "ghost", "destructive") + variants = map[string]ui.Variant{"outline": ui.VariantOutline, "secondary": ui.VariantSecondary, "ghost": ui.VariantGhost, "destructive": ui.VariantDestructive} + sizeProp = enum("size", "the icon sizes are for a button with no text", "sm", "xs", "md", "lg", "icon", "icon-xs", "icon-sm", "icon-lg") + sizes = map[string]ui.Size{"md": ui.SizeMD, "xs": ui.SizeXS, "sm": ui.SizeSM, "lg": ui.SizeLG, + "icon": ui.SizeIcon, "icon-xs": ui.SizeIconXS, "icon-sm": ui.SizeIconSM, "icon-lg": ui.SizeIconLG} +) + +func (k kit) buttons() { + k.add("Button", "", func(c *gockup.Ctx, th *ui.Theme) D { + b := th.Button(gockup.State[ui.Clickable](c, nil), c.String("text")) + b.Icon, b.Keys, b.AlsoKeys = icon(c, "icon"), c.String("keys"), c.String("alsoKeys") + b.Variant, b.Size = variants[c.Enum("variant")], sizes[c.Enum("size")] + b.Disabled, b.Lit, b.Busy = c.Bool("disabled"), c.Bool("lit"), c.Bool("busy") + return b.Layout(c.Gtx) + }, str("text", ""), iconProp("icon"), variantProp, sizeProp, + str("keys", `the key that presses it, "Ctrl+Enter"`), str("alsoKeys", "a second key, Esc"), + flag("lit", "drawn hovered"), flag("busy", "a spinner for the icon"), flag("disabled", "")) + + k.add("IconAction", "a panel header's bare icon button", func(c *gockup.Ctx, th *ui.Theme) D { + s := gockup.State[ui.IconActionState](c, nil) + return th.IconAction(new(ui.Root), s, iconOr(c, "icon"), "", c.Bool("on")).Layout(c.Gtx) + }, iconProp("icon"), flag("on", "")) + k.add("CornerButton", "a card's corner control", func(c *gockup.Ctx, th *ui.Theme) D { + return th.CornerButton(gockup.State[ui.Clickable](c, nil), iconOr(c, "icon"), c.Bool("on")).Layout(c.Gtx) + }, iconProp("icon"), flag("on", "the glyph filled, as a set pin")) + k.add("BarIconButton", "the top bar's icon button", func(c *gockup.Ctx, th *ui.Theme) D { + click := gockup.State[ui.Clickable](c, nil) + if c.Bool("disabled") { + click = nil + } + d := th.BarIconButton(click, iconOr(c, "icon"), c.Bool("on")).Layout(c.Gtx) + if c.Bool("notify") { + th.NotifyDot(c.Gtx, d.Size) + } + return d + }, iconProp("icon"), flag("on", "its popover is open"), flag("notify", "the red dot"), flag("disabled", "")) + k.add("CardIconButton", "a small glyph in a square of a card", func(c *gockup.Ctx, th *ui.Theme) D { + if c.Bool("disabled") { + return th.CardIconDisabled(c.Gtx, iconOr(c, "icon"), c.Dp("side"), 0) + } + return th.CardIconButton(c.Gtx, gockup.State[ui.Clickable](c, nil), iconOr(c, "icon"), c.Dp("side"), 0) + }, iconProp("icon"), num("side", 24, "dp"), flag("disabled", "")) + k.add("GhostTextButton", "a small word that is a button", func(c *gockup.Ctx, th *ui.Theme) D { + return th.GhostTextButton(c.Gtx, gockup.State[ui.Clickable](c, nil), c.String("text"), tone(c, th)) + }, str("text", ""), toneProp("foreground")) + k.add("Link", "a muted link, with an icon at its end when it leaves the window", func(c *gockup.Ctx, th *ui.Theme) D { + click := gockup.State[ui.Clickable](c, nil) + if ic := icon(c, "icon"); ic != nil { + return th.TrailingIconLink(c.Gtx, click, c.String("text"), ic) + } + return th.HoverLink(c.Gtx, click, c.String("text")) + }, str("text", ""), iconProp("icon")) + k.add("Chip", "a filter with the count of what it shows", func(c *gockup.Ctx, th *ui.Theme) D { + return th.Chip(gockup.State[ui.Clickable](c, nil), c.String("text"), int(c.Number("count")), c.Bool("on")).Layout(c.Gtx) + }, str("text", ""), num("count", 0, ""), flag("on", "")) +} + +func editor(c *gockup.Ctx) *widget.Editor { + return gockup.State(c, func(e *widget.Editor) { e.SetText(c.String("text")) }) +} + +func (k kit) fields() { + width := num("width", 0, "dp; left out, the whole row") + height := num("height", 0, "dp; 36 left out, 28 or 32 for a dense field") + small := flag("small", "text-xs, for a dense field") + textSize := func(c *gockup.Ctx) unit.Sp { + if c.Bool("small") { + return ui.TextXS + } + return 0 + } + k.add("Input", "a one-line field", func(c *gockup.Ctx, th *ui.Theme) D { + in := th.Input(editor(c), c.String("placeholder")) + in.Leading, in.Height, in.Width, in.TextSize = icon(c, "icon"), c.Dp("height"), c.Dp("width"), textSize(c) + in.Mono, in.Bare, in.Borderless = c.Bool("mono"), c.Bool("bare"), c.Bool("borderless") + in.Disabled, in.Invalid = c.Bool("disabled"), c.Bool("invalid") + if keys := c.String("keys"); keys != "" { + in.Trailing = th.KeyCap(keys).Layout + } + return in.Layout(c.Gtx) + }, str("text", ""), str("placeholder", ""), iconProp("icon"), width, height, small, flag("mono", ""), + str("keys", "the shortcut that focuses it"), flag("bare", "no box at all: the field of a picker"), + flag("borderless", "no border or ring: a field in a panel"), flag("invalid", ""), flag("disabled", "")) + + k.add("InputGroup", "a field with an icon or words inside its edges", func(c *gockup.Ctx, th *ui.Theme) D { + in := th.InputGroup(editor(c), c.String("placeholder")) + in.Width, in.Height, in.TextSize, in.Disabled, in.Invalid = c.Dp("width"), c.Dp("height"), textSize(c), c.Bool("disabled"), c.Bool("invalid") + inner := in.Inner() + if s := c.String("menu"); s != "" { + b := inner.InputGroupButton(gockup.State[ui.Clickable](c, nil), s) + b.Icon = lookupIcon("chevron-down") + in.Start.Items, in.Start.Button = append(in.Start.Items, b.Layout), true + } + if ic := icon(c, "icon"); ic != nil { + in.Start.Items = append(in.Start.Items, inner.InputGroupIcon(ic)) + } + if s := c.String("prefix"); s != "" { + in.Start.Items = append(in.Start.Items, inner.InputGroupText(s)) + } + if s := c.String("suffix"); s != "" { + in.End.Items = append(in.End.Items, inner.InputGroupText(s)) + } + if s := c.String("button"); s != "" { + b := inner.InputGroupButton(gockup.State[ui.Clickable](c, nil), s) + in.End.Items, in.End.Button = append(in.End.Items, b.Layout), true + } + return in.Layout(c.Gtx) + }, str("text", ""), str("placeholder", ""), iconProp("icon"), str("prefix", ""), str("suffix", ""), str("button", "a button's text at the end"), str("menu", "a menu's value, leading the field"), + width, height, small, flag("invalid", ""), flag("disabled", "")) + + k.add("Textarea", "a field of several lines", func(c *gockup.Ctx, th *ui.Theme) D { + area := th.Textarea(editor(c), c.String("placeholder")) + area.Width, area.MinHeight, area.Mono = c.Dp("width"), c.Dp("minHeight"), c.Bool("mono") + area.Disabled, area.Invalid = c.Bool("disabled"), c.Bool("invalid") + return area.Layout(c.Gtx) + }, str("text", ""), str("placeholder", ""), width, num("minHeight", 0, "dp"), flag("mono", ""), flag("invalid", ""), flag("disabled", "")) + + k.add("Select", "a select's closed field; its open list is a Popover", func(c *gockup.Ctx, th *ui.Theme) D { + s := th.SelectTrigger(c.String("value"), c.Dp("width")) + if ic := icon(c, "icon"); ic != nil { + s.Lead = func(gtx C) D { return ic.Layout(gtx, ui.Space(4), th.MutedForeground) } + } + return s.Layout(c.Gtx) + }, str("value", ""), iconProp("icon"), num("width", 240, "dp")) + + on := func(prop string) func(c *gockup.Ctx) *ui.Bool { + return func(c *gockup.Ctx) *ui.Bool { + return gockup.State(c, func(b *ui.Bool) { b.Value = c.Bool(prop) }) + } + } + k.add("Checkbox", "the box, with its label and a muted note under it when they are set", func(c *gockup.Ctx, th *ui.Theme) D { + box := on("checked")(c) + if c.String("label") != "" { + return th.CheckRow(c.Gtx, box, c.String("label"), c.String("note")) + } + check := th.Checkbox(box) + check.Disabled = c.Bool("disabled") + return check.Layout(c.Gtx) + }, flag("checked", ""), str("label", ""), str("note", ""), flag("disabled", "the bare box only")) + k.add("Switch", "", func(c *gockup.Ctx, th *ui.Theme) D { + s := th.Switch(gockup.State(c, func(s *ui.SwitchState) { s.Value = c.Bool("on") })) + s.Disabled = c.Bool("disabled") + if c.Enum("size") == "sm" { + s.Size = ui.SwitchSizeSM + } + return s.Layout(c.Gtx) + }, flag("on", ""), enum("size", "", "md", "sm"), flag("disabled", "")) + + toggleVariant := enum("variant", "", "default", "outline") + toggleSize := enum("size", "", "md", "sm", "lg") + look := func(c *gockup.Ctx) (v ui.ToggleVariant, s ui.ToggleSize) { + if c.Enum("variant") == "outline" { + v = ui.ToggleVariantOutline + } + return v, map[string]ui.ToggleSize{"sm": ui.ToggleSizeSM, "lg": ui.ToggleSizeLG}[c.Enum("size")] + } + k.add("Toggle", "a button that stays pressed", func(c *gockup.Ctx, th *ui.Theme) D { + t := th.Toggle(on("on")(c), c.String("text")) + t.Icon, t.Disabled = icon(c, "icon"), c.Bool("disabled") + t.Variant, t.Size = look(c) + return t.Layout(c.Gtx) + }, str("text", ""), iconProp("icon"), flag("on", ""), toggleVariant, toggleSize, flag("disabled", "")) + k.add("ToggleGroup", "one choice of a few, side by side", func(c *gockup.Ctx, th *ui.Theme) D { + texts, names := list(c.String("items")), list(c.String("icons")) + items := make([]ui.ToggleItem, max(len(texts), len(names))) + for i := range items { + items[i] = ui.ToggleItem{Value: position(i), Text: at(texts, i), Icon: lookupIcon(at(names, i))} + } + g := th.ToggleGroup(gockup.State(c, func(e *ui.Enum) { e.Value = position(int(c.Number("selected"))) }), items...) + g.Variant, g.Size = look(c) + if c.Bool("dense") { + // chat_switch.go's GUI | TUI selector + g.Size, g.ItemHeight, g.ItemPadX, g.ItemText, g.ItemIcon = ui.ToggleSizeSM, ui.Space(5), ui.Space(1.5), ui.Text2XS, ui.Space(3) + } + return g.Layout(c.Gtx) + }, str("items", `the texts, "List, Grid"`), iconsProp, num("selected", 0, "from 0"), toggleVariant, toggleSize, flag("dense", "the size of a session header's GUI | TUI")) + + k.holder("Tabs", "the tab list and, with children, the selected tab's panel under it: one child a tab, filling the space given", func(c *gockup.Ctx, th *ui.Theme) D { + texts, names, counts := list(c.String("items")), list(c.String("icons")), list(c.String("counts")) + triggers := make([]ui.TabsTrigger, max(len(texts), len(names))) + if len(triggers) == 0 { + return D{} + } + countSize := ui.TextSM + if c.Enum("variant") == "ghost" { + countSize = ui.TextXS + } + for i := range triggers { + triggers[i].Text = at(texts, i) + // a nil *icons.Icon in the Glyph would be an icon to draw + if ic := lookupIcon(at(names, i)); ic != nil { + triggers[i].Icon = ic + } + if n, alert := strings.CutPrefix(at(counts, i), "!"); n != "" { + fg := th.MutedForeground + if alert { + fg = th.Destructive + } + triggers[i].Trailing = th.Text(countSize, n).In(fg).Layout + } + } + t := th.Tabs(gockup.State(c, func(s *ui.TabsState) { s.Selected = int(c.Number("selected")) }), triggers...) + t.Variant = map[string]ui.TabsVariant{"line": ui.TabsVariantLine, "ghost": ui.TabsVariantGhost}[c.Enum("variant")] + if c.Bool("vertical") { + t.Orientation = ui.TabsVertical + } + var panels []layout.Widget + for _, n := range c.Children() { + panels = append(panels, n.Widget) + } + return t.Layout(c.Gtx, panels...) + }, str("items", `the texts, "Overview, Activity"`), iconsProp, str("counts", `one an item, muted after it, "!" before one that alerts: ", 4, !1"`), num("selected", 0, "from 0"), + enum("variant", "ghost is the dock header's", "default", "line", "ghost"), flag("vertical", "")) +} + +// iconsProp is one icon an item, an empty one for none: "list, , grid". +var iconsProp = func() gockup.Prop { + one := iconProp("icons") + check := one.Check + one.Doc = `one an item, "list, layout-grid"` + one.Check = func(s string) string { + for _, name := range list(s) { + if msg := check(name); msg != "" { + return msg + } + } + return "" + } + return one +}() + +func (k kit) rows() { + k.holder("NavRow", "a list's row, selected or not: children side by side, weight=1 on the one that takes the room", func(c *gockup.Ctx, th *ui.Theme) D { + click := gockup.State[ui.Clickable](c, nil) + return th.NavRow(c.Gtx, click, c.Bool("on"), c.Dp("height"), c.Dp("padX"), func(gtx C, th *ui.Theme) D { + return children(c, th, layout.Horizontal, c.Number("gap")/4)(gtx) + }) + }, flag("on", "selected"), num("height", 32, "dp"), num("padX", 6, "dp"), num("gap", 8, "dp")) + k.holder("PanelRow", "a panel's row, as high as its children, which sit side by side", func(c *gockup.Ctx, th *ui.Theme) D { + click := gockup.State[ui.Clickable](c, nil) + return th.PanelRow(c.Gtx, click, true, ui.Space(2), ui.Space(1.5), func(gtx C, th *ui.Theme) D { + return children(c, th, layout.Horizontal, c.Number("gap")/4)(gtx) + }) + }, num("gap", 8, "dp")) + k.holder("PopoverRow", "a Popover's row for what a MenuItem cannot say: children side by side", func(c *gockup.Ctx, th *ui.Theme) D { + row := th.PopoverRow(gockup.State[ui.Clickable](c, nil), func(gtx C, th *ui.Theme) D { + return children(c, th, layout.Horizontal, c.Number("gap")/4)(gtx) + }) + row.Lit = c.Bool("lit") + return row.Layout(c.Gtx) + }, flag("lit", "the row under the cursor"), num("gap", 8, "dp")) + k.add("TwoLines", "a name and, muted under it, what places it", func(c *gockup.Ctx, th *ui.Theme) D { + return th.TwoLines(c.String("top"), c.String("under")).Layout(c.Gtx) + }, str("top", ""), str("under", "")) +} diff --git a/native/gockup-lich/go.mod b/native/gockup-lich/go.mod new file mode 100644 index 000000000..b0e667279 --- /dev/null +++ b/native/gockup-lich/go.mod @@ -0,0 +1,47 @@ +module github.com/lichdotdev/lich/native/gockup-lich + +go 1.27.0 + +require ( + gioui.org v0.10.3 + github.com/lichdotdev/gockup v0.1.0 + github.com/lichdotdev/godemirror v0.15.0 + github.com/lichdotdev/lich/native v0.0.0 +) + +require ( + gioui.org/shader v1.0.9 // indirect + gioui.org/x v0.10.3 // indirect + git.wow.st/gmp/jni v0.0.0-20210610011705-34026c7e22d0 // indirect + github.com/alecthomas/chroma/v2 v2.27.0 // indirect + github.com/dlclark/regexp2/v2 v2.2.1 // indirect + github.com/dustin/go-humanize v1.1.0 // indirect + github.com/go-ole/go-ole v1.2.6 // indirect + github.com/go-text/typesetting v0.3.5 // indirect + github.com/godbus/dbus/v5 v5.2.2 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/mattn/go-isatty v0.0.24 // indirect + github.com/ncruces/go-strftime v1.1.0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + github.com/srwiley/oksvg v0.0.0-20261130192221-7226c6c68bde // indirect + github.com/srwiley/rasterx v0.0.0-20261128054241-78f2104be592 // indirect + github.com/yuin/goldmark v1.4.13 // indirect + golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect + golang.org/x/exp/shiny v0.0.0-20261009195045-ca0d7ba23607 // indirect + golang.org/x/image v0.47.0 // indirect + golang.org/x/net v0.55.0 // indirect + golang.org/x/sys v0.49.0 // indirect + golang.org/x/text v0.44.0 // indirect + modernc.org/libc v1.80.1 // indirect + modernc.org/mathutil v1.7.1 // indirect + modernc.org/memory v1.12.1 // indirect + modernc.org/sqlite v1.64.0 // indirect +) + +replace gioui.org => github.com/lichdotdev/gio v0.0.0-20261127143756-265ddb097d37 + +replace github.com/lichdotdev/lich/native => ../ + +replace github.com/lichdotdev/lich => ../../ + +replace github.com/lichdotdev/lich/extension => ../../extension diff --git a/native/gockup-lich/go.sum b/native/gockup-lich/go.sum new file mode 100644 index 000000000..1860f2584 --- /dev/null +++ b/native/gockup-lich/go.sum @@ -0,0 +1,100 @@ +eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d h1:ARo7NCVvN2NdhLlJE9xAbKweuI9L6UgfTbYb0YwPacY= +eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d/go.mod h1:OYVuxibdk9OSLX8vAqydtRPP87PyTFcT9uH3MlEGBQA= +gioui.org/cpu v0.0.0-20210808092351-bfe733dd3334/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ= +gioui.org/shader v1.0.9 h1:XxnqIfmClWpN49kizxH2W0JcCFrrEP4q3jZmNYaltbs= +gioui.org/shader v1.0.9/go.mod h1:mWdiME581d/kV7/iEhLmUgUK5iZ09XR5XpduXzbePVM= +gioui.org/x v0.10.3 h1:Q5ou4xPottXzyHq+x36ZcjFZfEV6rnyokWVPlMeyq9k= +gioui.org/x v0.10.3/go.mod h1:zAKc92Mp1xlficmcPkUFOcREY01zMiqLmO4qhiJEEO4= +git.wow.st/gmp/jni v0.0.0-20210610011705-34026c7e22d0 h1:bGG/g4ypjrCJoSvFrP5hafr9PPB5aw8SjcOWWila7ZI= +git.wow.st/gmp/jni v0.0.0-20210610011705-34026c7e22d0/go.mod h1:+axXBRUTIDlCeE73IKeD/os7LoEnTKdkp8/gQOFjqyo= +github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0= +github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= +github.com/alecthomas/chroma/v2 v2.27.0 h1:FodwmyOBgJULFYmDqibcp9pvfDLWdtPRh9v/r5BXYZs= +github.com/alecthomas/chroma/v2 v2.27.0/go.mod h1:NjJ3ciIgrqBNeIkWZ4e46nseoLDslxU1LmfCoL+wcY8= +github.com/alecthomas/repr v0.5.2 h1:SU73FTI9D1P5UNtvseffFSGmdNci/O6RsqzeXJtP0Qs= +github.com/alecthomas/repr v0.5.2/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= +github.com/dlclark/regexp2/v2 v2.2.1 h1:mf4KkFUj0gJuarK8P+LgiS+Lit7m9N1yAwEfPbee7R0= +github.com/dlclark/regexp2/v2 v2.2.1/go.mod h1:avUrQvPaLz2DrFNHJF0taWAFFX2C1GMSSoeiqFjcBmU= +github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= +github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= +github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-text/typesetting v0.3.5 h1:XZPUooClHY0Vf/rFyUyuPRNEkawARaFzLMQcXLSEyPk= +github.com/go-text/typesetting v0.3.5/go.mod h1:XZO1hD+nQVyvVa5IicQk7FsCa4PFQaJ2soWAP1f//68= +github.com/go-text/typesetting-utils v0.0.0-20260419141703-4ffe8874dabc h1:8FGo2It5K75XkavhTiCKExUfVaVDS1feBnLCru5qeoY= +github.com/go-text/typesetting-utils v0.0.0-20260419141703-4ffe8874dabc/go.mod h1:3/62I4La/HBRX9TcTpBj4eipLiwzf+vhI+7whTc9V7o= +github.com/godbus/dbus/v5 v5.2.2 h1:TUR3TgtSVDmjiXOgAAyaZbYmIeP3DPkld3jgKGV8mXQ= +github.com/godbus/dbus/v5 v5.2.2/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c= +github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50 h1:PlNWJRxu5APSV0pWqfIsZ0qt9OOGHSWfZQ4zvx+WgWs= +github.com/google/pprof v0.0.0-20261016150608-1facf8f91b50/go.mod h1:yUo1AMMpsPIDFGuwDK8nLcf8+vvAd5l2O6JDZ6hpwrM= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= +github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= +github.com/lichdotdev/gio v0.0.0-20261127143756-265ddb097d37 h1:0pCJYQW0lFw0TcpVILUO/yA6879JM2o+JWu7XEDOZ3Q= +github.com/lichdotdev/gio v0.0.0-20261127143756-265ddb097d37/go.mod h1:x8MAOooc/v4UUaB5o6BS/FpYdxnzh3LBvZlTBmYpnlQ= +github.com/lichdotdev/gockup v0.1.0 h1:1tFSI1/NjG0wlJqOJmHm2HHXbY/krGXDtQ68bMXJ638= +github.com/lichdotdev/gockup v0.1.0/go.mod h1:cx+8lod9xSPMdsv3OnLagGcE69fptoORFZu52tb2LY4= +github.com/lichdotdev/godemirror v0.15.0 h1:YWf3zPi+Ekkm17UKW6W1OCkPiWBj7N83l8g/wTlUkPU= +github.com/lichdotdev/godemirror v0.15.0/go.mod h1:Bpzw5JlSRuXiEVRcX2gD+nsP0ix8cDpy9TTH5Q3n3XY= +github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= +github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/ncruces/go-strftime v1.1.0 h1:vtPmPWMkb539RGV9WpWT9EwJGvrOCKvMNI4obWL47Hk= +github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2KmZ9Fj7xeG/g= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/srwiley/oksvg v0.0.0-20261130192221-7226c6c68bde h1:MF9B13ldBASIntHFFVpHAn3LqSN3n0TOcKr2ZE9AND4= +github.com/srwiley/oksvg v0.0.0-20261130192221-7226c6c68bde/go.mod h1:/IDkhd6oVxHu5mGDgEZ4saN5eGQzFLH8gM5fUh8Q4uk= +github.com/srwiley/rasterx v0.0.0-20261128054241-78f2104be592 h1:pd3VnNYlvepO3o7LMQI4wWkGfRO3ENSaVl6nR3gi0CM= +github.com/srwiley/rasterx v0.0.0-20261128054241-78f2104be592/go.mod h1:nXTWP6+gD5+LUJ8krVhhoeHjvHTutPxMYl5SvkcnJNE= +github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 h1:R84qjqJb5nVJMxqWYb3np9L5ZsaDtB+a39EqjV0JSUM= +golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0/go.mod h1:S9Xr4PYopiDyqSyp5NjCrhFrqg6A5zA2E/iPHPhqnS8= +golang.org/x/exp/shiny v0.0.0-20261009195045-ca0d7ba23607 h1:+d6HV3AHvcT254vs2elXnEOB602gvxOWsQtucpdtnAI= +golang.org/x/exp/shiny v0.0.0-20261009195045-ca0d7ba23607/go.mod h1:LjwWUAy73DRPU6z93L7ykfRhO3biQRlb+dRIYvdwhs8= +golang.org/x/image v0.47.0 h1:za/8gVz++PPZqmZJBNSISICN5Rrj5zixP/8K++fgmqo= +golang.org/x/image v0.47.0/go.mod h1:IkN5o4A7vq55vXN8FWjjnhg9Rs2GOtffqsSi9mWAKYE= +golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= +golang.org/x/mod v0.42.0/go.mod h1:u5fzIrRInkL8MaO1cbRkUHT64v303YIT5KEqpNr7jRs= +golang.org/x/net v0.55.0 h1:bcvxaJn3e1U6InsFWt1JUq1aSjnRxLzT2rtD2KfkDF8= +golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww= +golang.org/x/sync v0.25.0 h1:dJnifr0nMV8/SBnBvdo4eSGGnrc3O4FFx6IqvtLcmRs= +golang.org/x/sync v0.25.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= +golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/text v0.44.0 h1:YItdp/mQNGnsmrgpcdA5yQ3bnRxCfo/Tj/iByGW9GGo= +golang.org/x/text v0.44.0/go.mod h1:S4FR1waQ9LbjQTls97SyRjDaUUVkaM+cXwwuG/G+MwY= +golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs= +golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs= +modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b h1:WE5SLOURcNd8s6uPR/n4WV4weQxxYKi5OpEZkfNVvdE= +modernc.org/cc/v4 v4.30.6-0.20261101114202-1b9d1a076f6b/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI= +modernc.org/ccgo/v4 v4.38.1 h1:wdxJVBuFT8JufZnnQyS6Bbz3NOWhViD0xJOGR96Bpd4= +modernc.org/ccgo/v4 v4.38.1/go.mod h1:kOpEkJRbFwBvjUWwn4j1j9B7iFHbl1+orhgTMCDkDBk= +modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM= +modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU= +modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI= +modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito= +modernc.org/gc/v3 v3.2.0 h1:/Y3LGDMQaoYf55PebP1qsZ5FDGgAZyG1RYKEHeQzh1o= +modernc.org/gc/v3 v3.2.0/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY= +modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks= +modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI= +modernc.org/libc v1.80.1 h1:ejn7HtTV3GknNKbklqWQ1nvUrfX8mTxs1D+xp/JQwy4= +modernc.org/libc v1.80.1/go.mod h1:pvo25Rs3OJEbDqhUV9jdEGevxeLJrJeDmNid/AcQV1g= +modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= +modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= +modernc.org/memory v1.12.1 h1:nFMiWrpStgZczNl6XI9GnIk/rWhYIyHGUaR04pGbp9g= +modernc.org/memory v1.12.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg= +modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns= +modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w= +modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE= +modernc.org/sqlite v1.64.0 h1:xCoLcRc2g33gLkmaRrcsJLitr4TRMQGGb3H4HYs3mbM= +modernc.org/sqlite v1.64.0/go.mod h1:Jzjh1mt4s0HuQ0KvtB1LIuHiGRmU2D5pgj8dUhW7/SE= +modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0= +modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/native/gockup-lich/lich.go b/native/gockup-lich/lich.go new file mode 100644 index 000000000..358d74101 --- /dev/null +++ b/native/gockup-lich/lich.go @@ -0,0 +1,310 @@ +// Package lich registers the native window's widget kit (native/ui) with +// gockup, so mockups are drawn with the components the window ships. +package lich + +import ( + "image/color" + "strings" + "time" + + "gioui.org/font" + "gioui.org/font/gofont" + "gioui.org/io/input" + "gioui.org/layout" + "gioui.org/op" + "gioui.org/text" + + "github.com/lichdotdev/gockup" + "github.com/lichdotdev/godemirror" + + "github.com/lichdotdev/lich/native/fonts" + "github.com/lichdotdev/lich/native/ui" + "github.com/lichdotdev/lich/native/ui/icons" +) + +type ( + C = layout.Context + D = layout.Dimensions +) + +// highlightWait is how long a render waits for a code view to color its text. +const highlightWait = 300 * time.Millisecond + +func Register(r *gockup.Registry) { + dark, light := themes() + r.Background = dark.Surface + r.Themes = map[string]gockup.Theme{ + "dark": {Background: dark.Surface, Value: dark}, + "light": {Background: light.Surface, Value: light}, + } + r.Settle = highlightWait + k := kit{Registry: r, fallback: dark} + k.surfaces() + k.overlays() + k.text() + k.buttons() + k.fields() + k.rows() + k.marks() + k.gauges() + k.viewers() + k.app() +} + +// themes builds the window's two looks as lich-native does: Onest for the +// interface, the system's monospace for code, drawn on the canvas. +func themes() (dark, light *ui.Theme) { + sans, err := fonts.Faces() + if err != nil { + panic("gockup-lich: " + err.Error()) // Onest is embedded + } + collection := append(gofont.Collection(), sans...) + mono := font.Typeface("Go Mono") + // A machine with no monospace of its own still draws, in Go's. + if faces, err := fonts.Load(fonts.SystemMono); err == nil { + collection, mono = append(collection, faces...), fonts.SystemMono + } + shaper := text.NewShaper(text.WithCollection(collection)) + build := func(p ui.Palette, code godemirror.Theme) *ui.Theme { + return &ui.Theme{Palette: p, Shaper: shaper, Sans: fonts.Sans, Mono: mono, Surface: p.Canvas, Code: code} + } + return build(ui.Dark, godemirror.DarkTheme), build(ui.Light, godemirror.LightTheme) +} + +// kit is the registry and the theme of a screen that names none. +type kit struct { + *gockup.Registry + fallback *ui.Theme +} + +func (k kit) add(name, doc string, draw func(c *gockup.Ctx, th *ui.Theme) D, props ...gockup.Prop) { + k.register(name, doc, false, draw, props) +} + +// holder is add for a component with children. +func (k kit) holder(name, doc string, draw func(c *gockup.Ctx, th *ui.Theme) D, props ...gockup.Prop) { + k.register(name, doc, true, draw, props) +} + +func (k kit) register(name, doc string, children bool, draw func(c *gockup.Ctx, th *ui.Theme) D, props []gockup.Prop) { + k.Register(name, gockup.Component{Doc: doc, Props: props, Children: children, + Layout: func(c *gockup.Ctx) D { return draw(c, k.theme(c)) }}) +} + +// surfaceKey is where a component that paints a ground leaves the theme its +// children composite on. +type surfaceKey struct{} + +func (k kit) theme(c *gockup.Ctx) *ui.Theme { + for _, key := range []any{surfaceKey{}, gockup.ThemeKey{}} { + if v, ok := c.Lookup(key); ok { + if th, ok := v.(*ui.Theme); ok { + return th + } + } + } + return k.fallback +} + +// children lays the node's children out on th's surface, gap Space units +// apart; a child with a weight shares the free room. +func children(c *gockup.Ctx, th *ui.Theme, axis layout.Axis, gap float32) layout.Widget { + c.Provide(surfaceKey{}, th) + return stacked(c.Children(), axis, gap) +} + +func stacked(nodes []gockup.Child, axis layout.Axis, gap float32) layout.Widget { + return func(gtx C) D { + flex := layout.Flex{Axis: axis} + if axis == layout.Horizontal { + flex.Alignment = layout.Middle + } + var kids []layout.FlexChild + for i, n := range nodes { + if i > 0 && gap > 0 { + kids = append(kids, layout.Rigid(layout.Spacer{Width: ui.Space(gap), Height: ui.Space(gap)}.Layout)) + } + w := n.Widget + if axis == layout.Horizontal { + w = func(gtx C) D { gtx.Constraints.Min.Y = 0; return n.Widget(gtx) } + } + if n.Weight > 0 { + kids = append(kids, layout.Flexed(n.Weight, w)) + continue + } + kids = append(kids, layout.Rigid(w)) + } + return flex.Layout(gtx, kids...) + } +} + +// opened lays w out in an hour-old frame nobody sees, so what it opens on its +// first frame is done animating in the mockup's. +func opened(gtx C, w layout.Widget) { + gtx.Ops, gtx.Source, gtx.Now = new(op.Ops), input.Source{}, gtx.Now.Add(-time.Hour) + w(gtx) +} + +// sourced is w taking input from src: a child of an overlay that itself takes +// none, so it neither closes nor holds gockup's keyboard. +func sourced(src input.Source, w layout.Widget) layout.Widget { + return func(gtx C) D { gtx.Source = src; return w(gtx) } +} + +func str(name, doc string) gockup.Prop { return gockup.Prop{Name: name, Type: gockup.String, Doc: doc} } + +func flag(name, doc string) gockup.Prop { + return gockup.Prop{Name: name, Type: gockup.Bool, Default: false, Doc: doc} +} + +func num(name string, def float64, doc string) gockup.Prop { + return gockup.Prop{Name: name, Type: gockup.Number, Default: def, Doc: doc} +} + +func enum(name, doc string, values ...string) gockup.Prop { + return gockup.Prop{Name: name, Type: gockup.Enum, Enum: values, Doc: doc} +} + +// tokens are the palette's colors by the name a mockup picks: a widget takes a +// token, never a hex. +var tokens = []struct { + name string + of func(*ui.Theme) color.NRGBA +}{ + {"foreground", func(th *ui.Theme) color.NRGBA { return th.Foreground }}, + {"muted", func(th *ui.Theme) color.NRGBA { return th.MutedForeground }}, + {"pass", func(th *ui.Theme) color.NRGBA { return th.TonePass }}, + {"wait", func(th *ui.Theme) color.NRGBA { return th.ToneWait }}, + {"destructive", func(th *ui.Theme) color.NRGBA { return th.Destructive }}, + {"merged", func(th *ui.Theme) color.NRGBA { return th.ToneMerged }}, + {"primary", func(th *ui.Theme) color.NRGBA { return th.Primary }}, + {"primary-foreground", func(th *ui.Theme) color.NRGBA { return th.PrimaryForeground }}, +} + +var grounds = []struct { + name string + of func(*ui.Theme) color.NRGBA +}{ + {"sidebar", func(th *ui.Theme) color.NRGBA { return th.Sidebar }}, + {"background", func(th *ui.Theme) color.NRGBA { return th.Background }}, + {"canvas", func(th *ui.Theme) color.NRGBA { return th.Canvas }}, + {"card", func(th *ui.Theme) color.NRGBA { return th.Card }}, + {"popover", func(th *ui.Theme) color.NRGBA { return th.Popover }}, + {"accent", func(th *ui.Theme) color.NRGBA { return th.Accent }}, + {"secondary", func(th *ui.Theme) color.NRGBA { return th.Secondary }}, + {"muted", func(th *ui.Theme) color.NRGBA { return th.Palette.Muted }}, + {"primary", func(th *ui.Theme) color.NRGBA { return th.Primary }}, +} + +func toneProp(def string) gockup.Prop { + p := enum("tone", "") + for _, t := range tokens { + p.Enum = append(p.Enum, t.name) + } + p.Default = def + return p +} + +func groundProp(def string) gockup.Prop { + p := enum("ground", "the token painted under the children") + for _, g := range grounds { + p.Enum = append(p.Enum, g.name) + } + p.Default = def + return p +} + +func tone(c *gockup.Ctx, th *ui.Theme) color.NRGBA { + name := c.Enum("tone") + for _, t := range tokens { + if t.name == name { + return t.of(th) + } + } + return th.Foreground +} + +func ground(c *gockup.Ctx, th *ui.Theme) color.NRGBA { + name := c.Enum("ground") + for _, g := range grounds { + if g.name == name { + return th.Over(g.of(th)) + } + } + return th.Surface +} + +const iconDoc = `a Lucide name ("git-branch"), "lobe:claude" (claude, codex, antigravity, opencode, pi, cursor, kiro), "brand:github" (git, github, gitlab) or "lich"` + +func iconProp(name string) gockup.Prop { + p := str(name, iconDoc) + p.Check = func(s string) string { + if s != "" && lookupIcon(s) == nil { + return "no icon " + s + "; " + iconDoc + } + return "" + } + return p +} + +// iconCache keeps one Icon per name: a fresh one each frame is uploaded to the +// GPU again each frame. Only names the sets have get in, so it is bounded. +var iconCache = map[string]*icons.Icon{} + +func lookupIcon(name string) *icons.Icon { + if ic := iconCache[name]; ic != nil { + return ic + } + var ic *icons.Icon + set, mark, _ := strings.Cut(name, ":") + switch { + case set == "lobe": + ic, _ = icons.LookupLobe(mark) + case name == "brand:git", name == "brand:github", name == "brand:gitlab": + ic = icons.Brand(mark) + case name == "lich": + ic = icons.LichMark() + default: + ic, _ = icons.LookupLucide(name) + } + if ic != nil { + iconCache[name] = ic + } + return ic +} + +// icon is the icon prop name holds, nil for none. +func icon(c *gockup.Ctx, name string) *icons.Icon { + if s := c.String(name); s != "" { + return lookupIcon(s) + } + return nil +} + +// iconOr is icon for a widget that cannot draw none. +func iconOr(c *gockup.Ctx, name string) *icons.Icon { + if ic := icon(c, name); ic != nil { + return ic + } + return &icons.Icon{} +} + +// list splits a "a, b, c" prop. +func list(s string) []string { + if strings.TrimSpace(s) == "" { + return nil + } + parts := strings.Split(s, ",") + for i := range parts { + parts[i] = strings.TrimSpace(parts[i]) + } + return parts +} + +// at is list's i-th item, "" past its end. +func at(items []string, i int) string { + if i < len(items) { + return items[i] + } + return "" +} diff --git a/native/gockup-lich/lich_test.go b/native/gockup-lich/lich_test.go new file mode 100644 index 000000000..8c3a52e5b --- /dev/null +++ b/native/gockup-lich/lich_test.go @@ -0,0 +1,51 @@ +package lich + +import ( + "image" + "slices" + "testing" + "time" + + "gioui.org/layout" + "gioui.org/op" + "gioui.org/unit" + + "github.com/lichdotdev/gockup" + "github.com/lichdotdev/gockup/dsl" +) + +// TestKitLaysOut draws testdata/kit.gockup with no GPU, and holds it to show +// every component the adapter registers. +func TestKitLaysOut(t *testing.T) { + builtins := gockup.NewRegistry().Names() + r := gockup.NewRegistry() + Register(r) + file, problems, err := r.Load("testdata/kit.gockup") + if err != nil || len(problems) > 0 { + t.Fatalf("load: %v %v", err, problems) + } + + used := map[string]bool{} + var walk func(n *dsl.Node) + walk = func(n *dsl.Node) { + used[n.Type] = true + for _, child := range n.Children { + walk(child) + } + } + session := gockup.NewSession(r) + for _, screen := range file.Screens { + walk(screen.Root) + gtx := layout.Context{Ops: new(op.Ops), Now: time.Now(), Metric: unit.Metric{PxPerDp: 1, PxPerSp: 1}, + Constraints: layout.Exact(image.Pt(int(screen.Width), int(screen.Height)))} + for range 2 { + gtx.Ops.Reset() + session.Layout(gtx, screen) + } + } + for _, name := range r.Names() { + if !used[name] && !slices.Contains(builtins, name) { + t.Errorf("testdata/kit.gockup does not show %s", name) + } + } +} diff --git a/native/gockup-lich/marks.go b/native/gockup-lich/marks.go new file mode 100644 index 000000000..a501d93f1 --- /dev/null +++ b/native/gockup-lich/marks.go @@ -0,0 +1,238 @@ +package lich + +import ( + "image/color" + + "image" + "strconv" + "strings" + + "gioui.org/font" + "gioui.org/layout" + "gioui.org/text" + "gioui.org/unit" + + "github.com/lichdotdev/gockup" + + "github.com/lichdotdev/lich/native/ui" +) + +// colorProp is a color no tone has: a lane of the graph, a label's own. +func colorProp() gockup.Prop { + return gockup.Prop{Name: "color", Type: gockup.Color, Default: color.NRGBA{}, Doc: "over tone, for a color no tone has"} +} + +func inColor(c *gockup.Ctx, th *ui.Theme) color.NRGBA { + if fg := c.Color("color"); fg.A > 0 { + return fg + } + return tone(c, th) +} + +func position(i int) string { return strconv.Itoa(i) } + +var ( + textSizes = map[string]unit.Sp{"xxs": ui.Text2XS, "xs": ui.TextXS, "sm": ui.TextSM, "base": ui.TextBase} + weights = map[string]font.Weight{"medium": font.Medium, "semibold": font.SemiBold, "bold": font.Bold} + aligns = map[string]text.Alignment{"center": text.Middle, "end": text.End} +) + +func (k kit) text() { + k.add("Text", "the kit's text: sm is body, xs meta and paths, xxs a group's label", func(c *gockup.Ctx, th *ui.Theme) D { + size := textSizes[c.Enum("size")] + if sp := c.Number("sp"); sp > 0 { + size = unit.Sp(sp) + } + l := th.Text(size, c.String("text")).In(inColor(c, th)).Weight(weights[c.Enum("font")]) + l.MaxLines, l.Align = int(c.Number("lines")), aligns[c.Enum("align")] + if c.Bool("mono") { + l = l.Mono() + } + if c.Bool("italic") { + l = l.Italic() + } + if c.Bool("shimmer") { + l = l.Shimmer() + } + if c.Bool("rainbow") { + l = l.Rainbow() + } + return l.Layout(c.Gtx) + }, str("text", ""), enum("size", "", "sm", "xxs", "xs", "base"), num("sp", 0, "a size off the scale, for a screen's title"), + enum("font", "the weight", "normal", "medium", "semibold", "bold"), toneProp("foreground"), + num("lines", 1, "cut with … past them; 0 wraps"), enum("align", "in the width it is made to fill", "start", "center", "end"), + flag("mono", "code, paths, counts"), flag("italic", ""), flag("shimmer", "work in progress"), flag("rainbow", "the mode that asks for nothing"), colorProp()) + k.add("Label", "a form field's label", func(c *gockup.Ctx, th *ui.Theme) D { + l := th.Label(c.String("text")) + l.Disabled = c.Bool("disabled") + return l.Layout(c.Gtx) + }, str("text", ""), flag("disabled", "")) + k.add("SectionHeading", "a group's muted uppercase label", func(c *gockup.Ctx, th *ui.Theme) D { + h := th.SectionHeading(c.String("text")) + h.Size = textSizes[c.Enum("size")] + return h.Layout(c.Gtx) + }, str("text", ""), enum("size", "", "xxs", "xs")) + k.add("MarkedText", "a line of code with a search's hits marked", func(c *gockup.Ctx, th *ui.Theme) D { + return th.MarkedText(marked(c.String("text"), c.String("mark"))).Layout(c.Gtx) + }, str("text", ""), str("mark", "the text searched for")) + + notes := []struct { + name, doc string + draw func(th *ui.Theme, s string) layout.Widget + }{ + {"Notice", "the quiet line a panel shows instead of content", func(th *ui.Theme, s string) layout.Widget { return th.Notice(s).Layout }}, + {"Footnote", "what a list owes the reader about itself, under it", func(th *ui.Theme, s string) layout.Widget { return th.Footnote(s).Layout }}, + {"PanelNotice", "a full-size screen's notice", func(th *ui.Theme, s string) layout.Widget { return th.PanelNotice(s, ui.Space(3.5)).Layout }}, + {"BodyLine", "what a file's body without a diff says", func(th *ui.Theme, s string) layout.Widget { return th.BodyLine(s).Layout }}, + {"SpinnerLine", "what a dialog shows while it waits", func(th *ui.Theme, s string) layout.Widget { + return func(gtx C) D { return th.SpinnerLine(gtx, s) } + }}, + } + for _, n := range notes { + k.add(n.name, n.doc, func(c *gockup.Ctx, th *ui.Theme) D { return n.draw(th, c.String("text"))(c.Gtx) }, str("text", "")) + } + + k.holder("EmptyState", "a screen or panel with nothing to show, filling the space given; children are its actions, side by side", func(c *gockup.Ctx, th *ui.Theme) D { + var actions layout.Widget + if len(c.Children()) > 0 { + actions = children(c, th, layout.Horizontal, 2) + } + if c.Bool("inline") { + return th.InlineEmptyState(c.String("title"), c.String("description"), actions).Layout(c.Gtx) + } + gtx := c.Gtx + gtx.Constraints.Min = gtx.Constraints.Max + return th.EmptyState(icon(c, "icon"), c.String("title"), c.String("description"), actions).Layout(gtx) + }, str("title", ""), str("description", ""), iconProp("icon"), flag("inline", "compact, at the top of a card, no icon")) +} + +func marked(s, mark string) []ui.MarkedSegment { + if mark == "" { + return []ui.MarkedSegment{{Text: s}} + } + var segs []ui.MarkedSegment + for i, part := range strings.Split(s, mark) { + if i > 0 { + segs = append(segs, ui.MarkedSegment{Text: mark, Marked: true}) + } + if part != "" { + segs = append(segs, ui.MarkedSegment{Text: part}) + } + } + return segs +} + +func (k kit) marks() { + k.add("Icon", "", func(c *gockup.Ctx, th *ui.Theme) D { + ic := iconOr(c, "name") + if c.Bool("rainbow") { + return th.RainbowIcon(c.Gtx, ic, c.Dp("size")) + } + if c.Bool("filled") { + return ic.LayoutFilled(c.Gtx, c.Dp("size"), inColor(c, th)) + } + return ic.Layout(c.Gtx, c.Dp("size"), inColor(c, th)) + }, iconProp("name"), num("size", 16, "dp"), toneProp("foreground"), flag("filled", "its outlines filled"), flag("rainbow", "the mode that asks for nothing"), colorProp()) + k.add("FileIcon", "a file's icon, by its name", func(c *gockup.Ctx, th *ui.Theme) D { + return th.FileIcon(c.String("path"), c.Dp("size")).Layout(c.Gtx) + }, str("path", ""), num("size", 14, "dp")) + k.add("Avatar", "", func(c *gockup.Ctx, th *ui.Theme) D { + return th.Avatar(nil, c.String("initial"), c.Dp("size")).Layout(c.Gtx) + }, str("initial", ""), num("size", 24, "dp")) + k.add("StatusDot", "a state's dot; beating is something live", func(c *gockup.Ctx, th *ui.Theme) D { + fg := tone(c, th) + switch c.Enum("kind") { + case "hollow": + return ui.HollowDot(c.Gtx, fg) + case "beating": + return th.BeatingDot(c.Gtx, fg) + case "mark": + return ui.MarkDot(c.Gtx, fg) + case "swatch": + return ui.Swatch(c.Gtx, fg) + } + return ui.StatusDot(c.Gtx, fg) + }, toneProp("pass"), enum("kind", "mark sits beside a title, swatch is a color's square", "solid", "hollow", "beating", "mark", "swatch")) + k.add("MetaGlyph", "a small glyph and its words, in one tone: a branch, a state", func(c *gockup.Ctx, th *ui.Theme) D { + if c.Bool("mono") { + return th.MonoGlyph(iconOr(c, "icon"), c.String("text"), tone(c, th)).Layout(c.Gtx) + } + return th.MetaGlyph(iconOr(c, "icon"), c.String("text"), tone(c, th)).Layout(c.Gtx) + }, iconProp("icon"), str("text", ""), toneProp("muted"), flag("mono", "")) + k.add("Stat", "a state said in words that may wrap, after its glyph", func(c *gockup.Ctx, th *ui.Theme) D { + fg := tone(c, th) + glyph := func(gtx C) D { return iconOr(c, "icon").Layout(gtx, ui.Space(3.5), fg) } + if c.Bool("busy") { + glyph = func(gtx C) D { return th.BusyRing(gtx, ui.Space(3.5), fg, th.RingTrack(fg)) } + } + return th.Stat(c.Gtx, glyph, c.String("text"), fg) + }, iconProp("icon"), str("text", ""), toneProp("muted"), flag("busy", "a ring for the glyph")) + k.add("DiffStat", "+added -deleted", func(c *gockup.Ctx, th *ui.Theme) D { + return th.DiffStat(int(c.Number("added")), int(c.Number("deleted"))).Layout(c.Gtx) + }, num("added", 0, ""), num("deleted", 0, "")) + k.add("KeyCap", "a shortcut's caps, with the word beside them when set", func(c *gockup.Ctx, th *ui.Theme) D { + if s := c.String("label"); s != "" { + return th.KeyHint(c.String("keys"), s).Layout(c.Gtx) + } + return th.KeyCap(c.String("keys")).Layout(c.Gtx) + }, str("keys", `"Ctrl+K"`), str("label", "")) + k.add("Tag", "", func(c *gockup.Ctx, th *ui.Theme) D { + return th.Tag(c.String("text"), tone(c, th)).Layout(c.Gtx) + }, str("text", ""), toneProp("muted")) + k.add("TinyChip", "a state's word on a wash of its tone", func(c *gockup.Ctx, th *ui.Theme) D { + return th.TinyChip(c.String("text"), tone(c, th)).Layout(c.Gtx) + }, str("text", ""), toneProp("wait")) + k.add("OutlineBadge", "", func(c *gockup.Ctx, th *ui.Theme) D { + return th.OutlineBadge(c.String("text")).Layout(c.Gtx) + }, str("text", "")) + k.add("AbbrBadge", "a file's language in a tinted square", func(c *gockup.Ctx, th *ui.Theme) D { + fg := c.Color("color") + if fg.A == 0 { + return th.AbbrBadge(c.String("text"), th.Over(th.Muted), th.MutedForeground).Layout(c.Gtx) + } + return th.AbbrBadge(c.String("text"), th.Over(ui.Alpha(fg, 0.15)), fg).Layout(c.Gtx) + }, str("text", `"GO"`), gockup.Prop{Name: "color", Type: gockup.Color, Default: color.NRGBA{}, Doc: "left out, muted"}) + k.add("CountBadge", "what waits on the person, counted", func(c *gockup.Ctx, th *ui.Theme) D { + return th.CountBadge(c.Gtx, int(c.Number("count"))) + }, num("count", 0, "0 draws nothing")) +} + +func (k kit) gauges() { + percent := num("percent", 0, "0 to 100") + k.add("Progress", "", func(c *gockup.Ctx, th *ui.Theme) D { + return th.Progress(int(c.Number("percent"))).Layout(c.Gtx) + }, percent) + k.add("GaugeBar", "a share of a limit; in segments when they are set", func(c *gockup.Ctx, th *ui.Theme) D { + if n := int(c.Number("segments")); n > 0 { + return th.SegmentedBar(c.Gtx, int(c.Number("percent")), n, tone(c, th)) + } + return th.GaugeBar(c.Gtx, int(c.Number("percent")), tone(c, th)) + }, percent, toneProp("pass"), num("segments", 0, "")) + k.add("WaveBar", "a segmented bar for work with no share to tell", func(c *gockup.Ctx, th *ui.Theme) D { + return th.WaveBar(c.Gtx, max(int(c.Number("segments")), 1), tone(c, th)) + }, toneProp("wait"), num("segments", 20, "")) + k.add("DonutGauge", "", func(c *gockup.Ctx, th *ui.Theme) D { + return th.DonutGauge(c.Gtx, c.Dp("size"), int(c.Number("percent")), tone(c, th)) + }, percent, toneProp("pass"), num("size", 16, "dp")) + k.add("GaugeRow", "a labelled gauge, its reading and what is left; rows stack in a grid", func(c *gockup.Ctx, th *ui.Theme) D { + fg := tone(c, th) + return ui.GaugeRowStyle{ + LabelW: ui.Space(20), ReadingW: ui.Space(10), LeftW: ui.Space(16), + Label: th.Text(ui.TextXS, c.String("label")).Muted().Layout, + Bar: func(gtx C) D { return th.GaugeBar(gtx, int(c.Number("percent")), fg) }, + Reading: th.Text(ui.TextXS, position(int(c.Number("percent")))+"%").In(fg).Layout, + Left: th.Text(ui.TextXS, c.String("left")).Muted().Layout, + }.Layout(c.Gtx) + }, str("label", ""), percent, str("left", `"4h 10m"`), toneProp("pass")) + k.add("Spinner", "", func(c *gockup.Ctx, th *ui.Theme) D { + if fg := tone(c, th); c.Bool("ring") { + return th.BusyRing(c.Gtx, c.Dp("size"), fg, th.RingTrack(fg)) + } + return th.Spinner(c.Gtx, c.Dp("size"), tone(c, th)) + }, num("size", 16, "dp"), toneProp("muted"), flag("ring", "a turn's busy ring")) + k.add("Skeleton", "a block standing for content that loads", func(c *gockup.Ctx, th *ui.Theme) D { + gtx := c.Gtx + gtx.Constraints.Min = image.Point{} + return th.Skeleton(c.Dp("width"), c.Dp("height")).Layout(gtx) + }, num("width", 0, "dp; left out, the whole row"), num("height", 14, "dp")) +} diff --git a/native/gockup-lich/surfaces.go b/native/gockup-lich/surfaces.go new file mode 100644 index 000000000..df8fcea1e --- /dev/null +++ b/native/gockup-lich/surfaces.go @@ -0,0 +1,297 @@ +package lich + +import ( + "image" + "time" + + "gioui.org/f32" + "gioui.org/io/input" + "gioui.org/io/pointer" + "gioui.org/layout" + "gioui.org/op" + "gioui.org/op/clip" + "gioui.org/op/paint" + "gioui.org/unit" + "gioui.org/widget" + + "github.com/lichdotdev/gockup" + + "github.com/lichdotdev/lich/native/ui" +) + +func (k kit) surfaces() { + k.holder("Island", "a region of the window (sidebar, stage, dock): a rounded surface with a hairline, filling the space given; islands float on the canvas 6dp apart; children stack in a column", + func(c *gockup.Ctx, th *ui.Theme) D { + bg := ground(c, th) + gtx := c.Gtx + size := gtx.Constraints.Max + gtx.Constraints = layout.Exact(size) + ui.Fill(gtx, bg, ui.RadiusLG) + body := children(c, th.On(bg), layout.Vertical, 0) + area := clip.UniformRRect(image.Rectangle{Max: size}, gtx.Dp(ui.RadiusLG)).Push(gtx.Ops) + layout.UniformInset(c.Dp("padding")).Layout(gtx, body) + area.Pop() + ui.IslandBorder(gtx, size, gtx.Dp(ui.RadiusLG), th.On(bg).Over(th.Border)) + return D{Size: size} + }, groundProp("sidebar"), num("padding", 0, "dp")) + + k.holder("Surface", "a rounded fill under its children, which draw against it; use it, not Box, under kit components; children stack in a column", + func(c *gockup.Ctx, th *ui.Theme) D { + bg, radius := ground(c, th), c.Dp("radius") + body := children(c, th.On(bg), layout.Vertical, 0) + return layout.Background{}.Layout(c.Gtx, + func(gtx C) D { + if e, ok := elevations[c.Enum("elevation")]; ok { + e.Paint(gtx, th, gtx.Constraints.Min, gtx.Dp(radius)) + } + size, r := gtx.Constraints.Min, gtx.Dp(radius) + box := clip.UniformRRect(image.Rectangle{Max: size}, r) + if c.Bool("tail") { + box.SE = gtx.Dp(ui.RadiusSM) + } + paint.FillShape(gtx.Ops, bg, box.Op(gtx.Ops)) + if c.Bool("border") { + ui.InsetBorder(gtx, size, r, th.On(bg).Over(th.Border)) + } + if c.Bool("dashed") { + dashed(gtx, size, radius, ui.Alpha(th.Foreground, dashedOpacity)) + } + return D{Size: size} + }, + func(gtx C) D { + return layout.Inset{Left: c.Dp("padX"), Right: c.Dp("padX"), Top: c.Dp("padY"), Bottom: c.Dp("padY")}.Layout(gtx, body) + }) + }, groundProp("card"), num("radius", float64(ui.RadiusMD), "dp: 4, 6, 8 or 12"), num("padX", 0, "dp"), num("padY", 0, "dp"), + enum("elevation", "the shadow of what floats", "none", "low", "mid", "high"), + flag("border", "the 1px hairline of an active project tab"), flag("tail", "a tighter bottom right corner, the person's message"), flag("dashed", "the place a dragged file will take")) + + k.holder("Tray", "the sidebar's folder tray: children on a rounded accent wash, clipped to it", + func(c *gockup.Ctx, th *ui.Theme) D { + bg := th.Over(ui.Alpha(th.Accent, trayFill)) + return ui.Tray(c.Gtx, bg, children(c, th.On(bg), layout.Vertical, 0)) + }) + + k.holder("ScrollArea", "a vertical list of its children with the kit's scrollbar; fills the space given", + func(c *gockup.Ctx, th *ui.Theme) D { + state := gockup.State(c, func(l *widget.List) { l.Axis = layout.Vertical }) + area := th.ScrollArea(state) + if c.Bool("thin") { + area = th.ThinScrollArea(state) + } + nodes := c.Children() + gtx := c.Gtx + gtx.Constraints.Min = gtx.Constraints.Max + return area.Layout(gtx, len(nodes), func(gtx C, i int) D { return nodes[i].Widget(gtx) }) + }, flag("thin", "the bar laid over the rows")) + + k.holder("Wrap", "children left to right, onto a new line when the width runs out", + func(c *gockup.Ctx, _ *ui.Theme) D { + var kids []ui.WrapChild + for _, n := range c.Children() { + kids = append(kids, ui.WrapItem(n.Widget)) + } + justify := map[string]ui.Justify{"end": ui.JustifyEnd, "between": ui.JustifyBetween}[c.Enum("justify")] + return ui.Wrap{GapX: c.Dp("gapX"), GapY: c.Dp("gapY"), Justify: justify}.Layout(c.Gtx, kids...) + }, num("gapX", 8, "dp"), num("gapY", 8, "dp"), enum("justify", "", "start", "end", "between")) + + k.add("Separator", "a 1px rule; the window sets parts apart by space, so prefer a Spacer", + func(c *gockup.Ctx, th *ui.Theme) D { + if c.Bool("vertical") { + return th.VerticalRule(c.Gtx) + } + return th.Separator(c.Gtx) + }, flag("vertical", "as high as the space given: hold it in a Sized")) + k.add("OrRule", "a word between two rules, splitting a form's two ways of doing one thing", + func(c *gockup.Ctx, th *ui.Theme) D { return th.OrRule(c.Gtx, c.String("text")) }, str("text", "")) + k.add("DropHint", "covers what a file drag is over; fills the space given", + func(c *gockup.Ctx, th *ui.Theme) D { + gtx := c.Gtx + gtx.Constraints.Min = gtx.Constraints.Max + // its veil paints to the clip, which in the window is the pane's + defer clip.Rect{Max: gtx.Constraints.Max}.Push(gtx.Ops).Pop() + return th.DropHint(iconOr(c, "icon"), c.String("before"), c.String("label"), c.String("after"), c.String("detail")).Layout(gtx) + }, iconProp("icon"), str("before", ""), str("label", "the part in its own weight"), str("after", ""), str("detail", "")) +} + +// trayFill is the opacity of the accent under a folder at rest. +const trayFill = 0.4 + +var elevations = map[string]ui.Elevation{"low": ui.ElevationLow, "mid": ui.ElevationMid, "high": ui.ElevationHigh} + +func (k kit) overlays() { + k.holder("Popover", "an open menu or panel: the menus' surface around MenuHeader, MenuItem, MenuSeparator or rows of your own; place it with Overlay and Inset", + func(c *gockup.Ctx, th *ui.Theme) D { + gtx := c.Gtx + gtx.Constraints.Max.X = min(gtx.Constraints.Max.X, gtx.Dp(c.Dp("width"))) + return th.PopoverPanel(gtx, func(gtx C, th *ui.Theme) D { return children(c, th, layout.Vertical, 0)(gtx) }) + }, num("width", 224, "dp")) + + k.add("MenuItem", "a menu's row", + func(c *gockup.Ctx, th *ui.Theme) D { return menuItem(c, th) }, + str("text", ""), iconProp("icon"), str("shortcut", "at the row's end"), + flag("checked", "a check at the row's end"), flag("submenu", "a chevron at the row's end"), + flag("lit", "drawn hovered"), flag("destructive", ""), flag("disabled", "")) + k.add("MenuHeader", "a panel's title, its count at the end", + func(c *gockup.Ctx, th *ui.Theme) D { return th.MenuHeader(c.Gtx, c.String("text"), c.String("count")) }, + str("text", ""), str("count", "")) + k.add("MenuSeparator", "the space between a menu's groups", + func(c *gockup.Ctx, th *ui.Theme) D { return th.MenuSeparator(c.Gtx) }) + + k.holder("Dialog", "an open dialog over its scrim: put it last in an Overlay whose base is the screen; children are the body, a DialogActions last", + dialog, str("title", ""), str("description", ""), num("width", float64(ui.DialogWidthMD), "dp: 448 or 576"), + flag("hideClose", "no × in the corner"), flag("raw", "children are the whole panel, no padding, title or ×"), + num("top", 0, "raw only: the share of the height above the panel, 0 to center it"), num("scrim", 0, "the backdrop's opacity, 0 for the kit's")) + k.holder("DialogActions", "a dialog's footer: buttons on one line at the right, the one that resolves it last", + func(c *gockup.Ctx, th *ui.Theme) D { + return layout.E.Layout(spanning(c.Gtx), children(c, th, layout.Horizontal, 2)) + }) + + k.add("Toast", "a toast in its corner of the window: put it last in an Overlay whose base is the screen", + toast, enum("variant", "", "default", "success", "info", "warning", "error"), + str("title", ""), str("description", ""), str("action", "the filled button"), str("cancel", "the quiet button"), str("secondary", "a second quiet button, between cancel and action"), + iconProp("icon"), flag("spin", "a spinner for the icon"), num("progress", -1, "0 to 100; left out, no bar"), + enum("position", "", "top-right", "top-left", "bottom-right", "bottom-left")) + + k.holder("Tooltip", "its children with the tooltip they open on hover, drawn open", + tooltip, str("text", ""), enum("side", "", "top", "bottom", "left", "right")) +} + +func spanning(gtx C) C { gtx.Constraints.Min.X = gtx.Constraints.Max.X; return gtx } + +func menuItem(c *gockup.Ctx, th *ui.Theme) D { + fg := th.PopoverForeground + if c.Bool("destructive") { + fg = th.Destructive + } + gtx := c.Gtx + if c.Bool("disabled") { + fg = th.Over(ui.Alpha(fg, disabledOpacity)) + gtx = gtx.Disabled() + } + row := th.PopoverRow(gockup.State[ui.Clickable](c, nil), func(gtx C, th *ui.Theme) D { + var kids []layout.FlexChild + if ic := icon(c, "icon"); ic != nil { + kids = append(kids, layout.Rigid(func(gtx C) D { return ic.Layout(gtx, ui.Space(4), fg) }), ui.Gap(2)) + } + kids = append(kids, layout.Flexed(1, th.Text(ui.TextSM, c.String("text")).In(fg).Layout)) + if s := c.String("shortcut"); s != "" { + kids = append(kids, ui.Gap(2), layout.Rigid(th.Text(ui.TextXS, s).Muted().Layout)) + } + for _, mark := range [...]struct{ prop, icon string }{{"checked", "check"}, {"submenu", "chevron-right"}} { + if c.Bool(mark.prop) { + kids = append(kids, ui.Gap(2), layout.Rigid(func(gtx C) D { return lookupIcon(mark.icon).Layout(gtx, ui.Space(4), fg) })) + } + } + return layout.Flex{Alignment: layout.Middle}.Layout(gtx, kids...) + }) + row.Lit = c.Bool("lit") + return row.Layout(gtx) +} + +// disabledOpacity is the kit's, which it keeps to itself. +const disabledOpacity = 0.5 + +// dialogGap is the kit's gap between a dialog's parts, gap-6. +const dialogGap = 6 + +func dialog(c *gockup.Ctx, th *ui.Theme) D { + build := func(s *ui.DialogState) ui.DialogStyle { + d := th.Dialog(s, true, c.String("title"), c.String("description")) + d.MaxWidth, d.HideClose, d.Scrim = c.Dp("width"), c.Bool("hideClose"), float64(c.Number("scrim")) + return d + } + state := gockup.State(c, func(s *ui.DialogState) { + opened(c.Gtx, func(gtx C) D { build(s).Layout(gtx); return D{} }) + }) + d := build(state) + on, src := th.On(th.Popover), c.Gtx.Source + if c.Bool("raw") { + d.Raw, d.Top = sourced(src, children(c, on, layout.Vertical, 0)), c.Number("top") + } else if len(c.Children()) > 0 { + d.Body = sourced(src, children(c, on, layout.Vertical, dialogGap)) + } + gtx := c.Gtx + gtx.Source = input.Source{} + d.Layout(gtx) + return D{Size: gtx.Constraints.Max} +} + +func toast(c *gockup.Ctx, th *ui.Theme) D { + position := map[string]ui.ToastPosition{"top-left": ui.ToastTopLeft, "bottom-right": ui.ToastBottomRight, "bottom-left": ui.ToastBottomLeft}[c.Enum("position")] + draw := func(s *ui.Toaster, gtx C) { + t := th.Toaster(s, th.Palette.Background == ui.Light.Background) + t.Position = position + t.Layout(gtx) + } + state := gockup.State(c, func(s *ui.Toaster) { + s.Show(toastOf(c)) + opened(c.Gtx, func(gtx C) D { draw(s, gtx); return D{} }) + }) + gtx := c.Gtx + gtx.Source = input.Source{} + draw(state, gtx) + return D{Size: gtx.Constraints.Max} +} + +func toastOf(c *gockup.Ctx) ui.Toast { + variant := map[string]ui.ToastVariant{"success": ui.ToastSuccess, "info": ui.ToastInfo, "warning": ui.ToastWarning, "error": ui.ToastError}[c.Enum("variant")] + t := ui.Toast{Variant: variant, Title: c.String("title"), Description: c.String("description"), Icon: icon(c, "icon"), Spin: c.Bool("spin"), Duration: ui.ToastForever} + if s := c.String("action"); s != "" { + t.Action = &ui.ToastButton{Label: s} + } + if s := c.String("cancel"); s != "" { + t.Cancel = &ui.ToastButton{Label: s} + } + if s := c.String("secondary"); s != "" { + t.Secondary = &ui.ToastButton{Label: s} + } + if p := c.Number("progress"); p >= 0 { + t.Progress = &ui.ToastProgress{Percent: int(p)} + } + return t +} + +// tooltipRoom is a window so large that the bubble never flips or shifts: the +// mockup names its side. +const tooltipRoom = 1 << 14 + +type openTip struct { + root ui.Root + tip ui.Tooltip +} + +// tooltip opens the real tooltip, which only a pointer does: the first frame +// hovers it in a router of its own, an hour ago. +func tooltip(c *gockup.Ctx, th *ui.Theme) D { + trigger := children(c, th, layout.Horizontal, 0) + side := map[string]ui.Side{"bottom": ui.SideBottom, "left": ui.SideLeft, "right": ui.SideRight}[c.Enum("side")] + draw := func(s *openTip, gtx C, trigger layout.Widget) D { + t := th.Tooltip(&s.root, &s.tip, c.String("text")) + t.Side = side + return t.Layout(gtx, trigger) + } + state := gockup.State(c, func(s *openTip) { s.hover(c.Gtx, draw) }) + gtx := c.Gtx + src := gtx.Source + gtx.Source = input.Source{} + return draw(state, gtx, sourced(src, trigger)) +} + +func (s *openTip) hover(gtx C, draw func(*openTip, C, layout.Widget) D) { + var router input.Router + gtx.Ops, gtx.Source, gtx.Now = new(op.Ops), router.Source(), gtx.Now.Add(-time.Hour) + gtx.Constraints = layout.Constraints{Max: image.Pt(tooltipRoom, tooltipRoom)} + frame := func() { + gtx.Ops.Reset() + s.root.Layout(gtx, func(gtx C) D { + defer op.Offset(image.Pt(tooltipRoom/2, tooltipRoom/2)).Push(gtx.Ops).Pop() + return draw(s, gtx, func(gtx C) D { return layout.Spacer{Width: unit.Dp(10), Height: unit.Dp(10)}.Layout(gtx) }) + }) + router.Frame(gtx.Ops) + } + frame() + router.Queue(pointer.Event{Kind: pointer.Move, Source: pointer.Mouse, Position: f32.Pt(tooltipRoom/2+1, tooltipRoom/2+1)}) + frame() + gtx.Now = gtx.Now.Add(time.Minute) // past the tooltip's open delay + frame() +} diff --git a/native/gockup-lich/testdata/kit.gockup b/native/gockup-lich/testdata/kit.gockup new file mode 100644 index 000000000..29e0f42e2 --- /dev/null +++ b/native/gockup-lich/testdata/kit.gockup @@ -0,0 +1,173 @@ +// Every component of the kit, in both themes: what the adapter's test lays out. + +fragment Group title="" { + Surface ground=card radius=8 padX=16 padY=16 { + Flex spacing=12 align=stretch { SectionHeading text=$title Slot } + } +} + +fragment Line { Flex axis=horizontal spacing=12 align=center { Slot } } + +fragment Controls { + Flex axis=horizontal spacing=12 align=start { + Flex weight=1 spacing=12 align=stretch { + Group title="Buttons" { + Line { Button text="Run" icon="play" Button text="Run" variant=outline Button text="Run" variant=secondary Button text="Run" variant=ghost Button text="Delete" variant=destructive } + Line { Button text="Reply" size=xs keys="Ctrl+Enter" Button text="Reject" size=xs variant=outline keys="3" alsoKeys="Esc" Button text="Pushing" size=sm busy=true Button text="Off" size=sm disabled=true Button icon="x" size=icon-xs variant=ghost lit=true } + Line { IconAction icon="columns-2" IconAction icon="columns-2" on=true CornerButton icon="pin" on=true BarIconButton icon="settings" BarIconButton icon="bell" on=true notify=true BarIconButton icon="plus" disabled=true CardIconButton icon="refresh-cw" CardIconButton icon="refresh-cw" disabled=true } + Line { GhostTextButton text="Remove" tone=destructive Link text="Pull requests" Link text="Docs" icon="external-link" Chip text="Waiting" count=3 on=true Chip text="Done" count=12 Chip text="Failed" } + } + Group title="Fields" { + Label text="Branch" + Input placeholder="Branch name" icon="git-branch" + Input text="not a branch" invalid=true + Input placeholder="Filter…" height=28 small=true keys="Ctrl+F" + Input text="locked" disabled=true + InputGroup prefix="https://" placeholder="Repository URL" button="Copy" + InputGroup menu="Open" icon="search" placeholder="Search" suffix="12 hits" + Textarea placeholder="Describe what the agent should do…" + Select value="origin/main" icon="git-branch" + } + } + Flex weight=1 spacing=12 align=stretch { + Group title="Choices" { + Line { Checkbox Checkbox checked=true Checkbox checked=true disabled=true Switch Switch on=true Switch on=true size=sm Switch disabled=true } + Checkbox checked=true label="Carry my changes" note="Uncommitted work moves to the new checkout." + Line { Toggle text="Bold" icon="bold" on=true Toggle text="Wrap" variant=outline ToggleGroup icons="list, layout-grid" selected=1 ToggleGroup items="List, Grid, Board" variant=outline size=sm } + Tabs items="Overview, Activity" icons="layout-grid, activity" + Tabs items="Files, Diff, Artifacts" variant=ghost selected=1 + Sized height=64 { Tabs items="One, Two" counts="4, !1" variant=line selected=1 { Text text="first" color="#62a6f6" Text text="The second panel" tone=muted } } + } + Group title="Rows" { + NavRow on=true { Icon name="lobe:claude" Text text="Refactor the billing module" weight=1 StatusDot tone=wait } + NavRow { Icon name="lobe:codex" tone=muted Text text="Fix the flaky relay test" weight=1 DiffStat added=12 deleted=3 } + PanelRow { StatusDot tone=pass kind=beating Text text="dev server" weight=1 Text text=":5173" size=xs tone=muted mono=true } + TwoLines top="feature/toolbar" under="~/code/lich/native" + Tray { NavRow { Icon name="folder" tone=muted Text text="Review" weight=1 CountBadge count=2 } } + Separator + OrRule text="or" + } + Group title="App" { + Line { ProviderMark ProviderMark provider=codex ProviderMark provider=antigravity ProviderMark provider=opencode ProviderMark provider=omp ProviderMark provider=crush ProviderMark provider=cursor ProviderMark provider=kiro SessionStatus state=working SessionStatus state=waiting SessionStatus state=done SessionStatus state=pending SessionStatus ToggleGroup items="GUI, TUI" icons="sparkles, square-terminal" dense=true } + Surface ground=accent radius=12 padX=16 padY=12 tail=true { Text text="A message" } + Surface ground=sidebar radius=8 padX=14 padY=6 border=true { Text text="A tab" } + Composer placeholder="Ask Claude Code about this project" { Button icon="plus" variant=ghost size=icon-xs Spacer weight=1 Button icon="arrow-up" size=icon-sm } + Composer text="!git status" shell=true head=1 { Inset left=16 top=14 { Surface ground=accent dashed=true padX=12 padY=12 { Text text="notes.md" size=xs } } Text text="Shell mode" size=xs tone=wait Spacer weight=1 Button keys="↵" size=icon-sm } + Composer drag=true { Button icon="plus" variant=ghost size=icon-xs } + } + } + } +} + +fragment Marks { + Flex axis=horizontal spacing=12 align=start { + Flex weight=1 spacing=12 align=stretch { + Group title="Text" { + Text text="Screen title" sp=20 font=semibold + Text text="Body text, medium" font=medium + Text text="git rebase --onto main" mono=true size=xs tone=muted + Line { Text text="Thinking…" shimmer=true Icon name="zap" rainbow=true Text text="YOLO" rainbow=true } + Text text="Wraps onto as many lines as it needs when lines is zero, as a description under a title in Settings does." size=xs tone=muted lines=0 + MarkedText text="err := resolve(err)" mark="err" + Notice text="Nothing here yet." + Footnote text="Showing the 2 branches changed in the last 30 days." + PanelNotice text="Could not read the pull request." + BodyLine text="Only whitespace changed in this file." + SpinnerLine text="Waiting for the browser…" + } + Group title="Empty" { + Sized height=200 { EmptyState icon="folder-open" title="No project open" description="Open a folder to get started." { Button text="Open project" } } + EmptyState inline=true title="No preview" description="It is a binary format." { Button text="Open" size=sm variant=outline } + } + } + Flex weight=1 spacing=12 align=stretch { + Group title="Marks" { + Line { StatusDot tone=pass StatusDot tone=wait StatusDot tone=destructive StatusDot tone=muted kind=hollow StatusDot kind=mark tone=wait StatusDot kind=swatch tone=merged Avatar initial="M" FileIcon path="main.go" FileIcon path="README.md" Icon name="lich" Icon name="brand:github" Icon name="star" filled=true tone=wait Sized height=16 { Separator vertical=true } Icon name="git-merge" tone=merged } + Line { MetaGlyph icon="git-branch" text="main" mono=true MetaGlyph icon="check" text="Resolved" tone=pass DiffStat added=41 Tag text="lich" TinyChip text="waiting" AbbrBadge text="GO" color="#00d3f3" AbbrBadge text="PNG" OutlineBadge text="Draft" CountBadge count=12 } + Line { Stat icon="circle-check" text="Checks passing" tone=pass Stat busy=true text="Checks running" tone=wait KeyCap keys="Ctrl+Shift+P" KeyCap keys="Esc" label="Close" } + } + Group title="Gauges" { + Progress percent=30 + GaugeBar percent=70 tone=wait + GaugeBar percent=40 segments=20 + WaveBar + GaugeRow label="Session" percent=12 left="4h 10m" + GaugeRow label="Week" percent=78 left="2d" tone=wait + Line { DonutGauge percent=60 DonutGauge percent=25 size=32 tone=wait Spinner Spinner ring=true tone=wait Skeleton width=160 } + } + Group title="Wrap" { + Sized width=220 { Wrap { Tag text="one" Tag text="two" Tag text="three" Tag text="four" Tag text="five" Tag text="six" } } + Sized height=60 { ScrollArea thin=true { Text text="row" Text text="row" Text text="row" Text text="row" Text text="row" } } + Sized height=130 { DropHint icon="paperclip" before="Attach to " label="feature/toolbar" detail="Pasted at the prompt" } + } + } + } +} + +fragment Viewers { + Flex axis=horizontal spacing=6 align=stretch { + Sized width=240 { + Island { Inset all=8 { + FileTree paths="native/ui/tabs.go, native/ui/pill.go, native/ui/merge.go, README.md, NOTES.md" selected="native/ui/tabs.go" added="native/ui/pill.go" modified="native/ui/tabs.go" conflict="native/ui/merge.go" untracked="NOTES.md" + } } + } + Island weight=1 ground=background padding=12 { + Markdown text="## Markdown\n\nWith **bold**, `code` and a [link](https://example.com).\n\n- [x] done\n- [ ] to do\n\n> [!WARNING]\n> The migration drops `legacy_id`.\n\n```go\nfunc Greet(name string) string {\n\treturn \"hello, \" + name\n}\n```" + } + Flex weight=1 spacing=6 align=stretch { + Island weight=1 ground=background padding=12 { + Code weight=1 file="demo.go" text="// Package demo greets.\npackage demo\n\nimport \"fmt\"\n\nfunc Greet(name string) string {\n\treturn fmt.Sprintf(\"hello, %s\", name)\n}\n" + } + Island weight=1 ground=background padding=12 { + Diff patch="diff --git a/demo.go b/demo.go\n--- a/demo.go\n+++ b/demo.go\n@@ -1,4 +1,4 @@\n package demo\n \n-// Greet says hello.\n+// Hello says hello.\n func Greet(name string) string {\n" + } + } + } +} + +fragment Overlays { + Overlay align=nw { + Island ground=background padding=24 { + Flex axis=horizontal spacing=40 align=start { + Inset top=40 left=60 { Tooltip text="Opens after a short delay" { Button text="Top" variant=outline } } + Inset top=40 { Tooltip text="Below" side=bottom { Button text="Bottom" variant=outline } } + Inset top=40 { Tooltip text="Beside" side=right { Button text="Right" variant=outline } } + } + } + Inset left=24 top=160 { + Popover { + MenuHeader text="Session" count="3" + MenuItem text="Rename" icon="pencil" shortcut="F2" lit=true + MenuItem text="Pin to top" icon="pin" checked=true + MenuItem text="Move to" icon="folder" submenu=true + MenuItem text="Fork" icon="git-fork" disabled=true + MenuSeparator + MenuItem text="Close session" icon="trash-2" shortcut="Ctrl+W" destructive=true + PopoverRow { TwoLines top="claude" under="opus, high" } + } + } + Toast variant=warning title="lich quit unexpectedly last time" description="Sessions that were running were restored." action="Report" secondary="Open log" cancel="Dismiss" + Toast title="Downloading lich 1.4.0" description="30% of 41.0 MB" progress=30 position=bottom-right + } +} + +fragment Modal { + Overlay { + Island ground=background { EmptyState title="Behind the dialog" } + Dialog title="Delete this session?" description="Its terminal closes and its worktree is removed. This cannot be undone." { + Checkbox label="Keep the branch" checked=true + DialogActions { Button text="Cancel" variant=outline Button text="Delete" variant=destructive } + } + } +} + +screen "Controls" width=1100 height=1070 { Inset all=12 { Controls } } +screen "Controls – light" width=1100 height=1070 theme=light { Inset all=12 { Controls } } +screen "Marks" width=1100 height=940 { Inset all=12 { Marks } } +screen "Marks – light" width=1100 height=940 theme=light { Inset all=12 { Marks } } +screen "Viewers" width=1100 height=480 { Inset all=6 { Viewers } } +screen "Viewers – light" width=1100 height=480 theme=light { Inset all=6 { Viewers } } +screen "Overlays" width=900 height=520 { Inset all=6 { Overlays } } +screen "Overlays – light" width=900 height=520 theme=light { Inset all=6 { Overlays } } +screen "Dialog" width=900 height=420 { Modal } +screen "Dialog – light" width=900 height=420 theme=light { Modal } diff --git a/native/gockup-lich/viewers.go b/native/gockup-lich/viewers.go new file mode 100644 index 000000000..7519bcbbf --- /dev/null +++ b/native/gockup-lich/viewers.go @@ -0,0 +1,128 @@ +package lich + +import ( + "fmt" + "image" + "strings" + + "gioui.org/layout" + "gioui.org/unit" + + "github.com/lichdotdev/gockup" + "github.com/lichdotdev/godemirror" + + "github.com/lichdotdev/lich/native/fonts" + "github.com/lichdotdev/lich/native/ui" + "github.com/lichdotdev/lich/native/ui/codeview" + "github.com/lichdotdev/lich/native/ui/diffview" + "github.com/lichdotdev/lich/native/ui/filetree" + "github.com/lichdotdev/lich/native/ui/markdown" +) + +// codeSize is the window's default code size. +const codeSize unit.Sp = 13 + +func (k kit) viewers() { + alerts := map[markdown.Alert]string{markdown.AlertNote: "Note", markdown.AlertTip: "Tip", markdown.AlertImportant: "Important", markdown.AlertWarning: "Warning", markdown.AlertCaution: "Caution"} + markdown.Words = markdown.Labels{ + Alert: func(a markdown.Alert) string { return alerts[a] }, + More: func(count int) string { return fmt.Sprintf("Show %d more lines", count) }, + Less: func() string { return "Show less" }, + } + + k.add("Markdown", "an agent's message or a document: GFM, alerts, highlighted code", func(c *gockup.Ctx, th *ui.Theme) D { + cache := gockup.State(c, func(m *markdown.Cache) { *m = markdown.Cache{} }) + return cache.Layout(c.Gtx, th, "mockup", c.String("text"), nil) + }, str("text", "")) + k.add("Code", "the code view, filling the space given", func(c *gockup.Ctx, th *ui.Theme) D { + s := gockup.State(c, func(s *codeState) { + if s.view, s.err = codeview.New(fonts.SystemMono, codeSize); s.err == nil { + s.view.SetSource(codeview.Source{Filename: c.String("file"), Text: c.String("text")}) + } + }) + if s.err != nil { + return failed(c.Gtx, th, s.err) + } + gtx := c.Gtx + gtx.Constraints = layout.Exact(gtx.Constraints.Max) + d, err := s.view.Layout(gtx, th, codeview.OnSurface(th, th.Code)) + s.err = err + return d + }, str("text", ""), str("file", "its name picks the language")) + k.add("Diff", "one file's diff, as high as its rows", func(c *gockup.Ctx, th *ui.Theme) D { + s := gockup.State(c, func(s *diffState) { s.err = s.open(c.String("patch")) }) + if s.err != nil { + return failed(c.Gtx, th, s.err) + } + d, err := s.layout(c.Gtx, th, c.Bool("split")) + s.err = err + return d + }, str("patch", `a unified diff with its "diff --git", "---" and "+++" lines`), flag("split", "side by side")) + k.add("FileTree", "a tree of paths, every folder open, filling the space given", func(c *gockup.Ctx, th *ui.Theme) D { + t := gockup.State(c, func(t *filetree.Rows) { + t.List.Axis = layout.Vertical + t.Tree = filetree.Build(list(c.String("paths"))) + files := map[string]filetree.State{} + for prop, state := range treeStates { + for _, p := range list(c.String(prop)) { + files[p] = state + } + } + t.States = filetree.WithDirStates(t.Tree, files) + }) + t.Clicked(c.Gtx) + gtx := c.Gtx + gtx.Constraints = layout.Exact(gtx.Constraints.Max) + rows := filetree.VisibleRows(t.Tree, func(string) bool { return true }) + return t.Layout(gtx, th, rows, c.String("selected")) + }, str("paths", `"cmd/main.go, README.md"`), str("selected", "a path"), + str("added", "paths of these"), str("modified", "paths of these"), str("untracked", "paths of these"), str("conflict", "paths of these")) +} + +var treeStates = map[string]filetree.State{ + "added": filetree.Added, "modified": filetree.Modified, "untracked": filetree.Untracked, "conflict": filetree.Conflict, +} + +func failed(gtx C, th *ui.Theme, err error) D { + return th.Text(ui.TextXS, err.Error()).In(th.Destructive).Wrapped().Layout(gtx) +} + +type codeState struct { + view *codeview.View + err error +} + +type diffState struct { + view *diffview.View + patch godemirror.FilePatch + blocks diffview.Blocks + err error +} + +func (s *diffState) open(patch string) error { + files, err := godemirror.ParsePatch(strings.TrimSuffix(patch, "\n") + "\n") + if err != nil { + return err + } + if len(files) == 0 { + return fmt.Errorf("the patch names no file") + } + s.patch = files[0] + s.view, err = diffview.New(fonts.SystemMono, codeSize, diffview.Options{Buttons: diffview.NewButtons(diffview.Labels{ + Expand: func(count int) string { return fmt.Sprintf("Expand %d lines", count) }, + Revert: func() string { return "Revert" }, + })}) + return err +} + +func (s *diffState) layout(gtx C, th *ui.Theme, split bool) (D, error) { + f := diffview.Frame{Theme: th, Code: codeview.OnSurface(th, th.Code), Patch: s.patch, Split: split, Wrap: true, Blocks: &s.blocks} + height, err := s.view.Height(gtx, f, gtx.Constraints.Max.X) + if err != nil { + return D{}, err + } + f.Height = height + gtx.Constraints = layout.Exact(image.Pt(gtx.Constraints.Max.X, height)) + d, _, err := s.view.Layout(gtx, f) + return d, err +}