Skip to main content

Module draw_cell

Module draw_cell 

Source
Expand description

DrawCell and DrawRefCell — interior-mutable UI state that automatically requests a repaint when it changes.

§Why this exists

The most common bug in this framework is mutating paint-affecting state without calling crate::animation::request_draw, so part of a widget silently fails to repaint. The event dispatcher already makes a Consumed event schedule a draw (see crate::event::EventResult), but state shared between a widget and its owner via Rc<Cell<T>> / Rc<RefCell<T>> is often written from a callback (an on_change / on_click closure) that runs outside the widget’s own on_event, so the auto-invalidation doesn’t cover it.

DrawCell is the recommended replacement for Cell<T> in that role: its set requests a draw only when the value actually changes, so redundant writes stay free and callers no longer have to remember a manual request_draw().

§Example

use std::rc::Rc;
use agg_gui::DrawCell;

// Shared between a slider's `on_change` and the paint code.
let stroke_width = Rc::new(DrawCell::new(1.0_f64));

let for_cb = Rc::clone(&stroke_width);
let on_change = move |v: f64| for_cb.set(v); // auto-requests a draw on change

on_change(2.0);
assert_eq!(stroke_width.get(), 2.0);

Structs§

DrawCell
A Cell-like container for Copy + PartialEq UI state that requests a repaint whenever its value changes.
DrawRefCell
A RefCell-flavoured variant for state that is not Copy (strings, vectors, structs). Reading is unchanged; a mutable borrow requests a repaint when its guard drops.
DrawRefMut
Mutable-borrow guard from DrawRefCell::borrow_mut that requests a repaint on drop.