113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
// This file contains the function that generates message widgets depending on message stanza
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"github.com/diamondburned/gotk4/pkg/gtk/v4"
|
||
|
|
"gosrc.io/xmpp/stanza"
|
||
|
|
"mellium.im/xmpp/jid"
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"github.com/jasonlovesdoggo/gopen"
|
||
|
|
)
|
||
|
|
|
||
|
|
func generateMessageWidget(p stanza.Packet) gtk.Widgetter {
|
||
|
|
m, ok := p.(stanza.Message)
|
||
|
|
if !ok {
|
||
|
|
return gtk.NewLabel("Unsupported message.")
|
||
|
|
}
|
||
|
|
sid := StanzaID{}
|
||
|
|
m.Get(&sid)
|
||
|
|
|
||
|
|
mainBox := gtk.NewBox(gtk.OrientationVertical, 0)
|
||
|
|
gesture := gtk.NewGestureClick()
|
||
|
|
gesture.SetButton(3) // Right click
|
||
|
|
|
||
|
|
vis := false
|
||
|
|
reactions := gtk.NewBox(gtk.OrientationHorizontal, 0)
|
||
|
|
reactions.SetVisible(false)
|
||
|
|
|
||
|
|
reaction := []string{"👍", "👎", "♥️", "🤣", "😭",}
|
||
|
|
for _, v := range reaction {
|
||
|
|
like := gtk.NewButton()
|
||
|
|
like.SetLabel(v)
|
||
|
|
like.SetHExpand(true)
|
||
|
|
like.ConnectClicked(func() {
|
||
|
|
fmt.Println("licked")
|
||
|
|
client.SendRaw(fmt.Sprintf(`
|
||
|
|
<message from='%s' to='%s' id='%s' type='%s'>
|
||
|
|
<reactions id='%s' xmlns='urn:xmpp:reactions:0'>
|
||
|
|
<reaction>%s</reaction>
|
||
|
|
</reactions>
|
||
|
|
</message>
|
||
|
|
`, m.To, jid.MustParse(m.From).Bare().String(), uuid.New().String(), m.Type, sid.ID, v))
|
||
|
|
})
|
||
|
|
reactions.Append(like)
|
||
|
|
}
|
||
|
|
|
||
|
|
gesture.Connect("pressed", func(n_press, x, y int) {
|
||
|
|
if !vis {
|
||
|
|
vis = true
|
||
|
|
reactions.SetVisible(true)
|
||
|
|
} else {
|
||
|
|
vis = false
|
||
|
|
reactions.SetVisible(false)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
mainBox.AddController(gesture)
|
||
|
|
|
||
|
|
reply := Reply{}
|
||
|
|
ok = m.Get(&reply)
|
||
|
|
if ok {
|
||
|
|
replyBox := gtk.NewBox(gtk.OrientationHorizontal, 0)
|
||
|
|
replyBox.Append(gtk.NewLabel("↱ " + jid.MustParse(reply.To).Resourcepart()))
|
||
|
|
mainBox.Append(replyBox)
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
authorBox := gtk.NewBox(gtk.OrientationHorizontal, 10)
|
||
|
|
contentBox := gtk.NewBox(gtk.OrientationHorizontal, 0)
|
||
|
|
im := newImageFromPath("debug.png")
|
||
|
|
|
||
|
|
im.SetPixelSize(40)
|
||
|
|
authorBox.Append(im)
|
||
|
|
al := gtk.NewLabel(jid.MustParse(m.From).Resourcepart())
|
||
|
|
al.AddCSSClass("author")
|
||
|
|
|
||
|
|
authorBox.Append(al)
|
||
|
|
mlabel := gtk.NewLabel(m.Body)
|
||
|
|
// mlabel.SetMarkup(convertXEPToPango(m.Body))
|
||
|
|
mlabel.SetWrap(true)
|
||
|
|
mlabel.SetSelectable(true)
|
||
|
|
mlabel.SetHAlign(gtk.AlignFill)
|
||
|
|
|
||
|
|
contentBox.Append(mlabel)
|
||
|
|
|
||
|
|
mainBox.Append(authorBox)
|
||
|
|
mainBox.Append(contentBox)
|
||
|
|
|
||
|
|
mainBox.Append(reactions)
|
||
|
|
|
||
|
|
oob := stanza.OOB{}
|
||
|
|
ok = m.Get(&oob)
|
||
|
|
if ok {
|
||
|
|
// media := newPictureFromWeb(oob.URL)
|
||
|
|
// media.SetCanShrink(false)
|
||
|
|
// mainBox.Append(media)
|
||
|
|
// media.AddCSSClass("chat_image")
|
||
|
|
mbtn := gtk.NewButtonWithLabel("🖼️")
|
||
|
|
mbtn.ConnectClicked(func(){
|
||
|
|
gopen.Open(oob.URL)
|
||
|
|
})
|
||
|
|
authorBox.Append(mbtn)
|
||
|
|
}
|
||
|
|
|
||
|
|
return mainBox
|
||
|
|
}
|
||
|
|
|
||
|
|
func getVAdjustment(scrolledWindow *gtk.ScrolledWindow) *gtk.Adjustment {
|
||
|
|
val := scrolledWindow.ObjectProperty("vadjustment").(*gtk.Adjustment)
|
||
|
|
return val
|
||
|
|
}
|