From cf58594d465d9a2cb5cc69d0bc1363e319f0344c Mon Sep 17 00:00:00 2001 From: lick <2188718831@qq.com> Date: Wed, 11 Nov 2026 21:02:59 -0800 Subject: [PATCH] feat(components): let applications set tree arrows, bar geometry and tooltip surface Three surfaces that Viva painted with fixed geometry now take an application's own values, each falling back to Viva's when unset: - components.TreeDisclosure, through ui.TreeState.Disclosure: the collapsed and expanded icon, their size, and the width of the column before a branch's content. The ui keeps its row padding in step with the same slot. - components.ScrollBarStyle, through ScrollArea.Bars: track width, hover overlay width, the inset from the track's edge and the least thumb length. ScrollAreaGeometry records the inset, so a drag on a thumb maps back with the value the area drew with. - gfx.TooltipStyle, through Theme.Tooltip: radius, padding, text size, background, text and border. The arrow paints with the same radius and fill, and a border is a ring behind the body so the corners stay the body's. Tests cover each option and its default, and the init guide lists the Tooltip field. Co-Authored-By: AtomCode (deepseek-v4.1-flash) --- cmd/viva/initfiles/AGENTS.md.txt | 2 + components/scroll_area.go | 84 +++++++++++++++++++++++------ components/scroll_area_bars_test.go | 43 +++++++++++++++ components/tooltip.go | 52 +++++++++++++----- components/tooltip_style_test.go | 36 +++++++++++++ components/tree.go | 63 +++++++++++++++++++--- components/tree_disclosure_test.go | 34 ++++++++++++ gfx/theme.go | 16 ++++++ ui/tree.go | 18 ++++++- 9 files changed, 311 insertions(+), 37 deletions(-) create mode 100644 components/scroll_area_bars_test.go create mode 100644 components/tooltip_style_test.go create mode 100644 components/tree_disclosure_test.go diff --git a/cmd/viva/initfiles/AGENTS.md.txt b/cmd/viva/initfiles/AGENTS.md.txt index 0e93375df..821cccf1d 100644 --- a/cmd/viva/initfiles/AGENTS.md.txt +++ b/cmd/viva/initfiles/AGENTS.md.txt @@ -518,6 +518,8 @@ Colors, all `gfx.Color`: `Background`, `Foreground`, `Card`, `CardForeground`, ` `PopoverForeground`, `Muted`, `MutedForeground`, `Border`, `Hover`, `Input`, `Primary`, `PrimaryForeground`, `Selection`, `SelectionForeground`, `Secondary`, `SecondaryForeground`, `Accent`, `AccentForeground`, `Ring`, `Success`, `Danger`, `DangerForeground`, and `Chart` (an array of five). There is no `Surface`, `Text` or `Error`: use `Card`, `Foreground`, `Danger`. +`Tooltip` is a `gfx.TooltipStyle` that overrides the tooltip surface (radius, padding, text size, background, +text and border); zero fields keep Viva's own surface, so set only what your product changes. A `gfx.Color` is `{Red, Green, Blue, Alpha float32}` with each in 0..1 (so `gfx.Color{Alpha: 0.5}` is a black scrim); it has `Mix(other, t)`, `Over(backdrop)` and `WithAlpha(a)`, and no `R` or `A`. To write a color of your own, `gfx.RGB24(0x0a84ff)` (an integer constant, no string to parse), `gfx.RGB(10, 132, 255)` or `gfx.RGBA(10, 132, 255, 0.5)`; take theme colors where you can. A dark window is `gfx.DarkTheme()` in place diff --git a/components/scroll_area.go b/components/scroll_area.go index a06dfa11d..a91951b6e 100644 --- a/components/scroll_area.go +++ b/components/scroll_area.go @@ -9,6 +9,16 @@ const ( scrollAreaMinThumb float32 = 18 ) +// ScrollBarStyle overrides the geometry of a scroll area's bars. Zero fields keep +// Viva's own values, so an application changes only what it needs: IDEA's editor bar +// is an eleven point track with a five point thumb and a shorter least thumb. +type ScrollBarStyle struct { + Size float32 // the track width; 0 keeps 10 + HoverSize float32 // the width of the hover overlay thumb; 0 keeps 6 + Inset float32 // the gap between a bar and its track's edge; 0 keeps 1 + MinThumb float32 // the least thumb length; 0 keeps 18 +} + // ScrollAreaGeometry is the complete deterministic layout of one externally // controlled scroll area. Content is already translated by the clamped offset. type ScrollAreaGeometry struct { @@ -20,10 +30,13 @@ type ScrollAreaGeometry struct { HorizontalTrack gfx.Rect HorizontalThumb gfx.Rect Corner gfx.Rect - OffsetX float32 - OffsetY float32 - MaxOffsetX float32 - MaxOffsetY float32 + // Inset is the gap the bars keep from their tracks' edges, so a caller turning a drag + // on a thumb into an offset uses the same value the area drew with. + Inset float32 + OffsetX float32 + OffsetY float32 + MaxOffsetX float32 + MaxOffsetY float32 } // ScrollArea paints the visible scrollbars around a caller-painted clipped @@ -49,6 +62,7 @@ type ScrollArea struct { reserveBars bool hoverBars bool barsRevealed bool + bar ScrollBarStyle contentClipIsActive bool } @@ -124,11 +138,39 @@ func (area ScrollArea) Disabled() ScrollArea { // full-size bar. func (area ScrollArea) barSize() float32 { if area.hoverBars { + if area.bar.HoverSize > 0 { + return area.bar.HoverSize + } return scrollAreaHoverBarSize } + if area.bar.Size > 0 { + return area.bar.Size + } return scrollAreaBarSize } +// barInset is the gap between a bar and its track's edge. +func (area ScrollArea) barInset() float32 { + if area.bar.Inset > 0 { + return area.bar.Inset + } + return scrollAreaBarInset +} + +// minThumb is the least length of a thumb. +func (area ScrollArea) minThumb() float32 { + if area.bar.MinThumb > 0 { + return area.bar.MinThumb + } + return scrollAreaMinThumb +} + +// Bars overrides the geometry of both bars; the zero style keeps Viva's own. +func (area ScrollArea) Bars(style ScrollBarStyle) ScrollArea { + area.bar = style + return area +} + // Geometry returns clamped viewport, translated content, track, thumb, and // corner geometry without painting or changing Canvas state. func (area ScrollArea) Geometry() ScrollAreaGeometry { @@ -162,6 +204,7 @@ func (area ScrollArea) Geometry() ScrollAreaGeometry { }, OffsetX: offsetX, OffsetY: offsetY, MaxOffsetX: maximumX, MaxOffsetY: maximumY, + Inset: area.barInset(), } // The bars lie along the outer bounds. Where the viewport already excludes them @@ -183,14 +226,14 @@ func (area ScrollArea) Geometry() ScrollAreaGeometry { X: bounds.X + bounds.Width - barSize, Y: bounds.Y, Width: barSize, Height: verticalExtent, } - geometry.VerticalThumb = scrollAreaThumb(geometry.VerticalTrack, true, viewport.Height, contentHeight, offsetY, maximumY) + geometry.VerticalThumb = scrollAreaThumb(geometry.VerticalTrack, true, viewport.Height, contentHeight, offsetY, maximumY, area.barInset(), area.minThumb()) } if hasHorizontal { geometry.HorizontalTrack = gfx.Rect{ X: bounds.X, Y: bounds.Y + bounds.Height - barSize, Width: horizontalExtent, Height: barSize, } - geometry.HorizontalThumb = scrollAreaThumb(geometry.HorizontalTrack, false, viewport.Width, contentWidth, offsetX, maximumX) + geometry.HorizontalThumb = scrollAreaThumb(geometry.HorizontalTrack, false, viewport.Width, contentWidth, offsetX, maximumX, area.barInset(), area.minThumb()) } return geometry } @@ -231,11 +274,12 @@ func scrollAreaReservedViewport(bounds gfx.Rect, contentWidth, contentHeight, ba // owned offset with it, without repeating the track insets. It is zero when // the area does not scroll vertically. func (geometry ScrollAreaGeometry) VerticalOffsetForThumb(thumbTop float32) float32 { - travel := scrollAreaMax(geometry.VerticalTrack.Height-scrollAreaBarInset*2, 0) - geometry.VerticalThumb.Height + inset := geometry.inset() + travel := scrollAreaMax(geometry.VerticalTrack.Height-inset*2, 0) - geometry.VerticalThumb.Height if travel <= 0 || geometry.MaxOffsetY <= 0 { return 0 } - ratio := (thumbTop - (geometry.VerticalTrack.Y + scrollAreaBarInset)) / travel + ratio := (thumbTop - (geometry.VerticalTrack.Y + inset)) / travel return scrollAreaClamp(ratio*geometry.MaxOffsetY, 0, geometry.MaxOffsetY) } @@ -243,14 +287,24 @@ func (geometry ScrollAreaGeometry) VerticalOffsetForThumb(thumbTop float32) floa // of the horizontal thumb at thumbLeft. It is zero when the area does not scroll // horizontally. func (geometry ScrollAreaGeometry) HorizontalOffsetForThumb(thumbLeft float32) float32 { - travel := scrollAreaMax(geometry.HorizontalTrack.Width-scrollAreaBarInset*2, 0) - geometry.HorizontalThumb.Width + inset := geometry.inset() + travel := scrollAreaMax(geometry.HorizontalTrack.Width-inset*2, 0) - geometry.HorizontalThumb.Width if travel <= 0 || geometry.MaxOffsetX <= 0 { return 0 } - ratio := (thumbLeft - (geometry.HorizontalTrack.X + scrollAreaBarInset)) / travel + ratio := (thumbLeft - (geometry.HorizontalTrack.X + inset)) / travel return scrollAreaClamp(ratio*geometry.MaxOffsetX, 0, geometry.MaxOffsetX) } +// inset is the gap the bars keep from their tracks' edges: what the area drew with, or +// Viva's own when the geometry came from an area with the default style. +func (geometry ScrollAreaGeometry) inset() float32 { + if geometry.Inset > 0 { + return geometry.Inset + } + return scrollAreaBarInset +} + // BeginContent pushes the viewport clip and returns translated content geometry. // Call EndContent after painting the caller-owned content. func (area *ScrollArea) BeginContent() ScrollAreaGeometry { @@ -342,11 +396,11 @@ func (area ScrollArea) ScrollForKey(x, y float32, key gfx.Key, step float32) (fl return scrollAreaClamp(x, 0, geometry.MaxOffsetX), scrollAreaClamp(y, 0, geometry.MaxOffsetY), true } -func scrollAreaThumb(track gfx.Rect, vertical bool, viewportExtent, contentExtent, offset, maximum float32) gfx.Rect { +func scrollAreaThumb(track gfx.Rect, vertical bool, viewportExtent, contentExtent, offset, maximum, inset, minThumb float32) gfx.Rect { inner := gfx.Rect{ - X: track.X + scrollAreaBarInset, Y: track.Y + scrollAreaBarInset, - Width: scrollAreaMax(track.Width-scrollAreaBarInset*2, 0), - Height: scrollAreaMax(track.Height-scrollAreaBarInset*2, 0), + X: track.X + inset, Y: track.Y + inset, + Width: scrollAreaMax(track.Width-inset*2, 0), + Height: scrollAreaMax(track.Height-inset*2, 0), } trackExtent := inner.Width if vertical { @@ -356,7 +410,7 @@ func scrollAreaThumb(track gfx.Rect, vertical bool, viewportExtent, contentExten return gfx.Rect{} } thumbExtent := trackExtent * viewportExtent / contentExtent - thumbExtent = scrollAreaClamp(thumbExtent, scrollAreaMin(scrollAreaMinThumb, trackExtent), trackExtent) + thumbExtent = scrollAreaClamp(thumbExtent, scrollAreaMin(minThumb, trackExtent), trackExtent) position := float32(0) if maximum > 0 { position = (trackExtent - thumbExtent) * offset / maximum diff --git a/components/scroll_area_bars_test.go b/components/scroll_area_bars_test.go new file mode 100644 index 000000000..39fe8166e --- /dev/null +++ b/components/scroll_area_bars_test.go @@ -0,0 +1,43 @@ +package components + +import ( + "testing" + + "github.com/vivaui/viva/gfx" +) + +// A caller can match the bars to its own chrome: the track width, the inset and the +// least thumb come from the requested style, and the thumb keeps the track less both +// insets. +func TestScrollAreaBarsStyleChangesTracksAndThumbs(t *testing.T) { + var canvas gfx.Canvas + area := BeginScrollArea(&canvas, gfx.LightTheme(), gfx.Rect{Width: 200, Height: 100}, gfx.Size{Width: 200, Height: 1000}). + Bars(ScrollBarStyle{Size: 11, Inset: 2, MinThumb: 20}) + geometry := area.Geometry() + if geometry.VerticalTrack.Width != 11 { + t.Fatalf("track width = %v, want 11", geometry.VerticalTrack.Width) + } + if geometry.Inset != 2 { + t.Fatalf("inset = %v, want 2", geometry.Inset) + } + if got := geometry.VerticalThumb.Width; got != 11-2*2 { + t.Fatalf("thumb width = %v, want the track less both insets", got) + } + // The drag inversion uses the same inset the area drew with. + if top := geometry.VerticalOffsetForThumb(geometry.VerticalTrack.Y + 2); top != 0 { + t.Fatalf("offset at the track's start = %v, want 0", top) + } +} + +// A scroll area that sets no style keeps Viva's own bar geometry. +func TestScrollAreaBarsDefaultToTheSharedGeometry(t *testing.T) { + var canvas gfx.Canvas + area := BeginScrollArea(&canvas, gfx.LightTheme(), gfx.Rect{Width: 200, Height: 100}, gfx.Size{Width: 200, Height: 1000}) + geometry := area.Geometry() + if geometry.VerticalTrack.Width != scrollAreaBarSize { + t.Fatalf("track width = %v, want %v", geometry.VerticalTrack.Width, scrollAreaBarSize) + } + if geometry.Inset != scrollAreaBarInset { + t.Fatalf("inset = %v, want %v", geometry.Inset, scrollAreaBarInset) + } +} diff --git a/components/tooltip.go b/components/tooltip.go index c74d9bf42..3135561dc 100644 --- a/components/tooltip.go +++ b/components/tooltip.go @@ -122,21 +122,43 @@ func (tooltip Tooltip) Paint(label string) gfx.Rect { return gfx.Rect{} } metrics := tooltip.theme.Metrics + style := tooltip.theme.Tooltip + padX, padY := float32(12), float32(6) + if style.PadX > 0 { + padX = style.PadX + } + if style.PadY > 0 { + padY = style.PadY + } + fontSize := metrics.FontSizeS + if style.FontSize > 0 { + fontSize = style.FontSize + } + radius := metrics.RadiusControl + if style.Radius > 0 { + radius = style.Radius + } + background, text := tooltip.theme.Foreground, tooltip.theme.Background + if style.Background != (gfx.Color{}) { + background = style.Background + } + if style.Text != (gfx.Color{}) { + text = style.Text + } maxWidth := tooltip.options.MaxWidth if maxWidth <= 0 { maxWidth = 320 } maxWidth = min(maxWidth, tooltip.viewport.Width-8) - const padX, padY = float32(12), float32(6) var scratch overlayLines - lines := overlayWrap(tooltip.canvas, &scratch, label, metrics.FontSizeS, gfx.WeightRegular, maxWidth-2*padX) + lines := overlayWrap(tooltip.canvas, &scratch, label, fontSize, gfx.WeightRegular, maxWidth-2*padX) // One line takes the width of its text; a wrapped label fills the whole // maximum, as a `w-fit max-w-xs` box does. textWidth := maxWidth - 2*padX if len(lines) == 1 { - textWidth = tooltip.canvas.TextWidthWeight(lines[0], metrics.FontSizeS, gfx.WeightRegular) + textWidth = tooltip.canvas.TextWidthWeight(lines[0], fontSize, gfx.WeightRegular) } - lineHeight := tooltipLineHeight(metrics) + lineHeight := fontSize + 5 size := gfx.Size{Width: textWidth + 2*padX, Height: float32(len(lines))*lineHeight + 2*padY} offset := tooltip.options.SideOffset if offset == 0 { @@ -149,19 +171,24 @@ func (tooltip Tooltip) Paint(label string) gfx.Rect { tooltip.canvas.PushLayer(tooltip.options.Layer) tooltip.canvas.BeginPresence(overlayKey(tooltip.options.ID), overlaySurfacePart, popupPresence(tooltip.theme, tooltipDuration, side), popupPivot(content, side, tooltip.anchor)) - tooltip.canvas.Rounded(content, metrics.RadiusControl, tooltip.theme.Foreground) + if style.BorderSize > 0 && style.Border != (gfx.Color{}) { + // The border is a ring behind the body, so the body keeps its own corners. + ring := gfx.Rect{ + X: content.X - style.BorderSize, Y: content.Y - style.BorderSize, + Width: content.Width + style.BorderSize*2, Height: content.Height + style.BorderSize*2, + } + tooltip.canvas.Rounded(ring, radius+style.BorderSize, style.Border) + } + tooltip.canvas.Rounded(content, radius, background) overlayParagraph(tooltip.canvas, lines, gfx.Rect{X: content.X + padX, Y: content.Y + padY, Width: content.Width - 2*padX}, lineHeight, gfx.TextStyle{ - Size: metrics.FontSizeS, Weight: gfx.WeightRegular, Color: tooltip.theme.Background, + Size: fontSize, Weight: gfx.WeightRegular, Color: text, }, false) - tooltip.paintArrow(content, tooltipSideOf(side)) + tooltip.paintArrow(content, tooltipSideOf(side), radius, background) tooltip.canvas.EndPresence() tooltip.canvas.PopLayer() return content } -// tooltipLineHeight is the 16px line of `text-xs`. -func tooltipLineHeight(metrics gfx.Metrics) float32 { return metrics.FontSizeS + 5 } - func tooltipOverlaySide(side TooltipSide) OverlaySide { switch side { case TooltipRight: @@ -200,10 +227,9 @@ const ( tooltipArrowInset = float32(1) ) -func (tooltip Tooltip) paintArrow(content gfx.Rect, side TooltipSide) { +func (tooltip Tooltip) paintArrow(content gfx.Rect, side TooltipSide, radius float32, fill gfx.Color) { // Center on the trigger, but keep the base on the flat part of the body so // it never straddles a rounded corner after the body was clamped on screen. - radius := tooltip.theme.Metrics.RadiusControl edge, center := float32(0), float32(0) switch side { case TooltipRight: @@ -230,7 +256,7 @@ func (tooltip Tooltip) paintArrow(content gfx.Rect, side TooltipSide) { default: bounds = gfx.Rect{X: center - width/2, Y: edge - tooltipArrowInset, Width: width, Height: reach + tooltipArrowInset} } - tooltip.canvas.Fill(bounds, tooltip.theme.Foreground) + tooltip.canvas.Fill(bounds, fill) } } diff --git a/components/tooltip_style_test.go b/components/tooltip_style_test.go new file mode 100644 index 000000000..c77b49fee --- /dev/null +++ b/components/tooltip_style_test.go @@ -0,0 +1,36 @@ +package components + +import ( + "testing" + + "github.com/vivaui/viva/gfx" +) + +// A theme that sets the tooltip surface paints that surface: the padding and text size +// change the box, so an application's own tip geometry comes from its theme alone. +func TestTooltipStyleSizesTheSurface(t *testing.T) { + theme := gfx.LightTheme() + theme.Tooltip = gfx.TooltipStyle{PadX: 6, PadY: 10, FontSize: 13, Radius: 4} + var canvas gfx.Canvas + viewport := gfx.Rect{Width: 400, Height: 300} + anchor := gfx.Rect{X: 100, Y: 100, Width: 40, Height: 20} + content := BeginTooltip(&canvas, theme, viewport, anchor, TooltipOptions{}).Paint("hint") + if want := float32((13 + 5) + 2*10); content.Height != want { + t.Fatalf("height = %v, want %v", content.Height, want) + } + if content.Width < 2*6 { + t.Fatalf("width = %v, want at least the horizontal padding", content.Width) + } +} + +// A theme that sets nothing keeps Viva's own surface: the small text and the padded box. +func TestTooltipStyleDefaultsToTheSharedSurface(t *testing.T) { + theme := gfx.LightTheme() + var canvas gfx.Canvas + viewport := gfx.Rect{Width: 400, Height: 300} + anchor := gfx.Rect{X: 100, Y: 100, Width: 40, Height: 20} + content := BeginTooltip(&canvas, theme, viewport, anchor, TooltipOptions{}).Paint("hint") + if want := (theme.Metrics.FontSizeS + 5) + 2*6; content.Height != want { + t.Fatalf("height = %v, want %v", content.Height, want) + } +} diff --git a/components/tree.go b/components/tree.go index fd4cfea04..9817e44d9 100644 --- a/components/tree.go +++ b/components/tree.go @@ -2,16 +2,28 @@ package components import "github.com/vivaui/viva/gfx" +// TreeDisclosure replaces the chevrons a tree draws for its branches: the icon of a +// closed and of an open branch, their size, and the width of the column they occupy +// before the row's content. Zero fields keep Viva's own chevrons (16 points in a 24 +// point column), so an application that ships IDEA's arrows can use them without +// moving the rows. +type TreeDisclosure struct { + Collapsed, Expanded gfx.Icon + Size float32 + Slot float32 +} + // Tree paints a compact vertical hierarchy. Applications own the hierarchy, // expansion, selection, and scrolling; the component owns indentation, // disclosure geometry, focus/hover feedback, and typed hit semantics. type Tree struct { - canvas *gfx.Canvas - theme gfx.Theme - bounds gfx.Rect - cursorY float32 - rowHeight float32 - indent float32 + canvas *gfx.Canvas + theme gfx.Theme + bounds gfx.Rect + cursorY float32 + rowHeight float32 + indent float32 + disclosure TreeDisclosure } // TreeItem describes one already-flattened visible tree row. Branches use a @@ -65,6 +77,22 @@ func (tree Tree) Indent(width float32) Tree { return tree } +// Disclosure replaces the arrows the tree draws for branches and the width of the +// column they occupy. Zero fields keep the tree's own chevrons. +func (tree Tree) Disclosure(style TreeDisclosure) Tree { + tree.disclosure = style + return tree +} + +// disclosureSlot is the width of the column before a branch's content: the requested +// slot, or the 24 points Viva's own chevrons take. +func (tree *Tree) disclosureSlot() float32 { + if tree.disclosure.Slot > 0 { + return tree.disclosure.Slot + } + return 24 +} + // Item paints one visible row and returns its caller-owned content area. The // component never walks children or allocates a backing model. func (tree *Tree) Item(item TreeItem) TreeRow { @@ -96,12 +124,12 @@ func (tree *Tree) Item(item TreeItem) TreeRow { disclosure := gfx.Rect{X: leading, Y: row.Y + (row.Height-20)/2, Width: 20, Height: 20} contentX := leading if item.Branch { - contentX += 24 + contentX += tree.disclosureSlot() color := tree.theme.MutedForeground if item.Disabled { color.Alpha *= 0.50 } - paintTreeChevron(tree.canvas, disclosure, item.Expanded, color) + tree.paintDisclosure(disclosure, item.Expanded, color) if !item.DisclosureID.IsZero() { tree.canvas.HitWithOptions(item.DisclosureID, disclosure, item.Label, gfx.HitOptions{ Role: gfx.RoleButton, Disabled: item.Disabled, Expanded: item.Expanded, Expandable: true, @@ -122,6 +150,25 @@ func (tree *Tree) Item(item TreeItem) TreeRow { return TreeRow{Bounds: row, Content: content, Disclosure: disclosure} } +// paintDisclosure draws the branch arrow: the requested icon and size, or the tree's +// own chevron when the application supplied none. +func (tree *Tree) paintDisclosure(bounds gfx.Rect, expanded bool, color gfx.Color) { + icon, size := &chevronRightIcon, float32(16) + if expanded { + icon = &chevronDownIcon + } + if tree.disclosure.Size > 0 { + size = tree.disclosure.Size + } + if expanded && len(tree.disclosure.Expanded.Commands) > 0 { + icon = &tree.disclosure.Expanded + } + if !expanded && len(tree.disclosure.Collapsed.Commands) > 0 { + icon = &tree.disclosure.Collapsed + } + paintChevronSized(tree.canvas, icon, bounds, size, color) +} + func paintTreeChevron(canvas *gfx.Canvas, bounds gfx.Rect, expanded bool, color gfx.Color) { icon := &chevronRightIcon if expanded { diff --git a/components/tree_disclosure_test.go b/components/tree_disclosure_test.go new file mode 100644 index 000000000..748618f7a --- /dev/null +++ b/components/tree_disclosure_test.go @@ -0,0 +1,34 @@ +package components + +import ( + "testing" + + "github.com/vivaui/viva/gfx" +) + +// A tree that ships its own disclosure arrows keeps its rows in step: the column the +// arrow takes is the one the application asked for, and a leaf keeps only the indent. +func TestTreeDisclosureSlotMovesRowContent(t *testing.T) { + var canvas gfx.Canvas + tree := BeginTree(&canvas, gfx.LightTheme(), gfx.Rect{Width: 320, Height: 240}).RowHeight(24).Indent(20) + tree = tree.Disclosure(TreeDisclosure{Slot: 17, Size: 16}) + branch := tree.Item(TreeItem{ID: gfx.Item(1), DisclosureID: gfx.Item(2), Label: "src", Branch: true, Expanded: true}) + if branch.Content.X != 17 { + t.Fatalf("content X = %v, want the requested 17 point slot", branch.Content.X) + } + leaf := tree.Item(TreeItem{ID: gfx.Item(3), Label: "app.go", Depth: 1}) + if leaf.Content.X != 20 { + t.Fatalf("leaf content X = %v, want one 20 point indent", leaf.Content.X) + } +} + +// Leaving the disclosure option out keeps Viva's own chevron column, so a tree that +// sets nothing looks and lays out as before. +func TestTreeDisclosureDefaultsToTheChevronColumn(t *testing.T) { + var canvas gfx.Canvas + tree := BeginTree(&canvas, gfx.LightTheme(), gfx.Rect{Width: 320, Height: 240}).RowHeight(24).Indent(20) + branch := tree.Item(TreeItem{ID: gfx.Item(1), DisclosureID: gfx.Item(2), Label: "src", Branch: true}) + if branch.Content.X != 24 { + t.Fatalf("content X = %v, want the 24 point chevron column", branch.Content.X) + } +} diff --git a/gfx/theme.go b/gfx/theme.go index 6664bb5bb..f45115118 100644 --- a/gfx/theme.go +++ b/gfx/theme.go @@ -85,6 +85,20 @@ func (m Metrics) WithDensity(density Density) Metrics { } // Theme contains semantic design tokens rather than runtime CSS. +// TooltipStyle overrides the surface of a tooltip. Zero fields keep Viva's own surface, +// so an application changes only what it needs: IDEA draws its hover tips with a four +// point radius, a one point border and its own two colours. +type TooltipStyle struct { + Radius float32 + PadX, PadY float32 + FontSize float32 + Background Color + Text Color + Border Color + BorderSize float32 +} + +// Theme is the resolved palette and metrics a declaration is painted with. type Theme struct { Background Color Foreground Color @@ -118,6 +132,8 @@ type Theme struct { // Transition is the default state-change motion for controls. A zero // value disables animation for hand-built themes. Transition Motion + // Tooltip overrides the tooltip surface; zero fields keep Viva's own. + Tooltip TooltipStyle } // WithRadius is the theme with another corner radius for its controls diff --git a/ui/tree.go b/ui/tree.go index ef22b9ecb..7dc27d1af 100644 --- a/ui/tree.go +++ b/ui/tree.go @@ -43,6 +43,10 @@ type TreeState struct { // into itself or what is inside it, a row not before a sibling of another kind. // It is asked as the pointer moves; nil makes the rows not draggable. CanDrop func(from, to int, where TreeDrop) bool + // Disclosure replaces the arrows the Tree draws for branches and the width of the + // column they occupy; nil keeps Viva's own chevrons. An application with its own + // arrows sets it, and its row padding follows Slot. + Disclosure *components.TreeDisclosure toggled, chosen int dropped TreeMove @@ -205,6 +209,15 @@ func (s *TreeState) indent(theme *Theme) float32 { return 16 * theme.Metrics.ControlHeight / 32 } +// disclosureSlot is the column a branch spends before its content, in step with the +// component that draws the arrow. +func (s *TreeState) disclosureSlot() float32 { + if s.Disclosure != nil && s.Disclosure.Slot > 0 { + return s.Disclosure.Slot + } + return 24 +} + func (s *TreeState) key(i int) int { if s.Key != nil { return s.Key(i) @@ -237,7 +250,7 @@ func (s *TreeState) row(i int, rowBox Box) { theme := &f.theme left := float32(item.Depth) * s.indent(theme) if node.Branch { - left += 24 // the chevron, as components.Tree places it + left += s.disclosureSlot() } rowBox.Children(func() { b := Row(c).Height(s.heightOf(node, theme)).Shrink(0).PadRight(8).PadLeft(left).Gap(theme.Metrics.GapItem).Items(Center).Draw(s.paint) @@ -261,6 +274,9 @@ func (s *TreeState) draw(d *DrawContext) { continue } tree := components.BeginTree(d.Canvas, *d.Theme, d.Rect).RowHeight(d.Rect.Height).Indent(s.indent(d.Theme)) + if s.Disclosure != nil { + tree = tree.Disclosure(*s.Disclosure) + } tree.Item(row.item) if s.drag.active { s.dragOver(row, d)