From bfece0bebacb5cec4fdf5682b886333e03b6549b Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 1 Aug 2019 10:06:19 +0200 Subject: [PATCH] ui: change area ops to use rectangles, not sizes And then use the more general rectangles to add a buffer around text.Editor click and scroll area. Signed-off-by: Elias Naur --- ui/app/internal/input/pointer.go | 30 +++++++++++++++++++++--------- ui/internal/ops/ops.go | 2 +- ui/layout/list.go | 2 +- ui/pointer/pointer.go | 16 +++++++++------- ui/text/editor.go | 8 +++++++- 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/ui/app/internal/input/pointer.go b/ui/app/internal/input/pointer.go index 4f7b79eb..a6d6cd47 100644 --- a/ui/app/internal/input/pointer.go +++ b/ui/app/internal/input/pointer.go @@ -47,7 +47,7 @@ type pointerHandler struct { type areaOp struct { kind areaKind - size image.Point + rect image.Rectangle } type areaNode struct { @@ -268,28 +268,40 @@ func (op *areaOp) Decode(d []byte) { panic("invalid op") } bo := binary.LittleEndian - size := image.Point{ - X: int(bo.Uint32(d[2:])), - Y: int(bo.Uint32(d[6:])), + rect := image.Rectangle{ + Min: image.Point{ + X: int(int32(bo.Uint32(d[2:]))), + Y: int(int32(bo.Uint32(d[6:]))), + }, + Max: image.Point{ + X: int(int32(bo.Uint32(d[10:]))), + Y: int(int32(bo.Uint32(d[14:]))), + }, } *op = areaOp{ kind: areaKind(d[1]), - size: size, + rect: rect, } } func (op *areaOp) Hit(pos f32.Point) bool { + min := f32.Point{ + X: float32(op.rect.Min.X), + Y: float32(op.rect.Min.Y), + } + pos = pos.Sub(min) + size := op.rect.Size() switch op.kind { case areaRect: - if 0 <= pos.X && pos.X < float32(op.size.X) && - 0 <= pos.Y && pos.Y < float32(op.size.Y) { + if 0 <= pos.X && pos.X < float32(size.X) && + 0 <= pos.Y && pos.Y < float32(size.Y) { return true } else { return false } case areaEllipse: - rx := float32(op.size.X) / 2 - ry := float32(op.size.Y) / 2 + rx := float32(size.X) / 2 + ry := float32(size.Y) / 2 rx2 := rx * rx ry2 := ry * ry xh := pos.X - rx diff --git a/ui/internal/ops/ops.go b/ui/internal/ops/ops.go index abc8f581..0cb6fe02 100644 --- a/ui/internal/ops/ops.go +++ b/ui/internal/ops/ops.go @@ -35,7 +35,7 @@ const ( TypeImageLen = 1 + 4*4 TypeDrawLen = 1 + 4*4 TypeColorLen = 1 + 4 - TypeAreaLen = 1 + 1 + 2*4 + TypeAreaLen = 1 + 1 + 4*4 TypePointerHandlerLen = 1 + 1 TypePassLen = 1 + 1 TypeKeyHandlerLen = 1 + 1 diff --git a/ui/layout/list.go b/ui/layout/list.go index 2df153db..96c79558 100644 --- a/ui/layout/list.go +++ b/ui/layout/list.go @@ -243,7 +243,7 @@ func (l *List) Layout() Dimens { } dims := axisPoint(l.Axis, mainc.Constrain(pos), maxCross) l.macro.Stop() - pointer.RectAreaOp{Size: dims}.Add(ops) + pointer.RectAreaOp{Rect: image.Rectangle{Max: dims}}.Add(ops) l.scroll.Add(ops) l.macro.Add(ops) return Dimens{Size: dims} diff --git a/ui/pointer/pointer.go b/ui/pointer/pointer.go index f19331aa..92b43ab6 100644 --- a/ui/pointer/pointer.go +++ b/ui/pointer/pointer.go @@ -25,17 +25,17 @@ type Event struct { } type RectAreaOp struct { - Size image.Point + Rect image.Rectangle } type EllipseAreaOp struct { - Size image.Point + Rect image.Rectangle } // Must match the structure in input.areaOp type areaOp struct { kind areaKind - size image.Point + rect image.Rectangle } type HandlerOp struct { @@ -83,14 +83,14 @@ const ( func (op RectAreaOp) Add(ops *ui.Ops) { areaOp{ kind: areaRect, - size: op.Size, + rect: op.Rect, }.add(ops) } func (op EllipseAreaOp) Add(ops *ui.Ops) { areaOp{ kind: areaEllipse, - size: op.Size, + rect: op.Rect, }.add(ops) } @@ -99,8 +99,10 @@ func (op areaOp) add(o *ui.Ops) { data[0] = byte(ops.TypeArea) data[1] = byte(op.kind) bo := binary.LittleEndian - bo.PutUint32(data[2:], uint32(op.size.X)) - bo.PutUint32(data[6:], uint32(op.size.Y)) + bo.PutUint32(data[2:], uint32(op.rect.Min.X)) + bo.PutUint32(data[6:], uint32(op.rect.Min.Y)) + bo.PutUint32(data[10:], uint32(op.rect.Max.X)) + bo.PutUint32(data[14:], uint32(op.rect.Max.Y)) o.Write(data) } diff --git a/ui/text/editor.go b/ui/text/editor.go index 3659ee5b..327abfb7 100644 --- a/ui/text/editor.go +++ b/ui/text/editor.go @@ -248,7 +248,13 @@ func (e *Editor) Layout(cfg ui.Config, queue input.Queue, ops *ui.Ops, cs layout stack.Pop() baseline := e.padTop + e.dims.Baseline - pointer.RectAreaOp{Size: e.viewSize}.Add(ops) + pointerPadding := cfg.Px(ui.Dp(4)) + r := image.Rectangle{Max: e.viewSize} + r.Min.X -= pointerPadding + r.Min.Y -= pointerPadding + r.Max.X += pointerPadding + r.Max.X += pointerPadding + pointer.RectAreaOp{Rect: r}.Add(ops) e.scroller.Add(ops) e.clicker.Add(ops) return layout.Dimens{Size: e.viewSize, Baseline: baseline}