2020-07-01 01:09:22 +00:00
|
|
|
package completion
|
|
|
|
|
|
|
|
import (
|
2020-10-15 06:32:11 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts/httputil"
|
2020-07-01 01:34:13 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives/scrollinput"
|
2020-10-15 06:32:11 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich/parser/markup"
|
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-07-01 01:09:22 +00:00
|
|
|
"github.com/diamondburned/cchat/utils/split"
|
2020-10-15 06:32:11 +00:00
|
|
|
"github.com/diamondburned/imgutil"
|
2020-07-01 01:09:22 +00:00
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
)
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
const (
|
|
|
|
ImageSmall = 25
|
|
|
|
ImageLarge = 40
|
|
|
|
ImagePadding = 6
|
|
|
|
)
|
2020-07-01 01:09:22 +00:00
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
// post-processor icon
|
|
|
|
var ppIcon = []imgutil.Processor{imgutil.Round(true)}
|
2020-07-01 01:09:22 +00:00
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
type Completer struct {
|
2020-07-01 01:09:22 +00:00
|
|
|
Input *gtk.TextView
|
2020-07-14 07:24:55 +00:00
|
|
|
Buffer *gtk.TextBuffer
|
2020-07-01 01:09:22 +00:00
|
|
|
List *gtk.ListBox
|
|
|
|
Popover *gtk.Popover
|
2020-10-15 06:32:11 +00:00
|
|
|
popdown bool
|
|
|
|
|
|
|
|
Splitter split.SplitFunc
|
2020-07-01 01:09:22 +00:00
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
words []string
|
|
|
|
index int64
|
|
|
|
cursor int64
|
|
|
|
|
|
|
|
entries []cchat.CompletionEntry
|
|
|
|
completer cchat.Completer
|
2020-07-01 01:09:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
func WrapCompleter(input *gtk.TextView) {
|
|
|
|
NewCompleter(input)
|
2020-07-01 01:09:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
func NewCompleter(input *gtk.TextView) *Completer {
|
2020-07-01 01:09:22 +00:00
|
|
|
l, _ := gtk.ListBoxNew()
|
|
|
|
l.Show()
|
|
|
|
|
2020-07-01 01:34:13 +00:00
|
|
|
s := scrollinput.NewVScroll(150)
|
|
|
|
s.Add(l)
|
|
|
|
s.Show()
|
|
|
|
|
2020-07-01 01:09:22 +00:00
|
|
|
p := NewPopover(input)
|
2020-07-01 01:34:13 +00:00
|
|
|
p.Add(s)
|
2020-07-01 01:09:22 +00:00
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
input.Connect("key-press-event", KeyDownHandler(l, input.GrabFocus))
|
|
|
|
ibuf, _ := input.GetBuffer()
|
|
|
|
|
2020-07-01 01:09:22 +00:00
|
|
|
c := &Completer{
|
2020-10-15 06:32:11 +00:00
|
|
|
Input: input,
|
|
|
|
Buffer: ibuf,
|
|
|
|
List: l,
|
|
|
|
Popover: p,
|
|
|
|
Splitter: split.SpaceIndexed,
|
2020-07-01 01:09:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// This one is for buffer modification.
|
|
|
|
ibuf.Connect("end-user-action", c.onChange)
|
|
|
|
// This one is for when the cursor moves.
|
|
|
|
input.Connect("move-cursor", c.onChange)
|
2020-07-01 01:09:22 +00:00
|
|
|
|
|
|
|
l.Connect("row-activated", func(l *gtk.ListBox, r *gtk.ListBoxRow) {
|
2020-10-15 06:32:11 +00:00
|
|
|
SwapWord(ibuf, c.entries[r.GetIndex()].Raw, c.cursor)
|
|
|
|
c.onChange() // signal change
|
|
|
|
c.Popdown()
|
2020-07-01 01:09:22 +00:00
|
|
|
input.GrabFocus()
|
|
|
|
})
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
// SetCompleter sets the current completer. If completer is nil, then the
|
|
|
|
// completer is disabled.
|
|
|
|
func (c *Completer) SetCompleter(completer cchat.Completer) {
|
|
|
|
c.Popdown()
|
|
|
|
c.completer = completer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Completer) Reset() {
|
|
|
|
c.SetCompleter(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Completer) Popup() {
|
|
|
|
if c.popdown {
|
|
|
|
c.Popover.Popup()
|
|
|
|
c.popdown = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Completer) Popdown() {
|
|
|
|
if !c.popdown {
|
|
|
|
c.Popover.Popdown()
|
|
|
|
c.popdown = true
|
2020-10-20 22:45:28 +00:00
|
|
|
c.Clear()
|
2020-10-15 06:32:11 +00:00
|
|
|
}
|
2020-07-01 01:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Completer) Clear() {
|
2020-07-01 01:09:22 +00:00
|
|
|
var children = c.List.GetChildren()
|
|
|
|
if children.Length() == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
children.Foreach(func(i interface{}) {
|
|
|
|
w := i.(gtk.IWidget).ToWidget()
|
|
|
|
c.List.Remove(w)
|
|
|
|
w.Destroy()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
// Words returns the buffer content split into words.
|
|
|
|
func (c *Completer) Content() []string {
|
|
|
|
// This method not to be confused with c.words, which contains the state of
|
|
|
|
// completer words.
|
|
|
|
|
|
|
|
text, _ := c.Buffer.GetText(c.Buffer.GetStartIter(), c.Buffer.GetEndIter(), true)
|
|
|
|
if text == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
words, _ := c.Splitter(text, 0)
|
|
|
|
return words
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
func (c *Completer) onChange() {
|
|
|
|
t, v, blank := State(c.Buffer)
|
2020-10-15 06:32:11 +00:00
|
|
|
c.cursor = v
|
2020-07-14 07:24:55 +00:00
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
// If the cursor is on a blank character, then we should not
|
2020-07-14 07:24:55 +00:00
|
|
|
// autocomplete anything, so we set the states to nil.
|
|
|
|
if blank {
|
2020-10-15 06:32:11 +00:00
|
|
|
c.words = nil
|
|
|
|
c.index = -1
|
2020-10-20 22:45:28 +00:00
|
|
|
c.Popdown()
|
2020-10-15 06:32:11 +00:00
|
|
|
return
|
2020-07-14 07:24:55 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
c.words, c.index = c.Splitter(t, v)
|
2020-07-14 07:24:55 +00:00
|
|
|
c.complete()
|
|
|
|
}
|
|
|
|
|
2020-07-01 01:09:22 +00:00
|
|
|
func (c *Completer) complete() {
|
2020-07-01 01:34:13 +00:00
|
|
|
c.Clear()
|
2020-07-01 01:09:22 +00:00
|
|
|
|
|
|
|
var widgets []gtk.IWidget
|
2020-10-15 06:32:11 +00:00
|
|
|
if len(c.words) > 0 {
|
|
|
|
widgets = c.update()
|
2020-07-01 01:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(widgets) > 0 {
|
|
|
|
c.Popover.SetPointingTo(CursorRect(c.Input))
|
2020-10-15 06:32:11 +00:00
|
|
|
c.Popup()
|
2020-07-01 01:09:22 +00:00
|
|
|
} else {
|
2020-10-15 06:32:11 +00:00
|
|
|
c.Popdown()
|
2020-07-14 07:24:55 +00:00
|
|
|
return
|
2020-07-01 01:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, widget := range widgets {
|
|
|
|
r, _ := gtk.ListBoxRowNew()
|
|
|
|
r.Add(widget)
|
|
|
|
r.Show()
|
|
|
|
|
|
|
|
c.List.Add(r)
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
c.List.SelectRow(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 06:32:11 +00:00
|
|
|
|
|
|
|
func (c *Completer) update() []gtk.IWidget {
|
|
|
|
// If we don't have a completer, then don't run.
|
|
|
|
if c.completer == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.entries = c.completer.Complete(c.words, c.index)
|
|
|
|
|
|
|
|
var widgets = make([]gtk.IWidget, len(c.entries))
|
|
|
|
|
|
|
|
for i, entry := range c.entries {
|
|
|
|
// Container that holds the label.
|
|
|
|
lbox, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
|
|
|
|
lbox.SetVAlign(gtk.ALIGN_CENTER)
|
|
|
|
lbox.Show()
|
|
|
|
|
|
|
|
// Label for the primary text.
|
|
|
|
l := rich.NewLabel(entry.Text)
|
|
|
|
l.Show()
|
|
|
|
lbox.PackStart(l, false, false, 0)
|
|
|
|
|
|
|
|
// Get the iamge size so we can change and use if needed. The default
|
|
|
|
var size = ImageSmall
|
|
|
|
if !entry.Secondary.IsEmpty() {
|
|
|
|
size = ImageLarge
|
|
|
|
|
|
|
|
s := rich.NewLabel(text.Rich{})
|
|
|
|
s.SetMarkup(fmt.Sprintf(
|
|
|
|
`<span alpha="50%%" size="small">%s</span>`,
|
|
|
|
markup.Render(entry.Secondary),
|
|
|
|
))
|
|
|
|
s.Show()
|
|
|
|
|
|
|
|
lbox.PackStart(s, false, false, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
|
|
|
|
b.PackEnd(lbox, true, true, ImagePadding)
|
|
|
|
b.Show()
|
|
|
|
|
|
|
|
// Do we have an icon?
|
|
|
|
if entry.IconURL != "" {
|
|
|
|
img, _ := gtk.ImageNew()
|
|
|
|
img.SetMarginStart(ImagePadding)
|
|
|
|
img.SetSizeRequest(size, size)
|
|
|
|
img.Show()
|
|
|
|
|
|
|
|
// Prepend the image into the box.
|
|
|
|
b.PackEnd(img, false, false, 0)
|
|
|
|
|
|
|
|
var pps []imgutil.Processor
|
|
|
|
if !entry.Image {
|
|
|
|
pps = ppIcon
|
|
|
|
}
|
|
|
|
|
|
|
|
httputil.AsyncImageSized(img, entry.IconURL, size, size, pps...)
|
|
|
|
}
|
|
|
|
|
|
|
|
widgets[i] = b
|
|
|
|
}
|
|
|
|
|
|
|
|
return widgets
|
|
|
|
}
|