widgetry/widgets/
stash.rs

1use std::cell::RefCell;
2use std::rc::Rc;
3
4use crate::{EventCtx, GfxCtx, ScreenDims, ScreenPt, Widget, WidgetImpl, WidgetOutput};
5
6/// An invisible widget that stores some arbitrary data on the Panel. Users of the panel can read
7/// and write the value. This is one method for "returning" data when a State completes.
8pub struct Stash<T> {
9    value: Rc<RefCell<T>>,
10}
11
12impl<T: 'static> Stash<T> {
13    pub fn new_widget(name: &str, value: T) -> Widget {
14        Widget::new(Box::new(Stash {
15            value: Rc::new(RefCell::new(value)),
16        }))
17        .named(name)
18    }
19
20    pub(crate) fn get_value(&self) -> Rc<RefCell<T>> {
21        self.value.clone()
22    }
23}
24
25impl<T: 'static> WidgetImpl for Stash<T> {
26    fn get_dims(&self) -> ScreenDims {
27        ScreenDims::square(0.0)
28    }
29
30    fn set_pos(&mut self, _: ScreenPt) {}
31
32    fn event(&mut self, _: &mut EventCtx, _: &mut WidgetOutput) {}
33    fn draw(&self, _: &mut GfxCtx) {}
34}