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

Feature/contrast adjustment and image zoom issue #8800 #8881

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

shinkar94
Copy link

@shinkar94 shinkar94 commented Dec 27, 2024

Motivation and context

How has this been tested?

Checklist

  • I submit my changes into the develop branch
  • I have created a changelog fragment
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • I have linked related issues (see GitHub docs)
  • I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • New Features

    • Added fullscreen functionality for the ContextImage component.
    • Introduced dynamic brightness and contrast adjustments based on mouse movements in the CanvasWrapper.
    • Added a new custom hook, useCanvasControl, for managing canvas interactions.
  • Bug Fixes

    • Improved cleanup of event listeners in the CanvasWrapper component.
  • Style

    • Enhanced styles for the ContextImage component, including new styles for buttons and layout adjustments.

@shinkar94 shinkar94 requested a review from bsekachev as a code owner December 27, 2024 06:31
Copy link
Contributor

coderabbitai bot commented Dec 27, 2024

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The pull request introduces enhanced canvas interaction capabilities in the CVAT UI, focusing on context image rendering and user interaction. The changes primarily involve adding a new useCanvasControl hook that manages canvas zoom, dragging, and color adjustments. The modifications span multiple components, including ContextImage, CanvasLayout, and CanvasWrapper, to support fullscreen functionality, mouse-based brightness and contrast control, and improved canvas interactivity.

Changes

File Change Summary
cvat-ui/src/components/annotation-page/canvas/grid-layout/canvas-layout.tsx Updated ViewFabric function signature to accept optional fullscreenKey
cvat-ui/src/components/annotation-page/canvas/views/canvas2d/canvas-wrapper.tsx Added onCanvasMouseMove method to handle mouse movement and adjust brightness/contrast
cvat-ui/src/components/annotation-page/canvas/views/context-image/context-image.tsx Added fullscreenKey prop, integrated useCanvasControl hook, enhanced user interaction
cvat-ui/src/components/annotation-page/canvas/views/context-image/model/hooks/index.ts Exported useCanvasControl hook
cvat-ui/src/components/annotation-page/canvas/views/context-image/model/hooks/useCanvasControl.tsx New hook for managing canvas zoom, dragging, and color adjustments
cvat-ui/src/components/annotation-page/canvas/views/context-image/model/index.ts Exported useCanvasControl hook
cvat-ui/src/components/annotation-page/canvas/views/context-image/styles.scss Added new styles for reset button and draggable wrapper

Sequence Diagram

sequenceDiagram
    participant User
    participant ContextImage
    participant CanvasControl as useCanvasControl
    
    User->>ContextImage: Interact with Canvas
    alt Zoom
        User->>ContextImage: Scroll Mouse Wheel
        ContextImage->>CanvasControl: handleZoomChange()
        CanvasControl-->>ContextImage: Update Zoom Level
    end
    
    alt Drag
        User->>ContextImage: Press Left Mouse Button
        ContextImage->>CanvasControl: handleMouseDown()
        User->>ContextImage: Move Mouse
        ContextImage->>CanvasControl: handleMouseMove()
        User->>ContextImage: Release Mouse Button
        ContextImage->>CanvasControl: handleMouseUp()
    end
    
    alt Color Adjustment
        User->>ContextImage: Press Right Mouse Button
        ContextImage->>CanvasControl: Adjust Brightness/Contrast
    end
Loading

Poem

🐰 A Canvas of Delight

Zoom in, drag around with might,
Brightness dancing left and right,
Pixels dance to user's command,
A rabbit's hook, a magic hand!
Canvas control, oh what a sight! 🎨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@shinkar94
Copy link
Author

Added gamma brightness handling when right mouse button is held down.
Added moving and scaling the image for the rest of the image blocks.
Added refresh button for color adjustment.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (11)
cvat-ui/src/components/annotation-page/canvas/views/context-image/model/hooks/useCanvasControl.tsx (4)

28-33: Consider resetting zoom and offset on fullscreen exit
Currently, positionRef.current is reset and the transform style is applied to the canvas whenever fullscreenKey changes. However, if the user exits fullscreen after panning or zooming, the canvas remains in the transformed state. You might consider deciding whether to also reset the zoom/offset state on fullscreen exit to ensure the user sees a clean default view.


40-47: Avoid rapid zoom changes on trackpads
Using event.deltaY * 0.001 can lead to rapid zoom changes on sensitive trackpads. Consider adding a configurable "zoom step" or applying an exponential factor for smoother zoom in/out experiences on different hardware.


67-79: Right-click brightness/contrast updates
Handling brightness/contrast changes on mouse movement with right-click is a nice UI trick. However, it might be more discoverable to provide some tooltip or visual indicator. Also, consider whether a large movement could inadvertently push the values to their extreme (50 or 200) too easily.


120-125: Consider enabling handleMouseLeave
Commented-out code in handleMouseLeave suggests potential intention to reset drag state. If the user drags outside the canvas, it might be beneficial to stop the dragging to maintain consistent UX.

cvat-ui/src/components/annotation-page/canvas/views/context-image/context-image.tsx (3)

12-12: Icon import usage
You’re importing more icons (SettingOutlined) than are used for a single minimal purpose. This is absolutely fine for clarity, but ensure they are all necessary to avoid bundling overhead if your build environment is sensitive to that.


100-104: Consider using onMouseLeave on the canvas
Attaching onMouseLeave to the wrapper could inadvertently miss cases where the mouse leaves the <canvas> but not the wrapper. If it is the canvas area that matters, adding this to the <canvas> itself might be more precise.


114-117: Reset color button
A ReloadOutlined icon is used to reset color settings. The meaning might not be obvious to all users. A brief tooltip or “Reset color settings” label could improve discoverability.

cvat-ui/src/components/annotation-page/canvas/grid-layout/canvas-layout.tsx (1)

204-204: DRY principle
The repeated pattern of ViewFabric(value, fullscreenKey) for each layout item is straightforward, but if further expansions appear, consider a more scalable approach such as mapping additional props in a single step inside ViewFabric.

cvat-ui/src/components/annotation-page/canvas/views/canvas2d/canvas-wrapper.tsx (2)

749-766: Potential user confusion on brightness/contrast
The code updates brightness and contrast on a right-click drag in onCanvasMouseMove. If not documented or indicated, some users may find it confusing. Consider providing a small hint or an in-app message the first time the user tries right-click dragging.


1078-1078: Event listener subscription
All event listeners are added at once in initialSetup(). If your component grows in complexity, you might consider modularizing these listeners or grouping by feature to keep it cleaner.

cvat-ui/src/components/annotation-page/canvas/views/context-image/styles.scss (1)

66-75: Consider standardizing button opacity values

The reset button's opacity behavior (0.6 to 1.0) differs slightly from the setup button's hover behavior (0.6 to 0.9). Consider standardizing these values for consistency.

 &:hover {
-    opacity: 1;
+    opacity: 0.9;
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 185a00b and 245e8fb.

📒 Files selected for processing (7)
  • cvat-ui/src/components/annotation-page/canvas/grid-layout/canvas-layout.tsx (3 hunks)
  • cvat-ui/src/components/annotation-page/canvas/views/canvas2d/canvas-wrapper.tsx (3 hunks)
  • cvat-ui/src/components/annotation-page/canvas/views/context-image/context-image.tsx (5 hunks)
  • cvat-ui/src/components/annotation-page/canvas/views/context-image/model/hooks/index.ts (1 hunks)
  • cvat-ui/src/components/annotation-page/canvas/views/context-image/model/hooks/useCanvasControl.tsx (1 hunks)
  • cvat-ui/src/components/annotation-page/canvas/views/context-image/model/index.ts (1 hunks)
  • cvat-ui/src/components/annotation-page/canvas/views/context-image/styles.scss (2 hunks)
✅ Files skipped from review due to trivial changes (2)
  • cvat-ui/src/components/annotation-page/canvas/views/context-image/model/hooks/index.ts
  • cvat-ui/src/components/annotation-page/canvas/views/context-image/model/index.ts
🔇 Additional comments (7)
cvat-ui/src/components/annotation-page/canvas/views/context-image/context-image.tsx (3)

21-21: Fullscreen key optionality
fullscreenKey?: string; is helpful. Ensure that parent components properly check for existence before passing an undefined string to child components, preventing potential type confusion if deciding to do more advanced logic with fullscreenKey.


43-53: Correctness check
The destructured return from useCanvasControl includes items like canvasStyle and event handlers. Ensure that the wrapper <div className='cvat-context-image-wrapper'> is sized appropriately so that your canvas transformations (like zoom or drag) do not visually clip.


128-139: Conditionally render the draggable wrapper
The draggable-wrapper div is conditionally rendered if contextImageOffset < Object.keys(contextImageData).length. That’s good for preventing empty canvas usage. Make sure this condition correctly handles all edge cases where offset might be invalid or out-of-bounds (e.g., negative offset).

cvat-ui/src/components/annotation-page/canvas/grid-layout/canvas-layout.tsx (1)

Line range hint 40-52: Ensure type-safety and consistent usage
fullscreenKey?: string is passed to ViewFabric. Confirm that any usage respects the possibility of undefined so that no runtime errors occur if an undefined key is passed.

cvat-ui/src/components/annotation-page/canvas/views/canvas2d/canvas-wrapper.tsx (1)

594-594: Event listener removal
The removal of the mousemove listener here is crucial for preventing memory leaks. This looks correct, but be sure future additions of event listeners follow the same removal pattern in componentWillUnmount.

cvat-ui/src/components/annotation-page/canvas/views/context-image/styles.scss (2)

12-13: LGTM: Wrapper styling improvements

The addition of overflow: hidden properly contains the content, and the border-radius styling creates a cohesive design with rounded top corners.


78-91: LGTM: Draggable wrapper implementation

The flex layout with centered alignment and absolute positioning of the canvas provides a robust foundation for the zoom and drag functionality. The overflow handling prevents content spillage during interactions.

Let's verify that these styles work correctly with the associated React component:

✅ Verification successful

Let me try to find the associated component with these alternative search strategies.


LGTM: Draggable wrapper styles are correctly integrated

The styles are properly integrated with the React component in context-image.tsx. The component uses a canvas element within the draggable wrapper div, which aligns perfectly with the CSS implementation. The flex layout, absolute positioning, and overflow handling will work as intended for the canvas element referenced by canvasRef.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Find the corresponding React component to ensure proper integration
ast-grep --pattern 'class $_ extends $_ {
  $$$
  render() {
    $$$
    draggable-wrapper
    $$$
  }
}'

Length of output: 107


Script:

#!/bin/bash
# Let's try a different approach to find the React component using ripgrep
rg -l "draggable-wrapper" --type ts --type tsx --type jsx

Length of output: 85


Script:

#!/bin/bash
# Also search in the specific directory where the styles are located
rg -l "draggable-wrapper" "cvat-ui/src/components/annotation-page/canvas/views/context-image/"

Length of output: 254


Script:

#!/bin/bash
# Search for any React component file in the context-image directory
fd -e tsx -e jsx "cvat-ui/src/components/annotation-page/canvas/views/context-image/"

Length of output: 655


Script:

#!/bin/bash
# Let's examine the context-image.tsx file to verify the implementation
rg "draggable-wrapper" -B 2 -A 2 "cvat-ui/src/components/annotation-page/canvas/views/context-image/context-image.tsx"

Length of output: 341

…age/model/hooks/useCanvasControl.tsx

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@shinkar94
Copy link
Author

Added zoom limitation when zooming out.
Changed zoom direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant