fission_core/ui/widgets/
composite.rs1use crate::lowering::{LoweringContext, NodeBuilder};
2use crate::ui::traits::Lower;
3use crate::ui::Node;
4use fission_ir::{CompositeScalar, CompositeStyle, NodeId, Op, StructuralOp, WidgetNodeId};
5use serde::{Deserialize, Serialize};
6use std::hash::{Hash, Hasher};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Composite {
10 pub id: Option<NodeId>,
11 pub style: CompositeStyle,
12 pub child: Box<Node>,
13}
14
15impl Default for Composite {
16 fn default() -> Self {
17 Self {
18 id: None,
19 style: CompositeStyle::default(),
20 child: Box::new(crate::ui::widgets::spacer::Spacer::default().into_node()),
21 }
22 }
23}
24
25impl Composite {
26 pub fn new(child: Node) -> Self {
27 Self {
28 child: Box::new(child),
29 ..Default::default()
30 }
31 }
32
33 pub fn opacity(mut self, value: f32) -> Self {
34 self.style.opacity = Some(CompositeScalar::new(value));
35 self
36 }
37
38 pub fn animated_opacity(mut self, target: WidgetNodeId, base: f32) -> Self {
39 self.style.opacity = Some(CompositeScalar::new(base).animated(target));
40 self
41 }
42
43 pub fn translate_x(mut self, value: f32) -> Self {
44 self.style.translate_x = Some(CompositeScalar::new(value));
45 self
46 }
47
48 pub fn animated_translate_x(mut self, target: WidgetNodeId, base: f32) -> Self {
49 self.style.translate_x = Some(CompositeScalar::new(base).animated(target));
50 self
51 }
52
53 pub fn translate_y(mut self, value: f32) -> Self {
54 self.style.translate_y = Some(CompositeScalar::new(value));
55 self
56 }
57
58 pub fn animated_translate_y(mut self, target: WidgetNodeId, base: f32) -> Self {
59 self.style.translate_y = Some(CompositeScalar::new(base).animated(target));
60 self
61 }
62
63 pub fn scale(mut self, value: f32) -> Self {
64 self.style.scale = Some(CompositeScalar::new(value));
65 self
66 }
67
68 pub fn animated_scale(mut self, target: WidgetNodeId, base: f32) -> Self {
69 self.style.scale = Some(CompositeScalar::new(base).animated(target));
70 self
71 }
72
73 pub fn rotation(mut self, value: f32) -> Self {
74 self.style.rotation = Some(CompositeScalar::new(value));
75 self
76 }
77
78 pub fn animated_rotation(mut self, target: WidgetNodeId, base: f32) -> Self {
79 self.style.rotation = Some(CompositeScalar::new(base).animated(target));
80 self
81 }
82
83 pub fn clip_to_bounds(mut self, clip: bool) -> Self {
84 self.style.clip_to_bounds = clip;
85 self
86 }
87
88 pub fn repaint_boundary(mut self, enabled: bool) -> Self {
89 self.style.repaint_boundary = enabled;
90 self
91 }
92
93 pub fn into_node(self) -> Node {
94 Node::Composite(self)
95 }
96}
97
98impl Lower for Composite {
99 fn lower(&self, cx: &mut LoweringContext) -> NodeId {
100 let id = self.id.unwrap_or_else(|| cx.next_node_id());
101
102 cx.push_scope(id);
103 let child_id = self.child.lower(cx);
104 cx.pop_scope();
105
106 let mut hasher = std::collections::hash_map::DefaultHasher::new();
107 id.hash(&mut hasher);
108 self.style.hash(&mut hasher);
109 let stable_hash = hasher.finish();
110
111 let mut builder = NodeBuilder::new(id, Op::Structural(StructuralOp::Group { stable_hash }))
112 .composite(self.style.clone());
113 builder.add_child(child_id);
114 builder.build(cx)
115 }
116}