Skip to content

Commit a9987cb

Browse files
committed
Introduce absolute_offset_reversed to scrollable::Viewport
1 parent 11287c8 commit a9987cb

1 file changed

Lines changed: 17 additions & 58 deletions

File tree

widget/src/scrollable.rs

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

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

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

566559
return event::Status::Captured;
567560
}
@@ -599,7 +592,6 @@ pub fn update<Message>(
599592
on_scroll,
600593
bounds,
601594
content_bounds,
602-
direction,
603595
shell,
604596
);
605597
}
@@ -645,7 +637,6 @@ pub fn update<Message>(
645637
on_scroll,
646638
bounds,
647639
content_bounds,
648-
direction,
649640
shell,
650641
);
651642

@@ -681,7 +672,6 @@ pub fn update<Message>(
681672
on_scroll,
682673
bounds,
683674
content_bounds,
684-
direction,
685675
shell,
686676
);
687677
}
@@ -722,7 +712,6 @@ pub fn update<Message>(
722712
on_scroll,
723713
bounds,
724714
content_bounds,
725-
direction,
726715
shell,
727716
);
728717
}
@@ -758,7 +747,6 @@ pub fn update<Message>(
758747
on_scroll,
759748
bounds,
760749
content_bounds,
761-
direction,
762750
shell,
763751
);
764752

@@ -974,7 +962,6 @@ fn notify_on_scroll<Message>(
974962
on_scroll: &Option<Box<dyn Fn(Viewport) -> Message + '_>>,
975963
bounds: Rectangle,
976964
content_bounds: Rectangle,
977-
direction: Direction,
978965
shell: &mut Shell<'_, Message>,
979966
) {
980967
if let Some(on_scroll) = on_scroll {
@@ -984,23 +971,11 @@ fn notify_on_scroll<Message>(
984971
return;
985972
}
986973

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-
997974
let viewport = Viewport {
998975
offset_x: state.offset_x,
999976
offset_y: state.offset_y,
1000977
bounds,
1001978
content_bounds,
1002-
vertical_alignment,
1003-
horizontal_alignment,
1004979
};
1005980

1006981
// Don't publish redundant viewports to shell
@@ -1105,8 +1080,6 @@ pub struct Viewport {
11051080
offset_y: Offset,
11061081
bounds: Rectangle,
11071082
content_bounds: Rectangle,
1108-
vertical_alignment: Alignment,
1109-
horizontal_alignment: Alignment,
11101083
}
11111084

11121085
impl Viewport {
@@ -1122,6 +1095,22 @@ impl Viewport {
11221095
AbsoluteOffset { x, y }
11231096
}
11241097

1098+
/// Returns the [`AbsoluteOffset`] of the current [`Viewport`], but with its
1099+
/// alignment reversed.
1100+
///
1101+
/// This method can be useful to switch the alignment of a [`Scrollable`]
1102+
/// while maintaining its scrolling position.
1103+
pub fn absolute_offset_reversed(&self) -> AbsoluteOffset {
1104+
let AbsoluteOffset { x, y } = self.absolute_offset();
1105+
1106+
AbsoluteOffset {
1107+
x: ((self.content_bounds.width - self.bounds.width).max(0.0) - x)
1108+
.max(0.0),
1109+
y: ((self.content_bounds.height - self.bounds.height).max(0.0) - y)
1110+
.max(0.0),
1111+
}
1112+
}
1113+
11251114
/// Returns the [`RelativeOffset`] of the current [`Viewport`].
11261115
pub fn relative_offset(&self) -> RelativeOffset {
11271116
let AbsoluteOffset { x, y } = self.absolute_offset();
@@ -1131,36 +1120,6 @@ impl Viewport {
11311120

11321121
RelativeOffset { x, y }
11331122
}
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-
}
11641123
}
11651124

11661125
impl State {

0 commit comments

Comments
 (0)