feat: improve crop mathematics and expand touch padding#12
Open
rubenalfon wants to merge 3 commits into
Open
Conversation
This commit overhauls the gesture logic to allow precise resizing. It also improves UX by expanding touch areas. Specific changes: - Implemented `getNewRectMeasuresLocked` for aspect-ratio locked resizing. - Added `calculateNewCropRect` as a unified entry point. - Refactored `CropStateManager` to track `dragOffset`. - Updated canvas to include `contentPadding` for better edge interaction. - Increased default `touchPadding` to 20.dp. This implementation effectively covers the goals of PR Tanish-Ranjan#9. Closes Tanish-Ranjan#9
Specifically: - Updated `getNewRectMeasuresLocked` to pre-calculate `minWidth` and `minHeight` based on the aspect ratio and `minCropSize`. - Added logic to constrain the `handleOffset` before calculating distances, preventing the crop rectangle from shrinking below minimum dimensions. - Refactored `directionX` and `directionY` calculation to rely on the `activeHandle` type rather than coordinate comparisons for more reliable growth direction tracking. - Refined horizontal and vertical bounds checking to ensure the aspect ratio is maintained when scaling down to fit within the image boundaries.
This commit moves the `dragOffset` state from the `CropState` data class to a local variable within `CropStateManager`. This change centralizes drag-related logic within the `CropStateManager` and simplifies the `CropState` data class, which no longer needs to track this transient UI state. The `dragOffset` is now managed directly where drag gestures are handled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR significantly improves the UX of the cropping tool by overhauling the gesture calculation logic. Previously, resizing with a locked aspect ratio could feel "jumpy" or unresponsive. I have implemented a vector projection approach that ensures the crop rectangle follows the user's finger naturally while respecting the aspect ratio constraints.
Additionally, this PR addresses touch handling issues at the edges of the image (related to PR #9) by adding content padding to the canvas, ensuring handles are always clickable.
Key Changes:
getNewRectMeasuresLockedusing vector projection for precise, locked resizing.calculateNewCropRectas a unified entry point to cleanly switch between free-form and locked logic.CropStateManagerto trackdragOffsetfor smoother updates.touchPaddingto20.dpand updated canvas initialization to include content padding.Fixes #9
Type of change
expected)
How Has This Been Tested?
I have performed manual testing on an Android phone to verify the gesture behavior.
Checklist: