Rename Context to State#4114
Conversation
| @@ -662,26 +673,26 @@ def _draw(self, context: Any) -> None: | |||
|
|
|||
|
|
|||
| class FillContext(ClosedPathContext): | |||
There was a problem hiding this comment.
In a vacuum, I'd also be changing these class names too, e.g. FillState. However, I've left them alone because they'll soon be going away, once their "standalone" counterparts (e.g. Fill) start doing double duty as context managers.
| return f"{self.__class__.__name__}(x={self.x}, y={self.y})" | ||
|
|
||
| def _draw(self, context: Any) -> None: | ||
| """Used by parent to draw all objects that are part of the context.""" |
There was a problem hiding this comment.
This lone _draw method was docstringed, unlike all the others. I removed it here, and added one to the abstract State._draw.
| # A fresh state has been created as a sub-state of the canvas. | ||
| assert isinstance(closed_path, ClosedPathContext) | ||
| assert closed_path != widget.context | ||
| assert closed_path is not widget.root_state |
There was a problem hiding this comment.
I noticed several spots in these tests was checking states with !=. This has worked in the past, but:
- Dataclasses actually implement equality checking, so this can't be trusted anymore to be equivalent to
is not. is notmakes the intent clearer.
There was a problem hiding this comment.
Thanks for the clarification!
| 1. We first define the drawing operations we want to perform in a new function: | ||
|
|
||
| 2. Next we create a new Canvas: ```python self.canvas = toga.Canvas(flex=1) ``` | ||
| ```python | ||
| def draw_eyes(self): | ||
| with self.canvas.Fill(color=WHITE) as eye_whites: | ||
| eye_whites.arc(58, 92, 15) | ||
| eye_whites.arc(88, 92, 15, math.pi, 3 * math.pi) | ||
| ``` | ||
|
|
||
| Notice that we also created and used a new fill state called `eye_whites`. The `with` keyword that is used for the fill operation causes everything draw using the state to be filled with a color. In this example we filled two circular eyes with the color white. | ||
|
|
||
| 2. Next we create a new Canvas: | ||
|
|
||
| ```python | ||
| self.canvas = toga.Canvas(flex=1) | ||
| ``` |
There was a problem hiding this comment.
I think this may have been a casualty of the Markdown changeover — I only just noticed it'd been reflowed onto one line.
There was a problem hiding this comment.
Almost certainly was a casualty. There were issues with the reflow initially, and I tried to catch them all, but missed a few.
There was a problem hiding this comment.
All good! Just wanted to explain where all these new lines came from. I'm honestly impressed at how rarely I've seen artifacts like this, considering how massive a migration that was.
kattni
left a comment
There was a problem hiding this comment.
This looks pretty good to me! I have a few questions that are probably me needing a little help understanding things, and not actual problems.
kattni
left a comment
There was a problem hiding this comment.
Thanks for the clarifications! Looks good to me.
| # Use the font to write on a canvas. | ||
| canvas = toga.Canvas() | ||
| canvas.context.write_text("Hello", font=my_font) | ||
| canvas.root_tate.write_text("Hello", font=my_font) |
There was a problem hiding this comment.
@HalfWhitt Just saw this typo on a quick scan; might want to piggyback a fix onto another canvas PR.
Lots of changes, but pretty mechanical for the most part.
The Canvas docs, tutorial, and example are going to need an overhaul for #3994, but I think it makes sense to wait on that until most or all of the changes are available. For now, I'm only aiming to replace vocabulary and references as necessary to keep them functional and accurate through this intermediate process.
PR Checklist: