2019-10-25 03:47:34 +02:00
|
|
|
use crate::{Primitive, Renderer};
|
2019-12-31 21:35:42 +01:00
|
|
|
use iced_native::{
|
|
|
|
|
scrollable, Background, Color, 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 {
|
2020-01-06 21:01:09 +01:00
|
|
|
type Style = Box<dyn iced_style::scrollable::StyleSheet>;
|
|
|
|
|
|
2019-12-03 06:48:29 +01:00
|
|
|
fn scrollbar(
|
|
|
|
|
&self,
|
2019-11-30 17:30:42 +01:00
|
|
|
bounds: Rectangle,
|
|
|
|
|
content_bounds: Rectangle,
|
2019-12-02 18:51:34 +01:00
|
|
|
offset: u32,
|
2019-12-03 06:48:29 +01:00
|
|
|
) -> Option<scrollable::Scrollbar> {
|
|
|
|
|
if content_bounds.height > bounds.height {
|
|
|
|
|
let scrollbar_bounds = 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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let ratio = bounds.height / content_bounds.height;
|
|
|
|
|
let scrollbar_height = bounds.height * ratio;
|
|
|
|
|
let y_offset = offset as f32 * ratio;
|
|
|
|
|
|
|
|
|
|
let scroller_bounds = 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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Some(scrollable::Scrollbar {
|
|
|
|
|
bounds: scrollbar_bounds,
|
|
|
|
|
scroller: scrollable::Scroller {
|
|
|
|
|
bounds: scroller_bounds,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
None
|
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-12-03 06:48:29 +01:00
|
|
|
_content_bounds: Rectangle,
|
2019-11-21 13:47:20 +01:00
|
|
|
is_mouse_over: bool,
|
|
|
|
|
is_mouse_over_scrollbar: bool,
|
2019-12-03 06:48:29 +01:00
|
|
|
scrollbar: Option<scrollable::Scrollbar>,
|
2019-11-21 13:47:20 +01:00
|
|
|
offset: u32,
|
2020-01-06 21:01:09 +01:00
|
|
|
style_sheet: &Self::Style,
|
2019-11-21 13:47:20 +01:00
|
|
|
(content, mouse_cursor): Self::Output,
|
2019-10-25 03:47:34 +02:00
|
|
|
) -> Self::Output {
|
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-12-03 06:48:29 +01:00
|
|
|
if let Some(scrollbar) = scrollbar {
|
2020-01-07 03:18:39 +01:00
|
|
|
let style = if state.is_scroller_grabbed() {
|
|
|
|
|
style_sheet.dragging()
|
|
|
|
|
} else if is_mouse_over_scrollbar {
|
|
|
|
|
style_sheet.hovered()
|
|
|
|
|
} else {
|
|
|
|
|
style_sheet.active()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let is_scrollbar_visible =
|
|
|
|
|
style.background.is_some() || style.border_width > 0;
|
2020-01-06 21:01:09 +01:00
|
|
|
|
2020-01-07 03:18:39 +01:00
|
|
|
let scroller = if is_mouse_over
|
|
|
|
|
|| state.is_scroller_grabbed()
|
|
|
|
|
|| is_scrollbar_visible
|
|
|
|
|
{
|
|
|
|
|
Primitive::Quad {
|
2019-12-03 06:48:29 +01:00
|
|
|
bounds: scrollbar.scroller.bounds,
|
2020-01-06 21:01:09 +01:00
|
|
|
background: Background::Color(style.scroller.color),
|
|
|
|
|
border_radius: style.scroller.border_radius,
|
|
|
|
|
border_width: style.scroller.border_width,
|
|
|
|
|
border_color: style.scroller.border_color,
|
2020-01-07 03:18:39 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Primitive::None
|
|
|
|
|
};
|
2019-12-03 06:48:29 +01:00
|
|
|
|
2020-01-07 03:18:39 +01:00
|
|
|
let scrollbar = if is_scrollbar_visible {
|
|
|
|
|
Primitive::Quad {
|
|
|
|
|
bounds: Rectangle {
|
|
|
|
|
x: scrollbar.bounds.x + f32::from(SCROLLBAR_MARGIN),
|
|
|
|
|
width: scrollbar.bounds.width
|
|
|
|
|
- f32::from(2 * SCROLLBAR_MARGIN),
|
|
|
|
|
..scrollbar.bounds
|
|
|
|
|
},
|
|
|
|
|
background: style
|
|
|
|
|
.background
|
|
|
|
|
.unwrap_or(Background::Color(Color::TRANSPARENT)),
|
|
|
|
|
border_radius: style.border_radius,
|
|
|
|
|
border_width: style.border_width,
|
|
|
|
|
border_color: style.border_color,
|
2019-10-29 05:09:54 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2020-01-07 03:18:39 +01:00
|
|
|
Primitive::None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Primitive::Group {
|
|
|
|
|
primitives: vec![clip, scrollbar, scroller],
|
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
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|