implement suggestions

This commit is contained in:
Richard Dodd 2025-03-10 15:15:33 +00:00
parent b4dfea6b66
commit ec5c6530a6
2 changed files with 16 additions and 11 deletions

View File

@ -1,8 +1,6 @@
// Copyright 2025 the Xilem Authors and the Druid Authors // Copyright 2025 the Xilem Authors and the Druid Authors
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
#![warn(missing_docs)]
//! A canvas widget. //! A canvas widget.
use std::sync::Arc; 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 /// Set the text that will be used to communicate the meaning of the canvas to
/// those using screen readers. /// 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 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 `""`. /// 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 --- // --- MARK: WIDGETMUT ---
impl Canvas { impl Canvas {
/// Update the draw function /// Update the draw function
pub fn update_draw( pub fn set_painter(
this: WidgetMut<'_, Self>, this: WidgetMut<'_, Self>,
draw: impl Fn(&mut Scene, Size) + Send + Sync + 'static, 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 /// Update the draw function
pub fn update_from_arc( pub fn set_painter_arc(
mut this: WidgetMut<'_, Self>, mut this: WidgetMut<'_, Self>,
draw: Arc<dyn Fn(&mut Scene, Size) + Send + Sync + 'static>, draw: Arc<dyn Fn(&mut Scene, Size) + Send + Sync + 'static>,
) { ) {
this.widget.draw = draw; this.widget.draw = draw;
this.ctx.request_render(); 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 --- // --- MARK: IMPL WIDGET ---
@ -77,7 +85,7 @@ impl Widget for Canvas {
fn on_pointer_event(&mut self, _ctx: &mut EventCtx, _event: &PointerEvent) {} fn on_pointer_event(&mut self, _ctx: &mut EventCtx, _event: &PointerEvent) {}
fn accepts_pointer_interaction(&self) -> bool { fn accepts_pointer_interaction(&self) -> bool {
false true
} }
fn on_text_event(&mut self, _ctx: &mut EventCtx, _event: &TextEvent) {} 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) { fn accessibility(&mut self, _ctx: &mut AccessCtx, node: &mut Node) {
// TODO: is this correct?
if let Some(text) = &self.alt_text { if let Some(text) = &self.alt_text {
node.set_description(text.clone()); node.set_description(text.clone());
} else {
node.clear_description();
} }
} }

View File

@ -71,7 +71,7 @@ impl<State, Action> View<State, Action, ViewCtx> for Canvas {
element: Mut<Self::Element>, element: Mut<Self::Element>,
) { ) {
if !Arc::ptr_eq(&self.draw, &prev.draw) { 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());
} }
} }