2019-10-25 03:47:34 +02:00
|
|
|
use crate::{Primitive, Renderer};
|
|
|
|
|
use iced_native::{
|
2019-11-25 20:27:15 +01:00
|
|
|
scrollable, Background, MouseCursor, Point, Rectangle, ScrollbarGrab,
|
|
|
|
|
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-11-25 20:27:15 +01:00
|
|
|
fn background_bounds(bounds: Rectangle) -> Rectangle {
|
2019-10-29 05:09:54 +01:00
|
|
|
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-25 20:27:15 +01:00
|
|
|
fn scroller_bounds(
|
|
|
|
|
bounds: Rectangle,
|
|
|
|
|
content_bounds: Rectangle,
|
|
|
|
|
background_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: background_bounds.x + f32::from(SCROLLBAR_MARGIN),
|
|
|
|
|
y: background_bounds.y + y_offset,
|
|
|
|
|
width: background_bounds.width - f32::from(2 * SCROLLBAR_MARGIN),
|
|
|
|
|
height: scrollbar_height,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 03:47:34 +02:00
|
|
|
impl scrollable::Renderer for Renderer {
|
2019-11-25 20:27:15 +01:00
|
|
|
fn scrollbar_grab(
|
2019-10-29 05:09:54 +01:00
|
|
|
&self,
|
|
|
|
|
bounds: Rectangle,
|
|
|
|
|
content_bounds: Rectangle,
|
2019-11-25 20:27:15 +01:00
|
|
|
offset: u32,
|
2019-10-29 05:09:54 +01:00
|
|
|
cursor_position: Point,
|
2019-11-25 20:27:15 +01:00
|
|
|
) -> Option<(ScrollbarGrab, Rectangle)> {
|
|
|
|
|
let background_bounds = background_bounds(bounds);
|
|
|
|
|
if content_bounds.height > bounds.height
|
|
|
|
|
&& background_bounds.contains(cursor_position)
|
|
|
|
|
{
|
|
|
|
|
let scroller_bounds = scroller_bounds(
|
|
|
|
|
bounds,
|
|
|
|
|
content_bounds,
|
|
|
|
|
background_bounds,
|
|
|
|
|
offset,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let scrollbar_grab = if scroller_bounds.contains(cursor_position) {
|
|
|
|
|
ScrollbarGrab::Scroller
|
|
|
|
|
} else {
|
|
|
|
|
ScrollbarGrab::Background
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Some((scrollbar_grab, scroller_bounds))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
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,
|
|
|
|
|
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-11-25 20:27:15 +01:00
|
|
|
let background_bounds = background_bounds(bounds);
|
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-11-25 20:27:15 +01:00
|
|
|
&& (is_mouse_over || state.currently_grabbed())
|
2019-10-29 05:09:54 +01:00
|
|
|
{
|
2019-11-25 20:27:15 +01:00
|
|
|
let scroller_bounds = scroller_bounds(
|
|
|
|
|
bounds,
|
|
|
|
|
content_bounds,
|
|
|
|
|
background_bounds,
|
|
|
|
|
offset,
|
|
|
|
|
);
|
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-11-25 20:27:15 +01:00
|
|
|
if is_mouse_over_scrollbar || state.currently_grabbed() {
|
2019-10-29 05:09:54 +01:00
|
|
|
let scrollbar_background = Primitive::Quad {
|
|
|
|
|
bounds: Rectangle {
|
2019-11-25 20:27:15 +01:00
|
|
|
x: background_bounds.x
|
|
|
|
|
+ f32::from(SCROLLBAR_MARGIN),
|
|
|
|
|
width: background_bounds.width
|
2019-10-29 05:09:54 +01:00
|
|
|
- f32::from(2 * SCROLLBAR_MARGIN),
|
2019-11-25 20:27:15 +01:00
|
|
|
..background_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-11-25 20:27:15 +01:00
|
|
|
if is_mouse_over_scrollbar || state.currently_grabbed() {
|
2019-10-29 05:09:54 +01:00
|
|
|
MouseCursor::Idle
|
|
|
|
|
} else {
|
|
|
|
|
mouse_cursor
|
|
|
|
|
},
|
2019-10-25 03:47:34 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|