1
0
Fork 0
mirror of https://github.com/Luzifer/streamdeck.git synced 2024-12-20 17:51:21 +00:00

Add caption support for image buttons

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-05-27 12:45:40 +02:00
parent 01dc62e9d2
commit dd86543f47
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

View file

@ -4,13 +4,13 @@ import (
"context" "context"
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"image"
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
"io" "io"
"net/http" "net/http"
"os" "os"
"path" "path"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -55,25 +55,27 @@ func (d displayElementImage) Display(ctx context.Context, idx int, attributes at
} }
} }
f, err := os.Open(filename) var (
if err != nil { err error
return errors.Wrap(err, "Unable to open image") imgRenderer = newTextOnImageRenderer()
} )
defer f.Close()
img, _, err := image.Decode(f) if err = imgRenderer.DrawBackgroundFromFile(filename); err != nil {
if err != nil { return errors.Wrap(err, "drawing background from disk")
return errors.Wrap(err, "Umable to decode image")
} }
img = autoSizeImage(img, sd.IconSize()) if strings.TrimSpace(attributes.Caption) != "" {
if err = imgRenderer.DrawCaptionText(strings.TrimSpace(attributes.Caption)); err != nil {
return errors.Wrap(err, "rendering caption")
}
}
if err := ctx.Err(); err != nil { if err := ctx.Err(); err != nil {
// Page context was cancelled, do not draw // Page context was cancelled, do not draw
return err return err
} }
return errors.Wrap(sd.FillImage(idx, img), "Unable to set image") return errors.Wrap(sd.FillImage(idx, imgRenderer.GetImage()), "setting image")
} }
func (d displayElementImage) getCacheFileName(url string) (string, error) { func (d displayElementImage) getCacheFileName(url string) (string, error) {