Skip to content

Fix reset_transform in Cocoa/iOS#4045

Merged
freakboy3742 merged 19 commits into
beeware:mainfrom
HalfWhitt:canvas-cocoa-rotate
Jun 26, 2026
Merged

Fix reset_transform in Cocoa/iOS#4045
freakboy3742 merged 19 commits into
beeware:mainfrom
HalfWhitt:canvas-cocoa-rotate

Conversation

@HalfWhitt

@HalfWhitt HalfWhitt commented Jan 1, 2026

Copy link
Copy Markdown
Member

Fixes #4044

Adding the test first...

So far I have a fix that seems to work for Cocoa, but breaks iOS in new and interesting ways.

PR Checklist:

  • I will abide by the BeeWare Code of Conduct
  • I have read and have followed the CONTRIBUTING.md file
  • This PR was generated or assisted using an AI tool

@HalfWhitt

This comment was marked as resolved.

Comment thread cocoa/src/toga_cocoa/widgets/canvas.py Outdated
@HalfWhitt

Copy link
Copy Markdown
Member Author

This and the test_attributes testbed test demonstrate that there are a lot of differences between backends in how they handle dashed lines at corners. It might be worth looking into whether / how specifically that's defined in the spec, and if so, if there are settings in the various platforms to conform more closely to it.

@HalfWhitt

Copy link
Copy Markdown
Member Author

Interesting. Apparently while Cocoa uses a standard identity matrix when rendering to the screen and a different one for rendering to a cache, iOS never uses identity. I guess they handle flipping differently, somewhere along the way.

@HalfWhitt HalfWhitt marked this pull request as ready for review June 19, 2026 20:03
@HalfWhitt HalfWhitt requested a review from freakboy3742 June 19, 2026 20:03
Comment thread testbed/tests/widgets/canvas/test_canvas.py Outdated

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general approach here makes sense; a couple of questions about specifics of the implementation.

EDIT: Also - the comments about the Cocoa backend also apply to the iOS backend.

Comment thread testbed/tests/widgets/canvas/test_canvas.py Outdated
Comment thread cocoa/src/toga_cocoa/widgets/canvas.py Outdated
if not context.using_standard_coords:
context.inverse_original = core_graphics.CGAffineTransformInvert(
context.original_matrix
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why we need to store these reference matrices - but do we ned to re-create them on every call to draw the widget? Won't they be the same on every usage?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least on Cocoa, they'll be different if the call is to draw it to the screen, or to draw it to the buffer for as_image(). But that's a good point, we could just store each of those separately, as cached properties. And I think on iOS it's always the same — at least I know it's never the actual identity matrix. If I can figure out why Briefcase won't run iOS for me lately, I can go back in and double-check that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having refamiliarized myself with this a bit more: the original matrix (and its inverse) are dependent on the canvas's height. Granted, it should still be the same from draw to draw as long as the widget isn't resized, but we'd need to make sure to update it on resize. However, I've put the inverse inside a cached property, so it's only called if and when reset_transform is actually called (and still only once per draw).

@HalfWhitt

Copy link
Copy Markdown
Member Author

(The test failure is from the Ubuntu domains being down again.)

@HalfWhitt HalfWhitt requested a review from freakboy3742 June 26, 2026 05:28

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this all makes sense - thanks for getting to the bottom of all this.

@freakboy3742 freakboy3742 merged commit b9ba45d into beeware:main Jun 26, 2026
64 checks passed
@HalfWhitt HalfWhitt deleted the canvas-cocoa-rotate branch June 26, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cocoa/iOS Canvas reset_transform incorrect behavior

3 participants