Align Canvas method parameters better with JavaScript#4330
Align Canvas method parameters better with JavaScript#4330freakboy3742 merged 10 commits intobeeware:mainfrom
Conversation
|
I'm not sure if this change note should be misc or removal. It's not technically changing anything that was already there, but it is changing the thing that's replacing it. |
|
I think the intent is good. There may need to be some backwards compatibility about the attributes on However I need to catch up on the changes made in #4159 so this may have already been addressed/made moot. |
Hm, that's a good point. The docs say not to ever directly create drawing actions, but they do describe editing their attributes, and that particular one by name. In any event, I do need to update the docs. |
freakboy3742
left a comment
There was a problem hiding this comment.
Looks good; a couple of minor suggestions inline.
The one bigger suggestion is about the nature of the change itself. I accept that fillStyle is how HTML5 describes this property. That's potentially a little confusing here, for two reasons:
- We don't support the other options for fill style (gradients and patterns). We could, but we don't at present
- "color" is a more "humane" interface to the API for ... changing color.
At the very least, I think the docstrings for fill_style arguments should point out that we don't support gradient or pattern arguments, just so it's clear that some value HTML5 values won't be honoured.
However, I also wonder if, even though color isn't a legal HTML5 attribute, it's worth preserving as an argument (with error handling preventing the use of both). That breaks the HTML5 compatibility purity... but given the mapping is so straightforward... does that matter? If the aim is an API that is trivial to use, fill(color='red') is completely unambiguous in intention and interpretation... is there any reason to disallow it, especially given that it has been historically legal?
(Call that suggestion a "strong opinion, weakly held" - I won't push back on it too hard. Strict HTML5 compatibility definitely has appeal, and it's undeniably simpler to not have to deal with the duplication and edge cases).
| """A canvas can produce a Stroke sub-state.""" | ||
| with widget.stroke(color="rebeccapurple", line_width=5, line_dash=[2, 7]) as stroke: | ||
| with widget.stroke( | ||
| stroke_style="rebeccapurple", line_width=5, line_dash=[2, 7] |
There was a problem hiding this comment.
| stroke_style="rebeccapurple", line_width=5, line_dash=[2, 7] | |
| stroke_style="rebeccapurple", | |
| line_width=5, | |
| line_dash=[2, 7] |
Co-authored-by: Russell Keith-Magee <russell@keith-magee.com>
Hm... My thought was that we hopefully will add support for gradients and/or patterns, so the "style" parameters are more future-proof. I won't deny, though, that as as by far the most likely option to be used, "color" is certainly more user-friendly in most cases. One exception would be the function signature in the docs — the kludge wouldn't be just on on our end. Lemme try hacking something up, and seeing how awful it is or isn't. |
I completely agree that should be a long-term goal. However, even when it is implemented, "just change the color" is an obvious use case, and
👍 |
|
I've pushed an update that adds an extra layer of method shimming. I opted to issue these warnings unconditionally, whether or not the arguments in question were actually supplied. That's partly to be thorough and helpful, but also partly because without more use of |
|
...The more I think about it, the more I wonder if it would be worth it to still support supplying |
If we can do it cleanly, I agree this would be very convenient. |
freakboy3742
left a comment
There was a problem hiding this comment.
Ok - I'm happy with where this is; if you want to merge it, go ahead. The only reason I'm not merging it is that I wasn't sure if you wanted to include the "color as positional argumnet" handling as part of this refactor - but I think we could easily add that later (and after the next Toga release) if we wanted to.
I'm a little torn. In terms of "making the best possible API", yes, this is fine as is, and can be improved later. On the other hand, in terms of easing the transition of existing code, it's entirely possible (even likely) that people are currently passing color as a positional argument. True, they'll have to change methods anyway, and the warning brings it up, but it would be nice to have one less thing to have to change... Feel free to merge this if you'd like. For context, I have #4332 (adding save, restore, and attributes) mostly finished, stymied a bit by #4334, which I think I also have mostly buttoned up. The only other thing past that is rewriting the docs. I probably won't be able to get to any of that till next week, though. If either you or @corranwebster (or @kattni, particularly on docs?) are eager enough for a release that you feel like wading into any of that, I'd happily welcome the help. Otherwise, I'll tackle it when I'm able. |
|
I'm going to merge this on the basis that it is a self-contained change; and addressing API for specifying color can be handled as a follow up - either before or after a release. Agreed it would be preferable to do this before a release to minimize the impact on end users, but it's not essential. |
Part of #3994
This alters the parameters of Canvas's (new)
fillandstrokemethods to better match the HTML/JavaScript version. Only parameters valid in JavaScript can be provided as positional arguments; any of the extra parameters Toga accepts must be by keyword. It also renamescolortofill_style/stroke_stylein their respective methods. (I could also see an argument for naming them simplystyle.)Because Canvas is only now gaining these methods, we don't need any new shims; the existing shims on the capitalized versions (and the methods on context managers) can do the necessary translation.
So far, I've fixed existing tests, but not yet written new ones (or done any particularly thorough double-checking yet). Wanted to go ahead and put this out there before I head to bed, in case there are questions or feedback on the general intent.
PR Checklist: