From ec5c6530a68447c4116ecd4112fc660c62253fa3 Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Mon, 10 Mar 2025 15:15:33 +0000 Subject: [PATCH] implement suggestions --- masonry/src/widgets/canvas.rs | 25 +++++++++++++++---------- xilem/src/view/canvas.rs | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/masonry/src/widgets/canvas.rs b/masonry/src/widgets/canvas.rs index f428fb51..261b3a3e 100644 --- a/masonry/src/widgets/canvas.rs +++ b/masonry/src/widgets/canvas.rs @@ -1,8 +1,6 @@ // Copyright 2025 the Xilem Authors and the Druid Authors // SPDX-License-Identifier: Apache-2.0 -#![warn(missing_docs)] - //! A canvas widget. use std::sync::Arc; @@ -42,7 +40,7 @@ impl Canvas { /// Set the text that will be used to communicate the meaning of the canvas to /// those using screen readers. /// - /// Users are strongly encouraged to set alt text for the canvas. + /// 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 `""`. @@ -55,21 +53,31 @@ impl Canvas { // --- MARK: WIDGETMUT --- impl Canvas { /// Update the draw function - pub fn update_draw( + pub fn set_painter( this: WidgetMut<'_, Self>, draw: impl Fn(&mut Scene, Size) + Send + Sync + 'static, ) { - Self::update_from_arc(this, Arc::new(draw)); + Self::set_painter_arc(this, Arc::new(draw)); } /// Update the draw function - pub fn update_from_arc( + pub fn set_painter_arc( mut this: WidgetMut<'_, Self>, draw: Arc, ) { this.widget.draw = draw; this.ctx.request_render(); } + + pub fn set_alt_text(mut this: WidgetMut<'_, Self>, alt_text: String) { + this.widget.alt_text = Some(alt_text); + this.ctx.request_accessibility_update(); + } + + pub fn remove_alt_text(mut this: WidgetMut<'_, Self>) { + this.widget.alt_text = None; + this.ctx.request_accessibility_update(); + } } // --- MARK: IMPL WIDGET --- @@ -77,7 +85,7 @@ impl Widget for Canvas { fn on_pointer_event(&mut self, _ctx: &mut EventCtx, _event: &PointerEvent) {} fn accepts_pointer_interaction(&self) -> bool { - false + true } fn on_text_event(&mut self, _ctx: &mut EventCtx, _event: &TextEvent) {} @@ -102,11 +110,8 @@ impl Widget for Canvas { } fn accessibility(&mut self, _ctx: &mut AccessCtx, node: &mut Node) { - // TODO: is this correct? if let Some(text) = &self.alt_text { node.set_description(text.clone()); - } else { - node.clear_description(); } } diff --git a/xilem/src/view/canvas.rs b/xilem/src/view/canvas.rs index e573ea17..83bf259d 100644 --- a/xilem/src/view/canvas.rs +++ b/xilem/src/view/canvas.rs @@ -71,7 +71,7 @@ impl View for Canvas { element: Mut, ) { if !Arc::ptr_eq(&self.draw, &prev.draw) { - widgets::Canvas::update_from_arc(element, self.draw.clone()); + widgets::Canvas::set_painter_arc(element, self.draw.clone()); } }