summaryrefslogtreecommitdiffstats
path: root/wgpu/src/renderer/scrollable.rs
blob: 5b93a76546fa0443249065b8ccfb43a6c762799f (plain) (blame)
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
65
66
67
68
69
use crate::{Primitive, Renderer};
use iced_native::{
    scrollable, Background, Color, Layout, Point, Rectangle, Scrollable, Widget,
};

impl scrollable::Renderer for Renderer {
    fn draw<Message>(
        &mut self,
        scrollable: &Scrollable<'_, Message, Self>,
        layout: Layout<'_>,
        cursor_position: Point,
    ) -> Self::Output {
        let bounds = layout.bounds();
        let is_mouse_over = bounds.contains(cursor_position);

        let content = layout.children().next().unwrap();
        let content_bounds = content.bounds();

        let offset = scrollable.state.offset(bounds, content_bounds);

        let cursor_position = if bounds.contains(cursor_position) {
            Point::new(cursor_position.x, cursor_position.y + offset as f32)
        } else {
            Point::new(cursor_position.x, -1.0)
        };

        let (content, mouse_cursor) =
            scrollable.content.draw(self, content, cursor_position);

        let primitive = Primitive::Scrollable {
            bounds,
            offset,
            content: Box::new(content),
        };

        (
            Primitive::Group {
                primitives: if is_mouse_over
                    && content_bounds.height > bounds.height
                {
                    let ratio = bounds.height / content_bounds.height;
                    let scrollbar_height = bounds.height * ratio;
                    let y_offset = offset as f32 * ratio;

                    let scrollbar = Primitive::Quad {
                        bounds: Rectangle {
                            x: bounds.x + bounds.width - 12.0,
                            y: bounds.y + y_offset,
                            width: 10.0,
                            height: scrollbar_height,
                        },
                        background: Background::Color(Color {
                            r: 0.0,
                            g: 0.0,
                            b: 0.0,
                            a: 0.5,
                        }),
                        border_radius: 5,
                    };

                    vec![primitive, scrollbar]
                } else {
                    vec![primitive]
                },
            },
            mouse_cursor,
        )
    }
}