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
35 changes: 27 additions & 8 deletions graphview.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ type GraphView struct {
Palette []RGBA
// MaxEdgeWidth caps an edge's stroke (default 6).
MaxEdgeWidth int
// NodeWidth is the width of a node box in painter units (default 72). A
// 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
// ShowLegend draws the category legend along the bottom edge.
ShowLegend bool
// OnActivate is called with the selection on Enter or a double selection.
Expand All @@ -112,6 +116,7 @@ type GraphView struct {
// Vertical scrolling: when the clusters need more height than the widget
// has, scrollY (px, >= 0) is how far the content is moved up, contentH its
// full height and viewH the height it shows in (the bounds minus the legend).
laidNodeW int // the node width the layout was computed for
scrollY, contentH, viewH int
hasBar bool
sbDrag scrollDrag
Expand Down Expand Up @@ -167,17 +172,27 @@ const (
graphHitSlack = 3
)

// nodeW is the width of a node box.
func (g *GraphView) nodeW() int {
if g.NodeWidth > 0 {
return g.NodeWidth
}
return graphNodeW
}

// layout places clusters on a grid filling the bounds and nodes on a sub-grid
// inside each cluster. Deterministic: the same lists and bounds give the
// same boxes.
func (g *GraphView) layout() {
g.bind()
if g.layoutValid {
nw := g.nodeW()
if g.layoutValid && g.laidNodeW == nw {
return
}
r := g.Bounds()
g.layoutFor = r
g.layoutValid = true
g.laidNodeW = nw
g.clusterBox = map[string]Rect{}
g.nodeBox = map[string]Rect{}
g.order = g.order[:0]
Expand All @@ -204,10 +219,10 @@ func (g *GraphView) layout() {
// A cluster is at least as wide as its title, so a long label never
// spills over the next cluster.
titleW := func(c GraphCluster) int { return g.textWidth(c.Label) + 2*graphGap }
boxes, perRows, h := flowClusters(clusters, members, area.W, titleW)
boxes, perRows, h := flowClusters(clusters, members, area.W, nw, titleW)
if h > area.H {
g.hasBar = true
boxes, perRows, h = flowClusters(clusters, members, area.W-scrollbarWidth, titleW)
boxes, perRows, h = flowClusters(clusters, members, area.W-scrollbarWidth, nw, titleW)
}
g.contentH = h
g.scrollY = clampInt(g.scrollY, 0, maxInt(0, h-area.H))
Expand All @@ -217,8 +232,8 @@ func (g *GraphView) layout() {
g.clusterBox[c.ID] = box
for j, n := range members[c.ID] {
nc, nr := j%perRows[i], j/perRows[i]
g.nodeBox[n.ID] = Rect{X: box.X + graphGap + nc*(graphNodeW+graphGap),
Y: box.Y + graphTitleH + graphGap + nr*(graphNodeH+graphGap), W: graphNodeW, H: graphNodeH}
g.nodeBox[n.ID] = Rect{X: box.X + graphGap + nc*(nw+graphGap),
Y: box.Y + graphTitleH + graphGap + nr*(graphNodeH+graphGap), W: nw, H: graphNodeH}
g.order = append(g.order, n.ID)
}
}
Expand All @@ -228,13 +243,13 @@ func (g *GraphView) layout() {
// to right in the width, wrapping when the next one does not fit. It returns
// the boxes relative to the content origin, each cluster's nodes per row, and
// the content height.
func flowClusters(clusters []GraphCluster, members map[string][]GraphNode, width int, titleW func(GraphCluster) int) (boxes []Rect, perRows []int, height int) {
func flowClusters(clusters []GraphCluster, members map[string][]GraphNode, width, nodeW int, titleW func(GraphCluster) int) (boxes []Rect, perRows []int, height int) {
x, y, rowH := graphGap, graphGap, 0
for _, c := range clusters {
n := maxInt(1, len(members[c.ID]))
perRow := int(math.Ceil(math.Sqrt(float64(n))))
rowsIn := (n + perRow - 1) / perRow
cw := maxInt(perRow*(graphNodeW+graphGap)+graphGap, titleW(c))
cw := maxInt(perRow*(nodeW+graphGap)+graphGap, titleW(c))
ch := graphTitleH + rowsIn*(graphNodeH+graphGap) + graphGap
if x > graphGap && x+cw+graphGap > width {
x, y, rowH = graphGap, y+rowH+graphGap, 0
Expand Down Expand Up @@ -417,7 +432,11 @@ func (g *GraphView) Draw(p painter.Painter, theme *Theme) {
if luminance(col) < 0.4 {
ink = RGB(0xFF, 0xFF, 0xFF)
}
g.drawText(p, box.X+3, box.Y+(box.H-g.glyphHeight())/2, n.Label, ink)
// The label is clipped to its box: a long one must not run into the
// node beside it.
withClip(p, box, func() {
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)
}
Expand Down
69 changes: 69 additions & 0 deletions graphview_nodewidth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2026 the go-widgets/toolkit authors. All rights reserved.
// Use of this source code is governed by a BSD-3-Clause license that can be
// found in the LICENSE file at the root of this repository.

package toolkit

import "testing"

func TestGraphViewNodeWidth(t *testing.T) {
m := manyClusters(1, 4)
g := NewGraphView(m)
g.SetBounds(Rect{W: 800, H: 300})
b0, _ := g.NodeBox("n0_0")
if b0.W != graphNodeW {
t.Fatalf("default node width %d, want %d", b0.W, graphNodeW)
}
box72 := g.clusterBox["c0"]
// Changing the width after the first layout re-lays out.
g.NodeWidth = 150
b1, _ := g.NodeBox("n0_0")
if b1.W != 150 {
t.Fatalf("node width %d after NodeWidth=150", b1.W)
}
if g.clusterBox["c0"].W <= box72.W {
t.Fatalf("the cluster did not widen: %v -> %v", box72, g.clusterBox["c0"])
}
// Nodes sit side by side without overlapping.
b2, _ := g.NodeBox("n0_1")
if b2.X < b1.X+b1.W {
t.Fatalf("nodes overlap: %v %v", b1, b2)
}
}

// A label wider than its node must not paint outside the node box.
func TestGraphViewLabelIsClippedToItsNode(t *testing.T) {
const w, h = 420, 120
draw := func(label string) []byte {
m := NewGraphViewModel()
m.Clusters.Append(GraphCluster{ID: "c", Label: "c"})
m.Nodes.Append(GraphNode{ID: "a", Label: label, Cluster: "c", Category: "k"}, GraphNode{ID: "b", Label: "", Cluster: "c", Category: "k"})
g := NewGraphView(m)
g.SetBounds(Rect{W: w, H: h})
surf := makeSurface(w, h)
g.Draw(newP(surf, w), DefaultLight())
return surf
}
short, long := draw("a"), draw("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW")
m := NewGraphViewModel()
m.Clusters.Append(GraphCluster{ID: "c", Label: "c"})
m.Nodes.Append(GraphNode{ID: "a", Cluster: "c"}, GraphNode{ID: "b", Cluster: "c"})
g := NewGraphView(m)
g.SetBounds(Rect{W: w, H: h})
a, _ := g.NodeBox("a")
differs := false
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
if pixelAt(short, w, x, y) == pixelAt(long, w, x, y) {
continue
}
if x < a.X || x >= a.X+a.W || y < a.Y || y >= a.Y+a.H {
t.Fatalf("the long label painted outside its node %v at (%d,%d)", a, x, y)
}
differs = true
}
}
if !differs {
t.Fatal("the long label painted nothing")
}
}
2 changes: 1 addition & 1 deletion mvvm_gate_test.go
Original file line number Diff line number Diff line change
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},
"GraphView": {"Model": true, "Palette": true, "MaxEdgeWidth": true, "ShowLegend": true, "NodeWidth": true},
}

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