Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ in headless tests (screenshot-hash regressions).
`LoadGTKTheme(css)` parses libadwaita / GTK3 `@define-color`
declarations into a `Theme`, so any GTK-desktop palette (Adwaita,
Juno, WhiteSur, Solarized …) drives the widget ink colours.
`SlateLight(brand)` / `SlateDark(brand)` / `Slate(brand, dark)` is the Tailwind
slate look with one brand accent (modelled on the go-authn account page, whose
`#0079a8` it reproduces with the derived hover and ink): a slate-50 page, white
cards, soft borders and shadows. `Frame.Card` is the card (rounded surface,
shadow, heading over a hairline), `Chip.Style` the plain and brand pills, and
`GraphView.CornerRadius` the rounded nodes and clusters. The host reads the
system's light/dark preference and picks the variant.
- **Pure Go + CGO=0**: no C toolchain, no shared libraries. Builds
for every Go target including `GOOS=js GOARCH=wasm`.
- **A11y bridge**: core widgets implement an opt-in `Accessible`
Expand Down
66 changes: 62 additions & 4 deletions chip.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type Chip struct {
// (A == 0) draws no dot and reproduces the original layout exactly,
// so the field is fully backward-compatible.
Dot RGBA
// Style picks the look: ChipDefault (the zero value, the original
// rectangle), ChipPlain or ChipBrand (rounded pills). Set-once appearance
// config.
Style ChipStyle
}

// Chip sizing constants. PadX / PadY are the inner insets from the
Expand Down Expand Up @@ -97,20 +101,74 @@ func (c *Chip) Draw(p painter.Painter, theme *Theme) {
}
c.SetBounds(r)
}
fillRect(p, r.X, r.Y, r.W, r.H, theme.SurfaceAlt)
strokeRect(p, r.X, r.Y, r.W, r.H, theme.Border)
ink := theme.OnSurface
closeInk := theme.Border
switch c.Style {
case ChipPlain, ChipBrand:
// A pill: fully rounded, no outline, a quiet tinted fill.
bg := themeChipBg(theme, c.Style)
ink = themeChipInk(theme, c.Style)
closeInk = ink
fillRoundRect(p, r.X, r.Y, r.W, r.H, r.H/2, bg)
default:
fillRect(p, r.X, r.Y, r.W, r.H, theme.SurfaceAlt)
strokeRect(p, r.X, r.Y, r.W, r.H, theme.Border)
}
tx := r.X + padX
if hasDot {
dotY := r.Y + (r.H-dotD)/2
fillRoundRect(p, r.X+padX, dotY, dotD, dotD, dotD/2, c.Dot)
tx += dotD + dotGap
}
ty := r.Y + (r.H-c.glyphHeight())/2
c.drawText(p, tx, ty, c.Text, theme.OnSurface)
c.drawText(p, tx, ty, c.Text, ink)
if c.Closable {
cx := r.X + r.W - padX - closeW + (closeW-c.textWidth("x"))/2
c.drawText(p, cx, ty, "x", theme.Border)
c.drawText(p, cx, ty, "x", closeInk)
}
}

// ChipStyle is how a Chip is painted.
type ChipStyle int

const (
// ChipDefault is the original chip: a SurfaceAlt rectangle with a Border
// outline.
ChipDefault ChipStyle = iota
// ChipPlain is a quiet pill: fully rounded, a neutral tint, no outline
// (Tailwind's chip-plain). A theme's Extra["chip_plain_bg"] and
// ["chip_plain_ink"] set its colours; without them it is SurfaceAlt and
// OnSurface.
ChipPlain
// ChipBrand is the same pill in the brand colour: the brand at 10% for the
// fill and the brand-as-text for the ink (Extra["chip_brand_bg"] and
// ["brand_ink"]; without them the Accent over the Surface).
ChipBrand
)

// themeChipBg is the fill of a pill chip of style s.
func themeChipBg(theme *Theme, s ChipStyle) RGBA {
if s == ChipBrand {
if c, ok := theme.Extra["chip_brand_bg"]; ok {
return c
}
return blendRGBA(theme.Accent, theme.Surface, 0.10)
}
if c, ok := theme.Extra["chip_plain_bg"]; ok {
return c
}
return theme.SurfaceAlt
}

// themeChipInk is the text of a pill chip of style s.
func themeChipInk(theme *Theme, s ChipStyle) RGBA {
if s == ChipBrand {
return themeInk(theme)
}
if c, ok := theme.Extra["chip_plain_ink"]; ok {
return c
}
return theme.OnSurface
}

// OnEvent fires OnClose when an EventClick lands in the right-hand
Expand Down
33 changes: 29 additions & 4 deletions graphview.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ type GraphView struct {
// host whose labels are longer than a few characters sets it once, before
// the first draw; a node's label is clipped to its box.
NodeWidth int
// CornerRadius rounds the node pills and, by half as much again, the
// cluster boxes, in logical pixels (default 0: square, as before). A
// set-once appearance config, like NodeWidth.
CornerRadius int
// ShowLegend draws the category legend along the bottom edge.
ShowLegend bool
// OnActivate is called with the selection on Enter or a double selection.
Expand Down Expand Up @@ -172,6 +176,27 @@ const (
graphHitSlack = 3
)

// clusterRadius is the rounding of a cluster box: half again the node pill's.
func (g *GraphView) clusterRadius() int { return g.CornerRadius * 3 / 2 }

// fillBox fills r, rounded by radius when radius > 0, square otherwise.
func (g *GraphView) fillBox(p painter.Painter, r Rect, radius int, c RGBA) {
if radius > 0 {
fillRoundRect(p, r.X, r.Y, r.W, r.H, scaled(radius), c)
return
}
fillRect(p, r.X, r.Y, r.W, r.H, c)
}

// strokeBox outlines r, rounded by radius when radius > 0, square otherwise.
func (g *GraphView) strokeBox(p painter.Painter, r Rect, radius int, c RGBA) {
if radius > 0 {
strokeRoundRect(p, r.X, r.Y, r.W, r.H, scaled(radius), c)
return
}
strokeRect(p, r.X, r.Y, r.W, r.H, c)
}

// nodeW is the width of a node box.
func (g *GraphView) nodeW() int {
if g.NodeWidth > 0 {
Expand Down Expand Up @@ -404,8 +429,8 @@ func (g *GraphView) Draw(p painter.Painter, theme *Theme) {
visible := map[string]bool{}
withClip(p, r, func() {
for id, box := range g.clusterBox {
fillRect(p, box.X, box.Y, box.W, box.H, theme.Surface)
strokeRect(p, box.X, box.Y, box.W, box.H, theme.Border)
g.fillBox(p, box, g.clusterRadius(), theme.Surface)
g.strokeBox(p, box, g.clusterRadius(), theme.Border)
g.drawText(p, box.X+graphGap, box.Y+2, labels[id], theme.OnSurface)
}
nodes := g.Model.Nodes.Slice()
Expand All @@ -426,7 +451,7 @@ func (g *GraphView) Draw(p painter.Painter, theme *Theme) {
if !visible[n.ID] {
col = blendRGBA(col, theme.Background, 0.25)
}
fillRect(p, box.X, box.Y, box.W, box.H, col)
g.fillBox(p, box, g.CornerRadius, col)
// Label ink by the swatch's lightness, so it reads on every colour.
ink := RGB(0x10, 0x10, 0x10)
if luminance(col) < 0.4 {
Expand All @@ -438,7 +463,7 @@ func (g *GraphView) Draw(p painter.Painter, theme *Theme) {
g.drawText(p, box.X+3, box.Y+(box.H-g.glyphHeight())/2, n.Label, ink)
})
if sel == "node:"+n.ID {
strokeRect(p, box.X-2, box.Y-2, box.W+4, box.H+4, theme.Accent)
g.strokeBox(p, Rect{X: box.X - 2, Y: box.Y - 2, W: box.W + 4, H: box.H + 4}, g.CornerRadius+2, theme.Accent)
}
}
if g.ShowLegend {
Expand Down
60 changes: 60 additions & 0 deletions layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,16 @@ type Frame struct {
// on the bar toggles the collapsed state. It forces a title bar even when
// Title is "".
Collapsible bool
// Card turns the frame into a CARD: a rounded Theme.Surface with a soft
// Theme.Border outline and, when the theme has one (Extra["shadow"]), the
// soft two-layer card shadow, padded by Padding (default 12) with a title,
// if any, set as a heading over a hairline rather than as a filled band. It
// is the surface of a Tailwind-style page: Slate gives it its colours. The
// zero value keeps the plain border-only frame. Set-once appearance config,
// like Padding.
Card bool
// CornerRadius is a Card's corner rounding in logical pixels (default 14).
CornerRadius int

// collapsed holds the reactive collapse state, MVVM-only, exposed via
// [Frame.Collapsed]. Meaningful only when a title bar is present.
Expand All @@ -704,6 +714,43 @@ func (f *Frame) Collapsed() *mvvm.Observable[bool] {
// the Frame lays it out at scaled(FrameTitleH) device pixels.
const FrameTitleH = 22

// Card-style frame metrics, in logical pixels.
const (
frameCardPadding = 12
frameCardRadius = 14
)

// cardRadius is the corner rounding of a Card frame.
func (f *Frame) cardRadius() int {
if f.CornerRadius > 0 {
return scaled(f.CornerRadius)
}
return scaled(frameCardRadius)
}

// drawCard paints a Card frame's ground: the shadow (when the theme has one),
// the rounded Theme.Surface, and the soft outline.
func (f *Frame) drawCard(p painter.Painter, theme *Theme, r Rect) {
rad := f.cardRadius()
if sh, ok := theme.Extra["shadow"]; ok && sh.A != 0 {
// Two soft layers, as CSS `0 1px 2px, 0 4px 16px -4px`: a tight one under
// the edge and a wide faint one below it.
fillRoundRect(p, r.X, r.Y+1, r.W, r.H, rad, RGBA{R: sh.R, G: sh.G, B: sh.B, A: sh.A / 2})
fillRoundRect(p, r.X+scaled(4), r.Y+scaled(5), r.W-2*scaled(4), r.H, rad, RGBA{R: sh.R, G: sh.G, B: sh.B, A: sh.A / 2})
}
fillRoundRect(p, r.X, r.Y, r.W, r.H, rad, theme.Surface)
strokeRoundRect(p, r.X, r.Y, r.W, r.H, rad, theme.Border)
}

// drawCardTitle paints a Card's heading in the theme's ink over a hairline.
func (f *Frame) drawCardTitle(p painter.Painter, theme *Theme, r Rect, hh int) {
bw := strokeWidth()
pad := f.inset()
ty := r.Y + bw + scaled(frameCardPadding)/2 + (hh-f.glyphHeight())/2
f.drawText(p, r.X+pad, ty, f.Title, theme.OnSurface)
fillRect(p, r.X+pad, r.Y+bw+scaled(frameCardPadding)/2+hh, r.W-2*pad, 1, theme.Border)
}

// NewFrame wraps child in a Frame. child may be nil (the Frame then
// just draws its border + accepts no events).
func NewFrame(child Widget) *Frame { return &Frame{child: child} }
Expand All @@ -724,6 +771,9 @@ func (f *Frame) inset() int {
pad := f.Padding
if pad == 0 {
pad = defaultPadding
if f.Card {
pad = frameCardPadding
}
}
if pad < 0 {
pad = 0
Expand Down Expand Up @@ -802,6 +852,16 @@ func (f *Frame) drawTitleBar(p painter.Painter, theme *Theme, r Rect, hh int) {
func (f *Frame) Draw(p painter.Painter, theme *Theme) {
r := f.Bounds()
hh := f.headerH()
if f.Card {
f.drawCard(p, theme, r)
if f.Title != "" {
f.drawCardTitle(p, theme, r, hh)
}
if f.child != nil {
f.child.Draw(p, theme)
}
return
}
if f.Collapsed().Get() && hh > 0 {
strokeRect(p, r.X, r.Y, r.W, hh+2*strokeWidth(), theme.Border)
f.drawTitleBar(p, theme, r, hh)
Expand Down
6 changes: 3 additions & 3 deletions mvvm_gate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var mvvmMigratedWidgets = map[string]map[string]bool{
"Expander": {"Label": true, "Content": true},
// Batch 3 (index/selection widgets): the current index / open / collapsed
// state is unexported behind accessors; the slices + flags below are config.
"Frame": {"Padding": true, "Title": true, "Collapsible": true},
"Frame": {"Padding": true, "Title": true, "Collapsible": true, "Card": true, "CornerRadius": true},
"ViewSwitcher": {"Views": true},
"CycleButton": {"Options": true},
"Pagination": {"Total": true},
Expand Down Expand Up @@ -298,7 +298,7 @@ var mvvmMigratedWidgets = map[string]map[string]bool{
"Card": {"Body": true, "Footer": true, "Title": true},
"CardMeta": {"Author": true, "Comments": true, "Score": true, "Time": true},
"ChatBubble": {"Sender": true, "Text": true},
"Chip": {"Closable": true, "Dot": true, "Text": true},
"Chip": {"Closable": true, "Dot": true, "Text": true, "Style": true},
"Container": {"Layout": true},
"DatabaseEditor": {"BarHeight": true, "EditorHeight": true, "ErrorHeight": true, "TreeWidth": true},
// Closable is a set-once capability flag (like Window.Closable); Input is the
Expand Down Expand Up @@ -364,7 +364,7 @@ var mvvmMigratedWidgets = map[string]map[string]bool{
// (Splitter.Open), enabled is the button's Disabled Observable; the hover
// timer is unexported.
"ClosablePane": {"Title": true, "Body": true, "CloseLabel": true, "LastLabel": true, "Button": true},
"GraphView": {"Model": true, "Palette": true, "MaxEdgeWidth": true, "ShowLegend": true, "NodeWidth": true},
"GraphView": {"Model": true, "Palette": true, "MaxEdgeWidth": true, "ShowLegend": true, "NodeWidth": true, "CornerRadius": true},
}

// TestMigratedWidgetsHaveNoImperativeState is the enforcement gate. It parses the
Expand Down
Loading
Loading