summaryrefslogtreecommitdiffstats
path: root/examples/lib-dfscq-log/src/spacer.rs
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2022-12-22 12:19:59 +0100
committerMinijackson <minijackson@riseup.net>2022-12-22 12:19:59 +0100
commit92a02c34628343153b33602eae00cef46e28d191 (patch)
tree8622ec528d24e456be22d984d93aa9bcafc97399 /examples/lib-dfscq-log/src/spacer.rs
downloaddiaphragm-92a02c34628343153b33602eae00cef46e28d191.tar.gz
diaphragm-92a02c34628343153b33602eae00cef46e28d191.zip
WIP
Diffstat (limited to 'examples/lib-dfscq-log/src/spacer.rs')
-rw-r--r--examples/lib-dfscq-log/src/spacer.rs133
1 files changed, 133 insertions, 0 deletions
diff --git a/examples/lib-dfscq-log/src/spacer.rs b/examples/lib-dfscq-log/src/spacer.rs
new file mode 100644
index 0000000..6878183
--- /dev/null
+++ b/examples/lib-dfscq-log/src/spacer.rs
@@ -0,0 +1,133 @@
1use diaphragm_core::{
2 types::{Float, ShapeContext},
3 ComplexShape, DrawResult, DynDrawable, SolverContext,
4};
5
6#[derive(Clone)]
7pub struct Spacer {
8 pub margin_left: Float,
9 pub margin_right: Float,
10 pub margin_top: Float,
11 pub margin_bottom: Float,
12 pub content: DynDrawable,
13}
14
15impl Spacer {
16 pub fn builder<T: Into<DynDrawable>>(drawable: T) -> SpacerBuilder {
17 SpacerBuilder::new(drawable)
18 }
19}
20
21impl ComplexShape for Spacer {
22 fn draw(&self, context: &ShapeContext, solver: &mut dyn SolverContext) -> DrawResult {
23 let mut result = DrawResult::new();
24
25 let bounds_left = context.bounds().left(solver);
26 let bounds_right = context.bounds().right(solver);
27 let bounds_top = context.bounds().top(solver);
28 let bounds_bottom = context.bounds().bottom(solver);
29
30 let wanted_content_left = solver.float_add(&[bounds_left, self.margin_left]);
31 let content_left = self.content.bounds().left(solver);
32 let content_left_constraint = solver.float_eq(content_left, wanted_content_left);
33 solver.constrain(content_left_constraint);
34
35 let wanted_content_right = solver.float_sub(&[bounds_right, self.margin_right]);
36 let content_right = self.content.bounds().right(solver);
37 let content_right_constraint = solver.float_eq(content_right, wanted_content_right);
38 solver.constrain(content_right_constraint);
39
40 let wanted_content_top = solver.float_add(&[bounds_top, self.margin_top]);
41 let content_top = self.content.bounds().top(solver);
42 let content_top_constraint = solver.float_eq(content_top, wanted_content_top);
43 solver.constrain(content_top_constraint);
44
45 let wanted_content_bottom = solver.float_sub(&[bounds_bottom, self.margin_bottom]);
46 let content_bottom = self.content.bounds().bottom(solver);
47 let content_bottom_constraint = solver.float_eq(content_bottom, wanted_content_bottom);
48 solver.constrain(content_bottom_constraint);
49
50 result.push_dyn(self.content.clone());
51
52 result
53 }
54}
55
56#[derive(Clone)]
57pub struct SpacerBuilder {
58 pub margin_left: Option<Float>,
59 pub margin_right: Option<Float>,
60 pub margin_top: Option<Float>,
61 pub margin_bottom: Option<Float>,
62 pub content: DynDrawable,
63}
64
65impl SpacerBuilder {
66 pub fn new<T: Into<DynDrawable>>(drawable: T) -> Self {
67 SpacerBuilder {
68 margin_left: None,
69 margin_right: None,
70 margin_top: None,
71 margin_bottom: None,
72 content: drawable.into(),
73 }
74 }
75
76 pub fn margin_left(&mut self, margin_left: Float) -> &mut Self {
77 self.margin_left = Some(margin_left);
78 self
79 }
80
81 pub fn margin_right(&mut self, margin_right: Float) -> &mut Self {
82 self.margin_right = Some(margin_right);
83 self
84 }
85
86 pub fn margin_top(&mut self, margin_top: Float) -> &mut Self {
87 self.margin_top = Some(margin_top);
88 self
89 }
90
91 pub fn margin_bottom(&mut self, margin_bottom: Float) -> &mut Self {
92 self.margin_bottom = Some(margin_bottom);
93 self
94 }
95
96 pub fn vertical_align_center(&mut self, solver: &mut dyn SolverContext) -> &mut Self {
97 let vertical_margin = solver.new_free_float();
98 self.margin_top = Some(vertical_margin);
99 self.margin_bottom = Some(vertical_margin);
100 self
101 }
102
103 pub fn vertical_align_center_with(&mut self, margin: Float) -> &mut Self {
104 self.margin_top = Some(margin);
105 self.margin_bottom = Some(margin);
106 self
107 }
108
109 pub fn horizontal_align_center(&mut self, solver: &mut dyn SolverContext) -> &mut Self {
110 let horizontal_margin = solver.new_free_float();
111 self.margin_left = Some(horizontal_margin);
112 self.margin_right = Some(horizontal_margin);
113 self
114 }
115
116 pub fn horizontal_align_center_with(&mut self, margin: Float) -> &mut Self {
117 self.margin_left = Some(margin);
118 self.margin_right = Some(margin);
119 self
120 }
121
122 pub fn build(&self, solver: &mut dyn SolverContext) -> Spacer {
123 Spacer {
124 margin_left: self.margin_left.unwrap_or_else(|| solver.new_free_float()),
125 margin_right: self.margin_right.unwrap_or_else(|| solver.new_free_float()),
126 margin_top: self.margin_top.unwrap_or_else(|| solver.new_free_float()),
127 margin_bottom: self
128 .margin_bottom
129 .unwrap_or_else(|| solver.new_free_float()),
130 content: self.content.clone(),
131 }
132 }
133}