1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-16 11:12:44 +00:00
cchat-gtk/internal/ui/messages/input/input.go

218 lines
5.1 KiB
Go
Raw Normal View History

2020-05-26 06:51:06 +00:00
package input
import (
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/log"
"github.com/diamondburned/cchat-gtk/internal/ui/messages/input/completion"
2020-05-26 06:51:06 +00:00
"github.com/gotk3/gotk3/gtk"
"github.com/pkg/errors"
2020-05-26 06:51:06 +00:00
)
// Controller is an interface to control message containers.
type Controller interface {
AddPresendMessage(msg PresendMessage) (onErr func(error))
LatestMessageFrom(userID string) (messageID string, ok bool)
}
type InputView struct {
*gtk.Box
*Field
Completer *completion.View
}
func NewView(ctrl Controller) *InputView {
text, _ := gtk.TextViewNew()
text.SetSensitive(false)
text.SetWrapMode(gtk.WRAP_WORD_CHAR)
text.SetProperty("top-margin", inputmargin)
text.SetProperty("left-margin", inputmargin)
text.SetProperty("right-margin", inputmargin)
text.SetProperty("bottom-margin", inputmargin)
text.Show()
// Bind the text event handler to text first.
c := completion.New(text)
c.Show()
// Bind the input callback later.
f := NewField(text, ctrl)
f.Show()
b, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
b.PackStart(c, false, true, 0)
b.PackStart(f, false, false, 0)
b.Show()
// Connect to the field's revealer. On resize, we want the autocompleter to
// have the right padding too.
f.username.Connect("size-allocate", func(w gtk.IWidget) {
// Set the autocompleter's left margin to be the same.
c.SetMarginStart(w.ToWidget().GetAllocatedWidth())
})
return &InputView{b, f, c}
}
func (v *InputView) SetSender(session cchat.Session, sender cchat.ServerMessageSender) {
v.Field.SetSender(session, sender)
// Ignore ok; completer can be nil.
completer, _ := sender.(cchat.ServerMessageSendCompleter)
v.Completer.SetCompleter(completer)
}
2020-05-26 06:51:06 +00:00
type Field struct {
2020-06-04 23:00:41 +00:00
*gtk.Box
2020-06-06 00:47:28 +00:00
username *usernameContainer
2020-06-04 23:00:41 +00:00
TextScroll *gtk.ScrolledWindow
text *gtk.TextView
buffer *gtk.TextBuffer
UserID string
Sender cchat.ServerMessageSender
editor cchat.ServerMessageEditor
2020-05-26 06:51:06 +00:00
ctrl Controller
// editing state
editingID string // never empty
2020-06-04 23:00:41 +00:00
}
2020-06-06 00:47:28 +00:00
const inputmargin = 4
2020-06-04 23:00:41 +00:00
func NewField(text *gtk.TextView, ctrl Controller) *Field {
2020-06-06 00:47:28 +00:00
username := newUsernameContainer()
2020-06-04 23:00:41 +00:00
username.Show()
2020-05-26 06:51:06 +00:00
buf, _ := text.GetBuffer()
sw, _ := gtk.ScrolledWindowNew(nil, nil)
2020-06-04 23:00:41 +00:00
sw.Add(text)
2020-05-26 06:51:06 +00:00
sw.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
2020-06-04 23:00:41 +00:00
sw.SetProperty("propagate-natural-height", true)
2020-05-26 06:51:06 +00:00
sw.SetProperty("max-content-height", 150)
2020-06-04 23:00:41 +00:00
sw.Show()
2020-05-26 06:51:06 +00:00
2020-06-04 23:00:41 +00:00
box, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
2020-06-06 00:47:28 +00:00
box.PackStart(username, false, false, 0)
2020-06-04 23:00:41 +00:00
box.PackStart(sw, true, true, 0)
box.Show()
field := &Field{
Box: box,
username: username,
TextScroll: sw,
text: text,
buffer: buf,
ctrl: ctrl,
2020-05-26 06:51:06 +00:00
}
2020-06-04 23:00:41 +00:00
text.SetFocusHAdjustment(sw.GetHAdjustment())
text.SetFocusVAdjustment(sw.GetVAdjustment())
text.Connect("key-press-event", field.keyDown)
return field
2020-05-26 06:51:06 +00:00
}
2020-06-07 07:06:13 +00:00
// Reset prepares the field before SetSender() is called.
func (f *Field) Reset() {
// Paranoia.
f.text.SetSensitive(false)
f.UserID = ""
f.Sender = nil
f.editor = nil
2020-06-07 07:06:13 +00:00
f.username.Reset()
// reset the input
f.buffer.Delete(f.buffer.GetBounds())
}
2020-05-26 06:51:06 +00:00
// SetSender changes the sender of the input field. If nil, the input will be
2020-06-07 07:06:13 +00:00
// disabled. Reset() should be called first.
2020-06-04 23:00:41 +00:00
func (f *Field) SetSender(session cchat.Session, sender cchat.ServerMessageSender) {
2020-06-07 04:27:28 +00:00
// Update the left username container in the input.
f.username.Update(session, sender)
f.UserID = session.ID()
2020-06-04 23:00:41 +00:00
// Set the sender.
2020-06-07 07:06:13 +00:00
if sender != nil {
f.Sender = sender
2020-06-07 07:06:13 +00:00
f.text.SetSensitive(true)
// Do we support message editing?
if editor, ok := sender.(cchat.ServerMessageEditor); ok {
f.editor = editor
}
}
}
// Editable returns whether or not the input field has editing capabilities.
func (f *Field) Editable() bool {
return f.editor != nil
}
func (f *Field) StartEditing(msgID string) bool {
// Do we support message editing? If not, exit.
if !f.Editable() {
return false
}
// Try and request the old message content for editing.
content, err := f.editor.RawMessageContent(msgID)
if err != nil {
// TODO: show error
log.Error(errors.Wrap(err, "Failed to get message content"))
return false
}
// Set the current editing state and set the input after requesting the
// content.
f.editingID = msgID
f.buffer.SetText(content)
return true
}
// StopEditing cancels the current editing message. It returns a false and does
// nothing if the editor is not editing anything.
func (f *Field) StopEditing() bool {
if f.editingID == "" {
return false
2020-06-07 07:06:13 +00:00
}
f.editingID = ""
f.clearText()
return true
2020-05-26 06:51:06 +00:00
}
// yankText cuts the text from the input field and returns it.
func (f *Field) yankText() string {
start, end := f.buffer.GetBounds()
text, _ := f.buffer.GetText(start, end, false)
if text != "" {
f.buffer.Delete(start, end)
}
return text
}
// clearText wipes the input field
func (f *Field) clearText() {
f.buffer.Delete(f.buffer.GetBounds())
}
// getText returns the text from the input, but it doesn't cut it.
func (f *Field) getText() string {
start, end := f.buffer.GetBounds()
text, _ := f.buffer.GetText(start, end, false)
return text
}
func (f *Field) textLen() int {
return f.buffer.GetCharCount()
}