Conversation
Co-authored-by: Olivier FAURE <couteaubleu@gmail.com>
Returning childids now
Made no op as it previosly did not exist
Now exposed in other modules
|
To be clear: this is based on #875. |
PoignardAzur
left a comment
There was a problem hiding this comment.
Thanks for picking up this work!
masonry/src/widgets/screenshots/masonry__widgets__canvas__tests__hello.png
Show resolved
Hide resolved
There was a problem hiding this comment.
We almost never do these kinds of snapshot tests anymore. You should remove this file.
xilem/src/view/canvas.rs
Outdated
| /// ```ignore | ||
| /// use xilem::view::canvas; | ||
| /// use masonry::{Scene, Size}; | ||
| /// | ||
| /// let my_canvas = canvas(|scene: &mut Scene, size: Size| { | ||
| /// // Drawing a simple rectangle that fills the canvas. | ||
| /// scene.fill_rect((0.0, 0.0, size.width, size.height), [0.2, 0.4, 0.8, 1.0]); | ||
| /// }); |
There was a problem hiding this comment.
I think we should strive towards not ignoring the examples in doc-comments.
This has the tendency to quickly be out of sync, I think you can already see that in the diff.
| /// ```ignore | |
| /// use xilem::view::canvas; | |
| /// use masonry::{Scene, Size}; | |
| /// | |
| /// let my_canvas = canvas(|scene: &mut Scene, size: Size| { | |
| /// // Drawing a simple rectangle that fills the canvas. | |
| /// scene.fill_rect((0.0, 0.0, size.width, size.height), [0.2, 0.4, 0.8, 1.0]); | |
| /// }); | |
| /// ``` | |
| /// use xilem::masonry::{kurbo::{Rect, Size}, peniko::Fill, vello::Scene}; | |
| /// use xilem::{Affine, view::canvas}; | |
| /// # use xilem::WidgetView; | |
| /// | |
| /// # fn fill_canvas() -> impl WidgetView<()> + use<> { | |
| /// let my_canvas = canvas(|scene: &mut Scene, size: Size| { | |
| /// // Drawing a simple rectangle that fills the canvas. | |
| /// scene.fill( | |
| /// Fill::NonZero, | |
| /// Affine::IDENTITY, | |
| /// xilem::palette::css::AQUA, | |
| /// None, | |
| /// &Rect::new(0.0, 0.0, size.width, size.height), | |
| /// ); | |
| /// }); | |
| /// # my_canvas | |
| /// # } |
DJMcNab
left a comment
There was a problem hiding this comment.
Thanks for reviving this! It's an important capability to have.
Thanks also for retaining Richard's commits, so this will have properly attributed authors when it lands.
Most of my comments are the same as in #875, for things which still need addressing.
xilem/src/view/canvas.rs
Outdated
| fn build(&self, ctx: &mut ViewCtx, _state: &mut State) -> (Self::Element, Self::ViewState) { | ||
| let widget = widgets::Canvas::from_arc(self.draw.clone()); | ||
|
|
||
| let widget_pod = ctx.create_pod(widget); | ||
| (widget_pod, ()) | ||
| } | ||
| fn rebuild( | ||
| &self, | ||
| prev: &Self, | ||
| (): &mut Self::ViewState, | ||
| _ctx: &mut ViewCtx, | ||
| element: Mut<'_, Self::Element>, | ||
| _state: &mut State, | ||
| ) { | ||
| if !Arc::ptr_eq(&self.draw, &prev.draw) { | ||
| widgets::Canvas::set_painter_arc(element, self.draw.clone()); | ||
| } | ||
| } |
There was a problem hiding this comment.
There are two answers here.
Firstly, the WidgetMut methods on the Canvas widget should be accepting &mut WidgetMut<Self>, rather than just plain WidgetMut. For example, you can look in:
xilem/masonry/src/widgets/button.rs
Lines 77 to 83 in d408dd6
That would solve this issue.
However, there is also a trick for when you do need to fork a WidgetMut. That's the reborrow_mut method.
| /// Users are encouraged to set alt text for the canvas. | ||
| /// If possible, the alt-text should succinctly describe what the canvas represents. | ||
| /// | ||
| /// If the canvas is decorative or too hard to describe through text, users should set alt text to `""`. |
There was a problem hiding this comment.
Again, the same comment applies as in #875, namely:
| /// If the canvas is decorative or too hard to describe through text, users should set alt text to `""`. | |
| /// If the canvas is decorative users should set alt text to `""`. | |
| /// If it's too hard to describe through text, the alt text should be left unset. | |
| /// This allows accessibility clients to know that there is no accessible description of the canvas content. |
|
|
||
| // --- MARK: WIDGETMUT --- | ||
| impl Canvas { | ||
| /// Update the draw function |
There was a problem hiding this comment.
| /// Update the draw function | |
| /// Update the draw function. |
And the same for the other items. We want all doc comments to end with a full stop, for consistency.
Unfortunately, this currently isn't linted for.
| } | ||
|
|
||
| fn accepts_pointer_interaction(&self) -> bool { | ||
| true |
There was a problem hiding this comment.
Previous discussion at #875 (comment).
The only case this really impacts is using a canvas within a button; see for example #1429. I think that in most cases, we would want this to be false, as was my position in that original thread. But fixing this can be a follow-up; we certainly don't want to block on this.
That is concretely, please leave this as-is!
masonry/src/widgets/canvas.rs
Outdated
| type Action | ||
| = NoAction | ||
| where | ||
| Self: Sized; |
There was a problem hiding this comment.
| type Action | |
| = NoAction | |
| where | |
| Self: Sized; | |
| type Action = NoAction; |
For consistency with the rest of the widgets, I'd probably also move this to the start of this implementation. But that isn't blocking.
xilem/src/view/canvas.rs
Outdated
| /// ); | ||
| /// }); | ||
| /// ``` | ||
| pub fn canvas(draw: impl Fn(&mut Scene, Size) + Send + Sync + 'static) -> Canvas { |
There was a problem hiding this comment.
The same comment applies as in #875, that this signature is wrong.
This signature can only be:
| pub fn canvas(draw: impl Fn(&mut Scene, Size) + Send + Sync + 'static) -> Canvas { | |
| pub fn canvas(draw: Arc<dyn Fn(&mut Scene, Size) + Send + Sync + 'static>) -> Canvas | |
| // or | |
| pub fn canvas(draw: &Arc<dyn Fn(&mut Scene, Size) + Send + Sync + 'static>) -> Canvas |
Because we want this to allow not resetting the draw function every rebuild. Currently the !Arc::ptr_eq line will always return true (i.e. that the items are not equal) because a new Arc is allocated in each call to canvas.
xilem/src/view/canvas.rs
Outdated
| /// | ||
| /// ``` | ||
| /// use xilem::view::canvas; | ||
| /// use vello::{ |
There was a problem hiding this comment.
More of a nit, and I see reasons for using vello directly, my suggestion of using vello via xilem was intentional, as it's for one not necessary to have vello as direct dependency, and it avoids possible version mismatch.
There was a problem hiding this comment.
I don't see it re-exported anywhere though... Should I do that?
There was a problem hiding this comment.
Check out my original suggestion (which I actually tested locally, and includes the suggestion below as well)
There was a problem hiding this comment.
Now included :) Apologies, I should have integrated the suggestion at the time as I quickly forgot...
xilem/src/view/canvas.rs
Outdated
| /// Scene, | ||
| /// }; | ||
| /// | ||
| /// let my_canvas = canvas(|scene: &mut Scene, size| { |
There was a problem hiding this comment.
Same here, wrapping this in a function (even if not visible in the doc for the user) returning impl WidgetView has the advantage of checking whether this is actually a view (i.e. test on the type-checking-level).
|
How to save a canvas as an image |
This will need to be a follow-up. To avoid locking up the entire app, it would require a lot of care. |
Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com>
|
Welp... I picked these changes up again and I'm confused what was added? There's a new bound for ViewArgument and &mut state is no longer allowed as a parameter... |
|
These changes came from #1444 |
|
Thank you! It's not impossible to work around, I was just overwhelmed trying to figure out its source.
▰▰▰▰▰
Miles Wirht 🙃
… From: Philipp Mildenberger ***@***.***>
Sent: 12 November 2025 02:22
To: linebender/xilem ***@***.***>
Cc: Miles Wirht ***@***.***>, Mention ***@***.***>
Subject: Re: [linebender/xilem] Add Canvas widget - cont. (PR #1445)
Philipp-M left a comment (linebender/xilem#1445)
These changes came from #1444
--
Reply to this email directly or view it on GitHub:
#1445 (comment)
You are receiving this because you were mentioned.
Message ID: ***@***.***>
|
This is shorter, plays better with rustfmt, and is less likely to lead to merge conflicts if two people add a module in parallel.
This is shorter, plays better with rustfmt, and is less likely to lead to merge conflicts if two people add a module in parallel.
Unneeded &mut
I can't find the vello rexport :(
Thanks to @Philipp-M!!! I forgot about the suggestion
No description provided.