2019-10-25 03:47:34 +02:00
|
|
|
use crate::{Primitive, Renderer};
|
2019-12-02 18:51:34 +01:00
|
|
|
use iced_native::{scrollable, Background, MouseCursor, Rectangle, Vector};
|
2019-10-25 03:47:34 +02:00
|
|
|
|
2019-10-29 05:09:54 +01:00
|
|
|
const SCROLLBAR_WIDTH: u16 = 10;
|
2019-11-02 01:42:51 +01:00
|
|
|
const SCROLLBAR_MARGIN: u16 = 2;
|
2019-10-29 05:09:54 +01:00
|
|
|
|
2019-10-25 03:47:34 +02:00
|
|
|
impl scrollable::Renderer for Renderer {
|
2019-12-02 18:51:34 +01:00
|
|
|
fn scrollbar_bounds(bounds: Rectangle) -> Rectangle {
|
|
|
|
|
Rectangle {
|
|
|
|
|
x: bounds.x + bounds.width
|
|
|
|
|
- f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN),
|
|
|
|
|
y: bounds.y,
|
|
|
|
|
width: f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN),
|
|
|
|
|
height: bounds.height,
|
|
|
|
|
}
|
2019-11-30 17:30:42 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-02 18:51:34 +01:00
|
|
|
fn scroller_bounds(
|
2019-11-30 17:30:42 +01:00
|
|
|
bounds: Rectangle,
|
|
|
|
|
content_bounds: Rectangle,
|
2019-12-02 18:51:34 +01:00
|
|
|
scrollbar_bounds: Rectangle,
|
|
|
|
|
offset: u32,
|
|
|
|
|
) -> Rectangle {
|
|
|
|
|
let ratio = bounds.height / content_bounds.height;
|
|
|
|
|
let scrollbar_height = bounds.height * ratio;
|
|
|
|
|
let y_offset = offset as f32 * ratio;
|
|
|
|
|
Rectangle {
|
|
|
|
|
x: scrollbar_bounds.x + f32::from(SCROLLBAR_MARGIN),
|
|
|
|
|
y: scrollbar_bounds.y + y_offset,
|
|
|
|
|
width: scrollbar_bounds.width - f32::from(2 * SCROLLBAR_MARGIN),
|
|
|
|
|
height: scrollbar_height,
|
2019-11-25 20:27:15 +01:00
|
|
|
}
|
2019-10-29 05:09:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-21 13:47:20 +01:00
|
|
|
fn draw(
|
2019-10-25 03:47:34 +02:00
|
|
|
&mut self,
|
2019-11-21 13:47:20 +01:00
|
|
|
state: &scrollable::State,
|
2019-10-29 02:13:22 +01:00
|
|
|
bounds: Rectangle,
|
2019-11-21 13:47:20 +01:00
|
|
|
content_bounds: Rectangle,
|
|
|
|
|
is_mouse_over: bool,
|
|
|
|
|
is_mouse_over_scrollbar: bool,
|
2019-12-02 18:51:34 +01:00
|
|
|
scrollbar_bounds: Rectangle,
|
|
|
|
|
scroller_bounds: Rectangle,
|
2019-11-21 13:47:20 +01:00
|
|
|
offset: u32,
|
|
|
|
|
(content, mouse_cursor): Self::Output,
|
2019-10-25 03:47:34 +02:00
|
|
|
) -> Self::Output {
|
2019-10-29 05:09:54 +01:00
|
|
|
let is_content_overflowing = content_bounds.height > bounds.height;
|
2019-10-25 03:47:34 +02:00
|
|
|
|
2019-10-30 03:31:07 +01:00
|
|
|
let clip = Primitive::Clip {
|
2019-10-25 03:47:34 +02:00
|
|
|
bounds,
|
2019-11-05 03:16:46 +01:00
|
|
|
offset: Vector::new(0, offset),
|
2019-10-25 03:47:34 +02:00
|
|
|
content: Box::new(content),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
(
|
2019-10-29 05:09:54 +01:00
|
|
|
if is_content_overflowing
|
2019-12-02 18:51:34 +01:00
|
|
|
&& (is_mouse_over || state.is_scroller_grabbed())
|
2019-10-29 05:09:54 +01:00
|
|
|
{
|
2019-10-29 02:00:17 +01:00
|
|
|
let scrollbar = Primitive::Quad {
|
2019-11-25 20:27:15 +01:00
|
|
|
bounds: scroller_bounds,
|
2019-11-07 01:03:29 -05:00
|
|
|
background: Background::Color([0.0, 0.0, 0.0, 0.7].into()),
|
2019-10-29 02:00:17 +01:00
|
|
|
border_radius: 5,
|
|
|
|
|
};
|
2019-10-29 05:09:54 +01:00
|
|
|
|
2019-12-02 18:51:34 +01:00
|
|
|
if is_mouse_over_scrollbar || state.is_scroller_grabbed() {
|
2019-10-29 05:09:54 +01:00
|
|
|
let scrollbar_background = Primitive::Quad {
|
|
|
|
|
bounds: Rectangle {
|
2019-12-02 18:51:34 +01:00
|
|
|
x: scrollbar_bounds.x + f32::from(SCROLLBAR_MARGIN),
|
|
|
|
|
width: scrollbar_bounds.width
|
2019-10-29 05:09:54 +01:00
|
|
|
- f32::from(2 * SCROLLBAR_MARGIN),
|
2019-12-02 18:51:34 +01:00
|
|
|
..scrollbar_bounds
|
2019-10-29 05:09:54 +01:00
|
|
|
},
|
2019-11-07 01:03:29 -05:00
|
|
|
background: Background::Color(
|
|
|
|
|
[0.0, 0.0, 0.0, 0.3].into(),
|
|
|
|
|
),
|
2019-10-29 05:09:54 +01:00
|
|
|
border_radius: 5,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Primitive::Group {
|
2019-10-30 03:31:07 +01:00
|
|
|
primitives: vec![clip, scrollbar_background, scrollbar],
|
2019-10-29 05:09:54 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Primitive::Group {
|
2019-10-30 03:31:07 +01:00
|
|
|
primitives: vec![clip, scrollbar],
|
2019-10-29 05:09:54 +01:00
|
|
|
}
|
2019-10-29 02:00:17 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2019-10-30 03:31:07 +01:00
|
|
|
clip
|
2019-10-25 03:47:34 +02:00
|
|
|
},
|
2019-12-02 18:51:34 +01:00
|
|
|
if is_mouse_over_scrollbar || state.is_scroller_grabbed() {
|
2019-10-29 05:09:54 +01:00
|
|
|
MouseCursor::Idle
|
|
|
|
|
} else {
|
|
|
|
|
mouse_cursor
|
|
|
|
|
},
|
2019-10-25 03:47:34 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|