Skip to content

Commit 11287c8

Browse files
committed
Expose methods to change viewport alignment
1 parent 21bd514 commit 11287c8

1 file changed

Lines changed: 58 additions & 1 deletion

File tree

widget/src/scrollable.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,14 @@ pub fn update<Message>(
554554

555555
state.scroll(delta, direction, bounds, content_bounds);
556556

557-
notify_on_scroll(state, on_scroll, bounds, content_bounds, shell);
557+
notify_on_scroll(
558+
state,
559+
on_scroll,
560+
bounds,
561+
content_bounds,
562+
direction,
563+
shell,
564+
);
558565

559566
return event::Status::Captured;
560567
}
@@ -592,6 +599,7 @@ pub fn update<Message>(
592599
on_scroll,
593600
bounds,
594601
content_bounds,
602+
direction,
595603
shell,
596604
);
597605
}
@@ -637,6 +645,7 @@ pub fn update<Message>(
637645
on_scroll,
638646
bounds,
639647
content_bounds,
648+
direction,
640649
shell,
641650
);
642651

@@ -672,6 +681,7 @@ pub fn update<Message>(
672681
on_scroll,
673682
bounds,
674683
content_bounds,
684+
direction,
675685
shell,
676686
);
677687
}
@@ -712,6 +722,7 @@ pub fn update<Message>(
712722
on_scroll,
713723
bounds,
714724
content_bounds,
725+
direction,
715726
shell,
716727
);
717728
}
@@ -747,6 +758,7 @@ pub fn update<Message>(
747758
on_scroll,
748759
bounds,
749760
content_bounds,
761+
direction,
750762
shell,
751763
);
752764

@@ -962,6 +974,7 @@ fn notify_on_scroll<Message>(
962974
on_scroll: &Option<Box<dyn Fn(Viewport) -> Message + '_>>,
963975
bounds: Rectangle,
964976
content_bounds: Rectangle,
977+
direction: Direction,
965978
shell: &mut Shell<'_, Message>,
966979
) {
967980
if let Some(on_scroll) = on_scroll {
@@ -971,11 +984,23 @@ fn notify_on_scroll<Message>(
971984
return;
972985
}
973986

987+
let horizontal_alignment = direction
988+
.horizontal()
989+
.map(|p| p.alignment)
990+
.unwrap_or_default();
991+
992+
let vertical_alignment = direction
993+
.vertical()
994+
.map(|p| p.alignment)
995+
.unwrap_or_default();
996+
974997
let viewport = Viewport {
975998
offset_x: state.offset_x,
976999
offset_y: state.offset_y,
9771000
bounds,
9781001
content_bounds,
1002+
vertical_alignment,
1003+
horizontal_alignment,
9791004
};
9801005

9811006
// Don't publish redundant viewports to shell
@@ -1080,6 +1105,8 @@ pub struct Viewport {
10801105
offset_y: Offset,
10811106
bounds: Rectangle,
10821107
content_bounds: Rectangle,
1108+
vertical_alignment: Alignment,
1109+
horizontal_alignment: Alignment,
10831110
}
10841111

10851112
impl Viewport {
@@ -1104,6 +1131,36 @@ impl Viewport {
11041131

11051132
RelativeOffset { x, y }
11061133
}
1134+
1135+
/// Returns a new [`Viewport`] with the supplied vertical [`Alignment`].
1136+
pub fn with_vertical_alignment(self, alignment: Alignment) -> Self {
1137+
if self.vertical_alignment != alignment {
1138+
let relative = 1.0 - self.relative_offset().y;
1139+
1140+
Self {
1141+
offset_y: Offset::Relative(relative),
1142+
vertical_alignment: alignment,
1143+
..self
1144+
}
1145+
} else {
1146+
self
1147+
}
1148+
}
1149+
1150+
/// Returns a new [`Viewport`] with the supplied horizontal [`Alignment`].
1151+
pub fn with_horizontal_alignment(self, alignment: Alignment) -> Self {
1152+
if self.horizontal_alignment != alignment {
1153+
let relative = 1.0 - self.relative_offset().x;
1154+
1155+
Self {
1156+
offset_x: Offset::Relative(relative),
1157+
horizontal_alignment: alignment,
1158+
..self
1159+
}
1160+
} else {
1161+
self
1162+
}
1163+
}
11071164
}
11081165

11091166
impl State {

0 commit comments

Comments
 (0)