Skip to content

High performance self-hosted photo and video management solution.

fix(mobile): aspect ratio change (#30939)

Damiano Ferrari290861e347cc
diff --git a/mobile/lib/presentation/pages/edit/editor.provider.dart b/mobile/lib/presentation/pages/edit/editor.provider.dart
index 39cf4cc3ff..8f3c22f3df 100644
--- a/mobile/lib/presentation/pages/edit/editor.provider.dart
+++ b/mobile/lib/presentation/pages/edit/editor.provider.dart
@@ -65,8 +65,33 @@ class EditorProvider extends Notifier<EditorState> {
state = state.copyWith(crop: crop, hasUnsavedEdits: true);
}
+ /// Returns the required shift that has to be done to fit a segment in a given
+ /// constraint
+ double _shiftIntoBounds(double start, double end, {double min = 0.0, double max = 1.0}) {
+ if (start < min) {
+ return min - start;
+ }
+ if (end > max) {
+ return max - end;
+ }
+ return 0.0;
+ }
+
void setAspectRatio(CropAspectRatio preset) {
- state = state.copyWith(aspectRatio: preset, hasUnsavedEdits: true);
+ // Expand the initial selection into a square. We have to keep the longest
+ // side the same as before when the user changes the aspect ratio. Otherwise
+ // the selection might shrink
+ Rect startingSquare = Rect.fromCenter(
+ center: state.crop.center,
+ width: state.crop.longestSide,
+ height: state.crop.longestSide,
+ );
+ // If one side of the square is outside of the image border after the
+ // expansion, move it towards the center of the required amount
+ final dx = _shiftIntoBounds(startingSquare.left, startingSquare.right);
+ final dy = _shiftIntoBounds(startingSquare.top, startingSquare.bottom);
+ startingSquare = startingSquare.shift(Offset(dx, dy));
+ state = state.copyWith(aspectRatio: preset, hasUnsavedEdits: true, crop: startingSquare);
}
void resetEdits() {