Skip to content

Commit

Permalink
Expose widget frame count (#827)
Browse files Browse the repository at this point in the history
Saw this issue asking for a way to get the frame count for an Animation or Marquee widget #823

Looks like that's defined for all widgets (and is 1 in case it's not animated). This just updates the template for the render module to expose it as a readonly method, following the way size() is available for Text widgets.

Added test cases. Also checked that it gives a sensible answer for marquees in particular, which gets longer as the text gets longer.
  • Loading branch information
dinosaursrarr authored Jul 31, 2023
1 parent 382ac97 commit 62848ce
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ A quick note about colors. When specifying colors, use a CSS-like
hexdecimal color specification. Pixlet supports "#rgb", "#rrggbb",
"#rgba", and "#rrggbbaa" color specifications.

For animated widgets like Marquee, you can also call `frame_count()`
to work out how many frames are required to display the whole
animation. You can also call `size()` on dynamically-sized widgets
like Text to get the width and height.


## Animation
Animations turns a list of children into an animation, where each
Expand Down
5 changes: 5 additions & 0 deletions runtime/gen/docs/render.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ A quick note about colors. When specifying colors, use a CSS-like
hexdecimal color specification. Pixlet supports "#rgb", "#rrggbb",
"#rgba", and "#rrggbbaa" color specifications.

For animated widgets like Marquee, you can also call `frame_count()`
to work out how many frames are required to display the whole
animation. You can also call `size()` on dynamically-sized widgets
like Text to get the width and height.

{{range .}}{{if .Documentation}}{{$name := .GoName}}
## {{.GoName}}
{{.Documentation}}
Expand Down
24 changes: 24 additions & 0 deletions runtime/gen/type.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type {{.GoName}} struct {
{{if .HasSize}}
size *starlark.Builtin
{{end}}
{{if .GoWidgetName}}
frame_count *starlark.Builtin
{{end}}
}

func new{{.GoName}}(
Expand Down Expand Up @@ -44,6 +47,9 @@ func new{{.GoName}}(
{{if .HasSize}}
w.size = starlark.NewBuiltin("size", {{.GoName|ToLower}}Size)
{{end}}
{{if .GoWidgetName}}
w.frame_count = starlark.NewBuiltin("frame_count", {{.GoName|ToLower}}FrameCount)
{{end}}

{{if .HasInit}}
if err := w.Init(); err != nil {
Expand Down Expand Up @@ -93,6 +99,10 @@ func (w *{{.GoName}}) Attr(name string) (starlark.Value, error) {
{{if .HasSize}}
case "size":
return w.size.BindReceiver(w), nil
{{end}}
{{if .GoWidgetName}}
case "frame_count":
return w.frame_count.BindReceiver(w), nil
{{end}}
default:
return nil, nil
Expand Down Expand Up @@ -126,3 +136,17 @@ func {{.GoName|ToLower}}Size(
}

{{end}}
{{if .GoWidgetName}}
func {{.GoName|ToLower}}FrameCount(
thread *starlark.Thread,
b *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple) (starlark.Value, error) {

w := b.Receiver().(*{{.GoName}})
count := w.FrameCount()

return starlark.MakeInt(count), nil
}

{{end}}
38 changes: 38 additions & 0 deletions runtime/modules/animation_runtime/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 62848ce

Please sign in to comment.