ui/text,ui/layout: move ui.Config and input.Queue to parameters

I too often forget to initialize widgets' config and queue. Moving
them from fields to parameters fix that. The change results in a
little more verbosity but cleaner code.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-07-21 11:44:48 +02:00
parent 6c0850ce60
commit b6290990ad
3 changed files with 31 additions and 30 deletions
+7 -5
View File
@@ -18,8 +18,6 @@ type scrollChild struct {
} }
type List struct { type List struct {
Config ui.Config
Inputs input.Queue
Axis Axis Axis Axis
Invert bool Invert bool
CrossAxisAlignment CrossAxisAlignment CrossAxisAlignment CrossAxisAlignment
@@ -27,9 +25,11 @@ type List struct {
// The distance scrolled since last call to Init. // The distance scrolled since last call to Init.
Distance int Distance int
config ui.Config
ops *ui.Ops
queue input.Queue
macro ui.MacroOp macro ui.MacroOp
child ui.MacroOp child ui.MacroOp
ops *ui.Ops
scroll gesture.Scroll scroll gesture.Scroll
scrollDir int scrollDir int
@@ -57,10 +57,12 @@ const (
) )
// Init prepares the list for iterating through its elements with Next. // Init prepares the list for iterating through its elements with Next.
func (l *List) Init(ops *ui.Ops, cs Constraints, len int) { func (l *List) Init(cfg ui.Config, q input.Queue, ops *ui.Ops, cs Constraints, len int) {
if l.more { if l.more {
panic("unfinished element") panic("unfinished element")
} }
l.config = cfg
l.queue = q
l.update() l.update()
l.ops = ops l.ops = ops
l.dir = iterateNone l.dir = iterateNone
@@ -82,7 +84,7 @@ func (l *List) Dragging() bool {
func (l *List) update() { func (l *List) update() {
l.Distance = 0 l.Distance = 0
d := l.scroll.Scroll(l.Config, l.Inputs, gesture.Axis(l.Axis)) d := l.scroll.Scroll(l.config, l.queue, gesture.Axis(l.Axis))
if l.Invert { if l.Invert {
d = -d d = -d
} }
+5 -4
View File
@@ -17,7 +17,7 @@ import (
) )
type Faces struct { type Faces struct {
Config ui.Config config ui.Config
faceCache map[faceKey]*textFace faceCache map[faceKey]*textFace
layoutCache map[layoutKey]cachedLayout layoutCache map[layoutKey]cachedLayout
pathCache map[pathKey]cachedPath pathCache map[pathKey]cachedPath
@@ -57,7 +57,8 @@ type textFace struct {
font *opentype font *opentype
} }
func (f *Faces) Frame() { func (f *Faces) Reset(c ui.Config) {
f.config = c
f.init() f.init()
for pk, p := range f.pathCache { for pk, p := range f.pathCache {
if !p.active { if !p.active {
@@ -102,7 +103,7 @@ func (f *Faces) init() {
} }
func (f *textFace) Layout(str string, opts text.LayoutOptions) *text.Layout { func (f *textFace) Layout(str string, opts text.LayoutOptions) *text.Layout {
ppem := fixed.Int26_6(f.faces.Config.Px(f.size) * 64) ppem := fixed.Int26_6(f.faces.config.Px(f.size) * 64)
lk := layoutKey{ lk := layoutKey{
f: f.font.Font, f: f.font.Font,
ppem: ppem, ppem: ppem,
@@ -120,7 +121,7 @@ func (f *textFace) Layout(str string, opts text.LayoutOptions) *text.Layout {
} }
func (f *textFace) Path(str text.String) ui.MacroOp { func (f *textFace) Path(str text.String) ui.MacroOp {
ppem := fixed.Int26_6(f.faces.Config.Px(f.size) * 64) ppem := fixed.Int26_6(f.faces.config.Px(f.size) * 64)
pk := pathKey{ pk := pathKey{
f: f.font.Font, f: f.font.Font,
ppem: ppem, ppem: ppem,
+19 -21
View File
@@ -21,8 +21,6 @@ import (
) )
type Editor struct { type Editor struct {
Config ui.Config
Inputs input.Queue
Face Face Face Face
Alignment Alignment Alignment Alignment
SingleLine bool SingleLine bool
@@ -72,9 +70,9 @@ const (
func (s ChangeEvent) isEditorEvent() {} func (s ChangeEvent) isEditorEvent() {}
func (s SubmitEvent) isEditorEvent() {} func (s SubmitEvent) isEditorEvent() {}
func (e *Editor) Next() (EditorEvent, bool) { func (e *Editor) Next(cfg ui.Config, queue input.Queue) (EditorEvent, bool) {
// Crude configuration change detection. // Crude configuration change detection.
if scale := e.Config.Px(ui.Sp(100)); scale != e.oldScale { if scale := cfg.Px(ui.Sp(100)); scale != e.oldScale {
e.invalidate() e.invalidate()
e.oldScale = scale e.oldScale = scale
} }
@@ -88,7 +86,7 @@ func (e *Editor) Next() (EditorEvent, bool) {
axis = gesture.Vertical axis = gesture.Vertical
smin, smax = sbounds.Min.Y, sbounds.Max.Y smin, smax = sbounds.Min.Y, sbounds.Max.Y
} }
sdist := e.scroller.Scroll(e.Config, e.Inputs, axis) sdist := e.scroller.Scroll(cfg, queue, axis)
var soff int var soff int
if e.SingleLine { if e.SingleLine {
e.scrollOff.X += sdist e.scrollOff.X += sdist
@@ -97,26 +95,26 @@ func (e *Editor) Next() (EditorEvent, bool) {
e.scrollOff.Y += sdist e.scrollOff.Y += sdist
soff = e.scrollOff.Y soff = e.scrollOff.Y
} }
for _, evt := range e.clicker.Events(e.Inputs) { for _, evt := range e.clicker.Events(queue) {
switch { switch {
case evt.Type == gesture.TypePress && evt.Source == pointer.Mouse, case evt.Type == gesture.TypePress && evt.Source == pointer.Mouse,
evt.Type == gesture.TypeClick && evt.Source == pointer.Touch: evt.Type == gesture.TypeClick && evt.Source == pointer.Touch:
e.blinkStart = e.Config.Now() e.blinkStart = cfg.Now()
e.moveCoord(image.Point{ e.moveCoord(image.Point{
X: int(math.Round(float64(evt.Position.X))), X: int(math.Round(float64(evt.Position.X))),
Y: int(math.Round(float64(evt.Position.Y))), Y: int(math.Round(float64(evt.Position.Y))),
}) })
e.requestFocus = true e.requestFocus = true
if !e.scroller.Active() { if !e.scroller.Active() {
e.scrollToCaret() e.scrollToCaret(cfg)
} }
} }
} }
if (sdist > 0 && soff >= smax) || (sdist < 0 && soff <= smin) { if (sdist > 0 && soff >= smax) || (sdist < 0 && soff <= smin) {
e.scroller.Stop() e.scroller.Stop()
} }
for _, ke := range e.Inputs.Events(e) { for _, ke := range queue.Events(e) {
e.blinkStart = e.Config.Now() e.blinkStart = cfg.Now()
switch ke := ke.(type) { switch ke := ke.(type) {
case key.FocusEvent: case key.FocusEvent:
e.focused = ke.Focus e.focused = ke.Focus
@@ -130,11 +128,11 @@ func (e *Editor) Next() (EditorEvent, bool) {
} }
} }
if e.command(ke) { if e.command(ke) {
e.scrollToCaret() e.scrollToCaret(cfg)
e.scroller.Stop() e.scroller.Stop()
} }
case key.EditEvent: case key.EditEvent:
e.scrollToCaret() e.scrollToCaret(cfg)
e.scroller.Stop() e.scroller.Stop()
e.append(ke.Text) e.append(ke.Text)
} }
@@ -145,8 +143,8 @@ func (e *Editor) Next() (EditorEvent, bool) {
return nil, false return nil, false
} }
func (e *Editor) caretWidth() fixed.Int26_6 { func (e *Editor) caretWidth(c ui.Config) fixed.Int26_6 {
oneDp := e.Config.Px(ui.Dp(1)) oneDp := c.Px(ui.Dp(1))
return fixed.Int26_6(oneDp * 64) return fixed.Int26_6(oneDp * 64)
} }
@@ -154,10 +152,10 @@ func (e *Editor) Focus() {
e.requestFocus = true e.requestFocus = true
} }
func (e *Editor) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens { func (e *Editor) Layout(cfg ui.Config, queue input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens {
for _, ok := e.Next(); ok; _, ok = e.Next() { for _, ok := e.Next(cfg, queue); ok; _, ok = e.Next(cfg, queue) {
} }
twoDp := e.Config.Px(ui.Dp(2)) twoDp := cfg.Px(ui.Dp(2))
e.padLeft, e.padRight = twoDp, twoDp e.padLeft, e.padRight = twoDp, twoDp
maxWidth := cs.Width.Max maxWidth := cs.Width.Max
if e.SingleLine { if e.SingleLine {
@@ -217,14 +215,14 @@ func (e *Editor) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens {
stack.Pop() stack.Pop()
} }
if e.focused { if e.focused {
now := e.Config.Now() now := cfg.Now()
dt := now.Sub(e.blinkStart) dt := now.Sub(e.blinkStart)
blinking := dt < maxBlinkDuration blinking := dt < maxBlinkDuration
const timePerBlink = time.Second / blinksPerSecond const timePerBlink = time.Second / blinksPerSecond
nextBlink := now.Add(timePerBlink/2 - dt%(timePerBlink/2)) nextBlink := now.Add(timePerBlink/2 - dt%(timePerBlink/2))
on := !blinking || dt%timePerBlink < timePerBlink/2 on := !blinking || dt%timePerBlink < timePerBlink/2
if on { if on {
carWidth := e.caretWidth() carWidth := e.caretWidth(cfg)
carX -= carWidth / 2 carX -= carWidth / 2
carAsc, carDesc := -lines[carLine].Bounds.Min.Y, lines[carLine].Bounds.Max.Y carAsc, carDesc := -lines[carLine].Bounds.Min.Y, lines[carLine].Bounds.Max.Y
carRect := image.Rectangle{ carRect := image.Rectangle{
@@ -530,8 +528,8 @@ func (e *Editor) moveEnd() {
e.carXOff = l.Width + a - x e.carXOff = l.Width + a - x
} }
func (e *Editor) scrollToCaret() { func (e *Editor) scrollToCaret(cfg ui.Config) {
carWidth := e.caretWidth() carWidth := e.caretWidth(cfg)
carLine, _, x, y := e.layoutCaret() carLine, _, x, y := e.layoutCaret()
l := e.lines[carLine] l := e.lines[carLine]
if e.SingleLine { if e.SingleLine {