2020-06-13 07:29:32 +00:00
|
|
|
package completion
|
|
|
|
|
|
|
|
import (
|
2020-07-09 04:14:56 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
"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"
|
2020-07-14 07:24:55 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich/parser/markup"
|
2020-07-09 04:14:56 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/diamondburned/imgutil"
|
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-07-09 04:14:56 +00:00
|
|
|
ImageSmall = 25
|
|
|
|
ImageLarge = 40
|
2020-07-01 01:34:13 +00:00
|
|
|
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 {
|
2020-07-09 04:14:56 +00:00
|
|
|
// Container that holds the label.
|
|
|
|
lbox, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
|
|
|
|
lbox.SetVAlign(gtk.ALIGN_CENTER)
|
|
|
|
lbox.Show()
|
|
|
|
|
|
|
|
// Label for the primary text.
|
2020-06-13 07:29:32 +00:00
|
|
|
l := rich.NewLabel(entry.Text)
|
|
|
|
l.Show()
|
2020-07-09 04:14:56 +00:00
|
|
|
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.Empty() {
|
|
|
|
size = ImageLarge
|
|
|
|
|
|
|
|
s := rich.NewLabel(text.Rich{})
|
|
|
|
s.SetMarkup(fmt.Sprintf(
|
|
|
|
`<span alpha="50%%" size="small">%s</span>`,
|
2020-07-14 07:24:55 +00:00
|
|
|
markup.Render(entry.Secondary),
|
2020-07-09 04:14:56 +00:00
|
|
|
))
|
|
|
|
s.Show()
|
|
|
|
|
|
|
|
lbox.PackStart(s, false, false, 0)
|
|
|
|
}
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2020-07-09 04:14:56 +00:00
|
|
|
b, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
|
|
|
|
b.PackEnd(lbox, true, true, ImagePadding)
|
|
|
|
b.Show()
|
2020-06-13 07:29:32 +00:00
|
|
|
|
|
|
|
// Do we have an icon?
|
|
|
|
if entry.IconURL != "" {
|
2020-07-09 04:14:56 +00:00
|
|
|
img, _ := gtk.ImageNew()
|
2020-07-01 18:49:20 +00:00
|
|
|
img.SetMarginStart(ImagePadding)
|
2020-07-09 04:14:56 +00:00
|
|
|
img.SetSizeRequest(size, size)
|
2020-07-01 01:34:13 +00:00
|
|
|
img.Show()
|
2020-07-01 18:49:20 +00:00
|
|
|
|
2020-07-09 04:14:56 +00:00
|
|
|
// Prepend the image into the box.
|
|
|
|
b.PackEnd(img, false, false, 0)
|
|
|
|
|
2020-07-01 18:49:20 +00:00
|
|
|
var pps []imgutil.Processor
|
|
|
|
if !entry.Image {
|
|
|
|
pps = ppIcon
|
|
|
|
}
|
|
|
|
|
2020-07-09 04:14:56 +00:00
|
|
|
httputil.AsyncImageSized(img, entry.IconURL, size, size, pps...)
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|