Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedded Scrollbar support for Scrollable #2269

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions core/src/padding.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Size;
use crate::{Pixels, Size};

/// An amount of space to pad for each side of a box
///
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Padding {
left: 0.0,
};

/// Create a Padding that is equal on all sides
/// Create a [`Padding`] that is equal on all sides.
pub const fn new(padding: f32) -> Padding {
Padding {
top: padding,
Expand All @@ -64,6 +64,38 @@ impl Padding {
}
}

/// Create some top [`Padding`].
pub fn top(padding: impl Into<Pixels>) -> Self {
Self {
top: padding.into().0,
..Self::ZERO
}
}

/// Create some right [`Padding`].
pub fn right(padding: impl Into<Pixels>) -> Self {
Self {
right: padding.into().0,
..Self::ZERO
}
}

/// Create some bottom [`Padding`].
pub fn bottom(padding: impl Into<Pixels>) -> Self {
Self {
bottom: padding.into().0,
..Self::ZERO
}
}

/// Create some left [`Padding`].
pub fn left(padding: impl Into<Pixels>) -> Self {
Self {
left: padding.into().0,
..Self::ZERO
}
}

/// Returns the total amount of vertical [`Padding`].
pub fn vertical(self) -> f32 {
self.top + self.bottom
Expand Down
61 changes: 30 additions & 31 deletions examples/scrollable/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use iced::widget::scrollable::Properties;
use iced::widget::{
button, column, container, horizontal_space, progress_bar, radio, row,
scrollable, slider, text, vertical_space, Scrollable,
scrollable, slider, text, vertical_space,
};
use iced::{Alignment, Border, Color, Element, Length, Task, Theme};

Expand Down Expand Up @@ -203,7 +202,7 @@ impl ScrollableDemo {

let scrollable_content: Element<Message> =
Element::from(match self.scrollable_direction {
Direction::Vertical => Scrollable::with_direction(
Direction::Vertical => scrollable(
column![
scroll_to_end_button(),
text("Beginning!"),
Expand All @@ -216,19 +215,19 @@ impl ScrollableDemo {
.align_items(Alignment::Center)
.padding([40, 0, 40, 0])
.spacing(40),
scrollable::Direction::Vertical(
Properties::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment),
),
)
.direction(scrollable::Direction::Vertical(
scrollable::Scrollbar::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment),
))
.width(Length::Fill)
.height(Length::Fill)
.id(SCROLLABLE_ID.clone())
.on_scroll(Message::Scrolled),
Direction::Horizontal => Scrollable::with_direction(
Direction::Horizontal => scrollable(
row![
scroll_to_end_button(),
text("Beginning!"),
Expand All @@ -242,19 +241,19 @@ impl ScrollableDemo {
.align_items(Alignment::Center)
.padding([0, 40, 0, 40])
.spacing(40),
scrollable::Direction::Horizontal(
Properties::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment),
),
)
.direction(scrollable::Direction::Horizontal(
scrollable::Scrollbar::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment),
))
.width(Length::Fill)
.height(Length::Fill)
.id(SCROLLABLE_ID.clone())
.on_scroll(Message::Scrolled),
Direction::Multi => Scrollable::with_direction(
Direction::Multi => scrollable(
//horizontal content
row![
column![
Expand Down Expand Up @@ -284,19 +283,19 @@ impl ScrollableDemo {
.align_items(Alignment::Center)
.padding([0, 40, 0, 40])
.spacing(40),
{
let properties = Properties::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment);

scrollable::Direction::Both {
horizontal: properties,
vertical: properties,
}
},
)
.direction({
let scrollbar = scrollable::Scrollbar::new()
.width(self.scrollbar_width)
.margin(self.scrollbar_margin)
.scroller_width(self.scroller_width)
.alignment(self.alignment);

scrollable::Direction::Both {
horizontal: scrollbar,
vertical: scrollbar,
}
})
.width(Length::Fill)
.height(Length::Fill)
.id(SCROLLABLE_ID.clone())
Expand Down
27 changes: 12 additions & 15 deletions widget/src/overlay/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,18 @@ where
class,
} = menu;

let list = Scrollable::with_direction(
List {
options,
hovered_option,
on_selected,
on_option_hovered,
font,
text_size,
text_line_height,
text_shaping,
padding,
class,
},
scrollable::Direction::default(),
);
let list = Scrollable::new(List {
options,
hovered_option,
on_selected,
on_option_hovered,
font,
text_size,
text_line_height,
text_shaping,
padding,
class,
});

state.tree.diff(&list as &dyn Widget<_, _, _>);

Expand Down
Loading
Loading