-
Notifications
You must be signed in to change notification settings - Fork 1
/
textinput_linux.go
124 lines (107 loc) · 3.4 KB
/
textinput_linux.go
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
package goey
import (
"unsafe"
"bitbucket.org/rj/goey/base"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
type textinputElement struct {
Control
onChange func(string)
shChange glib.SignalHandle
onFocus focusSlot
onBlur blurSlot
onEnterKey func(string)
shEnterKey glib.SignalHandle
}
func (w *TextInput) mount(parent base.Control) (base.Element, error) {
control, err := gtk.EntryNew()
if err != nil {
return nil, err
}
parent.Handle.Add(control)
control.SetText(w.Value)
control.SetPlaceholderText(w.Placeholder)
control.SetSensitive(!w.Disabled)
control.SetVisibility(!w.Password)
control.SetEditable(!w.ReadOnly)
retval := &textinputElement{
Control: Control{&control.Widget},
onChange: w.OnChange,
onEnterKey: w.OnEnterKey,
}
control.Connect("destroy", textinputOnDestroy, retval)
retval.shChange = setSignalHandler(&control.Widget, 0, retval.onChange != nil, "changed", textinputOnChanged, retval)
retval.onFocus.Set(&control.Widget, w.OnFocus)
retval.onBlur.Set(&control.Widget, w.OnBlur)
retval.shEnterKey = setSignalHandler(&control.Widget, 0, retval.onEnterKey != nil, "activate", textinputOnActivate, retval)
control.Show()
return retval, nil
}
func textinputOnActivate(obj *glib.Object, mounted *textinputElement) {
// Not sure why, but the widget comes into this callback as a glib.Object,
// and not the gtk.Entry. Need to wrap the value. This pokes into the internals
// of the gtk package.
widget := gtk.Entry{gtk.Widget{glib.InitiallyUnowned{obj}}, gtk.Editable{obj}}
text, err := widget.GetText()
if err != nil {
// TODO: What is the correct reporting here
return
}
mounted.onEnterKey(text)
}
func textinputOnChanged(widget *gtk.Entry, mounted *textinputElement) {
if mounted.onChange == nil {
return
}
text, err := widget.GetText()
if err != nil {
// TODO: What is the correct reporting here
return
}
mounted.onChange(text)
}
func textinputOnDestroy(widget *gtk.Entry, mounted *textinputElement) {
mounted.handle = nil
}
func (w *textinputElement) entry() *gtk.Entry {
return (*gtk.Entry)(unsafe.Pointer(w.handle))
}
func (w *textinputElement) Props() base.Widget {
entry := w.entry()
value, err := entry.GetText()
if err != nil {
panic("could not get text, " + err.Error())
}
placeholder, err := entry.GetPlaceholderText()
if err != nil {
panic("could not get placeholder text, " + err.Error())
}
return &TextInput{
Value: value,
Disabled: !entry.GetSensitive(),
Placeholder: placeholder,
Password: !entry.GetVisibility(),
ReadOnly: !entry.GetEditable(),
OnChange: w.onChange,
OnFocus: w.onFocus.callback,
OnBlur: w.onBlur.callback,
OnEnterKey: w.onEnterKey,
}
}
func (w *textinputElement) updateProps(data *TextInput) error {
entry := w.entry()
w.onChange = nil // temporarily break OnChange to prevent event
entry.SetText(data.Value)
entry.SetEditable(!data.ReadOnly)
entry.SetPlaceholderText(data.Placeholder)
entry.SetSensitive(!data.Disabled)
entry.SetVisibility(!data.Password)
w.onChange = data.OnChange
w.shChange = setSignalHandler(&entry.Widget, w.shChange, data.OnChange != nil, "changed", textinputOnChanged, w)
w.onFocus.Set(&entry.Widget, data.OnFocus)
w.onBlur.Set(&entry.Widget, data.OnBlur)
w.onEnterKey = data.OnEnterKey
w.shEnterKey = setSignalHandler(&entry.Widget, w.shEnterKey, data.OnEnterKey != nil, "activate", textinputOnActivate, w)
return nil
}