Provide ctx in the draw closure of Canvas
#1552
Open
+61
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In a project I'm working on, I needed to draw text inside a
Canvaswidget. I initially created aFontContextand aLayoutContextinside the draw closure. I usedmasonry::core::render_textto render my text layout to the VelloScene. While theoretically functionnal, my solution was extremely slow and wrong.After realizing that both
FontContextandLayoutContextare meant to be global resources created only once per application, I set out to tinker with the source code of theCanvaswidget. What I came up with allowed me to do this:I updated the
FnOnceclosure to accept a new ctx parameter. Then in Masonry, inside theCanvas::update_scenefunction, I got the ctx from theWidgetMut<'_, Canvas>and passed it as a mutable reference to the closure. In Xilem, I simply pass the ctx from the closure to the draw closure.This PR includes the addition of a new screenshot test. The text_canvas test renders a 200x200 image with the text 'Canvas' stretched to fit inside.