From 3be0c6798c86cb2bcbe6bd423df46784438e0264 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Sun, 17 Sep 2023 15:14:23 +0100 Subject: [PATCH 1/3] Fixed font-size estimation In one of the commits that added support for the mini, and further commits, a new 'btnSize' parameter was introduced. This however was not reflected in the algorithm that determines the size. Without this fix, the text was always too wide (90px is more than the 72px my stream deck has), and also no longer virtually centered. This commit should address both these issues. I've only tested this with the v2. --- buttons/text.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/buttons/text.go b/buttons/text.go index cd1358d..18ae376 100644 --- a/buttons/text.go +++ b/buttons/text.go @@ -104,7 +104,7 @@ func getImageWithText(text string, textColour color.Color, backgroundColour colo width := 0 for size = 1; size < 60; size++ { width = getTextWidth(text, size) - if width > 90 { + if width > btnSize { size = size - 1 break } @@ -123,7 +123,7 @@ func getImageWithText(text string, textColour color.Color, backgroundColour colo c.SetClip(dstImg.Bounds()) x := int((btnSize - width) / 2) // Horizontally centre text - y := int(50 + (size / 3)) // Fudged vertical centre, erm, very "heuristic" + y := int(btnSize / 2) + int(size / 3) // Fudged vertical centre, erm, very "heuristic" pt := freetype.Pt(x, y) c.DrawString(text, pt) From fb6d2060bd36e210398f9aa8f1ea58644a7d57e9 Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 19 Sep 2023 00:47:36 +0100 Subject: [PATCH 2/3] Fixed off-by-1 error --- buttons/text.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/buttons/text.go b/buttons/text.go index 18ae376..3be1279 100644 --- a/buttons/text.go +++ b/buttons/text.go @@ -104,7 +104,7 @@ func getImageWithText(text string, textColour color.Color, backgroundColour colo width := 0 for size = 1; size < 60; size++ { width = getTextWidth(text, size) - if width > btnSize { + if width >= btnSize { size = size - 1 break } From 81e568b04115736e9a667e5b991a421572c9cbbf Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Tue, 17 Oct 2023 11:22:13 +0100 Subject: [PATCH 3/3] Allow for text buttons with/without margins to have space for borders --- buttons/text.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/buttons/text.go b/buttons/text.go index 3be1279..c43efe0 100644 --- a/buttons/text.go +++ b/buttons/text.go @@ -20,11 +20,12 @@ type TextButton struct { updateHandler func(streamdeck.Button) btnIndex int actionHandler streamdeck.ButtonActionHandler + margin int } // GetImageForButton is the interface implemention to get the button's image as an image.Image func (btn *TextButton) GetImageForButton(btnSize int) image.Image { - img := getImageWithText(btn.label, btn.textColour, btn.backgroundColour, btnSize) + img := getImageWithText(btn.label, btn.textColour, btn.backgroundColour, btnSize, btn.margin) return img } @@ -80,7 +81,15 @@ func (btn *TextButton) Pressed() { // background. The text will be set on a single line, and auto-sized to fill the button as best // as possible. func NewTextButton(label string) *TextButton { - btn := NewTextButtonWithColours(label, color.White, color.Black) + btn := NewTextButtonWithColoursAndMargin(label, color.White, color.Black, 6) + return btn +} + +// NewTextButtonWithMargin creates a new TextButton with the specified text on it, in white on a +// black background. The text will be set on a single line, and auto-sized to fill the button as +// best as possible, taking into account the margin on each side. +func NewTextButtonWithMargin(label string, margin int) *TextButton { + btn := NewTextButtonWithColoursAndMargin(label, color.White, color.Black, margin) return btn } @@ -88,11 +97,20 @@ func NewTextButton(label string) *TextButton { // text and background colours. The text will be set on a single line, and auto-sized to fill the // button as best as possible. func NewTextButtonWithColours(label string, textColour color.Color, backgroundColour color.Color) *TextButton { - btn := &TextButton{label: label, textColour: textColour, backgroundColour: backgroundColour} + btn := NewTextButtonWithColoursAndMargin(label, textColour, backgroundColour, 6) return btn } -func getImageWithText(text string, textColour color.Color, backgroundColour color.Color, btnSize int) image.Image { +// NewTextButtonWithColoursAndMargin creates a new TextButton with the specified text on it, in the +// specified text and background colours. The text will be set on a single line, and auto-sized to +// fill the button as best as possible, taking into account the margin on each side. +func NewTextButtonWithColoursAndMargin(label string, textColour color.Color, backgroundColour color.Color, margin int) *TextButton { + btn := &TextButton{label: label, textColour: textColour, backgroundColour: backgroundColour, margin: margin} + return btn +} + + +func getImageWithText(text string, textColour color.Color, backgroundColour color.Color, btnSize int, margin int) image.Image { size := float64(18) @@ -104,7 +122,7 @@ func getImageWithText(text string, textColour color.Color, backgroundColour colo width := 0 for size = 1; size < 60; size++ { width = getTextWidth(text, size) - if width >= btnSize { + if width >= btnSize - (margin * 2) { size = size - 1 break }