From 6ef3f0ca513acf7724e3ac7f1a816b63b92c07e5 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Wed, 18 Nov 2026 15:10:59 +0100 Subject: [PATCH] Table clips cells and selects on a plain click; GraphView sizes clusters by their own members and scrolls Table: cell text is clipped to its column; without MultiSelect a body click selects the row. GraphView: per-cluster sizing, row flow, scrollbar, wheel and keyboard reveal when the clusters overflow the view. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 2 +- graphview.go | 146 ++++++++++++++++++++++++++---- graphview_scroll_test.go | 189 +++++++++++++++++++++++++++++++++++++++ table.go | 12 ++- table_clip_test.go | 38 ++++++++ table_test.go | 25 ++++-- 6 files changed, 384 insertions(+), 28 deletions(-) create mode 100644 graphview_scroll_test.go create mode 100644 table_clip_test.go diff --git a/README.md b/README.md index 68dfd8a3..57af4959 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ every native target Go ships. | Text compare | `DiffView` (two texts compared side by side or inline: the changed words of a changed line marked (`WordChanges`), long lines wrapped with both sides level so nothing is cut, line numbers of both, unchanged runs folded around `Context` lines, an overview ruler of the changes, ↑/↓/PageUp/PageDown/Home/End, F7/Shift+F7 or n/p to the next/previous change (outlined); colours from the theme's darkness, text at 4.5:1 and markers at 3:1 in both default themes, +/- marks so it reads without colour); `textdiff` (a pure package, no widget): the shortest line edit script (Myers O(ND), linear space, a work budget past which a block is replaced whole), `Hunks` with context, `Unified` (diff -u), `SideBySide` rows; fuzzed | | Document | `PagedView` (a page viewer over pre-rendered bitmaps — or over sizes alone, with a host drawing the pages through `PageRect`: `SetPageSizes` lays out, cards and all, what it will not blit; `SetPageBitmap` fills one page in afterwards, so a viewer of a five-hundred-page document lays out all of them while holding bitmaps for a window around the reader, and a coarse stand-in can be replaced by a sharp rendering without the page changing shape): a continuous stack of page cards or one centred page with prev/next, its own toolbar, uniform zoom, keyboard and wheel navigation (a wheel notch past a page's edge flips, so a tall page scrolls within itself first), and two **sticky** fits — `PagedFitWidth` and `PagedFitPage` — re-derived on every resize, page change and new document, because a page fitted once and then resized is a page that no longer fits; a manual zoom turns the fit off. `PageAt` and `ScrollToPage` are an exact inverse pair, which is what turns a click on a page into a source position and a caret move back into a scroll. `TextSelection` is the selection over a host's text runs (press, drag, `SelectedText`, `CopySelection`): `SelectedRects` answers *where* the selection is, as the rectangles `Draw` paints — by construction, since `Draw` is a loop over it, so a host that writes a PDF highlight annotation over the selected words marks exactly what the reader sees lit, instead of re-deriving the covered spans and drifting | | Charts | `LineChart`, `BarChart`, `PieChart`, `AreaChart`, `ScatterChart`, `RadarChart`, `Gauge`, `Sparkline` | -| Graphs | `GraphView` over a `GraphViewModel` (clusters, nodes, edges, selection and filter as `mvvm` lists and observables): nodes in cluster boxes on a stable grid that never moves on an edge update, edge width by weight, denied edges dashed in the error colour, category colours (Okabe-Ito by default), legend, click and arrow-key selection, filter dimming | +| Graphs | `GraphView` over a `GraphViewModel` (clusters, nodes, edges, selection and filter as `mvvm` lists and observables): nodes in cluster boxes, each sized by its own members and flowed in rows on a stable layout that never moves on an edge update, a scrollbar, wheel and arrow-key reveal when the clusters overflow the view, edge width by weight, denied edges dashed in the error colour, category colours (Okabe-Ito by default), legend, click and arrow-key selection, filter dimming | | Shell | `StatusIcon`, `StatusArea`, `Wallpaper`, `Thumbnail` | | Motion | `Easing`/`Tween` (`Linear`, `EaseIn*/Out*/InOut*`), `GestureRecognizer` | | Theming | `LoadGTKTheme(css)` (GTK3 + libadwaita @define-color → Theme) | diff --git a/graphview.go b/graphview.go index 2123bfc3..498a6f8d 100644 --- a/graphview.go +++ b/graphview.go @@ -108,6 +108,13 @@ type GraphView struct { categories map[string]int unsub []func() bound *GraphViewModel + + // 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). + scrollY, contentH, viewH int + hasBar bool + sbDrag scrollDrag } // NewGraphView returns a GraphView over m (a new empty model when nil). @@ -177,35 +184,36 @@ func (g *GraphView) layout() { clusters := g.Model.Clusters.Slice() nodes := g.Model.Nodes.Slice() members := map[string][]GraphNode{} - most := 1 for _, n := range nodes { members[n.Cluster] = append(members[n.Cluster], n) - if k := len(members[n.Cluster]); k > most { - most = k - } } + g.contentH, g.viewH, g.hasBar = 0, 0, false if len(clusters) == 0 { + g.scrollY = 0 return } area := r if g.ShowLegend { area.H -= g.legendHeight() } - // Nodes per row inside a cluster, then cluster columns to fill the width. - perRow := int(math.Ceil(math.Sqrt(float64(most)))) - rowsIn := (most + perRow - 1) / perRow - cw := perRow*(graphNodeW+graphGap) + graphGap - ch := graphTitleH + rowsIn*(graphNodeH+graphGap) + graphGap - cols := maxInt(1, (area.W-graphGap)/(cw+graphGap)) - if cols > len(clusters) { - cols = len(clusters) - } + g.viewH = area.H + // Each cluster is sized by its OWN members and the clusters flow left to + // right, wrapping to a new row. When the rows need more height than the + // widget has, a scrollbar takes its column and the rows are flowed again in + // what is left of the width. + boxes, perRows, h := flowClusters(clusters, members, area.W) + if h > area.H { + g.hasBar = true + boxes, perRows, h = flowClusters(clusters, members, area.W-scrollbarWidth) + } + g.contentH = h + g.scrollY = clampInt(g.scrollY, 0, maxInt(0, h-area.H)) for i, c := range clusters { - col, row := i%cols, i/cols - box := Rect{X: area.X + graphGap + col*(cw+graphGap), Y: area.Y + graphGap + row*(ch+graphGap), W: cw, H: ch} + b := boxes[i] + box := Rect{X: area.X + b.X, Y: area.Y + b.Y - g.scrollY, W: b.W, H: b.H} g.clusterBox[c.ID] = box for j, n := range members[c.ID] { - nc, nr := j%perRow, j/perRow + 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.order = append(g.order, n.ID) @@ -213,6 +221,91 @@ func (g *GraphView) layout() { } } +// flowClusters sizes each cluster by its own member count and flows them left +// 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) (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 := perRow*(graphNodeW+graphGap) + graphGap + ch := graphTitleH + rowsIn*(graphNodeH+graphGap) + graphGap + if x > graphGap && x+cw+graphGap > width { + x, y, rowH = graphGap, y+rowH+graphGap, 0 + } + boxes = append(boxes, Rect{X: x, Y: y, W: cw, H: ch}) + perRows = append(perRows, perRow) + x += cw + graphGap + rowH = maxInt(rowH, ch) + } + return boxes, perRows, y + rowH + graphGap +} + +// graphScrollStep is the pixels one wheel row moves the content. +const graphScrollStep = graphNodeH + graphGap + +// graphThumbMin is the smallest scrollbar thumb, in pixels. +const graphThumbMin = 12 + +// scrollbarGeom is the vertical scrollbar's widget-local geometry, and whether +// it is live (the clusters overflow the view). +func (g *GraphView) scrollbarGeom() (sbGeom, bool) { + g.layout() + if !g.hasBar { + return sbGeom{}, false + } + r := g.Bounds() + maxScroll := g.contentH - g.viewH + thumb := maxInt(graphThumbMin, g.viewH*g.viewH/g.contentH) + return sbGeom{ + cross0: r.W - scrollbarWidth, + trackStart: 0, + trackLen: g.viewH, + thumbStart: g.scrollY * (g.viewH - thumb) / maxScroll, + thumbLen: thumb, + travelNum: g.viewH - thumb, + travelDen: maxScroll, + maxScroll: maxScroll, + }, true +} + +// ScrollTo scrolls the content so px pixels of it are above the view, clamped. +func (g *GraphView) ScrollTo(px int) { + g.layout() + px = clampInt(px, 0, maxInt(0, g.contentH-g.viewH)) + if px != g.scrollY { + g.scrollY = px + g.layoutValid = false + } +} + +// ScrollBy scrolls by delta pixels. +func (g *GraphView) ScrollBy(delta int) { + g.layout() + g.ScrollTo(g.scrollY + delta) +} + +// ScrollY is how far the content is scrolled, in pixels. +func (g *GraphView) ScrollY() int { return g.scrollY } + +// revealNode scrolls just enough to bring the node into the view. +func (g *GraphView) revealNode(id string) { + b, ok := g.NodeBox(id) + if !ok { + return + } + top := g.Bounds().Y + switch { + case b.Y < top: + g.ScrollBy(b.Y - top - graphGap) + case b.Y+b.H > top+g.viewH: + g.ScrollBy(b.Y + b.H - (top + g.viewH) + graphGap) + } +} + // NodeBox is the box a node is drawn in (ok=false when it is not drawn), for // hosts and tests. func (g *GraphView) NodeBox(id string) (Rect, bool) { @@ -327,8 +420,15 @@ func (g *GraphView) Draw(p painter.Painter, theme *Theme) { } } if g.ShowLegend { + // Clusters scrolled under the legend must not show through it. + h := g.legendHeight() + fillRect(p, r.X, r.Y+r.H-h, r.W, h, theme.Background) g.drawLegend(p, theme) } + if sg, ok := g.scrollbarGeom(); ok { + paintScrollTrack(p, theme, r.X+sg.cross0, r.Y+sg.trackStart, scrollbarWidth, sg.trackLen) + paintScrollThumb(p, theme, r.X+sg.cross0, r.Y+sg.thumbStart, scrollbarWidth, sg.thumbLen) + } }) g.drawFocusRing(p, theme, r) } @@ -527,7 +627,17 @@ func (g *GraphView) OnEvent(ev Event) { } r := g.Bounds() switch ev.Kind { + case EventScroll: + g.ScrollBy(ev.Delta * graphScrollStep) + case EventMouseDrag: + sg, ok := g.scrollbarGeom() + g.sbDrag.drag(sg, ok, ev, g.ScrollTo) + case EventMouseUp: + g.sbDrag.release() case EventClick: + if sg, ok := g.scrollbarGeom(); g.sbDrag.press(sg, ok, ev, g.viewH, g.ScrollBy) { + return + } hit := g.Hit(r.X+ev.X, r.Y+ev.Y) if hit != "" && hit == g.Model.Selected.Get() { g.activate() @@ -578,7 +688,9 @@ func (g *GraphView) key(code string) { if cur < 0 { next = 0 } - g.Model.Selected.Set("node:" + g.order[clampInt(next, 0, len(g.order)-1)]) + id := g.order[clampInt(next, 0, len(g.order)-1)] + g.Model.Selected.Set("node:" + id) + g.revealNode(id) } // A11y describes the graph: how many nodes and edges, how many denied, and diff --git a/graphview_scroll_test.go b/graphview_scroll_test.go new file mode 100644 index 00000000..c64044df --- /dev/null +++ b/graphview_scroll_test.go @@ -0,0 +1,189 @@ +// 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 ( + "strconv" + "testing" +) + +// manyClusters is n clusters of k nodes each, ids c and n_. +func manyClusters(n, k int) *GraphViewModel { + m := NewGraphViewModel() + for i := 0; i < n; i++ { + c := "c" + strconv.Itoa(i) + m.Clusters.Append(GraphCluster{ID: c, Label: c}) + for j := 0; j < k; j++ { + m.Nodes.Append(GraphNode{ID: "n" + strconv.Itoa(i) + "_" + strconv.Itoa(j), Label: "x", Cluster: c, Category: "k"}) + } + } + return m +} + +func TestGraphViewClustersAreSizedByTheirOwnMembers(t *testing.T) { + m := NewGraphViewModel() + m.Clusters.Append(GraphCluster{ID: "big", Label: "big"}, GraphCluster{ID: "small", Label: "small"}, GraphCluster{ID: "empty", Label: "empty"}) + for i := 0; i < 9; i++ { + m.Nodes.Append(GraphNode{ID: "b" + strconv.Itoa(i), Cluster: "big"}) + } + m.Nodes.Append(GraphNode{ID: "s", Cluster: "small"}) + g := NewGraphView(m) + g.SetBounds(Rect{W: 600, H: 400}) + g.layout() + big, small, empty := g.clusterBox["big"], g.clusterBox["small"], g.clusterBox["empty"] + if small.W >= big.W || small.H >= big.H { + t.Fatalf("a one-node cluster %v is not smaller than a nine-node one %v", small, big) + } + if empty.W != small.W || empty.H != small.H { + t.Fatalf("an empty cluster is sized like a one-node one: %v vs %v", empty, small) + } + if g.hasBar { + t.Fatal("a graph that fits has no scrollbar") + } +} + +func TestGraphViewScrollsWhenItOverflows(t *testing.T) { + g := NewGraphView(manyClusters(30, 4)) + g.ShowLegend = true + g.SetBounds(Rect{X: 5, Y: 7, W: 300, H: 150}) + g.layout() + if !g.hasBar || g.contentH <= g.viewH || g.ScrollY() != 0 { + t.Fatalf("hasBar %v contentH %d viewH %d scroll %d", g.hasBar, g.contentH, g.viewH, g.ScrollY()) + } + last := "n29_3" + if b, _ := g.NodeBox(last); b.Y < g.Bounds().Y+g.viewH { + t.Fatalf("the last node %v is already in view", b) + } + // Scrolling up past the top stays at the top. + g.ScrollBy(-100) + if g.ScrollY() != 0 { + t.Fatal("scrolled above the top") + } + // The wheel moves it, in rows. + g.OnEvent(Event{Kind: EventScroll, Delta: 3}) + if g.ScrollY() != 3*graphScrollStep { + t.Fatalf("wheel: scroll %d", g.ScrollY()) + } + // Scrolling past the end clamps, and then the last node is reachable. + g.ScrollTo(1 << 20) + if g.ScrollY() != g.contentH-g.viewH { + t.Fatalf("not clamped: %d of %d", g.ScrollY(), g.contentH-g.viewH) + } + b, _ := g.NodeBox(last) + if r := g.Bounds(); b.Y < r.Y || b.Y+b.H > r.Y+g.viewH { + t.Fatalf("the last node %v is not in the view after scrolling to the end", b) + } + // Scrolling to where it already is changes nothing. + g.ScrollTo(g.ScrollY()) + + // A click on a node that is scrolled into view selects it. + g.OnEvent(Event{Kind: EventClick, X: b.X - g.Bounds().X + 2, Y: b.Y - g.Bounds().Y + 2}) + if g.Model.Selected.Get() != "node:"+last { + t.Fatalf("selected %q", g.Model.Selected.Get()) + } + + // Drawing with the bar and the legend paints the bar and nothing under the legend. + surf := makeSurface(320, 180) + g.Draw(newP(surf, 320), DefaultLight()) + sg, ok := g.scrollbarGeom() + if !ok { + t.Fatal("no scrollbar geometry") + } + if pixelAt(surf, 320, g.Bounds().X+sg.cross0+2, g.Bounds().Y+sg.thumbStart+sg.thumbLen/2) == pixelAt(surf, 320, g.Bounds().X+sg.cross0-30, g.Bounds().Y+g.viewH/2) { + t.Log("thumb and content share a colour; geometry still asserted below") + } + if sg.thumbLen < graphThumbMin || sg.thumbStart+sg.thumbLen > sg.trackLen { + t.Fatalf("thumb %d+%d in a track of %d", sg.thumbStart, sg.thumbLen, sg.trackLen) + } +} + +func TestGraphViewScrollbarThumbTrackAndRelease(t *testing.T) { + g := NewGraphView(manyClusters(30, 4)) + g.SetBounds(Rect{W: 300, H: 150}) + g.layout() + sg, ok := g.scrollbarGeom() + if !ok { + t.Fatal("no scrollbar") + } + x := sg.cross0 + 2 + // A press on the track below the thumb pages down, and is not a node click. + g.OnEvent(Event{Kind: EventClick, X: x, Y: sg.trackLen - 2}) + if g.ScrollY() != g.viewH || g.Model.Selected.Get() != "" { + t.Fatalf("page down: scroll %d selected %q", g.ScrollY(), g.Model.Selected.Get()) + } + // And above the thumb pages back up. + sg, _ = g.scrollbarGeom() + g.OnEvent(Event{Kind: EventClick, X: x, Y: 0}) + if g.ScrollY() != 0 { + t.Fatalf("page up: scroll %d", g.ScrollY()) + } + // Grab the thumb, drag it to the bottom, release. + sg, _ = g.scrollbarGeom() + g.OnEvent(Event{Kind: EventClick, X: x, Y: sg.thumbStart + 1}) + if !g.sbDrag.active { + t.Fatal("the thumb was not grabbed") + } + g.OnEvent(Event{Kind: EventMouseDrag, X: x, Y: sg.trackLen + 50}) + if g.ScrollY() != g.contentH-g.viewH { + t.Fatalf("drag to the end: scroll %d", g.ScrollY()) + } + g.OnEvent(Event{Kind: EventMouseUp}) + if g.sbDrag.active { + t.Fatal("release did not end the drag") + } + // A drag with nothing grabbed does nothing. + g.OnEvent(Event{Kind: EventMouseDrag, X: x, Y: 0}) + if g.ScrollY() != g.contentH-g.viewH { + t.Fatal("an ungrabbed drag scrolled") + } +} + +func TestGraphViewKeyboardSelectionScrollsIntoView(t *testing.T) { + g := NewGraphView(manyClusters(30, 4)) + g.SetBounds(Rect{X: 3, Y: 4, W: 300, H: 150}) + g.layout() + g.Model.Selected.Set("node:n29_2") + g.OnEvent(Event{Kind: EventKeyDown, Code: "ArrowRight"}) // to n29_3, far below + b, _ := g.NodeBox("n29_3") + if r := g.Bounds(); b.Y+b.H > r.Y+g.viewH || b.Y < r.Y { + t.Fatalf("the selected node %v is not in the view", b) + } + if g.ScrollY() == 0 { + t.Fatal("the view did not scroll down") + } + g.Model.Selected.Set("node:n0_1") + g.OnEvent(Event{Kind: EventKeyDown, Code: "ArrowLeft"}) // to n0_0, far above + if b0, _ := g.NodeBox("n0_0"); b0.Y < g.Bounds().Y { + t.Fatalf("the node %v is above the view: scroll %d", b0, g.ScrollY()) + } + g.revealNode("nowhere") // an unknown node is a no-op +} + +func TestGraphViewScrollOnAnEmptyOrFittingGraph(t *testing.T) { + g := NewGraphView(nil) + g.SetBounds(Rect{W: 100, H: 50}) + g.ScrollBy(500) + if g.ScrollY() != 0 { + t.Fatal("an empty graph scrolled") + } + g.OnEvent(Event{Kind: EventScroll, Delta: 3}) + g.OnEvent(Event{Kind: EventClick, X: 99, Y: 5}) // no scrollbar to press + h := NewGraphView(classGraph()) + h.SetBounds(Rect{W: 400, H: 200}) + h.OnEvent(Event{Kind: EventScroll, Delta: 5}) + if h.ScrollY() != 0 { + t.Fatal("a graph that fits scrolled") + } + // Shrinking the bounds keeps a scroll position that is still legal and + // pulls back one that no longer is. + big := NewGraphView(manyClusters(30, 4)) + big.SetBounds(Rect{W: 300, H: 150}) + big.ScrollTo(1 << 20) + big.SetBounds(Rect{W: 300, H: 600}) + big.layout() + if big.ScrollY() > big.contentH-big.viewH { + t.Fatalf("scroll %d beyond %d", big.ScrollY(), big.contentH-big.viewH) + } +} diff --git a/table.go b/table.go index 1447ae4c..2b899657 100644 --- a/table.go +++ b/table.go @@ -965,7 +965,11 @@ func (t *Table) paintCell(p painter.Painter, widths []int, gutter, cellX, cty, y cellW -= gutter } if j < len(row) { - t.drawText(p, cellTextX(&t.Base, cellX, cellW, row[j], t.Columns[j].Align), cty, row[j], ink) + // The text is clipped to its own cell: a string wider than its column + // must not paint over the next one. + withClip(p, Rect{X: cellX, Y: y, W: cellW, H: t.rowH()}, func() { + t.drawText(p, cellTextX(&t.Base, cellX, cellW, row[j], t.Columns[j].Align), cty, row[j], ink) + }) } } @@ -2720,10 +2724,12 @@ func (t *Table) OnEvent(ev Event) { if t.reorderActive() && row >= 0 { t.dragRow = row } - if !t.MultiSelect { + if row < 0 { return } - if row < 0 { + if !t.MultiSelect { + // Single-select: a plain click selects the row it lands on. + t.setSelected(row) return } switch { diff --git a/table_clip_test.go b/table_clip_test.go new file mode 100644 index 00000000..49189006 --- /dev/null +++ b/table_clip_test.go @@ -0,0 +1,38 @@ +// 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" + +// A cell whose text is wider than its column must not paint into the next +// column: the next column's pixels are the same whether the first cell holds +// a short text or a very long one. +func TestTableCellTextIsClippedToItsColumn(t *testing.T) { + const w, h = 160, 90 + draw := func(first string) []byte { + tb := NewTable([]TableColumn{{Title: "A", Width: 40}, {Title: "B", Width: 100}}, + [][]string{{first, ""}, {first, ""}}) + tb.SetBounds(Rect{W: w, H: h}) + surf := makeSurface(w, h) + tb.Draw(newP(surf, w), DefaultLight()) + return surf + } + short, long := draw("a"), draw("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW") + differs := false + for y := TableHeaderHeight; y < h; y++ { + for x := 0; x < w; x++ { + if pixelAt(short, w, x, y) == pixelAt(long, w, x, y) { + continue + } + if x >= 40 { + t.Fatalf("the long text painted outside its column at (%d,%d)", x, y) + } + differs = true + } + } + if !differs { + t.Fatal("the long text painted nothing inside its own column") + } +} diff --git a/table_test.go b/table_test.go index d56f3c18..bb6f6640 100644 --- a/table_test.go +++ b/table_test.go @@ -915,19 +915,28 @@ func TestSelectRowRangeBothDirections(t *testing.T) { // --- Multi-row selection: OnEvent body-row clicks ----------------------- -func TestMultiSelectDisabledBodyClickIsNoOp(t *testing.T) { - // MultiSelect defaults false -- a body-row click must be a total - // no-op, exactly matching pre-MultiSelect behaviour. +func TestSingleSelectBodyClickSelectsTheRow(t *testing.T) { + // MultiSelect defaults false -- a body-row click selects that row (the + // cursor), and builds no multi-row set. tb := NewTable([]TableColumn{{Title: "A", Width: 100}}, [][]string{{"r0"}, {"r1"}}) tb.SetBounds(Rect{X: 0, Y: 0, W: 100, H: 200}) tb.Selected().Set(-1) tb.OnEvent(Event{Kind: EventClick, X: 10, Y: TableHeaderHeight + 5}) - if tb.Selected().Get() != -1 { - t.Fatalf("Selected mutated by body click w/o MultiSelect: %d", tb.Selected().Get()) + if tb.Selected().Get() != 0 { + t.Fatalf("body click w/o MultiSelect left Selected at %d, want 0", tb.Selected().Get()) + } + tb.OnEvent(Event{Kind: EventClick, X: 10, Y: TableHeaderHeight + TableRowHeight + 5}) + if tb.Selected().Get() != 1 { + t.Fatalf("second click: Selected %d, want 1", tb.Selected().Get()) } if got := tb.SelectedRows(); got != nil { - t.Fatalf("SelectedRows mutated by body click w/o MultiSelect: %v", got) + t.Fatalf("SelectedRows built by body click w/o MultiSelect: %v", got) + } + // A click below the last row selects nothing and leaves the cursor. + tb.OnEvent(Event{Kind: EventClick, X: 10, Y: 190}) + if tb.Selected().Get() != 1 { + t.Fatalf("click on empty space moved Selected to %d", tb.Selected().Get()) } } @@ -1761,13 +1770,15 @@ func TestTableReorderDropMovesRowUpAndShiftsIntermediateSelection(t *testing.T) tb := NewTable([]TableColumn{{Title: "A", Width: 100}}, [][]string{{"r0"}, {"r1"}, {"r2"}, {"r3"}, {"r4"}}) tb.Reorderable = true - tb.Selected().Set(2) // an intermediate row, in the shifted range [to, from) tb.SetBounds(Rect{X: 0, Y: 0, W: 100, H: 5*TableRowHeight + TableHeaderHeight}) tb.OnEvent(Event{Kind: EventClick, X: 10, Y: tableBodyClickY(4)}) if got := tb.DragData(); got != "tablerow:4" { t.Fatalf("DragData() = %q, want %q", got, "tablerow:4") } + // The press selected row 4; the selection under test is an intermediate + // row, in the shifted range [to, from). + tb.Selected().Set(2) var gotFrom, gotTo int tb.OnReorder = func(from, to int) { gotFrom, gotTo = from, to }