Skip to content

Reference-counted AreaHandlers for interactive canvas scenarios #118

@shankuniyogi

Description

@shankuniyogi

I'm using this library to implement widgets for a STEM toolkit I'm building. It's perfect, super simple, and has just what I need. Thank you!

Some of my components require both custom painting and interaction, but when I started to use Area and AreaHandler, I ran into some limitations. The AreaHandler object passed into Area::new is boxed, and there's no way to access that object from a reference to an Area, so its state can't be changed after the area is created.

let meter = Box::new(Meter { value: 100.0 });
let area = Area::new(&ui, meter);
// Can't change it anymore!
meter = 50.0;

In my fork, I've modified area.rs to also support reference-counted handlers (Rc<RefCell<dyn AreaHandler>>).

let meter = Rc::new(RefCell:new(Meter { value: 100.0 }));
let area = Area::new(&ui, meter.clone());
// Can change this any time.
meter.borrow_mut().value = 50.0;
area.queue_redraw_all(&ui);

The change still supports boxed handlers, and requires no changes to existing code - the existing canvas example runs perfectly - but I've also created a new example that shows how to build an interactive canvas with a ref-counted handler. The handler also implements a changed event that can be hooked by the application. In effect, someone could use this technique to build simple controls on top of an Area.

Would you be interested in a PR to add this change? I'd be happy to do the work to get it PR-ready.

Thanks again for this project!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions