From 9ad48ff310121c34b1178d14ab212af48bc7e27d Mon Sep 17 00:00:00 2001 From: sunglocto Date: Sat, 23 May 2026 06:45:18 +0100 Subject: [PATCH] a --- assets.go | 20 +++++++++++++++++++- cache.go | 10 +++++++--- gtk-helpers.go | 24 ++++++++++++------------ gtk-message.go | 8 ++++---- main.go | 21 ++++++++++----------- 5 files changed, 52 insertions(+), 31 deletions(-) diff --git a/assets.go b/assets.go index 2d772dd..5149abc 100644 --- a/assets.go +++ b/assets.go @@ -4,6 +4,7 @@ import ( _ "embed" "github.com/diamondburned/gotk4/pkg/gdk/v4" "github.com/diamondburned/gotk4/pkg/gdkpixbuf/v2" + "sync" ) //go:embed debug.png @@ -146,11 +147,28 @@ var failBytes []byte //go:embed assets/please_wait.gif var pleaseWaitBytes []byte +// var clientAssets map[string]gdk.Paintabler = make(map[string]gdk.Paintabler) +var clientAssets sync.Map + func loadAsset(key string, data []byte) { loader := gdkpixbuf.NewPixbufLoader() loader.Write(data) 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() { diff --git a/cache.go b/cache.go index eb52a02..7a837f1 100644 --- a/cache.go +++ b/cache.go @@ -14,6 +14,7 @@ import ( "os" "path/filepath" "sync" + "errors" ) // global or app-level map/cache @@ -46,7 +47,11 @@ func getTexture(path string) (gdk.Paintabler, error) { return nil, err } 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) { @@ -63,8 +68,7 @@ func newImageFromPath(path string) (*gtk.Image, error) { if err != nil { return nil, err } - img := gtk.NewImageFromPaintable(tex) - return img, nil + return gtk.NewImageFromPaintable(tex), nil } func newPictureFromWeb(url string) (*gtk.Picture, error) { diff --git a/gtk-helpers.go b/gtk-helpers.go index c047643..ec22f9a 100644 --- a/gtk-helpers.go +++ b/gtk-helpers.go @@ -98,7 +98,7 @@ func switchToTab(jid string, w *gtk.Window) { m, ok := mucmembers.Load(jid) if !ok { box := gtk.NewBox(gtk.OrientationVertical, 10) - failed_icon := gtk.NewImageFromPaintable(clientAssets["fail"]) + failed_icon := gtk.NewImageFromPaintable(clientAssetsLoad("fail")) failed_icon.SetPixelSize(100) 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.") @@ -110,7 +110,7 @@ func switchToTab(jid string, w *gtk.Window) { ma, ok := m.(mucUnit) if !ok { box := gtk.NewBox(gtk.OrientationVertical, 10) - failed_icon := gtk.NewImageFromPaintable(clientAssets["fail"]) + failed_icon := gtk.NewImageFromPaintable(clientAssetsLoad("fail")) failed_icon.SetPixelSize(100) 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.") @@ -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.SetHAlign(gtk.AlignEnd) @@ -204,13 +204,13 @@ func switchToTab(jid string, w *gtk.Window) { userbox.Prepend(status) if u.Status != "" { - status_message := gtk.NewImageFromPaintable(clientAssets["comment"]) + status_message := gtk.NewImageFromPaintable(clientAssetsLoad("comment")) status_message.SetTooltipText(u.Status) status_message.SetHAlign(gtk.AlignEnd) userbox.Append(status_message) } - medal := gtk.NewImageFromPaintable(clientAssets[mu.MucUserItem.Affiliation]) + medal := gtk.NewImageFromPaintable(clientAssetsLoad(mu.MucUserItem.Affiliation)) medal.SetTooltipText(mu.MucUserItem.Affiliation) medal.SetHAlign(gtk.AlignEnd) @@ -560,11 +560,11 @@ func switchToTab(jid string, w *gtk.Window) { headerBox := gtk.NewBox(gtk.OrientationHorizontal, 0) if i >= 500 { - headerBox.Append(gtk.NewImageFromPaintable(clientAssets["world"])) + headerBox.Append(gtk.NewImageFromPaintable(clientAssetsLoad("world"))) } else if i >= 50 { - headerBox.Append(gtk.NewImageFromPaintable(clientAssets["large_group"])) + headerBox.Append(gtk.NewImageFromPaintable(clientAssetsLoad("large_group"))) } else { - headerBox.Append(gtk.NewImageFromPaintable(clientAssets["group"])) + headerBox.Append(gtk.NewImageFromPaintable(clientAssetsLoad("group"))) } headerBox.Append(gtk.NewLabel(fmt.Sprintf("%d %s", i, loadedLocale["participants"]))) 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 if !loadedConfig.Identicons && !always_create { - i := gtk.NewImageFromPaintable(clientAssets["DefaultAvatar"]) - i.AddCSSClass(loadedConfig.CVD.String() + "_CVD") - return i + return gtk.NewImageFromPaintable(clientAssetsLoad("DefaultAvatar")) } 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.Write(buf.Bytes()) 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") return i diff --git a/gtk-message.go b/gtk-message.go index 2220b9d..41c5fef 100644 --- a/gtk-message.go +++ b/gtk-message.go @@ -32,16 +32,16 @@ func generatePresenceWidget(p stanza.Packet) gtk.Widgetter { ok := presence.Get(&mu) if ok { 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 + "!")) return b } } - b.Append(gtk.NewImageFromPaintable(clientAssets["door_out"])) + b.Append(gtk.NewImageFromPaintable(clientAssetsLoad("door_out"))) b.Append(gtk.NewLabel(JidMustParse(presence.From).Resource)) } else { - b.Append(gtk.NewImageFromPaintable(clientAssets["door_in"])) + b.Append(gtk.NewImageFromPaintable(clientAssetsLoad("door_in"))) b.Append(gtk.NewLabel(JidMustParse(presence.From).Resource)) } @@ -73,7 +73,7 @@ func generateMessageWidget(p stanza.Packet) gtk.Widgetter { if m.Error.Type != "" { 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_box.Append(cancel_img) diff --git a/main.go b/main.go index 700f10e..74833d9 100644 --- a/main.go +++ b/main.go @@ -76,7 +76,6 @@ var userdevices sync.Map var pingTimes = [][]float64{} -var clientAssets map[string]gdk.Paintabler = make(map[string]gdk.Paintabler) var xmlLog *os.File @@ -420,7 +419,7 @@ func main() { c, err := xmpp.NewClient(&config, router, func(err error) { connectionStatus.SetText(fmt.Sprintf("%s%s", loadedLocale["disconnected"], err.Error())) - connectionIcon.SetFromPaintable(clientAssets["disconnect"]) + connectionIcon.SetFromPaintable(clientAssetsLoad("disconnect")) }) if err != nil { @@ -497,7 +496,7 @@ func main() { glib.IdleAdd(func() { 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)) - connectionIcon.SetFromPaintable(clientAssets["connect"]) + connectionIcon.SetFromPaintable(clientAssetsLoad("connect")) }) // Enable carbons client.SendRaw(fmt.Sprintf( @@ -662,13 +661,13 @@ func main() { conc := func() { // time.Sleep(3 * time.Second) connectionStatus.SetText(loadedLocale["connecting"]) - connectionIcon.SetFromPaintable(clientAssets["hourglass"]) + connectionIcon.SetFromPaintable(clientAssetsLoad("hourglass")) err = cm.Run() if err != nil { fmt.Println(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) - create_jid := gtk.NewImageFromPaintable(clientAssets["jabber"]) + create_jid := gtk.NewImageFromPaintable(clientAssetsLoad("jabber")) gesture := gtk.NewGestureClick() gesture.SetButton(1) gesture.Connect("pressed", func() { @@ -990,7 +989,7 @@ func activate(app *gtk.Application) { warning_box.Append(header) addFeature := func(icon string, description string) { box := gtk.NewBox(gtk.OrientationHorizontal, 10) - box.Append(gtk.NewImageFromPaintable(clientAssets[icon])) + box.Append(gtk.NewImageFromPaintable(clientAssetsLoad(icon))) box.Append(gtk.NewLabel(description)) warning_box.Append(box) } @@ -1072,7 +1071,7 @@ func activate(app *gtk.Application) { window.Window.SetDefaultSize(500, 500) 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.SetVExpand(true) @@ -1088,7 +1087,7 @@ func activate(app *gtk.Application) { statBar := gtk.NewBox(gtk.OrientationHorizontal, 0) cBox := gtk.NewBox(gtk.OrientationHorizontal, 0) - connectionIcon = gtk.NewImageFromPaintable((clientAssets["disconnect"])) + connectionIcon = gtk.NewImageFromPaintable((clientAssetsLoad("disconnect"))) connectionIcon.AddCSSClass("icon") connectionStatus = gtk.NewLabel(loadedLocale["disconnected"]) @@ -1106,7 +1105,7 @@ func activate(app *gtk.Application) { switchToTab(current, &window.Window) }) - mIcon = gtk.NewImageFromPaintable((clientAssets["comment"])) + mIcon = gtk.NewImageFromPaintable((clientAssetsLoad("comment"))) mIcon.AddCSSClass("icon") mStatus = gtk.NewLabel("-") mStatus.AddController(gesture1) @@ -1167,7 +1166,7 @@ func activate(app *gtk.Application) { }) pBox.AddController(gesture) - i := (gtk.NewImageFromPaintable(clientAssets["chart_bar"])) + i := (gtk.NewImageFromPaintable(clientAssetsLoad("chart_bar"))) i.AddCSSClass("icon") pBox.Append(i)