2020-07-14 07:24:55 +00:00
|
|
|
package labeluri
|
2020-06-28 01:35:26 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-08 09:07:00 +00:00
|
|
|
"fmt"
|
|
|
|
"html"
|
2020-06-28 01:35:26 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts/httputil"
|
2020-07-08 09:07:00 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/log"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/dialog"
|
2020-06-28 01:35:26 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
|
2020-07-14 07:24:55 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives/roundimage"
|
2020-08-13 22:50:51 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives/scrollinput"
|
2020-07-14 07:24:55 +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-06-28 01:35:26 +00:00
|
|
|
"github.com/gotk3/gotk3/gdk"
|
|
|
|
"github.com/gotk3/gotk3/gtk"
|
2020-07-08 09:07:00 +00:00
|
|
|
"github.com/gotk3/gotk3/pango"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/skratchdot/open-golang/open"
|
2020-06-28 01:35:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-13 22:50:51 +00:00
|
|
|
AvatarSize = 96
|
|
|
|
PopoverWidth = 250
|
|
|
|
MaxWidth = 350
|
|
|
|
MaxHeight = 350
|
2020-06-28 01:35:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WidgetConnector interface {
|
|
|
|
gtk.IWidget
|
|
|
|
primitives.Connector
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ WidgetConnector = (*gtk.Label)(nil)
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// Labeler implements a rich label that stores an output state.
|
|
|
|
type Labeler interface {
|
|
|
|
WidgetConnector
|
|
|
|
rich.Labeler
|
|
|
|
Output() markup.RenderOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
// Label implements a label that's already bounded to the markup URI handlers.
|
|
|
|
type Label struct {
|
|
|
|
*rich.Label
|
|
|
|
output markup.RenderOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ Labeler = (*Label)(nil)
|
|
|
|
_ rich.SuperLabeler = (*Label)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewLabel(txt text.Rich) *Label {
|
|
|
|
l := &Label{}
|
|
|
|
l.Label = rich.NewInheritLabel(l)
|
|
|
|
l.Label.SetLabelUnsafe(txt) // test
|
|
|
|
|
|
|
|
// Bind and return.
|
|
|
|
BindRichLabel(l)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) Reset() {
|
|
|
|
l.output = markup.RenderOutput{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Label) SetLabelUnsafe(content text.Rich) {
|
|
|
|
l.output = markup.RenderCmplx(content)
|
|
|
|
l.SetMarkup(l.output.Markup)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output returns the label's markup output. This function is NOT
|
|
|
|
// thread-safe.
|
|
|
|
func (l *Label) Output() markup.RenderOutput {
|
|
|
|
return l.output
|
|
|
|
}
|
|
|
|
|
2020-07-18 07:16:47 +00:00
|
|
|
// SetOutput sets the internal output and label.
|
|
|
|
func (l *Label) SetOutput(o markup.RenderOutput) {
|
|
|
|
l.output = o
|
|
|
|
l.SetMarkup(o.Markup)
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
func BindRichLabel(label Labeler) {
|
|
|
|
bind(label, func(uri string, ptr gdk.Rectangle) bool {
|
|
|
|
var output = label.Output()
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
if segment := output.IsMention(uri); segment != nil {
|
|
|
|
if p := NewPopoverMentioner(label, output.Input, segment); p != nil {
|
2020-07-14 07:24:55 +00:00
|
|
|
p.SetPointingTo(ptr)
|
|
|
|
p.Popup()
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
func PopoverMentioner(rel gtk.IWidget, input string, mention text.Segment) {
|
2020-08-17 00:13:47 +00:00
|
|
|
if p := NewPopoverMentioner(rel, input, mention); p != nil {
|
2020-07-17 00:21:14 +00:00
|
|
|
p.Popup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
func NewPopoverMentioner(rel gtk.IWidget, input string, segment text.Segment) *gtk.Popover {
|
|
|
|
var mention = segment.AsMentioner()
|
|
|
|
if mention == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:21:14 +00:00
|
|
|
var info = mention.MentionInfo()
|
2020-10-15 06:32:11 +00:00
|
|
|
if info.IsEmpty() {
|
2020-07-17 00:21:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
start, end := segment.Bounds()
|
2020-08-13 22:50:51 +00:00
|
|
|
h := input[start:end]
|
|
|
|
|
|
|
|
box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
|
|
|
|
box.Show()
|
|
|
|
|
|
|
|
// Do we have an image or an avatar?
|
|
|
|
var url string
|
|
|
|
var round bool
|
|
|
|
|
2020-10-15 06:32:11 +00:00
|
|
|
if avatarer := segment.AsAvatarer(); avatarer != nil {
|
|
|
|
url = avatarer.Avatar()
|
2020-08-13 22:50:51 +00:00
|
|
|
round = true
|
2020-10-15 06:32:11 +00:00
|
|
|
} else if imager := segment.AsImager(); imager != nil {
|
|
|
|
url = imager.Image()
|
2020-08-13 22:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if url != "" {
|
|
|
|
box.PackStart(popoverImg(url, round), false, false, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
head, _ := gtk.LabelNew(largeText(h))
|
|
|
|
head.SetUseMarkup(true)
|
|
|
|
head.SetLineWrap(true)
|
|
|
|
head.SetLineWrapMode(pango.WRAP_WORD_CHAR)
|
|
|
|
head.SetMarginStart(8)
|
|
|
|
head.SetMarginEnd(8)
|
|
|
|
head.Show()
|
|
|
|
box.PackStart(head, false, false, 0)
|
|
|
|
|
|
|
|
// Left-align the label if we don't have an image.
|
|
|
|
if url == "" {
|
|
|
|
head.SetXAlign(0)
|
|
|
|
}
|
|
|
|
|
2020-07-17 00:21:14 +00:00
|
|
|
l, _ := gtk.LabelNew(markup.Render(info))
|
|
|
|
l.SetUseMarkup(true)
|
2020-08-13 22:50:51 +00:00
|
|
|
l.SetLineWrapMode(pango.WRAP_WORD_CHAR)
|
|
|
|
l.SetLineWrap(true)
|
2020-07-17 00:21:14 +00:00
|
|
|
l.SetXAlign(0)
|
2020-08-13 22:50:51 +00:00
|
|
|
l.SetMarginStart(8)
|
|
|
|
l.SetMarginEnd(8)
|
|
|
|
l.SetMarginTop(8)
|
|
|
|
l.SetMarginBottom(8)
|
2020-07-17 00:21:14 +00:00
|
|
|
l.Show()
|
|
|
|
|
|
|
|
// Enable images???
|
|
|
|
BindActivator(l)
|
|
|
|
|
2020-08-13 22:50:51 +00:00
|
|
|
// Make a scrolling text.
|
|
|
|
scr := scrollinput.NewVScroll(PopoverWidth)
|
|
|
|
scr.Show()
|
|
|
|
scr.Add(l)
|
|
|
|
box.PackStart(scr, false, false, 0)
|
|
|
|
|
2020-07-17 00:21:14 +00:00
|
|
|
p, _ := gtk.PopoverNew(rel)
|
2020-08-13 22:50:51 +00:00
|
|
|
p.Add(box)
|
|
|
|
p.SetSizeRequest(PopoverWidth, -1)
|
|
|
|
p.Connect("destroy", box.Destroy)
|
2020-07-17 00:21:14 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-08-13 22:50:51 +00:00
|
|
|
func largeText(text string) string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
`<span insert-hyphens="false" size="large">%s</span>`, html.EscapeString(text),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// popoverImg creates a new button with an image for it, which is used for the
|
|
|
|
// avatar in the user popover.
|
|
|
|
func popoverImg(url string, round bool) gtk.IWidget {
|
|
|
|
var btn *gtk.Button
|
2020-08-20 23:53:13 +00:00
|
|
|
var img *gtk.Image
|
2020-12-20 08:25:24 +00:00
|
|
|
var idl httputil.ImageContainer
|
2020-08-13 22:50:51 +00:00
|
|
|
|
|
|
|
if round {
|
|
|
|
b, _ := roundimage.NewButton()
|
2020-08-20 23:53:13 +00:00
|
|
|
img = b.Image.GetImage()
|
|
|
|
idl = b.Image
|
2020-08-13 22:50:51 +00:00
|
|
|
btn = b.Button
|
|
|
|
} else {
|
|
|
|
img, _ = gtk.ImageNew()
|
|
|
|
btn, _ = gtk.ButtonNew()
|
|
|
|
btn.Add(img)
|
2020-08-20 23:53:13 +00:00
|
|
|
idl = img
|
2020-08-13 22:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
img.SetSizeRequest(AvatarSize, AvatarSize)
|
|
|
|
img.SetHAlign(gtk.ALIGN_CENTER)
|
|
|
|
img.Show()
|
|
|
|
|
2020-12-20 08:18:23 +00:00
|
|
|
httputil.AsyncImageSized(idl, url)
|
2020-08-13 22:50:51 +00:00
|
|
|
|
|
|
|
btn.SetHAlign(gtk.ALIGN_CENTER)
|
|
|
|
btn.SetRelief(gtk.RELIEF_NONE)
|
|
|
|
btn.Connect("clicked", func() { PromptOpen(url) })
|
|
|
|
btn.Show()
|
|
|
|
|
|
|
|
return btn
|
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
func BindActivator(connector WidgetConnector) {
|
|
|
|
bind(connector, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bind connects activate-link. If activator returns true, then nothing is done.
|
|
|
|
// Activator can be nil.
|
|
|
|
func bind(connector WidgetConnector, activator func(uri string, r gdk.Rectangle) bool) {
|
2020-06-28 01:35:26 +00:00
|
|
|
// This implementation doesn't seem like a good idea. First off, is the
|
|
|
|
// closure really garbage collected? If it's not, then we have some huge
|
|
|
|
// issues. Second, if the closure is garbage collected, then when? If it's
|
|
|
|
// not garbage collecteed, then not only are we leaking 2 float64s per
|
|
|
|
// message, but we're also keeping alive the widget.
|
|
|
|
|
|
|
|
var x, y float64
|
|
|
|
connector.Connect("motion-notify-event", func(w gtk.IWidget, ev *gdk.Event) {
|
2020-07-01 18:49:20 +00:00
|
|
|
x, y = gdk.EventMotionNewFromEvent(ev).MotionVal()
|
2020-06-28 01:35:26 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
connector.Connect("activate-link", func(c WidgetConnector, uri string) bool {
|
2020-07-14 07:24:55 +00:00
|
|
|
// Make a new rectangle to use in the popover.
|
|
|
|
r := gdk.Rectangle{}
|
|
|
|
r.SetX(int(x))
|
|
|
|
r.SetY(int(y))
|
|
|
|
|
|
|
|
if activator != nil && activator(uri, r) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-28 01:35:26 +00:00
|
|
|
switch ext(uri) {
|
|
|
|
case ".jpg", ".jpeg", ".png", ".webp", ".gif":
|
2020-07-08 09:07:00 +00:00
|
|
|
// Make a new image that's asynchronously fetched inside a button.
|
2020-07-14 07:24:55 +00:00
|
|
|
// Cap the width and height if requested.
|
|
|
|
var w, h, round = markup.FragmentImageSize(uri, MaxWidth, MaxHeight)
|
|
|
|
|
|
|
|
var img *gtk.Image
|
|
|
|
if !round {
|
|
|
|
img, _ = gtk.ImageNew()
|
|
|
|
} else {
|
|
|
|
r, _ := roundimage.NewImage(0)
|
|
|
|
img = r.Image
|
|
|
|
}
|
|
|
|
|
2020-12-20 08:18:23 +00:00
|
|
|
img.SetSizeRequest(w, h)
|
2020-07-14 07:24:55 +00:00
|
|
|
img.SetFromIconName("image-loading", gtk.ICON_SIZE_BUTTON)
|
2020-06-28 01:35:26 +00:00
|
|
|
img.Show()
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
// Asynchronously fetch the image.
|
2020-12-20 08:18:23 +00:00
|
|
|
httputil.AsyncImageSized(img, uri)
|
2020-06-28 01:35:26 +00:00
|
|
|
|
2020-07-08 09:07:00 +00:00
|
|
|
btn, _ := gtk.ButtonNew()
|
|
|
|
btn.Add(img)
|
|
|
|
btn.SetRelief(gtk.RELIEF_NONE)
|
|
|
|
btn.Connect("clicked", func() { PromptOpen(uri) })
|
|
|
|
btn.Show()
|
|
|
|
|
2020-06-28 01:35:26 +00:00
|
|
|
p, _ := gtk.PopoverNew(c)
|
|
|
|
p.SetPointingTo(r)
|
|
|
|
p.Connect("closed", img.Destroy) // on close, destroy image
|
2020-07-08 09:07:00 +00:00
|
|
|
p.Add(btn)
|
2020-06-28 01:35:26 +00:00
|
|
|
p.Popup()
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
return true
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 07:24:55 +00:00
|
|
|
PromptOpen(uri)
|
|
|
|
|
2020-07-08 09:07:00 +00:00
|
|
|
// Never let Gtk open the dialog.
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const urlPrompt = `This link leads to the following URL:
|
2020-07-09 04:14:56 +00:00
|
|
|
<span weight="bold" insert_hyphens="false"><a href="%[1]s">%[1]s</a></span>
|
2020-07-08 09:07:00 +00:00
|
|
|
Click <b>Open</b> to proceed.`
|
|
|
|
|
|
|
|
var warnLabelCSS = primitives.PrepareCSS(`
|
|
|
|
label {
|
|
|
|
padding: 4px 8px;
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
|
|
|
// PromptOpen shows a dialog asking if the URL should be opened.
|
|
|
|
func PromptOpen(uri string) {
|
|
|
|
// Format the prompt body.
|
|
|
|
l, _ := gtk.LabelNew("")
|
|
|
|
l.SetJustify(gtk.JUSTIFY_CENTER)
|
|
|
|
l.SetLineWrap(true)
|
|
|
|
l.SetLineWrapMode(pango.WRAP_WORD_CHAR)
|
|
|
|
l.Show()
|
|
|
|
l.SetMarkup(fmt.Sprintf(urlPrompt, html.EscapeString(uri)))
|
|
|
|
|
|
|
|
// Style the label.
|
|
|
|
primitives.AttachCSS(l, warnLabelCSS)
|
|
|
|
|
2020-07-10 23:26:07 +00:00
|
|
|
open := func(m *dialog.Modal) {
|
|
|
|
// Close the dialog.
|
|
|
|
m.Destroy()
|
|
|
|
// Open the link.
|
2020-07-08 09:07:00 +00:00
|
|
|
if err := open.Start(uri); err != nil {
|
|
|
|
log.Error(errors.Wrap(err, "Failed to open URL after confirm"))
|
2020-06-28 01:35:26 +00:00
|
|
|
}
|
2020-07-08 09:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prompt the user if they want to open the URL.
|
2020-07-10 23:26:07 +00:00
|
|
|
dlg := dialog.NewModal(l, "Caution", "_Open", open)
|
2020-07-08 09:07:00 +00:00
|
|
|
dlg.SetSizeRequest(350, 100)
|
|
|
|
|
2020-07-10 23:26:07 +00:00
|
|
|
// Style the button to have a color.
|
|
|
|
primitives.SuggestAction(dlg.Action)
|
|
|
|
|
2020-07-08 09:07:00 +00:00
|
|
|
// Add a class to the dialog to allow theming.
|
|
|
|
primitives.AddClass(dlg, "url-warning")
|
|
|
|
|
2020-07-10 23:26:07 +00:00
|
|
|
// On link click, close the dialog, open the link ourselves, then return.
|
2020-07-08 09:07:00 +00:00
|
|
|
l.Connect("activate-link", func(l *gtk.Label, uri string) bool {
|
2020-07-10 23:26:07 +00:00
|
|
|
open(dlg)
|
2020-07-08 09:07:00 +00:00
|
|
|
return true
|
2020-06-28 01:35:26 +00:00
|
|
|
})
|
2020-07-08 09:07:00 +00:00
|
|
|
|
|
|
|
// Show the dialog.
|
|
|
|
dlg.Show()
|
2020-06-28 01:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ext parses and sanitizes the extension to something comparable.
|
|
|
|
func ext(uri string) string {
|
|
|
|
u, err := url.Parse(uri)
|
|
|
|
if err != nil {
|
|
|
|
return strings.ToLower(path.Ext(uri))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToLower(path.Ext(u.Path))
|
|
|
|
}
|