]> git.openfl.eu Git - chat-scroll.git/commitdiff
fix emote rendering
authorFl_GUI <flor.guilini@hotmail.com>
Thu, 19 Dec 2024 19:51:10 +0000 (20:51 +0100)
committerFl_GUI <flor.guilini@hotmail.com>
Thu, 19 Dec 2024 19:51:10 +0000 (20:51 +0100)
chat/create.go
chat/textlinerenderer.go
chat/wrappingtextline.go
main.go

index 1f33349e239b471c197638f1fe633046cfc80358..814013a56ac60dd5c8e4f4dd30e807a13e6fc0eb 100644 (file)
@@ -6,12 +6,14 @@ import (
        chatMessages "fl-gui.name/twitchchat/twitch/core/messages"
        "fl-gui.name/twitchchat/twitch/core/privmsg"
        "fyne.io/fyne/v2"
+       "fyne.io/fyne/v2/theme"
        "go.openfl.eu/chat-scroller/messages"
 )
 
 type ChatWindow struct {
        content      *scrollCanvas
        messagesSync sync.Mutex
+       textSize     float32
 }
 
 func (c *ChatWindow) OnMessage(m chatMessages.Message) {
@@ -23,7 +25,7 @@ func (c *ChatWindow) OnMessage(m chatMessages.Message) {
        defer c.messagesSync.Unlock()
 
        text := privmsg.Privmsg{m}
-       co := newWrappingTextLine(text)
+       co := newWrappingTextLine(text, c.textSize)
        c.content.Add(co)
 }
 
@@ -31,7 +33,7 @@ func NewChat(app fyne.App) fyne.Window {
        w := app.NewWindow("chat")
        content := newScrollingCanvas()
 
-       cw := ChatWindow{content, sync.Mutex{}}
+       cw := ChatWindow{content, sync.Mutex{}, app.Settings().Theme().Size(theme.SizeNameText)}
        messages.Listen(&cw)
 
        w.SetContent(content)
index 98db5d17c013498f4dda2f8e873c63286509bab0..f03687305cb1ca7d028bcf054aa86c46a2fd9e20 100644 (file)
@@ -5,11 +5,12 @@ import (
 )
 
 type TextLineRenderer struct {
-       objects []fyne.CanvasObject
+       objects    []fyne.CanvasObject
+       textHeight float32
 }
 
-func newTextLineRenderer(objects []fyne.CanvasObject) *TextLineRenderer {
-       return &TextLineRenderer{objects}
+func newTextLineRenderer(objects []fyne.CanvasObject, textHeight float32) *TextLineRenderer {
+       return &TextLineRenderer{objects, textHeight}
 }
 
 func (t *TextLineRenderer) Destroy() {
@@ -21,14 +22,14 @@ func (t *TextLineRenderer) Layout(size fyne.Size) {
        lineHeight := float32(0)
        for _, o := range t.objects {
                if currentPos.X+o.MinSize().Width > size.Width {
-                       currentPos = fyne.Position{0, currentPos.Y + lineHeight}
+                       currentPos = fyne.Position{0, currentPos.Y + lineHeight + 3}
                        lineHeight = 0
                } else {
                        if o.MinSize().Height > lineHeight {
                                lineHeight = o.MinSize().Height
                        }
                }
-               o.Move(currentPos)
+               o.Move(currentPos.Add(fyne.Position{0, (t.textHeight - o.MinSize().Height) / 2}))
                currentPos = currentPos.Add(fyne.Position{o.MinSize().Width, 0})
        }
 }
index dd667a33d21b8d356d22826d645c3d329bdb7576..9eef9189e3be477f90c61c9c03003fbb8e586fcd 100644 (file)
@@ -15,13 +15,15 @@ import (
 type WrappingTextLine struct {
        widget.BaseWidget
 
-       message privmsg.Privmsg
+       message  privmsg.Privmsg
+       textSize float32
 }
 
-func newWrappingTextLine(message privmsg.Privmsg) *WrappingTextLine {
+func newWrappingTextLine(message privmsg.Privmsg, textSize float32) *WrappingTextLine {
 
        item := &WrappingTextLine{
-               message: message,
+               message:  message,
+               textSize: textSize,
        }
        item.ExtendBaseWidget(item)
        return item
@@ -33,7 +35,7 @@ func (w *WrappingTextLine) CreateRenderer() fyne.WidgetRenderer {
        objects := make([]fyne.CanvasObject, 0, 3)
 
        // badges
-       appendBadges(&objects, w.message)
+       w.appendBadges(&objects, w.message)
 
        // username
        userColor, ok := hexColorToColor(w.message.Color())
@@ -49,24 +51,25 @@ func (w *WrappingTextLine) CreateRenderer() fyne.WidgetRenderer {
        objects = append(objects, sepObject)
 
        // message
-       appendMessages(&objects, w.message)
-       return newTextLineRenderer(objects)
+       w.appendMessages(&objects, w.message)
+       return newTextLineRenderer(objects, w.textSize)
 }
 
-func appendBadges(objects *[]fyne.CanvasObject, message privmsg.Privmsg) {
+func (w *WrappingTextLine) appendBadges(objects *[]fyne.CanvasObject, message privmsg.Privmsg) {
        for _, b := range strings.Split(message.Badges(), ",") {
                i := decorations.GetBadge(b)
                if i == nil {
                        continue
                }
                img := canvas.NewImageFromImage(*i)
-               img.SetMinSize(fyne.Size{18, 18})
-               img.Resize(fyne.Size{18, 18})
+               size := w.textSize
+               img.SetMinSize(fyne.Size{size, size})
+               img.Resize(fyne.Size{size, size})
                *objects = append(*objects, img)
        }
 }
 
-func appendMessages(objects *[]fyne.CanvasObject, message privmsg.Privmsg) {
+func (w *WrappingTextLine) appendMessages(objects *[]fyne.CanvasObject, message privmsg.Privmsg) {
        emotes := parseEmotesTag(message)
        text := message.UserMessage()
        textCol := color.RGBA{10, 10, 10, 255}
@@ -82,8 +85,9 @@ func appendMessages(objects *[]fyne.CanvasObject, message privmsg.Privmsg) {
                        return
                }
                img := canvas.NewImageFromImage(*i)
-               img.SetMinSize(fyne.Size{18, 18})
-               img.Resize(fyne.Size{18, 18})
+               size := w.textSize * 1.2 // should be configurable, but also fix emotes overlapping outside of a text line
+               img.Resize(fyne.Size{img.Aspect() * size, size})
+               img.SetMinSize(img.Size())
                *objects = append(*objects, img)
        }
 
diff --git a/main.go b/main.go
index dd9502fc62dfb9058039aeca5e6f79db6e7f5ce4..e1a0ef9681e193cf10f13758d263afc4e64bcc71 100644 (file)
--- a/main.go
+++ b/main.go
@@ -6,6 +6,7 @@ import (
        "os"
        "runtime/pprof"
 
+       "fyne.io/fyne/v2"
        "fyne.io/fyne/v2/app"
        "fyne.io/fyne/v2/data/binding"
        "fyne.io/fyne/v2/theme"
@@ -23,6 +24,17 @@ import (
 var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
 var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
 
+type chattheme struct {
+       fyne.Theme
+}
+
+func (c chattheme) Size(name fyne.ThemeSizeName) float32 {
+       if name == theme.SizeNameText {
+               return 24
+       }
+       return c.Theme.Size(name)
+}
+
 func main() {
        flag.Parse()
        if *cpuprofile != "" {
@@ -42,7 +54,7 @@ func main() {
        // this listener will trigger on initialization
        model.Application.Twitch.TokenState.AddListener(binding.NewDataListener(func() { token.Authenticate(&scroller) }))
 
-       scroller.Settings().SetTheme(theme.LightTheme())
+       scroller.Settings().SetTheme(chattheme{theme.LightTheme()})
 
        go status.CreateSetupWindow(scroller)