-
Notifications
You must be signed in to change notification settings - Fork 37
/
input.go
319 lines (272 loc) · 8.23 KB
/
input.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
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
package composer
import (
"context"
"io"
"mime"
"strings"
"sync"
"time"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/chatkit/components/autocomplete"
"github.com/diamondburned/chatkit/md/mdrender"
"github.com/diamondburned/gotk4/pkg/core/gioutil"
"github.com/diamondburned/gotk4/pkg/core/glib"
"github.com/diamondburned/gotk4/pkg/gdk/v4"
"github.com/diamondburned/gotk4/pkg/gio/v2"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/diamondburned/gotkit/app"
"github.com/diamondburned/gotkit/app/prefs"
"github.com/diamondburned/gotkit/gtkutil"
"github.com/diamondburned/gotkit/gtkutil/cssutil"
"github.com/diamondburned/gotkit/gtkutil/textutil"
"github.com/diamondburned/gotkit/utils/osutil"
"github.com/pkg/errors"
"libdb.so/dissent/internal/gtkcord"
)
var persistInput = prefs.NewBool(true, prefs.PropMeta{
Name: "Persist Input",
Section: "Composer",
Description: "Persist the input message between sessions (to disk). " +
"If disabled, the input is only persisted for the current session on memory.",
})
// InputController is the parent controller that Input controls.
type InputController interface {
// Send sends or edits everything in the current message buffer state.
Send()
// Escape is called when the Escape key is pressed. It is meant to stop any
// ongoing action and return true, or false if no action.
Escape() bool
// EditLastMessage is called when the user wants to edit their last message.
// False is returned if no messages can be found.
EditLastMessage() bool
// PasteClipboardFile is called everytime the user pastes a file from their
// clipboard. The file is usually (but not always) an image.
PasteClipboardFile(File)
}
// Input is the text field of the composer.
type Input struct {
*gtk.TextView
Buffer *gtk.TextBuffer
ac *autocomplete.Autocompleter
ctx context.Context
ctrl InputController
chID discord.ChannelID
guildID discord.GuildID
}
var inputCSS = cssutil.Applier("composer-input", `
.composer-input,
.composer-input text {
background-color: inherit;
}
.composer-input {
padding: 12px 2px;
margin-top: 0px;
}
.composer-input .autocomplete-row label {
margin: 0;
}
`)
var inputWYSIWYG = prefs.NewBool(true, prefs.PropMeta{
Name: "Rich Preview",
Section: "Composer",
Description: "Enable a semi-WYSIWYG feature that decorates the input Markdown text.",
})
// inputStateKey is the app state that stores the last input message.
var inputStateKey = app.NewStateKey[string]("input-state")
var inputStateMemory sync.Map // map[discord.ChannelID]string
// NewInput creates a new Input widget.
func NewInput(ctx context.Context, ctrl InputController, chID discord.ChannelID) *Input {
i := Input{
ctx: ctx,
ctrl: ctrl,
chID: chID,
}
i.TextView = gtk.NewTextView()
i.TextView.SetWrapMode(gtk.WrapWordChar)
i.TextView.SetAcceptsTab(true)
i.TextView.SetHExpand(true)
i.TextView.SetInputHints(0 |
gtk.InputHintEmoji |
gtk.InputHintSpellcheck |
gtk.InputHintWordCompletion |
gtk.InputHintUppercaseSentences,
)
textutil.SetTabSize(i.TextView)
inputCSS(i)
i.TextView.ConnectPasteClipboard(i.readClipboard)
i.ac = autocomplete.New(ctx, i.TextView)
i.ac.AddSelectedFunc(i.onAutocompleted)
i.ac.SetCancelOnChange(false)
i.ac.SetMinLength(2)
i.ac.SetTimeout(time.Second)
state := gtkcord.FromContext(ctx)
if ch, err := state.Cabinet.Channel(chID); err == nil {
i.guildID = ch.GuildID
i.ac.Use(
NewEmojiCompleter(i.guildID), // :
NewMemberCompleter(chID), // @
)
}
inputState := inputStateKey.Acquire(ctx)
i.Buffer = i.TextView.Buffer()
i.Buffer.ConnectChanged(func() {
if inputWYSIWYG.Value() {
mdrender.RenderWYSIWYG(ctx, i.Buffer)
}
i.ac.Autocomplete()
start, end := i.Buffer.Bounds()
// Persist input.
if end.Offset() == 0 {
if persistInput.Value() {
inputState.Delete(chID.String())
} else {
inputStateMemory.Delete(chID)
}
} else {
text := i.Buffer.Text(start, end, false)
if persistInput.Value() {
inputState.Set(chID.String(), text)
} else {
inputStateMemory.Store(chID, text)
}
}
})
enterKeyer := gtk.NewEventControllerKey()
enterKeyer.ConnectKeyPressed(i.onKey)
i.AddController(enterKeyer)
inputState.Get(chID.String(), func(text string) {
i.Buffer.SetText(text)
})
return &i
}
func (i *Input) onAutocompleted(row autocomplete.SelectedData) bool {
i.Buffer.BeginUserAction()
defer i.Buffer.EndUserAction()
i.Buffer.Delete(row.Bounds[0], row.Bounds[1])
switch data := row.Data.(type) {
case EmojiData:
state := gtkcord.FromContext(i.ctx)
start, end := i.Buffer.Bounds()
canUseEmoji := false ||
// has Nitro so can use anything
state.EmojiState.HasNitro() ||
// unicode emoji
!data.Emoji.ID.IsValid() ||
// same guild, not animated
(data.GuildID == i.guildID && !data.Emoji.Animated) ||
// adding a reaction, so we can't even use URL
textBufferIsReaction(i.Buffer.Text(start, end, false))
var content string
if canUseEmoji {
// Use the default emoji format. This string is subject to
// server-side validation.
content = data.Emoji.String()
} else {
// Use the emoji URL instead of the emoji code to allow
// non-Nitro users to send emojis by sending the image URL.
content = gtkcord.InjectSizeUnscaled(data.Emoji.EmojiURL(), gtkcord.LargeEmojiSize)
}
i.Buffer.Insert(row.Bounds[1], content)
return true
case MemberData:
i.Buffer.Insert(row.Bounds[1], discord.Member(data).Mention())
return true
}
return false
}
var sendOnEnter = prefs.NewBool(true, prefs.PropMeta{
Name: "Send Message on Enter",
Section: "Composer",
Description: "Send the message when the user hits the Enter key. Disable this for mobile.",
})
func (i *Input) onKey(val, _ uint, state gdk.ModifierType) bool {
switch val {
case gdk.KEY_Return:
if i.ac.Select() {
return true
}
// TODO: find a better way to do this. goldmark won't try to
// parse an incomplete codeblock (I think), but the changed
// signal will be fired after this signal.
//
// Perhaps we could use the FindChar method to avoid allocating
// a new string (twice) on each keypress.
head := i.Buffer.StartIter()
tail := i.Buffer.IterAtMark(i.Buffer.GetInsert())
uinput := i.Buffer.Text(head, tail, false)
// Check if the number of triple backticks is odd. If it is, then we're
// in one.
withinCodeblock := strings.Count(uinput, "```")%2 != 0
// Enter (without holding Shift) sends the message.
if sendOnEnter.Value() && !state.Has(gdk.ShiftMask) && !withinCodeblock {
i.ctrl.Send()
return true
}
case gdk.KEY_Tab:
return i.ac.Select()
case gdk.KEY_Escape:
return i.ctrl.Escape()
case gdk.KEY_Up:
if i.ac.MoveUp() {
return true
}
if i.Buffer.CharCount() == 0 {
return i.ctrl.EditLastMessage()
}
case gdk.KEY_Down:
return i.ac.MoveDown()
}
return false
}
func (i *Input) readClipboard() {
display := gdk.DisplayGetDefault()
clipboard := display.Clipboard()
mimeTypes := clipboard.Formats().MIMETypes()
// Ignore anything text.
for _, mime := range mimeTypes {
if mimeIsText(mime) {
return
}
}
clipboard.ReadAsync(i.ctx, mimeTypes, int(glib.PriorityDefault), func(res gio.AsyncResulter) {
typ, streamer, err := clipboard.ReadFinish(res)
if err != nil {
app.Error(i.ctx, errors.Wrap(err, "failed to read clipboard"))
return
}
gtkutil.Async(i.ctx, func() func() {
stream := gio.BaseInputStream(streamer)
reader := gioutil.Reader(i.ctx, stream)
defer reader.Close()
f, err := osutil.Consume(reader)
if err != nil {
app.Error(i.ctx, errors.Wrap(err, "cannot clone clipboard"))
return nil
}
s, err := f.Stat()
if err != nil {
app.Error(i.ctx, errors.Wrap(err, "cannot stat clipboard file"))
return nil
}
// We're too lazy to do reference-counting, so just forbid Open from
// being called more than once.
var openedOnce bool
file := File{
Name: "clipboard",
Type: typ,
Size: s.Size(),
Open: func() (io.ReadCloser, error) {
if !openedOnce {
openedOnce = true
return f, nil
}
return nil, errors.New("Open called more than once on TempFile")
},
}
if exts, _ := mime.ExtensionsByType(typ); len(exts) > 0 {
file.Name += exts[0]
}
return func() { i.ctrl.PasteClipboardFile(file) }
})
})
}