2020-05-19 17:15:44 +02:00
|
|
|
//! Navigate an endless amount of content with a scrollbar.
|
2021-10-14 16:07:22 +07:00
|
|
|
use crate::{Backend, Renderer};
|
2020-05-19 21:00:40 +02:00
|
|
|
use iced_native::scrollable;
|
2021-10-14 16:07:22 +07:00
|
|
|
use iced_native::Rectangle;
|
2020-05-19 17:15:44 +02:00
|
|
|
|
|
|
|
|
pub use iced_native::scrollable::State;
|
|
|
|
|
pub use iced_style::scrollable::{Scrollbar, Scroller, StyleSheet};
|
|
|
|
|
|
|
|
|
|
/// A widget that can vertically display an infinite amount of content
|
|
|
|
|
/// with a scrollbar.
|
|
|
|
|
///
|
|
|
|
|
/// This is an alias of an `iced_native` scrollable with a default
|
|
|
|
|
/// `Renderer`.
|
|
|
|
|
pub type Scrollable<'a, Message, Backend> =
|
|
|
|
|
iced_native::Scrollable<'a, Message, Renderer<Backend>>;
|
2020-05-19 21:00:40 +02:00
|
|
|
|
|
|
|
|
impl<B> scrollable::Renderer for Renderer<B>
|
|
|
|
|
where
|
|
|
|
|
B: Backend,
|
|
|
|
|
{
|
|
|
|
|
type Style = Box<dyn iced_style::scrollable::StyleSheet>;
|
|
|
|
|
|
|
|
|
|
fn scrollbar(
|
|
|
|
|
&self,
|
|
|
|
|
bounds: Rectangle,
|
|
|
|
|
content_bounds: Rectangle,
|
|
|
|
|
offset: u32,
|
2020-10-22 14:07:07 -05:00
|
|
|
scrollbar_width: u16,
|
|
|
|
|
scrollbar_margin: u16,
|
2020-10-22 16:05:44 -05:00
|
|
|
scroller_width: u16,
|
2020-05-19 21:00:40 +02:00
|
|
|
) -> Option<scrollable::Scrollbar> {
|
|
|
|
|
if content_bounds.height > bounds.height {
|
2020-10-22 16:05:44 -05:00
|
|
|
let outer_width =
|
|
|
|
|
scrollbar_width.max(scroller_width) + 2 * scrollbar_margin;
|
|
|
|
|
|
|
|
|
|
let outer_bounds = Rectangle {
|
|
|
|
|
x: bounds.x + bounds.width - outer_width as f32,
|
|
|
|
|
y: bounds.y,
|
|
|
|
|
width: outer_width as f32,
|
|
|
|
|
height: bounds.height,
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-19 21:00:40 +02:00
|
|
|
let scrollbar_bounds = Rectangle {
|
|
|
|
|
x: bounds.x + bounds.width
|
2020-10-22 16:05:44 -05:00
|
|
|
- f32::from(outer_width / 2 + scrollbar_width / 2),
|
2020-05-19 21:00:40 +02:00
|
|
|
y: bounds.y,
|
2020-10-22 16:05:44 -05:00
|
|
|
width: scrollbar_width as f32,
|
2020-05-19 21:00:40 +02:00
|
|
|
height: bounds.height,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let ratio = bounds.height / content_bounds.height;
|
2020-10-22 16:05:44 -05:00
|
|
|
let scroller_height = bounds.height * ratio;
|
2020-05-19 21:00:40 +02:00
|
|
|
let y_offset = offset as f32 * ratio;
|
|
|
|
|
|
|
|
|
|
let scroller_bounds = Rectangle {
|
2020-10-22 16:05:44 -05:00
|
|
|
x: bounds.x + bounds.width
|
|
|
|
|
- f32::from(outer_width / 2 + scroller_width / 2),
|
2020-05-19 21:00:40 +02:00
|
|
|
y: scrollbar_bounds.y + y_offset,
|
2020-10-22 16:05:44 -05:00
|
|
|
width: scroller_width as f32,
|
|
|
|
|
height: scroller_height,
|
2020-05-19 21:00:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Some(scrollable::Scrollbar {
|
2020-10-22 16:05:44 -05:00
|
|
|
outer_bounds,
|
2020-05-19 21:00:40 +02:00
|
|
|
bounds: scrollbar_bounds,
|
2020-10-22 14:07:07 -05:00
|
|
|
margin: scrollbar_margin,
|
2020-05-19 21:00:40 +02:00
|
|
|
scroller: scrollable::Scroller {
|
|
|
|
|
bounds: scroller_bounds,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|