2019-10-25 03:47:34 +02:00
|
|
|
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>,
|
2019-10-29 02:13:22 +01:00
|
|
|
bounds: Rectangle,
|
|
|
|
|
content: Layout<'_>,
|
2019-10-25 03:47:34 +02:00
|
|
|
cursor_position: Point,
|
|
|
|
|
) -> Self::Output {
|
|
|
|
|
let is_mouse_over = bounds.contains(cursor_position);
|
|
|
|
|
let content_bounds = content.bounds();
|
|
|
|
|
|
2019-10-27 01:24:08 +02:00
|
|
|
let offset = scrollable.state.offset(bounds, content_bounds);
|
|
|
|
|
|
2019-10-25 03:47:34 +02:00
|
|
|
let cursor_position = if bounds.contains(cursor_position) {
|
2019-10-27 01:24:08 +02:00
|
|
|
Point::new(cursor_position.x, cursor_position.y + offset as f32)
|
2019-10-25 03:47:34 +02:00
|
|
|
} else {
|
|
|
|
|
Point::new(cursor_position.x, -1.0)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let (content, mouse_cursor) =
|
|
|
|
|
scrollable.content.draw(self, content, cursor_position);
|
|
|
|
|
|
|
|
|
|
let primitive = Primitive::Scrollable {
|
|
|
|
|
bounds,
|
2019-10-27 01:24:08 +02:00
|
|
|
offset,
|
2019-10-25 03:47:34 +02:00
|
|
|
content: Box::new(content),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
(
|
2019-10-29 02:00:17 +01:00
|
|
|
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;
|
2019-10-25 03:47:34 +02:00
|
|
|
|
2019-10-29 02:00:17 +01:00
|
|
|
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.7,
|
|
|
|
|
}),
|
|
|
|
|
border_radius: 5,
|
|
|
|
|
};
|
|
|
|
|
Primitive::Group {
|
|
|
|
|
primitives: vec![primitive, scrollbar],
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
primitive
|
2019-10-25 03:47:34 +02:00
|
|
|
},
|
|
|
|
|
mouse_cursor,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|