Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render: make widget trees JSON serializable #1078

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion render/animation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package render

import (
"encoding/json"
"image"

"github.com/tidbyt/gg"
Expand All @@ -26,7 +27,8 @@ import (
// )
// EXAMPLE END
type Animation struct {
Widget
Type string `starlark:"-"`

Children []Widget
}

Expand Down Expand Up @@ -63,3 +65,26 @@ func (a Animation) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int) {

a.Children[ModInt(frameIdx, len(a.Children))].Paint(dc, bounds, frameIdx)
}

func (a *Animation) UnmarshalJSON(data []byte) error {
type Alias Animation
aux := &struct {
Children []json.RawMessage
*Alias
}{
Alias: (*Alias)(a),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
children := []Widget{}
for _, childData := range aux.Children {
child, err := UnmarshalWidgetJSON(childData)
if err != nil {
return err
}
children = append(children, child)
}
a.Children = children
return nil
}
3 changes: 2 additions & 1 deletion render/animation/positioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
// DOC(Hold): Delay after animation in frames
//
type AnimatedPositioned struct {
render.Widget
Type string `starlark:"-"`

Child render.Widget `starlark:"child,required"`
XStart int `starlark:"x_start"`
XEnd int `starlark:"x_end"`
Expand Down
2 changes: 1 addition & 1 deletion render/animation/transformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func findKeyframes(arr []Keyframe, p float64) (Keyframe, Keyframe, error) {
// ),
// EXAMPLE END
type Transformation struct {
render.Widget
Type string `starlark:"-"`

Child render.Widget `starlark:"child,required"`
Keyframes []Keyframe `starlark:"keyframes,required"`
Expand Down
59 changes: 56 additions & 3 deletions render/box.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package render

import (
"encoding/json"
"fmt"
"image"
"image/color"

Expand Down Expand Up @@ -31,11 +33,12 @@ import (
// )
// EXAMPLE END
type Box struct {
Widget
Type string `starlark:"-"`

Child Widget
Width, Height int
Padding int
Color color.Color
Color color.RGBA
}

func (b Box) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle {
Expand All @@ -58,7 +61,7 @@ func (b Box) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int) {
h = bounds.Dy()
}

if b.Color != nil {
if b.Color != (color.RGBA{}) {
dc.SetColor(b.Color)
dc.DrawRectangle(0, 0, float64(w), float64(h))
dc.Fill()
Expand Down Expand Up @@ -103,3 +106,53 @@ func (b Box) FrameCount() int {
}
return 1
}

func (b *Box) UnmarshalJSON(data []byte) error {
type Alias Box
aux := &struct {
Child json.RawMessage
Color string
*Alias
}{
Alias: (*Alias)(b),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}

if aux.Child != nil {
child, err := UnmarshalWidgetJSON(aux.Child)
if err != nil {
return err
}
b.Child = child
}

if aux.Color != "" {
col, err := ParseColor(aux.Color)
if err != nil {
return err
}
b.Color = col
}

return nil
}

func (b *Box) MarshalJSON() ([]byte, error) {
col := ""
if b.Color != (color.RGBA{}) {
r, g, b, a := b.Color.RGBA()
col = fmt.Sprintf("#%02x%02x%02x%02x", r>>8, g>>8, b>>8, a>>8)
}
type Alias Box
aux := &struct {
*Alias
Color string
}{
Alias: (*Alias)(b),
Color: col,
}

return json.Marshal(aux)
}
48 changes: 45 additions & 3 deletions render/circle.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package render

import (
"encoding/json"
"fmt"
"image"
"image/color"
"math"
Expand All @@ -24,11 +26,11 @@ import (
// )
// EXAMPLE END
type Circle struct {
Widget
Type string `starlark:"-"`

Child Widget
Color color.Color `starlark:"color, required"`
Diameter int `starlark:"diameter,required"`
Color color.RGBA `starlark:"color, required"`
Diameter int `starlark:"diameter,required"`
}

func (c Circle) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle {
Expand Down Expand Up @@ -69,3 +71,43 @@ func (c Circle) FrameCount() int {
}
return 1
}

func (c *Circle) UnmarshalJSON(data []byte) error {
type Alias Circle
aux := &struct {
Child json.RawMessage
*Alias
}{
Alias: (*Alias)(c),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}

if aux.Child != nil {
child, err := UnmarshalWidgetJSON(aux.Child)
if err != nil {
return err
}
c.Child = child
}
return nil
}

func (c *Circle) MarshalJSON() ([]byte, error) {
col := ""
if c.Color != (color.RGBA{}) {
r, g, b, a := c.Color.RGBA()
col = fmt.Sprintf("#%02x%02x%02x%02x", r>>8, g>>8, b>>8, a>>8)
}
type Alias Circle
aux := &struct {
*Alias
Color string
}{
Alias: (*Alias)(c),
Color: col,
}

return json.Marshal(aux)
}
14 changes: 7 additions & 7 deletions render/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
)

func ParseColor(scol string) (color.Color, error) {
func ParseColor(scol string) (color.RGBA, error) {
var format string
var fourBits bool
var hasAlpha bool
Expand All @@ -31,26 +31,26 @@ func ParseColor(scol string) (color.Color, error) {
fourBits = false
hasAlpha = true
default:
return color.Gray{0}, fmt.Errorf("color: %v is not a hex-color", scol)
return color.RGBA{}, fmt.Errorf("color: %v is not a hex-color", scol)
}

var r, g, b, a uint8

if hasAlpha {
n, err := fmt.Sscanf(scol, format, &r, &g, &b, &a)
if err != nil {
return color.Gray{0}, err
return color.RGBA{}, err
}
if n != 4 {
return color.Gray{0}, fmt.Errorf("color: %v is not a hex-color", scol)
return color.RGBA{}, fmt.Errorf("color: %v is not a hex-color", scol)
}
} else {
n, err := fmt.Sscanf(scol, format, &r, &g, &b)
if err != nil {
return color.Gray{0}, err
return color.RGBA{}, err
}
if n != 3 {
return color.Gray{0}, fmt.Errorf("color: %v is not a hex-color", scol)
return color.RGBA{}, fmt.Errorf("color: %v is not a hex-color", scol)
}
if fourBits {
a = 15
Expand All @@ -66,5 +66,5 @@ func ParseColor(scol string) (color.Color, error) {
a |= a << 4
}

return color.NRGBA{r, g, b, a}, nil
return color.RGBA{r, g, b, a}, nil
}
6 changes: 1 addition & 5 deletions render/colors_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package render

import (
"image/color"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,12 +14,9 @@ func ParseAndAssertColor(
expectedB uint8,
expectedA uint8,
) {
col, err := ParseColor(scol)
c, err := ParseColor(scol)
assert.Nil(t, err)

c, ok := col.(color.NRGBA)
assert.True(t, ok)

assert.Equal(t, expectedR, c.R)
assert.Equal(t, expectedG, c.G)
assert.Equal(t, expectedB, c.B)
Expand Down
26 changes: 25 additions & 1 deletion render/column.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package render

import (
"encoding/json"
"image"

"github.com/tidbyt/gg"
Expand Down Expand Up @@ -56,7 +57,7 @@ import (
// )
// EXAMPLE END
type Column struct {
Widget
Type string `starlark:"-"`

Children []Widget `starlark:"children,required"`
MainAlign string `starlark:"main_align"`
Expand Down Expand Up @@ -89,3 +90,26 @@ func (c Column) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int) {
func (c Column) FrameCount() int {
return MaxFrameCount(c.Children)
}

func (c *Column) UnmarshalJSON(data []byte) error {
type Alias Column
aux := &struct {
Children []json.RawMessage
*Alias
}{
Alias: (*Alias)(c),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
children := []Widget{}
for _, childData := range aux.Children {
child, err := UnmarshalWidgetJSON(childData)
if err != nil {
return err
}
children = append(children, child)
}
c.Children = children
return nil
}
39 changes: 38 additions & 1 deletion render/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package render

import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"image"
Expand Down Expand Up @@ -36,7 +38,8 @@ import (
// DOC(Height): Scale image to this height
// DOC(Delay): (Read-only) Frame delay in ms, for animated GIFs
type Image struct {
Widget
Type string `starlark:"-"`

Src string `starlark:"src,required"`
Width, Height int
Delay int `starlark:"delay,readonly"`
Expand Down Expand Up @@ -195,3 +198,37 @@ func (p *Image) Init() error {

return nil
}

func (p *Image) UnmarshalJSON(data []byte) error {
type Alias Image
aux := &struct {
*Alias
}{
Alias: (*Alias)(p),
}

if err := json.Unmarshal(data, &aux); err != nil {
return err
}

src, err := base64.StdEncoding.DecodeString(aux.Src)
if err != nil {
return err
}
p.Src = string(src)

return p.Init()
}

func (p *Image) MarshalJSON() ([]byte, error) {
type Alias Image
aux := &struct {
*Alias
}{
Alias: (*Alias)(p),
}

aux.Src = base64.StdEncoding.EncodeToString([]byte(p.Src))

return json.Marshal(aux)
}
Loading
Loading