fission_core/ui/widgets/
composite.rs1use crate::internal::InternalLower;
2use crate::lowering::{InternalIrBuilder, InternalLoweringCx};
3use crate::ui::Widget;
4use fission_ir::{CompositeScalar, CompositeStyle, Op, StructuralOp, WidgetId};
5use serde::{Deserialize, Serialize};
6use std::hash::{Hash, Hasher};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Composite {
10 pub id: Option<WidgetId>,
11 pub style: CompositeStyle,
12 pub child: Widget,
13}
14
15impl Default for Composite {
16 fn default() -> Self {
17 Self {
18 id: None,
19 style: CompositeStyle::default(),
20 child: crate::ui::widgets::spacer::Spacer::default().into(),
21 }
22 }
23}
24
25impl Composite {
26 pub fn new(child: impl Into<Widget>) -> Self {
27 Self {
28 child: child.into(),
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 motion_opacity(mut self, target: WidgetId, base: f32) -> Self {
39 self.style.opacity = Some(CompositeScalar::new(base).motion(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 motion_translate_x(mut self, target: WidgetId, base: f32) -> Self {
49 self.style.translate_x = Some(CompositeScalar::new(base).motion(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 motion_translate_y(mut self, target: WidgetId, base: f32) -> Self {
59 self.style.translate_y = Some(CompositeScalar::new(base).motion(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 motion_scale(mut self, target: WidgetId, base: f32) -> Self {
69 self.style.scale = Some(CompositeScalar::new(base).motion(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 motion_rotation(mut self, target: WidgetId, base: f32) -> Self {
79 self.style.rotation = Some(CompositeScalar::new(base).motion(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
94impl InternalLower for Composite {
95 fn lower(&self, cx: &mut InternalLoweringCx) -> WidgetId {
96 let id = self.id.map(Into::into).unwrap_or_else(|| cx.next_node_id());
97
98 cx.push_scope(id);
99 let child_id = self.child.lower(cx);
100 cx.pop_scope();
101
102 let mut hasher = std::collections::hash_map::DefaultHasher::new();
103 id.hash(&mut hasher);
104 self.style.hash(&mut hasher);
105 let stable_hash = hasher.finish();
106
107 let mut builder =
108 InternalIrBuilder::new(id, Op::Structural(StructuralOp::Group { stable_hash }))
109 .composite(self.style.clone());
110 builder.add_child(child_id);
111 builder.build(cx)
112 }
113}