~whereswaldon/sprig

ref: v0.0.16 sprig/widget/text-form.go -rw-r--r-- 1.0 KiB
fbfe7d32Chris Waldon fix: use concrete ds.ReplyData as target of reply methods and focus 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
package widget

import (
	"gioui.org/io/clipboard"
	"gioui.org/layout"
	"gioui.org/widget"
	materials "gioui.org/x/component"
)

// TextForm holds the theme-independent state of a simple form that
// allows a user to provide a single text value and supports pasting.
// It can be submitted with either the submit button or pressing enter
// on the keyboard.
type TextForm struct {
	submitted    bool
	TextField    materials.TextField
	SubmitButton widget.Clickable
	PasteButton  widget.Clickable
}

func (c *TextForm) Layout(gtx layout.Context) layout.Dimensions {
	c.submitted = false
	for _, e := range c.TextField.Events() {
		if _, ok := e.(widget.SubmitEvent); ok {
			c.submitted = true
		}
	}
	if c.SubmitButton.Clicked() {
		c.submitted = true
	}
	if c.PasteButton.Clicked() {
		clipboard.ReadOp{Tag: c}.Add(gtx.Ops)
	}
	for _, e := range gtx.Events(c) {
		switch e := e.(type) {
		case clipboard.Event:
			c.TextField.Editor.Insert(e.Text)
		}
	}
	return layout.Dimensions{}
}

func (c *TextForm) Submitted() bool {
	return c.submitted
}