agg_gui/widgets/
conditional.rs1use std::cell::Cell;
4use std::rc::Rc;
5
6use crate::draw_ctx::DrawCtx;
7use crate::event::{Event, EventResult};
8use crate::geometry::{Rect, Size};
9use crate::layout_props::{HAnchor, Insets, VAnchor, WidgetBase};
10use crate::widget::Widget;
11
12pub struct Conditional {
21 bounds: Rect,
22 children: Vec<Box<dyn Widget>>, base: WidgetBase,
24 visible: Rc<Cell<bool>>,
25}
26
27impl Conditional {
28 pub fn new(visible: Rc<Cell<bool>>, child: Box<dyn Widget>) -> Self {
29 Self {
30 bounds: Rect::default(),
31 children: vec![child],
32 base: WidgetBase::new(),
33 visible,
34 }
35 }
36
37 pub fn visibility_cell(&self) -> Rc<Cell<bool>> {
38 Rc::clone(&self.visible)
39 }
40}
41
42impl Widget for Conditional {
43 fn type_name(&self) -> &'static str {
44 "Conditional"
45 }
46 fn bounds(&self) -> Rect {
47 self.bounds
48 }
49 fn set_bounds(&mut self, b: Rect) {
50 self.bounds = b;
51 }
52 fn children(&self) -> &[Box<dyn Widget>] {
53 &self.children
54 }
55 fn children_mut(&mut self) -> &mut Vec<Box<dyn Widget>> {
56 &mut self.children
57 }
58
59 fn margin(&self) -> Insets {
60 self.base.margin
61 }
62 fn widget_base(&self) -> Option<&WidgetBase> {
63 Some(&self.base)
64 }
65 fn widget_base_mut(&mut self) -> Option<&mut WidgetBase> {
66 Some(&mut self.base)
67 }
68 fn h_anchor(&self) -> HAnchor {
69 self.base.h_anchor
70 }
71 fn v_anchor(&self) -> VAnchor {
72 self.base.v_anchor
73 }
74
75 fn layout(&mut self, available: Size) -> Size {
76 if !self.visible.get() {
77 self.bounds = Rect::new(0.0, 0.0, 0.0, 0.0);
78 return Size::new(0.0, 0.0);
79 }
80 if let Some(child) = self.children.first_mut() {
81 let s = child.layout(available);
82 child.set_bounds(Rect::new(0.0, 0.0, s.width, s.height));
83 self.bounds = Rect::new(0.0, 0.0, s.width, s.height);
84 return s;
85 }
86 Size::new(0.0, 0.0)
87 }
88
89 fn paint(&mut self, _ctx: &mut dyn DrawCtx) {}
90
91 fn is_visible(&self) -> bool {
92 self.visible.get()
93 }
94
95 fn on_event(&mut self, _e: &Event) -> EventResult {
96 EventResult::Ignored
97 }
98}