From: Fl_GUI Date: Thu, 19 Dec 2024 19:51:10 +0000 (+0100) Subject: fix emote rendering X-Git-Url: https://git.openfl.eu/?a=commitdiff_plain;h=ccb7f1f2559c851b0b5dbe74c4f6ad64bdfc8f07;p=chat-scroll.git fix emote rendering --- diff --git a/chat/create.go b/chat/create.go index 1f33349..814013a 100644 --- a/chat/create.go +++ b/chat/create.go @@ -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) diff --git a/chat/textlinerenderer.go b/chat/textlinerenderer.go index 98db5d1..f036873 100644 --- a/chat/textlinerenderer.go +++ b/chat/textlinerenderer.go @@ -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}) } } diff --git a/chat/wrappingtextline.go b/chat/wrappingtextline.go index dd667a3..9eef918 100644 --- a/chat/wrappingtextline.go +++ b/chat/wrappingtextline.go @@ -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 dd9502f..e1a0ef9 100644 --- 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)