rui/views/
cond.rs

1use crate::*;
2use std::any::Any;
3
4/// Struct for `cond`
5pub struct Cond<V0, V1> {
6    cond: bool,
7    if_true: V0,
8    if_false: V1,
9}
10
11impl<V0, V1> View for Cond<V0, V1>
12where
13    V0: View,
14    V1: View,
15{
16    fn process(
17        &self,
18        event: &Event,
19        id: ViewId,
20        cx: &mut Context,
21        actions: &mut Vec<Box<dyn Any>>,
22    ) {
23        if self.cond {
24            self.if_true.process(event, id.child(&0), cx, actions)
25        } else {
26            self.if_false.process(event, id.child(&1), cx, actions)
27        }
28    }
29
30    fn draw(&self, id: ViewId, args: &mut DrawArgs) {
31        if self.cond {
32            self.if_true.draw(id.child(&0), args)
33        } else {
34            self.if_false.draw(id.child(&1), args)
35        }
36    }
37
38    fn layout(&self, id: ViewId, args: &mut LayoutArgs) -> LocalSize {
39        if self.cond {
40            self.if_true.layout(id.child(&0), args)
41        } else {
42            self.if_false.layout(id.child(&1), args)
43        }
44    }
45
46    fn hittest(&self, id: ViewId, pt: LocalPoint, cx: &mut Context) -> Option<ViewId> {
47        if self.cond {
48            self.if_true.hittest(id.child(&0), pt, cx)
49        } else {
50            self.if_false.hittest(id.child(&1), pt, cx)
51        }
52    }
53
54    fn commands(&self, id: ViewId, cx: &mut Context, cmds: &mut Vec<CommandInfo>) {
55        if self.cond {
56            self.if_true.commands(id.child(&0), cx, cmds)
57        } else {
58            self.if_false.commands(id.child(&1), cx, cmds)
59        }
60    }
61
62    fn gc(&self, id: ViewId, cx: &mut Context, map: &mut Vec<ViewId>) {
63        if self.cond {
64            self.if_true.gc(id.child(&0), cx, map)
65        } else {
66            self.if_false.gc(id.child(&1), cx, map)
67        }
68    }
69
70    fn access(
71        &self,
72        id: ViewId,
73        cx: &mut Context,
74        nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>,
75    ) -> Option<accesskit::NodeId> {
76        if self.cond {
77            self.if_true.access(id.child(&0), cx, nodes)
78        } else {
79            self.if_false.access(id.child(&1), cx, nodes)
80        }
81    }
82}
83
84impl<V0, V1> private::Sealed for Cond<V0, V1> {}
85
86/// Switches between views according to a boolean.
87pub fn cond(cond: bool, if_true: impl View, if_false: impl View) -> impl View {
88    Cond {
89        cond,
90        if_true,
91        if_false,
92    }
93}