This commit is contained in:
2026-05-23 06:45:18 +01:00
parent af0ec78eae
commit 9ad48ff310
5 changed files with 52 additions and 31 deletions
+19 -1
View File
@@ -4,6 +4,7 @@ import (
_ "embed" _ "embed"
"github.com/diamondburned/gotk4/pkg/gdk/v4" "github.com/diamondburned/gotk4/pkg/gdk/v4"
"github.com/diamondburned/gotk4/pkg/gdkpixbuf/v2" "github.com/diamondburned/gotk4/pkg/gdkpixbuf/v2"
"sync"
) )
//go:embed debug.png //go:embed debug.png
@@ -146,11 +147,28 @@ var failBytes []byte
//go:embed assets/please_wait.gif //go:embed assets/please_wait.gif
var pleaseWaitBytes []byte var pleaseWaitBytes []byte
// var clientAssets map[string]gdk.Paintabler = make(map[string]gdk.Paintabler)
var clientAssets sync.Map
func loadAsset(key string, data []byte) { func loadAsset(key string, data []byte) {
loader := gdkpixbuf.NewPixbufLoader() loader := gdkpixbuf.NewPixbufLoader()
loader.Write(data) loader.Write(data)
loader.Close() loader.Close()
clientAssets[key] = gdk.NewTextureForPixbuf(loader.Pixbuf()) clientAssets.Store(key, gdk.NewTextureForPixbuf(loader.Pixbuf()))
}
func clientAssetsLoad(key string) gdk.Paintabler {
dat, ok := clientAssets.Load(key)
if !ok {
return nil
}
paintable, ok := dat.(gdk.Paintabler)
if !ok {
return nil
}
return paintable
} }
func init() { func init() {
+7 -3
View File
@@ -14,6 +14,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"errors"
) )
// global or app-level map/cache // global or app-level map/cache
@@ -46,7 +47,11 @@ func getTexture(path string) (gdk.Paintabler, error) {
return nil, err return nil, err
} }
textureCache.Store(path, tex) textureCache.Store(path, tex)
return tex.(gdk.Paintabler), nil t, ok := tex.(gdk.Paintabler)
if !ok {
return nil, errors.New("invalid type")
}
return t, nil
} }
func newPictureFromPath(path string) (*gtk.Picture, error) { func newPictureFromPath(path string) (*gtk.Picture, error) {
@@ -63,8 +68,7 @@ func newImageFromPath(path string) (*gtk.Image, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
img := gtk.NewImageFromPaintable(tex) return gtk.NewImageFromPaintable(tex), nil
return img, nil
} }
func newPictureFromWeb(url string) (*gtk.Picture, error) { func newPictureFromWeb(url string) (*gtk.Picture, error) {
+12 -12
View File
@@ -98,7 +98,7 @@ func switchToTab(jid string, w *gtk.Window) {
m, ok := mucmembers.Load(jid) m, ok := mucmembers.Load(jid)
if !ok { if !ok {
box := gtk.NewBox(gtk.OrientationVertical, 10) box := gtk.NewBox(gtk.OrientationVertical, 10)
failed_icon := gtk.NewImageFromPaintable(clientAssets["fail"]) failed_icon := gtk.NewImageFromPaintable(clientAssetsLoad("fail"))
failed_icon.SetPixelSize(100) failed_icon.SetPixelSize(100)
box.Append(failed_icon) box.Append(failed_icon)
label := gtk.NewLabel("There was an error loading the members of this room. Maybe the MUC does not give user presence, it does not exist, you have been banned from it, or you have to fill a CAPTCHA to access it. Try joining the room again or check if the room exists.") label := gtk.NewLabel("There was an error loading the members of this room. Maybe the MUC does not give user presence, it does not exist, you have been banned from it, or you have to fill a CAPTCHA to access it. Try joining the room again or check if the room exists.")
@@ -110,7 +110,7 @@ func switchToTab(jid string, w *gtk.Window) {
ma, ok := m.(mucUnit) ma, ok := m.(mucUnit)
if !ok { if !ok {
box := gtk.NewBox(gtk.OrientationVertical, 10) box := gtk.NewBox(gtk.OrientationVertical, 10)
failed_icon := gtk.NewImageFromPaintable(clientAssets["fail"]) failed_icon := gtk.NewImageFromPaintable(clientAssetsLoad("fail"))
failed_icon.SetPixelSize(100) failed_icon.SetPixelSize(100)
box.Append(failed_icon) box.Append(failed_icon)
label := gtk.NewLabel("There was an error loading the members of this room. Maybe the MUC does not give user presence, it does not exist, you have been banned from it, or you have to fill a CAPTCHA to access it. Try joining the room again or check if the room exists.") label := gtk.NewLabel("There was an error loading the members of this room. Maybe the MUC does not give user presence, it does not exist, you have been banned from it, or you have to fill a CAPTCHA to access it. Try joining the room again or check if the room exists.")
@@ -196,7 +196,7 @@ func switchToTab(jid string, w *gtk.Window) {
} }
} }
status := gtk.NewImageFromPaintable(clientAssets["status_"+string(u.Show)]) status := gtk.NewImageFromPaintable(clientAssetsLoad("status_"+string(u.Show)))
status.SetTooltipText(string(u.Show)) status.SetTooltipText(string(u.Show))
status.SetHAlign(gtk.AlignEnd) status.SetHAlign(gtk.AlignEnd)
@@ -204,13 +204,13 @@ func switchToTab(jid string, w *gtk.Window) {
userbox.Prepend(status) userbox.Prepend(status)
if u.Status != "" { if u.Status != "" {
status_message := gtk.NewImageFromPaintable(clientAssets["comment"]) status_message := gtk.NewImageFromPaintable(clientAssetsLoad("comment"))
status_message.SetTooltipText(u.Status) status_message.SetTooltipText(u.Status)
status_message.SetHAlign(gtk.AlignEnd) status_message.SetHAlign(gtk.AlignEnd)
userbox.Append(status_message) userbox.Append(status_message)
} }
medal := gtk.NewImageFromPaintable(clientAssets[mu.MucUserItem.Affiliation]) medal := gtk.NewImageFromPaintable(clientAssetsLoad(mu.MucUserItem.Affiliation))
medal.SetTooltipText(mu.MucUserItem.Affiliation) medal.SetTooltipText(mu.MucUserItem.Affiliation)
medal.SetHAlign(gtk.AlignEnd) medal.SetHAlign(gtk.AlignEnd)
@@ -560,11 +560,11 @@ func switchToTab(jid string, w *gtk.Window) {
headerBox := gtk.NewBox(gtk.OrientationHorizontal, 0) headerBox := gtk.NewBox(gtk.OrientationHorizontal, 0)
if i >= 500 { if i >= 500 {
headerBox.Append(gtk.NewImageFromPaintable(clientAssets["world"])) headerBox.Append(gtk.NewImageFromPaintable(clientAssetsLoad("world")))
} else if i >= 50 { } else if i >= 50 {
headerBox.Append(gtk.NewImageFromPaintable(clientAssets["large_group"])) headerBox.Append(gtk.NewImageFromPaintable(clientAssetsLoad("large_group")))
} else { } else {
headerBox.Append(gtk.NewImageFromPaintable(clientAssets["group"])) headerBox.Append(gtk.NewImageFromPaintable(clientAssetsLoad("group")))
} }
headerBox.Append(gtk.NewLabel(fmt.Sprintf("%d %s", i, loadedLocale["participants"]))) headerBox.Append(gtk.NewLabel(fmt.Sprintf("%d %s", i, loadedLocale["participants"])))
gen.Prepend(headerBox) gen.Prepend(headerBox)
@@ -614,9 +614,7 @@ func showErrorDialog(err error, w *gtk.Window) {
func createIdenticon(word string, always_create bool) *gtk.Image { // This function generates an identicon func createIdenticon(word string, always_create bool) *gtk.Image { // This function generates an identicon
if !loadedConfig.Identicons && !always_create { if !loadedConfig.Identicons && !always_create {
i := gtk.NewImageFromPaintable(clientAssets["DefaultAvatar"]) return gtk.NewImageFromPaintable(clientAssetsLoad("DefaultAvatar"))
i.AddCSSClass(loadedConfig.CVD.String() + "_CVD")
return i
} }
identicon.SetBackgroundColorFunction(func([]byte, color.Color) color.Color { identicon.SetBackgroundColorFunction(func([]byte, color.Color) color.Color {
@@ -636,7 +634,9 @@ func createIdenticon(word string, always_create bool) *gtk.Image { // This funct
loader := gdkpixbuf.NewPixbufLoader() loader := gdkpixbuf.NewPixbufLoader()
loader.Write(buf.Bytes()) loader.Write(buf.Bytes())
loader.Close() loader.Close()
i := gtk.NewImageFromPaintable(gdk.NewTextureForPixbuf(loader.Pixbuf())) p := loader.Pixbuf()
gt := gdk.NewTextureForPixbuf(p)
i := gtk.NewImageFromPaintable(gt)
i.AddCSSClass(loadedConfig.CVD.String() + "_CVD") i.AddCSSClass(loadedConfig.CVD.String() + "_CVD")
return i return i
+4 -4
View File
@@ -32,16 +32,16 @@ func generatePresenceWidget(p stanza.Packet) gtk.Widgetter {
ok := presence.Get(&mu) ok := presence.Get(&mu)
if ok { if ok {
if mu.MucUserItem.Affiliation == "outcast" { if mu.MucUserItem.Affiliation == "outcast" {
b.Append(gtk.NewImageFromPaintable(clientAssets["outcast"])) b.Append(gtk.NewImageFromPaintable(clientAssetsLoad("outcast")))
b.Append(gtk.NewLabel(JidMustParse(presence.From).Resource + loadedLocale["bannedWidget"] + mu.MucUserItem.Actor.Nick + "!")) b.Append(gtk.NewLabel(JidMustParse(presence.From).Resource + loadedLocale["bannedWidget"] + mu.MucUserItem.Actor.Nick + "!"))
return b return b
} }
} }
b.Append(gtk.NewImageFromPaintable(clientAssets["door_out"])) b.Append(gtk.NewImageFromPaintable(clientAssetsLoad("door_out")))
b.Append(gtk.NewLabel(JidMustParse(presence.From).Resource)) b.Append(gtk.NewLabel(JidMustParse(presence.From).Resource))
} else { } else {
b.Append(gtk.NewImageFromPaintable(clientAssets["door_in"])) b.Append(gtk.NewImageFromPaintable(clientAssetsLoad("door_in")))
b.Append(gtk.NewLabel(JidMustParse(presence.From).Resource)) b.Append(gtk.NewLabel(JidMustParse(presence.From).Resource))
} }
@@ -73,7 +73,7 @@ func generateMessageWidget(p stanza.Packet) gtk.Widgetter {
if m.Error.Type != "" { if m.Error.Type != "" {
error_box := gtk.NewBox(gtk.OrientationHorizontal, 0) error_box := gtk.NewBox(gtk.OrientationHorizontal, 0)
cancel_img := gtk.NewImageFromPaintable(clientAssets["cancel"]) cancel_img := gtk.NewImageFromPaintable(clientAssetsLoad("cancel"))
error_label := gtk.NewLabel(m.Error.Text + ": ") error_label := gtk.NewLabel(m.Error.Text + ": ")
error_box.Append(cancel_img) error_box.Append(cancel_img)
+10 -11
View File
@@ -76,7 +76,6 @@ var userdevices sync.Map
var pingTimes = [][]float64{} var pingTimes = [][]float64{}
var clientAssets map[string]gdk.Paintabler = make(map[string]gdk.Paintabler)
var xmlLog *os.File var xmlLog *os.File
@@ -420,7 +419,7 @@ func main() {
c, err := xmpp.NewClient(&config, router, func(err error) { c, err := xmpp.NewClient(&config, router, func(err error) {
connectionStatus.SetText(fmt.Sprintf("%s%s", loadedLocale["disconnected"], err.Error())) connectionStatus.SetText(fmt.Sprintf("%s%s", loadedLocale["disconnected"], err.Error()))
connectionIcon.SetFromPaintable(clientAssets["disconnect"]) connectionIcon.SetFromPaintable(clientAssetsLoad("disconnect"))
}) })
if err != nil { if err != nil {
@@ -497,7 +496,7 @@ func main() {
glib.IdleAdd(func() { glib.IdleAdd(func() {
connectionStatus.SetText(fmt.Sprintf("%s%s", loadedLocale["connectedAs"], JidMustParse(clientroot.Session.BindJid).Bare())) connectionStatus.SetText(fmt.Sprintf("%s%s", loadedLocale["connectedAs"], JidMustParse(clientroot.Session.BindJid).Bare()))
connectionStatus.SetTooltipText(fmt.Sprintf("%s%s\n%s%t", loadedLocale["bindedJid"], clientroot.Session.BindJid, loadedLocale["usingTLS"], clientroot.Session.TlsEnabled)) connectionStatus.SetTooltipText(fmt.Sprintf("%s%s\n%s%t", loadedLocale["bindedJid"], clientroot.Session.BindJid, loadedLocale["usingTLS"], clientroot.Session.TlsEnabled))
connectionIcon.SetFromPaintable(clientAssets["connect"]) connectionIcon.SetFromPaintable(clientAssetsLoad("connect"))
}) })
// Enable carbons // Enable carbons
client.SendRaw(fmt.Sprintf( client.SendRaw(fmt.Sprintf(
@@ -662,13 +661,13 @@ func main() {
conc := func() { conc := func() {
// time.Sleep(3 * time.Second) // time.Sleep(3 * time.Second)
connectionStatus.SetText(loadedLocale["connecting"]) connectionStatus.SetText(loadedLocale["connecting"])
connectionIcon.SetFromPaintable(clientAssets["hourglass"]) connectionIcon.SetFromPaintable(clientAssetsLoad("hourglass"))
err = cm.Run() err = cm.Run()
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
connectionStatus.SetText(fmt.Sprintf("%s%s", loadedLocale["disconnected"], err.Error())) connectionStatus.SetText(fmt.Sprintf("%s%s", loadedLocale["disconnected"], err.Error()))
connectionIcon.SetFromPaintable(clientAssets["disconnect"]) connectionIcon.SetFromPaintable(clientAssetsLoad("disconnect"))
} }
} }
@@ -831,7 +830,7 @@ func activate(app *gtk.Application) {
nick_entry.SetText(loadedConfig.Nick) nick_entry.SetText(loadedConfig.Nick)
create_jid := gtk.NewImageFromPaintable(clientAssets["jabber"]) create_jid := gtk.NewImageFromPaintable(clientAssetsLoad("jabber"))
gesture := gtk.NewGestureClick() gesture := gtk.NewGestureClick()
gesture.SetButton(1) gesture.SetButton(1)
gesture.Connect("pressed", func() { gesture.Connect("pressed", func() {
@@ -990,7 +989,7 @@ func activate(app *gtk.Application) {
warning_box.Append(header) warning_box.Append(header)
addFeature := func(icon string, description string) { addFeature := func(icon string, description string) {
box := gtk.NewBox(gtk.OrientationHorizontal, 10) box := gtk.NewBox(gtk.OrientationHorizontal, 10)
box.Append(gtk.NewImageFromPaintable(clientAssets[icon])) box.Append(gtk.NewImageFromPaintable(clientAssetsLoad(icon)))
box.Append(gtk.NewLabel(description)) box.Append(gtk.NewLabel(description))
warning_box.Append(box) warning_box.Append(box)
} }
@@ -1072,7 +1071,7 @@ func activate(app *gtk.Application) {
window.Window.SetDefaultSize(500, 500) window.Window.SetDefaultSize(500, 500)
menu = gtk.NewBox(gtk.OrientationVertical, 0) menu = gtk.NewBox(gtk.OrientationVertical, 0)
empty_dialog = gtk.NewImageFromPaintable(clientAssets["disabled_logo"]) empty_dialog = gtk.NewImageFromPaintable(clientAssetsLoad("disabled_logo"))
empty_dialog.SetPixelSize(100) empty_dialog.SetPixelSize(100)
empty_dialog.SetVExpand(true) empty_dialog.SetVExpand(true)
@@ -1088,7 +1087,7 @@ func activate(app *gtk.Application) {
statBar := gtk.NewBox(gtk.OrientationHorizontal, 0) statBar := gtk.NewBox(gtk.OrientationHorizontal, 0)
cBox := gtk.NewBox(gtk.OrientationHorizontal, 0) cBox := gtk.NewBox(gtk.OrientationHorizontal, 0)
connectionIcon = gtk.NewImageFromPaintable((clientAssets["disconnect"])) connectionIcon = gtk.NewImageFromPaintable((clientAssetsLoad("disconnect")))
connectionIcon.AddCSSClass("icon") connectionIcon.AddCSSClass("icon")
connectionStatus = gtk.NewLabel(loadedLocale["disconnected"]) connectionStatus = gtk.NewLabel(loadedLocale["disconnected"])
@@ -1106,7 +1105,7 @@ func activate(app *gtk.Application) {
switchToTab(current, &window.Window) switchToTab(current, &window.Window)
}) })
mIcon = gtk.NewImageFromPaintable((clientAssets["comment"])) mIcon = gtk.NewImageFromPaintable((clientAssetsLoad("comment")))
mIcon.AddCSSClass("icon") mIcon.AddCSSClass("icon")
mStatus = gtk.NewLabel("-") mStatus = gtk.NewLabel("-")
mStatus.AddController(gesture1) mStatus.AddController(gesture1)
@@ -1167,7 +1166,7 @@ func activate(app *gtk.Application) {
}) })
pBox.AddController(gesture) pBox.AddController(gesture)
i := (gtk.NewImageFromPaintable(clientAssets["chart_bar"])) i := (gtk.NewImageFromPaintable(clientAssetsLoad("chart_bar")))
i.AddCSSClass("icon") i.AddCSSClass("icon")
pBox.Append(i) pBox.Append(i)