Skip to content

Commit

Permalink
Allow for text buttons with/without margins to have space for borders
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed Oct 17, 2023
1 parent fb6d206 commit fb05f8d
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions buttons/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -80,19 +81,36 @@ 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, 4)
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
}

// NewTextButtonWithColours 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.
func NewTextButtonWithColours(label string, textColour color.Color, backgroundColour color.Color) *TextButton {
btn := &TextButton{label: label, textColour: textColour, backgroundColour: backgroundColour}
btn := NewTextButtonWithColoursAndMargin(label, textColour, backgroundColour, 4)
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)

Expand All @@ -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
}
Expand Down

0 comments on commit fb05f8d

Please sign in to comment.