Skip to main content

eternal_ui/
paint.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Turning the laid-out tree into drawing calls.
6
7use eternal_styler::Corners;
8
9use crate::render::Renderer;
10use crate::ui::{NodeId, Ui};
11use crate::widget::PaintCx;
12
13impl<M: 'static> Ui<M> {
14    /// Draws the whole tree, as last laid out, through `renderer`.
15    ///
16    /// For each element, in tree order: the background, the border, the
17    /// widget's own content, then its children. Nothing is clipped unless a
18    /// widget asks for it, so an element that overflows its parent shows.
19    pub fn paint(&self, renderer: &mut dyn Renderer) {
20        if let Some(root) = self.root() {
21            self.paint_node(root, renderer);
22        }
23    }
24
25    fn paint_node(&self, id: NodeId, renderer: &mut dyn Renderer) {
26        let node = self.node(id);
27        let style = &node.style;
28        let rect = node.rect;
29        if rect.width <= 0.0 || rect.height <= 0.0 {
30            return;
31        }
32        let translucent = style.opacity < 1.0;
33        if translucent {
34            renderer.push_opacity(style.opacity);
35        }
36
37        let radius = Corners::new(
38            style.border_radius.top_left.resolve(rect.width),
39            style.border_radius.top_right.resolve(rect.width),
40            style.border_radius.bottom_right.resolve(rect.width),
41            style.border_radius.bottom_left.resolve(rect.width),
42        );
43        if style.background_color.a > 0.0 {
44            renderer.fill_rect(rect, radius, style.background_color);
45        }
46        let widths = style.border_width;
47        if widths.top > 0.0 || widths.right > 0.0 || widths.bottom > 0.0 || widths.left > 0.0 {
48            renderer.border(rect, widths, style.border_color, radius);
49        }
50
51        let cx = PaintCx {
52            style,
53            state: self.state_of(id),
54        };
55        node.widget.paint(renderer, &cx, node.content);
56
57        for &child in &node.children {
58            self.paint_node(child, renderer);
59        }
60
61        if translucent {
62            renderer.pop_opacity();
63        }
64    }
65}