widgetry/widgets/
filler.rs

1use crate::{EventCtx, GfxCtx, ScreenDims, ScreenPt, Widget, WidgetImpl, WidgetOutput};
2
3/// Doesn't do anything by itself, just used for widgetsing. Something else reaches in, asks for the
4/// ScreenRectangle to use.
5pub struct Filler {
6    resize: ResizeRule,
7}
8
9enum ResizeRule {
10    FixedSize(ScreenDims),
11
12    // (ratio_of_parent_width, parent_width)
13    RatioWidthSquare(f64, f64),
14}
15
16impl ResizeRule {
17    fn dims(&self) -> ScreenDims {
18        match self {
19            Self::FixedSize(dims) => *dims,
20            Self::RatioWidthSquare(pct_width, width) => ScreenDims::square(pct_width * width),
21        }
22    }
23}
24
25impl Filler {
26    /// Creates a square filler, always some percentage of the window width.
27    pub fn square_width(ctx: &EventCtx, pct_width: f64) -> Widget {
28        Widget::new(Box::new(Filler {
29            resize: ResizeRule::RatioWidthSquare(pct_width, ctx.canvas.window_width),
30        }))
31    }
32
33    pub fn fixed_dims(dims: ScreenDims) -> Widget {
34        Widget::new(Box::new(Filler {
35            resize: ResizeRule::FixedSize(dims),
36        }))
37    }
38}
39
40impl WidgetImpl for Filler {
41    fn get_dims(&self) -> ScreenDims {
42        self.resize.dims()
43    }
44
45    fn set_pos(&mut self, _: ScreenPt) {}
46
47    fn event(&mut self, ctx: &mut EventCtx, _: &mut WidgetOutput) {
48        if ctx.input.is_window_resized() {
49            if let ResizeRule::RatioWidthSquare(_, ref mut parent_width) = self.resize {
50                *parent_width = ctx.canvas.window_width;
51            };
52        }
53    }
54
55    fn draw(&self, _g: &mut GfxCtx) {}
56}