summaryrefslogtreecommitdiffstats
path: root/examples/lib-dfscq-log/src/labeled.rs
blob: 20aebcd19ecabf3a1a8868721dec2baddc2f3915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use diaphragm_core::{
    core_shapes::Text,
    text::FontDescription,
    types::{Bounds, Float, ShapeContext},
    ComplexShape, DrawResult, DynClone, SolverContext,
};

pub struct Labeled {
    pub label: String,
    pub label_font: FontDescription,
    pub content: Box<dyn ComplexShape>,
    pub content_left: Float,
}

impl ComplexShape for Labeled {
    fn draw(&self, context: &ShapeContext, solver: &mut dyn SolverContext) -> DrawResult {
        let mut result = DrawResult::new();

        // Label

        let label = Text {
            content: self.label.clone(),
            font: self.label_font.clone(),
        };

        let mut label_context = context.clone();
        // TODO: make the text constrain the width
        label_context.bounds.width = solver.new_free_float();

        let label_right = solver.float_add(&[context.bounds.left, label_context.bounds.width]);
        let content_left_constrain = solver.float_eq(label_right, self.content_left);
        solver.constrain(content_left_constrain);

        result.push_shape(label, label_context);

        // Content

        let content_context = ShapeContext {
            bounds: Bounds {
                top: context.bounds.top,
                left: solver.float_add(&[context.bounds.left, self.content_left]),
                height: context.bounds.height,
                width: context.bounds.width,
            },
            fill: context.fill.clone(),
            stroke: context.stroke.clone(),
        };

        result.push_boxed_shape(self.content.dyn_clone(), content_context);

        result
    }
}

impl DynClone for Labeled {
    fn dyn_clone(&self) -> Box<dyn ComplexShape> {
        Box::new(Labeled {
            label: self.label.clone(),
            label_font: self.label_font.clone(),
            content: self.content.dyn_clone(),
            content_left: self.content_left.clone(),
        })
    }
}