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
2 changes: 2 additions & 0 deletions cmd/viva/initfiles/AGENTS.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
84 changes: 69 additions & 15 deletions components/scroll_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -49,6 +62,7 @@ type ScrollArea struct {
reserveBars bool
hoverBars bool
barsRevealed bool
bar ScrollBarStyle
contentClipIsActive bool
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -231,26 +274,37 @@ 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)
}

// HorizontalOffsetForThumb returns the content offset that places the left edge
// 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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
43 changes: 43 additions & 0 deletions components/scroll_area_bars_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
52 changes: 39 additions & 13 deletions components/tooltip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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)
}
}

Expand Down
36 changes: 36 additions & 0 deletions components/tooltip_style_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading