Rename ScrollbarProperties to Direction in scrollable
This commit is contained in:
parent
fa04f40524
commit
493571695a
2 changed files with 61 additions and 70 deletions
|
|
@ -1,12 +1,11 @@
|
||||||
use iced::widget::scrollable::{
|
use iced::widget::scrollable::{Properties, Scrollbar, Scroller};
|
||||||
Properties, ScrollbarProperties, Scrollbar, Scroller,
|
|
||||||
};
|
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
button, column, container, horizontal_space, progress_bar, radio, row,
|
button, column, container, horizontal_space, progress_bar, radio, row,
|
||||||
scrollable, slider, text, vertical_space,
|
scrollable, slider, text, vertical_space,
|
||||||
};
|
};
|
||||||
use iced::{executor, theme, Alignment, Color};
|
use iced::{executor, theme, Alignment, Color};
|
||||||
use iced::{Application, Command, Element, Length, Settings, Theme};
|
use iced::{Application, Command, Element, Length, Settings, Theme};
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
static SCROLLABLE_ID: Lazy<scrollable::Id> = Lazy::new(scrollable::Id::unique);
|
static SCROLLABLE_ID: Lazy<scrollable::Id> = Lazy::new(scrollable::Id::unique);
|
||||||
|
|
@ -201,7 +200,7 @@ impl Application for ScrollableDemo {
|
||||||
.spacing(40),
|
.spacing(40),
|
||||||
)
|
)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.scrollbar_properties(ScrollbarProperties::Vertical(
|
.direction(scrollable::Direction::Vertical(
|
||||||
Properties::new()
|
Properties::new()
|
||||||
.width(self.scrollbar_width)
|
.width(self.scrollbar_width)
|
||||||
.margin(self.scrollbar_margin)
|
.margin(self.scrollbar_margin)
|
||||||
|
|
@ -225,7 +224,7 @@ impl Application for ScrollableDemo {
|
||||||
.spacing(40),
|
.spacing(40),
|
||||||
)
|
)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.scrollbar_properties(ScrollbarProperties::Horizontal(
|
.direction(scrollable::Direction::Horizontal(
|
||||||
Properties::new()
|
Properties::new()
|
||||||
.width(self.scrollbar_width)
|
.width(self.scrollbar_width)
|
||||||
.margin(self.scrollbar_margin)
|
.margin(self.scrollbar_margin)
|
||||||
|
|
@ -266,16 +265,17 @@ impl Application for ScrollableDemo {
|
||||||
.spacing(40),
|
.spacing(40),
|
||||||
)
|
)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.scrollbar_properties(ScrollbarProperties::Both(
|
.direction({
|
||||||
Properties::new()
|
let properties = Properties::new()
|
||||||
.width(self.scrollbar_width)
|
.width(self.scrollbar_width)
|
||||||
.margin(self.scrollbar_margin)
|
.margin(self.scrollbar_margin)
|
||||||
.scroller_width(self.scroller_width),
|
.scroller_width(self.scroller_width);
|
||||||
Properties::new()
|
|
||||||
.width(self.scrollbar_width)
|
scrollable::Direction::Both {
|
||||||
.margin(self.scrollbar_margin)
|
horizontal: properties,
|
||||||
.scroller_width(self.scroller_width),
|
vertical: properties,
|
||||||
))
|
}
|
||||||
|
})
|
||||||
.style(theme::Scrollable::Custom(Box::new(
|
.style(theme::Scrollable::Custom(Box::new(
|
||||||
ScrollbarCustomStyle,
|
ScrollbarCustomStyle,
|
||||||
)))
|
)))
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ where
|
||||||
id: Option<Id>,
|
id: Option<Id>,
|
||||||
width: Length,
|
width: Length,
|
||||||
height: Length,
|
height: Length,
|
||||||
scrollbar_properties: ScrollbarProperties,
|
direction: Direction,
|
||||||
content: Element<'a, Message, Renderer>,
|
content: Element<'a, Message, Renderer>,
|
||||||
on_scroll: Option<Box<dyn Fn(Viewport) -> Message + 'a>>,
|
on_scroll: Option<Box<dyn Fn(Viewport) -> Message + 'a>>,
|
||||||
style: <Renderer::Theme as StyleSheet>::Style,
|
style: <Renderer::Theme as StyleSheet>::Style,
|
||||||
|
|
@ -46,7 +46,7 @@ where
|
||||||
id: None,
|
id: None,
|
||||||
width: Length::Shrink,
|
width: Length::Shrink,
|
||||||
height: Length::Shrink,
|
height: Length::Shrink,
|
||||||
scrollbar_properties: Default::default(),
|
direction: Default::default(),
|
||||||
content: content.into(),
|
content: content.into(),
|
||||||
on_scroll: None,
|
on_scroll: None,
|
||||||
style: Default::default(),
|
style: Default::default(),
|
||||||
|
|
@ -71,12 +71,9 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configures the scrollbar(s) of the [`Scrollable`] .
|
/// Sets the [`Direction`] of the [`Scrollable`] .
|
||||||
pub fn scrollbar_properties(
|
pub fn direction(mut self, direction: Direction) -> Self {
|
||||||
mut self,
|
self.direction = direction;
|
||||||
scrollbar_properties: ScrollbarProperties,
|
|
||||||
) -> Self {
|
|
||||||
self.scrollbar_properties = scrollbar_properties;
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -98,45 +95,50 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Properties of the scrollbar(s) within a [`Scrollable`].
|
/// The direction of [`Scrollable`].
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum ScrollbarProperties {
|
pub enum Direction {
|
||||||
/// Vertical Scrollbar.
|
/// Vertical scrolling
|
||||||
Vertical(Properties),
|
Vertical(Properties),
|
||||||
/// Horizontal Scrollbar.
|
/// Horizontal scrolling
|
||||||
Horizontal(Properties),
|
Horizontal(Properties),
|
||||||
/// Both Vertical and Horizontal Scrollbars.
|
/// Both vertical and horizontal scrolling
|
||||||
Both(Properties, Properties),
|
Both {
|
||||||
|
/// The properties of the vertical scrollbar.
|
||||||
|
vertical: Properties,
|
||||||
|
/// The properties of the horizontal scrollbar.
|
||||||
|
horizontal: Properties,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ScrollbarProperties {
|
impl Direction {
|
||||||
/// Returns the horizontal [`Properties`] of the [`ScrollbarProperties`].
|
/// Returns the [`Properties`] of the horizontal scrollbar, if any.
|
||||||
pub fn horizontal(&self) -> Option<&Properties> {
|
pub fn horizontal(&self) -> Option<&Properties> {
|
||||||
match self {
|
match self {
|
||||||
Self::Horizontal(properties) => Some(properties),
|
Self::Horizontal(properties) => Some(properties),
|
||||||
Self::Both(_, properties) => Some(properties),
|
Self::Both { horizontal, .. } => Some(horizontal),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the vertical [`Properties`] of the [`ScrollbarProperties`].
|
/// Returns the [`Properties`] of the vertical scrollbar, if any.
|
||||||
pub fn vertical(&self) -> Option<&Properties> {
|
pub fn vertical(&self) -> Option<&Properties> {
|
||||||
match self {
|
match self {
|
||||||
Self::Vertical(properties) => Some(properties),
|
Self::Vertical(properties) => Some(properties),
|
||||||
Self::Both(properties, _) => Some(properties),
|
Self::Both { vertical, .. } => Some(vertical),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ScrollbarProperties {
|
impl Default for Direction {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::Vertical(Properties::default())
|
Self::Vertical(Properties::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Properties of a scrollbar within a [`Scrollable`].
|
/// Properties of a scrollbar within a [`Scrollable`].
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct Properties {
|
pub struct Properties {
|
||||||
width: f32,
|
width: f32,
|
||||||
margin: f32,
|
margin: f32,
|
||||||
|
|
@ -218,8 +220,7 @@ where
|
||||||
limits,
|
limits,
|
||||||
self.width,
|
self.width,
|
||||||
self.height,
|
self.height,
|
||||||
self.scrollbar_properties.horizontal().is_some(),
|
&self.direction,
|
||||||
self.scrollbar_properties.vertical().is_some(),
|
|
||||||
|renderer, limits| {
|
|renderer, limits| {
|
||||||
self.content.as_widget().layout(renderer, limits)
|
self.content.as_widget().layout(renderer, limits)
|
||||||
},
|
},
|
||||||
|
|
@ -267,7 +268,7 @@ where
|
||||||
cursor,
|
cursor,
|
||||||
clipboard,
|
clipboard,
|
||||||
shell,
|
shell,
|
||||||
&self.scrollbar_properties,
|
&self.direction,
|
||||||
&self.on_scroll,
|
&self.on_scroll,
|
||||||
|event, layout, cursor, clipboard, shell| {
|
|event, layout, cursor, clipboard, shell| {
|
||||||
self.content.as_widget_mut().on_event(
|
self.content.as_widget_mut().on_event(
|
||||||
|
|
@ -299,7 +300,7 @@ where
|
||||||
theme,
|
theme,
|
||||||
layout,
|
layout,
|
||||||
cursor,
|
cursor,
|
||||||
&self.scrollbar_properties,
|
&self.direction,
|
||||||
&self.style,
|
&self.style,
|
||||||
|renderer, layout, cursor, viewport| {
|
|renderer, layout, cursor, viewport| {
|
||||||
self.content.as_widget().draw(
|
self.content.as_widget().draw(
|
||||||
|
|
@ -327,7 +328,7 @@ where
|
||||||
tree.state.downcast_ref::<State>(),
|
tree.state.downcast_ref::<State>(),
|
||||||
layout,
|
layout,
|
||||||
cursor,
|
cursor,
|
||||||
&self.scrollbar_properties,
|
&self.direction,
|
||||||
|layout, cursor, viewport| {
|
|layout, cursor, viewport| {
|
||||||
self.content.as_widget().mouse_interaction(
|
self.content.as_widget().mouse_interaction(
|
||||||
&tree.children[0],
|
&tree.children[0],
|
||||||
|
|
@ -429,8 +430,7 @@ pub fn layout<Renderer>(
|
||||||
limits: &layout::Limits,
|
limits: &layout::Limits,
|
||||||
width: Length,
|
width: Length,
|
||||||
height: Length,
|
height: Length,
|
||||||
horizontal_enabled: bool,
|
direction: &Direction,
|
||||||
vertical_enabled: bool,
|
|
||||||
layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
|
layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
|
||||||
) -> layout::Node {
|
) -> layout::Node {
|
||||||
let limits = limits.width(width).height(height);
|
let limits = limits.width(width).height(height);
|
||||||
|
|
@ -438,12 +438,12 @@ pub fn layout<Renderer>(
|
||||||
let child_limits = layout::Limits::new(
|
let child_limits = layout::Limits::new(
|
||||||
Size::new(limits.min().width, limits.min().height),
|
Size::new(limits.min().width, limits.min().height),
|
||||||
Size::new(
|
Size::new(
|
||||||
if horizontal_enabled {
|
if direction.horizontal().is_some() {
|
||||||
f32::INFINITY
|
f32::INFINITY
|
||||||
} else {
|
} else {
|
||||||
limits.max().width
|
limits.max().width
|
||||||
},
|
},
|
||||||
if vertical_enabled {
|
if direction.vertical().is_some() {
|
||||||
f32::MAX
|
f32::MAX
|
||||||
} else {
|
} else {
|
||||||
limits.max().height
|
limits.max().height
|
||||||
|
|
@ -466,7 +466,7 @@ pub fn update<Message>(
|
||||||
cursor: mouse::Cursor,
|
cursor: mouse::Cursor,
|
||||||
clipboard: &mut dyn Clipboard,
|
clipboard: &mut dyn Clipboard,
|
||||||
shell: &mut Shell<'_, Message>,
|
shell: &mut Shell<'_, Message>,
|
||||||
scrollbar_properties: &ScrollbarProperties,
|
direction: &Direction,
|
||||||
on_scroll: &Option<Box<dyn Fn(Viewport) -> Message + '_>>,
|
on_scroll: &Option<Box<dyn Fn(Viewport) -> Message + '_>>,
|
||||||
update_content: impl FnOnce(
|
update_content: impl FnOnce(
|
||||||
Event,
|
Event,
|
||||||
|
|
@ -482,8 +482,7 @@ pub fn update<Message>(
|
||||||
let content = layout.children().next().unwrap();
|
let content = layout.children().next().unwrap();
|
||||||
let content_bounds = content.bounds();
|
let content_bounds = content.bounds();
|
||||||
|
|
||||||
let scrollbars =
|
let scrollbars = Scrollbars::new(state, direction, bounds, content_bounds);
|
||||||
Scrollbars::new(state, &scrollbar_properties, bounds, content_bounds);
|
|
||||||
|
|
||||||
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
|
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
|
||||||
scrollbars.is_mouse_over(cursor);
|
scrollbars.is_mouse_over(cursor);
|
||||||
|
|
@ -567,13 +566,8 @@ pub fn update<Message>(
|
||||||
|
|
||||||
let delta = Vector::new(
|
let delta = Vector::new(
|
||||||
delta.x
|
delta.x
|
||||||
* scrollbar_properties
|
* direction.horizontal().map_or(0.0, |_| 1.0),
|
||||||
.horizontal()
|
delta.y * direction.vertical().map_or(0.0, |_| 1.0),
|
||||||
.map_or(0.0, |_| 1.0),
|
|
||||||
delta.y
|
|
||||||
* scrollbar_properties
|
|
||||||
.vertical()
|
|
||||||
.map_or(0.0, |_| 1.0),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
state.scroll(delta, bounds, content_bounds);
|
state.scroll(delta, bounds, content_bounds);
|
||||||
|
|
@ -758,7 +752,7 @@ pub fn mouse_interaction(
|
||||||
state: &State,
|
state: &State,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
cursor: mouse::Cursor,
|
cursor: mouse::Cursor,
|
||||||
scrollbar_properties: &ScrollbarProperties,
|
direction: &Direction,
|
||||||
content_interaction: impl FnOnce(
|
content_interaction: impl FnOnce(
|
||||||
Layout<'_>,
|
Layout<'_>,
|
||||||
mouse::Cursor,
|
mouse::Cursor,
|
||||||
|
|
@ -771,8 +765,7 @@ pub fn mouse_interaction(
|
||||||
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 scrollbars =
|
let scrollbars = Scrollbars::new(state, direction, bounds, content_bounds);
|
||||||
Scrollbars::new(state, scrollbar_properties, bounds, content_bounds);
|
|
||||||
|
|
||||||
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
|
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
|
||||||
scrollbars.is_mouse_over(cursor);
|
scrollbars.is_mouse_over(cursor);
|
||||||
|
|
@ -812,7 +805,7 @@ pub fn draw<Renderer>(
|
||||||
theme: &Renderer::Theme,
|
theme: &Renderer::Theme,
|
||||||
layout: Layout<'_>,
|
layout: Layout<'_>,
|
||||||
cursor: mouse::Cursor,
|
cursor: mouse::Cursor,
|
||||||
scrollbar_properties: &ScrollbarProperties,
|
direction: &Direction,
|
||||||
style: &<Renderer::Theme as StyleSheet>::Style,
|
style: &<Renderer::Theme as StyleSheet>::Style,
|
||||||
draw_content: impl FnOnce(&mut Renderer, Layout<'_>, mouse::Cursor, &Rectangle),
|
draw_content: impl FnOnce(&mut Renderer, Layout<'_>, mouse::Cursor, &Rectangle),
|
||||||
) where
|
) where
|
||||||
|
|
@ -823,8 +816,7 @@ pub fn draw<Renderer>(
|
||||||
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 scrollbars =
|
let scrollbars = Scrollbars::new(state, direction, bounds, content_bounds);
|
||||||
Scrollbars::new(state, &scrollbar_properties, bounds, content_bounds);
|
|
||||||
|
|
||||||
let cursor_over_scrollable = cursor.position_over(bounds);
|
let cursor_over_scrollable = cursor.position_over(bounds);
|
||||||
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
|
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
|
||||||
|
|
@ -1200,22 +1192,21 @@ impl Scrollbars {
|
||||||
/// Create y and/or x scrollbar(s) if content is overflowing the [`Scrollable`] bounds.
|
/// Create y and/or x scrollbar(s) if content is overflowing the [`Scrollable`] bounds.
|
||||||
fn new(
|
fn new(
|
||||||
state: &State,
|
state: &State,
|
||||||
scrollbar_properties: &ScrollbarProperties,
|
direction: &Direction,
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
content_bounds: Rectangle,
|
content_bounds: Rectangle,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let offset = state.offset(bounds, content_bounds);
|
let offset = state.offset(bounds, content_bounds);
|
||||||
|
|
||||||
let show_scrollbar_x =
|
let show_scrollbar_x = direction.horizontal().and_then(|h| {
|
||||||
scrollbar_properties.horizontal().and_then(|h| {
|
if content_bounds.width > bounds.width {
|
||||||
if content_bounds.width > bounds.width {
|
Some(h)
|
||||||
Some(h)
|
} else {
|
||||||
} else {
|
None
|
||||||
None
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
let show_scrollbar_y = scrollbar_properties.vertical().and_then(|v| {
|
let show_scrollbar_y = direction.vertical().and_then(|v| {
|
||||||
if content_bounds.height > bounds.height {
|
if content_bounds.height > bounds.height {
|
||||||
Some(v)
|
Some(v)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue