~whereswaldon/gio-x

ref: b0198a1b547035c76d814b60de9aec3fce6353a8 gio-x/component/text_field.go -rw-r--r-- 11.3 KiB
b0198a1bChris Waldon component: document and refactor tooltip types 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package component

import (
	"image"
	"image/color"
	"strconv"
	"time"

	"gioui.org/f32"
	"gioui.org/io/pointer"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/clip"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"
)

// TextField implements the Material Design Text Field
// described here: https://material.io/components/text-fields
type TextField struct {
	// Editor contains the edit buffer.
	widget.Editor
	// Hoverable detects mouse hovers.
	Hoverable Hoverable
	// Alignment specifies where to anchor the text.
	Alignment layout.Alignment

	// Helper text to give additional context to a field.
	Helper string
	// CharLimit specifies the maximum number of characters the text input
	// will allow. Zero means "no limit".
	CharLimit uint
	// Prefix appears before the content of the text input.
	Prefix layout.Widget
	// Suffix appears after the content of the text input.
	Suffix layout.Widget

	// Animation state.
	state
	label  label
	border border
	helper helper
	anim   *Progress

	// errored tracks whether the input is in an errored state.
	// This is orthogonal to the other states: the input can be both errored
	// and inactive for example.
	errored bool
}

// Validator validates text and returns a string describing the error.
// Error is displayed as helper text.
type Validator = func(string) string

type label struct {
	TextSize float32
	Inset    layout.Inset
	Smallest layout.Dimensions
}

type border struct {
	Thickness float32
	Color     color.NRGBA
}

type helper struct {
	Color color.NRGBA
	Text  string
}

type state int

const (
	inactive state = iota
	hovered
	activated
	focused
)

// IsActive if input is in an active state (Active, Focused or Errored).
func (in TextField) IsActive() bool {
	return in.state >= activated
}

// IsErrored if input is in an errored state.
// Typically this is when the validator has returned an error message.
func (in *TextField) IsErrored() bool {
	return in.errored
}

// SetError puts the input into an errored state with the specified error text.
func (in *TextField) SetError(err string) {
	in.errored = true
	in.helper.Text = err
}

// ClearError clears any errored status.
func (in *TextField) ClearError() {
	in.errored = false
	in.helper.Text = in.Helper
}

// Clear the input text and reset any error status.
func (in *TextField) Clear() {
	in.Editor.SetText("")
	in.ClearError()
}

// TextTooLong returns whether the current editor text exceeds the set character
// limit.
func (in *TextField) TextTooLong() bool {
	return !(in.CharLimit == 0 || uint(len(in.Editor.Text())) < in.CharLimit)
}

func (in *TextField) Update(gtx C, th *material.Theme, hint string) {
	var disabled = gtx.Queue == nil
	for in.Hoverable.Clicked() {
		in.Editor.Focus()
	}
	in.state = inactive
	if in.Hoverable.Hovered() && !disabled {
		in.state = hovered
	}
	if in.Editor.Len() > 0 {
		in.state = activated
	}
	if in.Editor.Focused() && !disabled {
		in.state = focused
	}
	const (
		duration = time.Millisecond * 100
	)
	if in.anim == nil {
		in.anim = &Progress{}
	}
	if in.state == activated {
		in.anim.Start(gtx.Now, Forward, 0)
	}
	if in.state == focused && in.Editor.Len() == 0 && !in.anim.Started() {
		in.anim.Start(gtx.Now, Forward, duration)
	}
	if in.state == inactive && in.Editor.Len() == 0 && in.anim.Finished() {
		in.anim.Start(gtx.Now, Reverse, duration)
	}
	if in.anim.Started() {
		op.InvalidateOp{}.Add(gtx.Ops)
	}
	in.anim.Update(gtx.Now)
	var (
		// Text size transitions.
		textNormal = th.TextSize
		textSmall  = th.TextSize.Scale(0.8)
		// Border color transitions.
		borderColor        = WithAlpha(th.Palette.Fg, 128)
		borderColorHovered = WithAlpha(th.Palette.Fg, 221)
		borderColorActive  = th.Palette.ContrastBg
		// TODO: derive from Theme.Error or Theme.Danger
		dangerColor = color.NRGBA{R: 200, A: 255}
		// Border thickness transitions.
		borderThickness       = float32(0.5)
		borderThicknessActive = float32(2.0)
	)
	in.label.TextSize = lerp(textSmall.V, textNormal.V, 1.0-in.anim.Progress())
	switch in.state {
	case inactive:
		in.border.Thickness = borderThickness
		in.border.Color = borderColor
		in.helper.Color = borderColor
	case hovered, activated:
		in.border.Thickness = borderThickness
		in.border.Color = borderColorHovered
		in.helper.Color = borderColorHovered
	case focused:
		in.border.Thickness = borderThicknessActive
		in.border.Color = borderColorActive
		in.helper.Color = borderColorHovered
	}
	if in.IsErrored() {
		in.border.Color = dangerColor
		in.helper.Color = dangerColor
	}
	// Calculate the dimensions of the smallest label size and store the
	// result for use in clipping.
	// Hack: Reset min constraint to 0 to avoid min == max.
	gtx.Constraints.Min.X = 0
	macro := op.Record(gtx.Ops)
	in.label.Smallest = layout.Inset{
		Left:  unit.Dp(4),
		Right: unit.Dp(4),
	}.Layout(gtx, func(gtx C) D {
		return material.Label(th, textSmall, hint).Layout(gtx)
	})
	macro.Stop()
	labelTopInsetNormal := float32(in.label.Smallest.Size.Y) - float32(in.label.Smallest.Size.Y/4)
	labelTopInsetActive := (labelTopInsetNormal / 2 * -1) - in.border.Thickness
	in.label.Inset = layout.Inset{
		Top:  unit.Px(lerp(labelTopInsetNormal, labelTopInsetActive, in.anim.Progress())),
		Left: unit.Dp(10),
	}
}

func (in *TextField) Layout(gtx C, th *material.Theme, hint string) D {
	in.Update(gtx, th, hint)
	defer op.Save(gtx.Ops).Load()
	// Offset accounts for label height, which sticks above the border dimensions.
	op.Offset(f32.Pt(0, float32(in.label.Smallest.Size.Y)/2)).Add(gtx.Ops)
	in.label.Inset.Layout(
		gtx,
		func(gtx C) D {
			return layout.Inset{
				Left:  unit.Dp(4),
				Right: unit.Dp(4),
			}.Layout(gtx, func(gtx C) D {
				label := material.Label(th, unit.Sp(in.label.TextSize), hint)
				label.Color = in.border.Color
				return label.Layout(gtx)
			})
		})
	dims := layout.Flex{
		Axis: layout.Vertical,
	}.Layout(
		gtx,
		layout.Rigid(func(gtx C) D {
			return layout.Stack{}.Layout(
				gtx,
				layout.Expanded(func(gtx C) D {
					var cornerRadius = unit.Dp(4)
					macro := op.Record(gtx.Ops)
					dims := widget.Border{
						Color:        in.border.Color,
						Width:        unit.Dp(in.border.Thickness),
						CornerRadius: cornerRadius,
					}.Layout(
						gtx,
						func(gtx C) D {
							return D{Size: image.Point{
								X: gtx.Constraints.Max.X,
								Y: gtx.Constraints.Min.Y,
							}}
						},
					)
					border := macro.Stop()
					if in.Editor.Focused() || in.Editor.Len() > 0 {
						// Clip a concave shape which ignores the label area.
						clips := []func(gtx C){
							func(gtx C) {
								clip.RRect{
									Rect: layout.FRect(image.Rectangle{
										Max: image.Point{
											X: gtx.Px(in.label.Inset.Left),
											Y: gtx.Constraints.Min.Y,
										},
									}),
									NW: float32(gtx.Px(cornerRadius)),
									SW: float32(gtx.Px(cornerRadius)),
								}.Add(gtx.Ops)
							},
							func(gtx C) {
								clip.Rect{
									Min: image.Point{
										X: gtx.Px(in.label.Inset.Left),
										Y: in.label.Smallest.Size.Y / 2,
									},
									Max: image.Point{
										X: gtx.Px(in.label.Inset.Left) + in.label.Smallest.Size.X,
										Y: gtx.Constraints.Min.Y,
									},
								}.Add(gtx.Ops)
							},
							func(gtx C) {
								clip.RRect{
									Rect: layout.FRect(image.Rectangle{
										Min: image.Point{
											X: gtx.Px(in.label.Inset.Left) + in.label.Smallest.Size.X,
										},
										Max: image.Point{
											X: gtx.Constraints.Max.X,
											Y: gtx.Constraints.Min.Y,
										},
									}),
									NE: float32(gtx.Px(cornerRadius)),
									SE: float32(gtx.Px(cornerRadius)),
								}.Add(gtx.Ops)
							},
						}
						for _, c := range clips {
							stack := op.Save(gtx.Ops)
							c(gtx)
							border.Add(gtx.Ops)
							stack.Load()
						}
					} else {
						border.Add(gtx.Ops)
					}
					return dims
				}),
				layout.Stacked(func(gtx C) D {
					return layout.UniformInset(unit.Dp(12)).Layout(
						gtx,
						func(gtx C) D {
							gtx.Constraints.Min.X = gtx.Constraints.Max.X
							return layout.Flex{
								Axis:      layout.Horizontal,
								Alignment: layout.Middle,
								Spacing: func() layout.Spacing {
									switch in.Alignment {
									case layout.Middle:
										return layout.SpaceSides
									case layout.End:
										return layout.SpaceStart
									default: // layout.Start and all others
										return layout.SpaceEnd
									}
								}(),
							}.Layout(
								gtx,
								layout.Rigid(func(gtx C) D {
									if in.IsActive() && in.Prefix != nil {
										return in.Prefix(gtx)
									}
									return D{}
								}),
								layout.Rigid(func(gtx C) D {
									return material.Editor(th, &in.Editor, "").Layout(gtx)
								}),
								layout.Rigid(func(gtx C) D {
									if in.IsActive() && in.Suffix != nil {
										return in.Suffix(gtx)
									}
									return D{}
								}),
							)
						},
					)
				}),
				layout.Expanded(func(gtx C) D {
					return in.Hoverable.Layout(gtx)
				}),
			)
		}),
		layout.Rigid(func(gtx C) D {
			return layout.Flex{
				Axis:      layout.Horizontal,
				Alignment: layout.Middle,
				Spacing:   layout.SpaceBetween,
			}.Layout(
				gtx,
				layout.Rigid(func(gtx C) D {
					if in.helper.Text == "" {
						return D{}
					}
					return layout.Inset{
						Top:  unit.Dp(4),
						Left: unit.Dp(10),
					}.Layout(
						gtx,
						func(gtx C) D {
							helper := material.Label(th, unit.Dp(12), in.helper.Text)
							helper.Color = in.helper.Color
							return helper.Layout(gtx)
						},
					)
				}),
				layout.Rigid(func(gtx C) D {
					if in.CharLimit == 0 {
						return D{}
					}
					return layout.Inset{
						Top:   unit.Dp(4),
						Right: unit.Dp(10),
					}.Layout(
						gtx,
						func(gtx C) D {
							count := material.Label(
								th,
								unit.Dp(12),
								strconv.Itoa(in.Editor.Len())+"/"+strconv.Itoa(int(in.CharLimit)),
							)
							count.Color = in.helper.Color
							return count.Layout(gtx)
						},
					)
				}),
			)
		}),
	)
	return D{
		Size: image.Point{
			X: dims.Size.X,
			Y: dims.Size.Y + in.label.Smallest.Size.Y/2,
		},
		Baseline: dims.Baseline,
	}
}

// interpolate linearly between two values based on progress.
//
// Progress is expected to be [0, 1]. Values greater than 1 will therefore be
// become a coeficient.
//
// For example, 2.5 is 250% progress.
func lerp(start, end, progress float32) float32 {
	return start + (end-start)*progress
}

// Hoverable tracks mouse hovers over some area.
type Hoverable struct {
	widget.Clickable
	hovered bool
}

// Hovered if mouse has entered the area.
func (h *Hoverable) Hovered() bool {
	return h.hovered
}

// Layout Hoverable according to min constraints.
func (h *Hoverable) Layout(gtx C) D {
	{
		stack := op.Save(gtx.Ops)
		pointer.PassOp{Pass: true}.Add(gtx.Ops)
		pointer.Rect(image.Rectangle{Max: gtx.Constraints.Min}).Add(gtx.Ops)
		h.Clickable.Layout(gtx)
		stack.Load()
	}
	h.update(gtx)
	{
		stack := op.Save(gtx.Ops)
		pointer.PassOp{Pass: true}.Add(gtx.Ops)
		pointer.Rect(image.Rectangle{Max: gtx.Constraints.Min}).Add(gtx.Ops)
		pointer.InputOp{
			Tag:   h,
			Types: pointer.Enter | pointer.Leave | pointer.Cancel,
		}.Add(gtx.Ops)
		stack.Load()
	}
	return D{Size: gtx.Constraints.Min}

}

func (h *Hoverable) update(gtx C) {
	for _, event := range gtx.Events(h) {
		if event, ok := event.(pointer.Event); ok {
			switch event.Type {
			case pointer.Enter:
				h.hovered = true
			case pointer.Leave, pointer.Cancel:
				h.hovered = false
			}
		}
	}
}