-
Notifications
You must be signed in to change notification settings - Fork 1
/
textinput.go
187 lines (175 loc) · 4.87 KB
/
textinput.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
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
package main
import (
"log"
"strings"
"github.com/lxn/walk"
"github.com/lxn/win"
)
func newMyLineEdit(parent walk.Container) *MyLineEdit {
le := new(MyLineEdit)
checkErr(walk.InitWindow(
le,
parent,
"EDIT",
win.WS_CHILD|win.WS_TABSTOP|win.WS_VISIBLE|win.ES_AUTOHSCROLL,
win.WS_EX_CLIENTEDGE,
))
le.tabComplete = &tabComplete{}
return le
}
type MyLineEdit struct {
walk.LineEdit
msgHistory []string
msgHistoryIndex int
tabComplete *tabComplete
}
type tabComplete struct {
Active bool
Entries []string
Index int
}
func (le *MyLineEdit) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
if msg == win.WM_GETDLGCODE {
if wParam == win.VK_TAB || wParam == win.VK_ESCAPE || wParam == win.VK_RETURN {
return win.DLGC_WANTMESSAGE
}
}
return le.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
func getCommandContext(t tabWithTextBuffer) *commandContext {
// FIXME(tso): resolve commandContext/tabWithContext
cmdctx := &commandContext{}
ctx := tabMan.Find(identityFinder(t))
if ctx == nil {
panic("textInput owner tab is not in tabManager!")
}
cmdctx.servConn = ctx.servConn
cmdctx.servState = ctx.servState
cmdctx.chanState = ctx.chanState
cmdctx.pmState = ctx.pmState
cmdctx.tab = t
return cmdctx
}
func NewTextInput(t tabWithTextBuffer) *MyLineEdit {
var tabPage *walk.TabPage
var sendFn func(string)
switch t.(type) {
case *tabServer:
tabPage = t.(*tabServer).tabPage
sendFn = func(str string) {
clientError(t, "cannot send messages to a SERVER!!", "\nnot sent:", color(str, LightGrey))
}
case *tabChannel:
tabPage = t.(*tabChannel).tabPage
sendFn = t.(*tabChannel).Send
case *tabPrivmsg:
tabPage = t.(*tabPrivmsg).tabPage
sendFn = t.(*tabPrivmsg).Send
default:
log.Panicf("unsupported type %T", t)
}
textInput := newMyLineEdit(tabPage)
textInput.KeyDown().Attach(func(key walk.Key) {
if r := insertCharacter(key); r != 0 {
text := []rune(textInput.Text())
s, e := textInput.TextSelection()
text = append(text[:s], append([]rune{r}, text[e:]...)...)
textInput.SetText(string(text))
textInput.SetTextSelection(s+1, e+1)
} else if key == walk.KeyReturn {
text := textInput.Text()
if len(text) < 1 {
return
}
textInput.msgHistory = append(textInput.msgHistory, text)
textInput.msgHistoryIndex = len(textInput.msgHistory) - 1
if text[0] == '/' && len(text) > 1 {
parts := strings.Split(text[1:], " ")
cmd := parts[0]
if cmd[0] == '/' {
sendFn(text[1:])
} else {
var args []string
if len(parts) > 1 {
args = parts[1:]
} else {
args = []string{}
}
if cmdFn, ok := clientCommands[cmd]; ok {
cmdFn(getCommandContext(t), args...)
} else {
clientError(t, "unrecognized command: ", cmd)
}
}
} else {
sendFn(text)
}
textInput.SetText("")
} else if key == walk.KeyUp {
if len(textInput.msgHistory) > 0 {
text := textInput.msgHistory[textInput.msgHistoryIndex]
textInput.SetText(text)
textInput.SetTextSelection(len(text), len(text))
textInput.msgHistoryIndex--
if textInput.msgHistoryIndex < 0 {
textInput.msgHistoryIndex = 0
}
}
} else if key == walk.KeyDown {
if len(textInput.msgHistory) > 0 {
textInput.msgHistoryIndex++
if textInput.msgHistoryIndex <= len(textInput.msgHistory)-1 {
text := textInput.msgHistory[textInput.msgHistoryIndex]
textInput.SetText(text)
textInput.SetTextSelection(len(text), len(text))
} else {
textInput.SetText("")
textInput.msgHistoryIndex = len(textInput.msgHistory) - 1
}
}
}
})
textInput.KeyUp().Attach(func(key walk.Key) {
if key == walk.KeyUp || key == walk.KeyDown {
text := textInput.Text()
textInput.SetTextSelection(len(text), len(text))
}
})
textInput.KeyPress().Attach(globalKeyHandler)
textInput.KeyPress().Attach(func(key walk.Key) {
if key == walk.KeyUp || key == walk.KeyDown {
text := textInput.Text()
textInput.SetTextSelection(len(text), len(text))
} else if key == walk.KeyTab && !walk.ControlDown() {
text := strings.Split(textInput.Text(), " ")
if textInput.tabComplete.Active {
textInput.tabComplete.Index++
if textInput.tabComplete.Index >= len(textInput.tabComplete.Entries) {
textInput.tabComplete.Index = 0
}
} else {
term := text[len(text)-1]
res := []string{}
ctx := getCommandContext(t)
if ctx.chanState != nil {
res = ctx.chanState.nickList.Search(term)
}
res = append(res, term)
textInput.tabComplete = &tabComplete{
Active: true,
Entries: res,
Index: 0,
}
}
text = append(text[:len(text)-1], textInput.tabComplete.Entries[textInput.tabComplete.Index])
txt := strings.Join(text, " ")
textInput.SetText(txt)
textInput.SetTextSelection(len(txt), len(txt))
} else {
if textInput.tabComplete.Active {
textInput.tabComplete = &tabComplete{}
}
}
})
return textInput
}