2020-06-13 07:29:32 +00:00
|
|
|
package completion
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts/httputil"
|
2020-07-01 01:09:22 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives/completion"
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich"
|
|
|
|
"github.com/diamondburned/imgutil"
|
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-07-01 01:34:13 +00:00
|
|
|
ImageSize = 25
|
|
|
|
ImagePadding = 6
|
2020-06-13 07:29:32 +00:00
|
|
|
)
|
|
|
|
|
2020-07-01 18:49:20 +00:00
|
|
|
var ppIcon = []imgutil.Processor{imgutil.Round(true)}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
type View struct {
|
2020-07-01 01:34:13 +00:00
|
|
|
*completion.Completer
|
|
|
|
entries []cchat.CompletionEntry
|
2020-06-13 07:29:32 +00:00
|
|
|
completer cchat.ServerMessageSendCompleter
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(text *gtk.TextView) *View {
|
2020-07-01 01:34:13 +00:00
|
|
|
v := &View{}
|
|
|
|
c := completion.NewCompleter(text, v)
|
|
|
|
v.Completer = c
|
2020-06-13 07:29:32 +00:00
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) Reset() {
|
|
|
|
v.SetCompleter(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) SetCompleter(completer cchat.ServerMessageSendCompleter) {
|
|
|
|
v.Clear()
|
2020-07-01 01:34:13 +00:00
|
|
|
v.Hide()
|
2020-06-13 07:29:32 +00:00
|
|
|
v.completer = completer
|
|
|
|
}
|
|
|
|
|
2020-07-01 01:34:13 +00:00
|
|
|
func (v *View) Update(words []string, i int) []gtk.IWidget {
|
2020-06-13 07:29:32 +00:00
|
|
|
// If we don't have a completer, then don't run.
|
|
|
|
if v.completer == nil {
|
2020-07-01 01:34:13 +00:00
|
|
|
return nil
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 01:34:13 +00:00
|
|
|
v.entries = v.completer.CompleteMessage(words, i)
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2020-07-01 01:34:13 +00:00
|
|
|
var widgets = make([]gtk.IWidget, len(v.entries))
|
2020-06-13 07:29:32 +00:00
|
|
|
|
|
|
|
for i, entry := range v.entries {
|
|
|
|
l := rich.NewLabel(entry.Text)
|
|
|
|
l.Show()
|
|
|
|
|
|
|
|
img, _ := gtk.ImageNew()
|
|
|
|
|
|
|
|
// Do we have an icon?
|
|
|
|
if entry.IconURL != "" {
|
2020-07-01 18:49:20 +00:00
|
|
|
img.SetMarginStart(ImagePadding)
|
2020-07-01 01:34:13 +00:00
|
|
|
img.SetSizeRequest(ImageSize, ImageSize)
|
|
|
|
img.Show()
|
2020-07-01 18:49:20 +00:00
|
|
|
|
|
|
|
var pps []imgutil.Processor
|
|
|
|
if !entry.Image {
|
|
|
|
pps = ppIcon
|
|
|
|
}
|
|
|
|
|
|
|
|
httputil.AsyncImageSized(img, entry.IconURL, ImageSize, ImageSize, pps...)
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
|
2020-07-01 18:49:20 +00:00
|
|
|
b.PackStart(img, false, false, 0) // image has pad left
|
|
|
|
b.PackStart(l, true, true, ImagePadding)
|
2020-06-13 07:29:32 +00:00
|
|
|
b.Show()
|
|
|
|
|
2020-07-01 01:34:13 +00:00
|
|
|
widgets[i] = b
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
2020-07-01 01:34:13 +00:00
|
|
|
|
|
|
|
return widgets
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 01:34:13 +00:00
|
|
|
func (v *View) Word(i int) string {
|
|
|
|
return v.entries[i].Raw
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|