Replace hard-coded params with struct members
This commit is contained in:
parent
23647d2f50
commit
09e67c5c27
3 changed files with 69 additions and 11 deletions
|
|
@ -15,9 +15,6 @@ pub use iced_style::scrollable::{Scrollbar, Scroller, StyleSheet};
|
||||||
pub type Scrollable<'a, Message, Backend> =
|
pub type Scrollable<'a, Message, Backend> =
|
||||||
iced_native::Scrollable<'a, Message, Renderer<Backend>>;
|
iced_native::Scrollable<'a, Message, Renderer<Backend>>;
|
||||||
|
|
||||||
const SCROLLBAR_WIDTH: u16 = 10;
|
|
||||||
const SCROLLBAR_MARGIN: u16 = 2;
|
|
||||||
|
|
||||||
impl<B> scrollable::Renderer for Renderer<B>
|
impl<B> scrollable::Renderer for Renderer<B>
|
||||||
where
|
where
|
||||||
B: Backend,
|
B: Backend,
|
||||||
|
|
@ -29,13 +26,16 @@ where
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
content_bounds: Rectangle,
|
content_bounds: Rectangle,
|
||||||
offset: u32,
|
offset: u32,
|
||||||
|
scrollbar_width: u16,
|
||||||
|
scrollbar_margin: u16,
|
||||||
|
_scroller_width: u16,
|
||||||
) -> Option<scrollable::Scrollbar> {
|
) -> Option<scrollable::Scrollbar> {
|
||||||
if content_bounds.height > bounds.height {
|
if content_bounds.height > bounds.height {
|
||||||
let scrollbar_bounds = Rectangle {
|
let scrollbar_bounds = Rectangle {
|
||||||
x: bounds.x + bounds.width
|
x: bounds.x + bounds.width
|
||||||
- f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN),
|
- f32::from(scrollbar_width + 2 * scrollbar_margin),
|
||||||
y: bounds.y,
|
y: bounds.y,
|
||||||
width: f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN),
|
width: f32::from(scrollbar_width + 2 * scrollbar_margin),
|
||||||
height: bounds.height,
|
height: bounds.height,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -44,14 +44,15 @@ where
|
||||||
let y_offset = offset as f32 * ratio;
|
let y_offset = offset as f32 * ratio;
|
||||||
|
|
||||||
let scroller_bounds = Rectangle {
|
let scroller_bounds = Rectangle {
|
||||||
x: scrollbar_bounds.x + f32::from(SCROLLBAR_MARGIN),
|
x: scrollbar_bounds.x + f32::from(scrollbar_margin),
|
||||||
y: scrollbar_bounds.y + y_offset,
|
y: scrollbar_bounds.y + y_offset,
|
||||||
width: scrollbar_bounds.width - f32::from(2 * SCROLLBAR_MARGIN),
|
width: scrollbar_bounds.width - f32::from(2 * scrollbar_margin),
|
||||||
height: scrollbar_height,
|
height: scrollbar_height,
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(scrollable::Scrollbar {
|
Some(scrollable::Scrollbar {
|
||||||
bounds: scrollbar_bounds,
|
bounds: scrollbar_bounds,
|
||||||
|
margin: scrollbar_margin,
|
||||||
scroller: scrollable::Scroller {
|
scroller: scrollable::Scroller {
|
||||||
bounds: scroller_bounds,
|
bounds: scroller_bounds,
|
||||||
},
|
},
|
||||||
|
|
@ -110,9 +111,9 @@ where
|
||||||
let scrollbar = if is_scrollbar_visible {
|
let scrollbar = if is_scrollbar_visible {
|
||||||
Primitive::Quad {
|
Primitive::Quad {
|
||||||
bounds: Rectangle {
|
bounds: Rectangle {
|
||||||
x: scrollbar.bounds.x + f32::from(SCROLLBAR_MARGIN),
|
x: scrollbar.bounds.x + f32::from(scrollbar.margin),
|
||||||
width: scrollbar.bounds.width
|
width: scrollbar.bounds.width
|
||||||
- f32::from(2 * SCROLLBAR_MARGIN),
|
- f32::from(2 * scrollbar.margin),
|
||||||
..scrollbar.bounds
|
..scrollbar.bounds
|
||||||
},
|
},
|
||||||
background: style
|
background: style
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,9 @@ impl scrollable::Renderer for Null {
|
||||||
_bounds: Rectangle,
|
_bounds: Rectangle,
|
||||||
_content_bounds: Rectangle,
|
_content_bounds: Rectangle,
|
||||||
_offset: u32,
|
_offset: u32,
|
||||||
|
_scrollbar_width: u16,
|
||||||
|
_scrollbar_margin: u16,
|
||||||
|
_scroller_width: u16,
|
||||||
) -> Option<scrollable::Scrollbar> {
|
) -> Option<scrollable::Scrollbar> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,9 @@ pub struct Scrollable<'a, Message, Renderer: self::Renderer> {
|
||||||
state: &'a mut State,
|
state: &'a mut State,
|
||||||
height: Length,
|
height: Length,
|
||||||
max_height: u32,
|
max_height: u32,
|
||||||
|
scrollbar_width: u16,
|
||||||
|
scrollbar_margin: u16,
|
||||||
|
scroller_width: u16,
|
||||||
content: Column<'a, Message, Renderer>,
|
content: Column<'a, Message, Renderer>,
|
||||||
style: Renderer::Style,
|
style: Renderer::Style,
|
||||||
}
|
}
|
||||||
|
|
@ -27,6 +30,9 @@ impl<'a, Message, Renderer: self::Renderer> Scrollable<'a, Message, Renderer> {
|
||||||
state,
|
state,
|
||||||
height: Length::Shrink,
|
height: Length::Shrink,
|
||||||
max_height: u32::MAX,
|
max_height: u32::MAX,
|
||||||
|
scrollbar_width: 10,
|
||||||
|
scrollbar_margin: 2,
|
||||||
|
scroller_width: 10,
|
||||||
content: Column::new(),
|
content: Column::new(),
|
||||||
style: Renderer::Style::default(),
|
style: Renderer::Style::default(),
|
||||||
}
|
}
|
||||||
|
|
@ -90,6 +96,32 @@ impl<'a, Message, Renderer: self::Renderer> Scrollable<'a, Message, Renderer> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the scrollbar width of the [`Scrollable`] .
|
||||||
|
/// Silently enforces a minimum value of 1.
|
||||||
|
///
|
||||||
|
/// [`Scrollable`]: struct.Scrollable.html
|
||||||
|
pub fn scrollbar_width(mut self, scrollbar_width: u16) -> Self {
|
||||||
|
self.scrollbar_width = scrollbar_width.max(1);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the scrollbar margin of the [`Scrollable`] .
|
||||||
|
///
|
||||||
|
/// [`Scrollable`]: struct.Scrollable.html
|
||||||
|
pub fn scrollbar_margin(mut self, scrollbar_margin: u16) -> Self {
|
||||||
|
self.scrollbar_margin = scrollbar_margin;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the scroller width of the [`Scrollable`] .
|
||||||
|
/// Silently enforces a minimum value of 1.
|
||||||
|
///
|
||||||
|
/// [`Scrollable`]: struct.Scrollable.html
|
||||||
|
pub fn scroller_width(mut self, scroller_width: u16) -> Self {
|
||||||
|
self.scroller_width = scroller_width.max(1);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the style of the [`Scrollable`] .
|
/// Sets the style of the [`Scrollable`] .
|
||||||
///
|
///
|
||||||
/// [`Scrollable`]: struct.Scrollable.html
|
/// [`Scrollable`]: struct.Scrollable.html
|
||||||
|
|
@ -178,7 +210,14 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
let offset = self.state.offset(bounds, content_bounds);
|
let offset = self.state.offset(bounds, content_bounds);
|
||||||
let scrollbar = renderer.scrollbar(bounds, content_bounds, offset);
|
let scrollbar = renderer.scrollbar(
|
||||||
|
bounds,
|
||||||
|
content_bounds,
|
||||||
|
offset,
|
||||||
|
self.scrollbar_width,
|
||||||
|
self.scrollbar_margin,
|
||||||
|
self.scroller_width,
|
||||||
|
);
|
||||||
let is_mouse_over_scrollbar = scrollbar
|
let is_mouse_over_scrollbar = scrollbar
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|scrollbar| scrollbar.is_mouse_over(cursor_position))
|
.map(|scrollbar| scrollbar.is_mouse_over(cursor_position))
|
||||||
|
|
@ -269,7 +308,14 @@ where
|
||||||
let content_layout = layout.children().next().unwrap();
|
let content_layout = layout.children().next().unwrap();
|
||||||
let content_bounds = content_layout.bounds();
|
let content_bounds = content_layout.bounds();
|
||||||
let offset = self.state.offset(bounds, content_bounds);
|
let offset = self.state.offset(bounds, content_bounds);
|
||||||
let scrollbar = renderer.scrollbar(bounds, content_bounds, offset);
|
let scrollbar = renderer.scrollbar(
|
||||||
|
bounds,
|
||||||
|
content_bounds,
|
||||||
|
offset,
|
||||||
|
self.scrollbar_width,
|
||||||
|
self.scrollbar_margin,
|
||||||
|
self.scroller_width,
|
||||||
|
);
|
||||||
|
|
||||||
let is_mouse_over = bounds.contains(cursor_position);
|
let is_mouse_over = bounds.contains(cursor_position);
|
||||||
let is_mouse_over_scrollbar = scrollbar
|
let is_mouse_over_scrollbar = scrollbar
|
||||||
|
|
@ -418,6 +464,11 @@ pub struct Scrollbar {
|
||||||
/// [`Scrollbar`]: struct.Scrollbar.html
|
/// [`Scrollbar`]: struct.Scrollbar.html
|
||||||
pub bounds: Rectangle,
|
pub bounds: Rectangle,
|
||||||
|
|
||||||
|
/// The margin within the [`Scrollbar`].
|
||||||
|
///
|
||||||
|
/// [`Scrollbar`]: struct.Scrollbar.html
|
||||||
|
pub margin: u16,
|
||||||
|
|
||||||
/// The bounds of the [`Scroller`].
|
/// The bounds of the [`Scroller`].
|
||||||
///
|
///
|
||||||
/// [`Scroller`]: struct.Scroller.html
|
/// [`Scroller`]: struct.Scroller.html
|
||||||
|
|
@ -486,6 +537,9 @@ pub trait Renderer: column::Renderer + Sized {
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
content_bounds: Rectangle,
|
content_bounds: Rectangle,
|
||||||
offset: u32,
|
offset: u32,
|
||||||
|
scrollbar_width: u16,
|
||||||
|
scrollbar_margin: u16,
|
||||||
|
scroller_width: u16,
|
||||||
) -> Option<Scrollbar>;
|
) -> Option<Scrollbar>;
|
||||||
|
|
||||||
/// Draws the [`Scrollable`].
|
/// Draws the [`Scrollable`].
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue