From a2642ea039de39465a4c061db93749f2afb089b4 Mon Sep 17 00:00:00 2001 From: Jakub Sobon Date: Sat, 23 Feb 2019 20:48:41 -0500 Subject: [PATCH] The LineChart now continuous to accept mouse events after terminal resize. And allowing it to receive global mouse events to be able to cancel highlight on clicks outside of the widget. --- widgets/linechart/linechart.go | 2 +- widgets/linechart/linechart_test.go | 59 +++++++++++++++++++++++++++++ widgets/linechart/zoom/zoom.go | 1 + widgets/linechart/zoom/zoom_test.go | 42 ++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/widgets/linechart/linechart.go b/widgets/linechart/linechart.go index 02f7931..3f4d7b1 100644 --- a/widgets/linechart/linechart.go +++ b/widgets/linechart/linechart.go @@ -492,7 +492,7 @@ func (lc *LineChart) Options() widgetapi.Options { return widgetapi.Options{ MinimumSize: lc.minSize(), - WantMouse: widgetapi.MouseScopeWidget, + WantMouse: widgetapi.MouseScopeGlobal, } } diff --git a/widgets/linechart/linechart_test.go b/widgets/linechart/linechart_test.go index c573642..08ec4cd 100644 --- a/widgets/linechart/linechart_test.go +++ b/widgets/linechart/linechart_test.go @@ -1303,6 +1303,65 @@ func TestLineChartDraws(t *testing.T) { return ft }, }, + { + desc: "tracks mouse clicks when canvas size increases, regression for #148", + opts: []Option{ + ZoomHightlightColor(cell.ColorNumber(13)), + }, + canvas: image.Rect(0, 0, 20, 10), + writes: func(lc *LineChart) error { + if err := lc.Series("first", []float64{0, 100}); err != nil { + return err + } + // Draw twice with different canvas size to simulate resize. + { + cvs := testcanvas.MustNew(image.Rect(0, 0, 20, 7)) + if err := lc.Draw(cvs); err != nil { + return err + } + } + { + cvs := testcanvas.MustNew(image.Rect(0, 0, 20, 10)) + if err := lc.Draw(cvs); err != nil { + return err + } + } + return lc.Mouse(&terminalapi.Mouse{ + Position: image.Point{6, 7}, + Button: mouse.ButtonLeft, + }) + }, + wantCapacity: 28, + want: func(size image.Point) *faketerm.Terminal { + ft := faketerm.MustNew(size) + c := testcanvas.MustNew(ft.Area()) + + // Y and X axis. + lines := []draw.HVLine{ + {Start: image.Point{5, 0}, End: image.Point{5, 8}}, + {Start: image.Point{5, 8}, End: image.Point{19, 8}}, + } + testdraw.MustHVLines(c, lines) + + // Value labels. + testdraw.MustText(c, "0", image.Point{4, 7}) + testdraw.MustText(c, "51.68", image.Point{0, 3}) + testdraw.MustText(c, "0", image.Point{6, 9}) + testdraw.MustText(c, "1", image.Point{19, 9}) + + // Braille line. + graphAr := image.Rect(6, 0, 20, 8) + bc := testbraille.MustNew(graphAr) + testdraw.MustBrailleLine(bc, image.Point{0, 31}, image.Point{26, 0}) + + // Highlighted area for zoom. + testbraille.MustSetAreaCellOpts(bc, image.Rect(0, 0, 1, 8), cell.BgColor(cell.ColorNumber(13))) + + testbraille.MustCopyTo(bc, c) + testcanvas.MustApply(c, ft) + return ft + }, + }, { desc: "zoom in on unscaled X axis", opts: []Option{ diff --git a/widgets/linechart/zoom/zoom.go b/widgets/linechart/zoom/zoom.go index cbedeac..861f999 100644 --- a/widgets/linechart/zoom/zoom.go +++ b/widgets/linechart/zoom/zoom.go @@ -136,6 +136,7 @@ func (t *Tracker) Update(baseX *axes.XDetails, cvsAr, graphAr image.Rectangle) e ac, sc := t.axisChanged(baseX), t.sizeChanged(cvsAr, graphAr) if sc { t.highlight.reset() + t.fsm.UpdateArea(graphAr) } if ac || sc { if t.zoomX != nil { diff --git a/widgets/linechart/zoom/zoom_test.go b/widgets/linechart/zoom/zoom_test.go index 2e04fdf..cc547f3 100644 --- a/widgets/linechart/zoom/zoom_test.go +++ b/widgets/linechart/zoom/zoom_test.go @@ -149,6 +149,48 @@ func TestTracker(t *testing.T) { }, ), }, + { + desc: "highlights single column in a new canvas portion after size increase, regression for #148", + xp: &axes.XProperties{ + Min: 0, + Max: 4, + ReqYWidth: 2, + }, + cvsAr: image.Rect(0, 0, 4, 4), + graphAr: image.Rect(2, 0, 4, 4), + mutate: func(tr *Tracker) error { + newX, err := axes.NewXDetails(image.Rect(0, 0, 6, 6), &axes.XProperties{ + Min: 0, + Max: 4, + ReqYWidth: 2, + }) + if err != nil { + return err + } + + if err := tr.Update( + newX, + image.Rect(0, 0, 6, 6), + image.Rect(2, 0, 6, 6), + ); err != nil { + return err + } + return tr.Mouse(&terminalapi.Mouse{ + Position: image.Point{2, 5}, + Button: mouse.ButtonLeft, + }) + }, + wantHighlight: true, + wantHighlightRange: &Range{Start: 0, End: 1, last: 0}, + wantZoom: mustNewXDetails( + image.Rect(0, 0, 6, 6), + &axes.XProperties{ + Min: 0, + Max: 4, + ReqYWidth: 2, + }, + ), + }, { desc: "highlights multiple columns to the right of start", xp: &axes.XProperties{