Update Canvas usage#319
Conversation
freakboy3742
left a comment
There was a problem hiding this comment.
All seems fairly straightforward application of the API cleanups
We will likely want to update the pin on toga-core usage once there's a release we can rely on.
| """ | ||
| _, b, w, h = figure.bbox.bounds | ||
| self.canvas.context.clear() | ||
| self.canvas.root_state.drawing_actions.clear() |
There was a problem hiding this comment.
Does this indicate the need for a self.canvas.clear() (or self.canvas.root_state.clear()?) method? While this is legal, it seems like a common enough use case that it might warrant an entry point.
There was a problem hiding this comment.
That's fair... it's just a little odd in terms of comparing it to the HTML API. Since Context2d doesn't give any direct access to the "history" of drawing actions, there's no way to explicitly tell it to discard prior drawing operations. As far as I'm aware, the standard way to erase the whole canvas is by calling clearRect and telling it the entire canvas as the rectangle. We don't have a clear_rect method yet, but once we do, that would presumably just be one more drawing operation added to the list. The effect would be the same... except that, of course, you could always delete the ClearRect action later, and everything should still be there.
Add to that the fact that, at least as far as things are currently implemented, Canvas already inherits clear() from Widget. Granted, it just throws an error, and I don't think there's any risk Canvas will ever need to have children, so we could give it is own custom clear() method that handles this. But Barbara Liskov might send someone to break our kneecaps.
So... yes, I do think it's a common use case, and a good button to expose. I'm just not positive of the best way to spell it, in betwixt our various idioms.
There was a problem hiding this comment.
Oh, the ironies... Now I remember: that the existence of clear() on the base Widget was the reason we moved all the drawing instructions to canvas.context...
There was a problem hiding this comment.
The good news is that sinceclear isn't part of the HTML API, we're not married to that name for such a method. (Although because it is a natural guess, we could overwrite Widget.clear just to add a more helpful error message that says what to use instead.)
Don't merge this as-is; it'll need to wait until a version of Toga's actually released with this syntax available.
This updates Toga Chart to use the new Canvas API in beeware/toga#4159. It also uses
Color.parse()instead ofcolor(), andrgbinstead ofrgba. I've resisted the urge to do any further updating or simplifying, and kept this to the bare minimum to update names and resolve all deprecation warnings.PR Checklist: