Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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

Fix: Keybinds in UI allow drawing disabled shape types #8685

Merged
merged 2 commits into from
Nov 13, 2024

Conversation

klakhov
Copy link
Contributor

@klakhov klakhov commented Nov 12, 2024

Motivation and context

Resolved: #8673

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

  • Bug Fixes

    • Resolved an issue preventing users from drawing only enabled shape types in the user interface.
  • New Features

    • Introduced a new function to determine shape types based on labels, enhancing the annotation process.
    • Updated the handling of active shape types to allow for a nullable state, improving flexibility.
  • Documentation

    • Added descriptive messages for the annotation process to clarify the shape being annotated.

@klakhov klakhov requested a review from bsekachev as a code owner November 12, 2024 10:24
Copy link
Contributor

coderabbitai bot commented Nov 12, 2024

Walkthrough

This pull request implements changes to address an issue in the user interface where users could draw disabled shape types using keybinds. The modifications include updates to the rememberObject function to allow for a nullable shape type, the introduction of a new function to determine shape types based on labels, and adjustments to the state management for active shape types. These changes enhance the integrity and clarity of the drawing functionality within the application.

Changes

File Path Change Summary
changelog.d/20241112_132508_klakhov_fix_remember_object.md Documented the fix for the issue allowing drawing of disabled shape types.
cvat-ui/src/actions/annotation-actions.ts Updated rememberObject method signature to allow activeShapeType to be `ShapeType
cvat-ui/src/components/annotation-page/single-shape-workspace/single-shape-sidebar/single-shape-sidebar.tsx Added import for labelShapeType and modified makeMessage function for better clarity in annotation messages.
cvat-ui/src/reducers/annotation-reducer.ts Introduced labelShapeType function to determine shape types based on labels, replacing inline logic. Initialized activeShapeType to null.
cvat-ui/src/reducers/index.ts Updated activeShapeType in AnnotationState from ShapeType to `ShapeType

Assessment against linked issues

Objective Addressed Explanation
Keybinds in UI allow drawing disabled shape types (#8673)

🐰 "In the garden where shapes play,
Keybinds now know the right way.
Disabled forms can't sneak and sway,
Drawing's clear, come what may!
With a hop and a skip, we cheer,
For a canvas bright and clear!" 🐇✨


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 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.

@klakhov klakhov removed the request for review from bsekachev November 12, 2024 10:24
@klakhov klakhov added the ui/ux label Nov 12, 2024
@klakhov klakhov requested a review from nmanovic as a code owner November 12, 2024 10:26
Copy link

sonarcloud bot commented Nov 12, 2024

@klakhov klakhov requested review from bsekachev and removed request for nmanovic November 12, 2024 10:31
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: 0

🧹 Outside diff range and nitpick comments (3)
changelog.d/20241112_132508_klakhov_fix_remember_object.md (1)

1-4: Consider enhancing the changelog entry with more specific details.

While the current entry correctly describes the fix, consider adding more context to help users better understand the scope of the change:

 ### Fixed

-Keybinds in UI allow drawing disabled shape types
+Keybinds in UI no longer allow drawing disabled shape types in Standard mode
+when certain shape types are not available for the selected label
  (<https://github.com/cvat-ai/cvat/pull/8685>)
cvat-ui/src/components/annotation-page/single-shape-workspace/single-shape-sidebar/single-shape-sidebar.tsx (1)

Line range hint 12-24: Consider adding error boundary protection

While the component's state management is robust, consider wrapping it with an error boundary to gracefully handle potential runtime errors, especially during canvas operations.

Example implementation:

+import ErrorBoundary from 'components/error-boundary/error-boundary';

 function SingleShapeSidebar(): JSX.Element {
+  return (
+    <ErrorBoundary>
       {/* existing component content */}
+    </ErrorBoundary>
+  );
 }
cvat-ui/src/reducers/annotation-reducer.ts (1)

34-42: LGTM! Consider documenting the default behavior.

The function implementation is clean and handles all cases correctly. The null return for TAG type will help prevent drawing disabled shape types.

Consider adding a JSDoc comment to document the default behavior:

+/**
+ * Determines the shape type for a given label
+ * @param label - The label to check
+ * @returns The shape type if available, null for TAG type, or RECTANGLE as default
+ */
 export function labelShapeType(label?: Label): ShapeType | null {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1e7ff33 and c87ca6b.

📒 Files selected for processing (5)
  • changelog.d/20241112_132508_klakhov_fix_remember_object.md (1 hunks)
  • cvat-ui/src/actions/annotation-actions.ts (1 hunks)
  • cvat-ui/src/components/annotation-page/single-shape-workspace/single-shape-sidebar/single-shape-sidebar.tsx (2 hunks)
  • cvat-ui/src/reducers/annotation-reducer.ts (3 hunks)
  • cvat-ui/src/reducers/index.ts (1 hunks)
🔇 Additional comments (7)
cvat-ui/src/reducers/index.ts (1)

772-772: LGTM! The nullable shape type enables proper handling of disabled states.

Making activeShapeType nullable is the right approach as it allows the UI to explicitly represent when no shape type should be active, which is necessary for handling disabled shape types.

Let's verify that components properly handle the null case:

✅ Verification successful

Based on the search results, I can now generate the final response as I have sufficient information about how activeShapeType is handled across the codebase.

The null case for activeShapeType is properly handled throughout the codebase

The codebase already has robust handling of the null case for activeShapeType:

  • In annotation-reducer.ts, it correctly initializes activeShapeType as null when dealing with tag labels
  • The drawing control mapping in annotation-reducer.ts only processes defined shape types, safely handling the null case
  • The rememberObject action in labels-list.tsx has proper type narrowing logic that considers shape type constraints
  • The setup tag popover explicitly sets activeShapeType to undefined, showing awareness of non-shape states
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for components that use activeShapeType to ensure they handle null
rg -l "activeShapeType" --type ts --type tsx | xargs rg -A 5 "activeShapeType"

Length of output: 11958

cvat-ui/src/components/annotation-page/single-shape-workspace/single-shape-sidebar/single-shape-sidebar.tsx (3)

24-24: LGTM: Import addition aligns with PR objective

The addition of labelShapeType import is appropriate for determining valid shape types from labels, which helps prevent drawing of disabled shapes.


Line range hint 431-435: LGTM: Shortcut handlers are safe with shape type validation

The SWITCH_DRAW_MODE_SINGLE_SHAPE handler safely disables drawing mode. Since shape type validation occurs during canvas initialization, it's not possible to bypass the validation through shortcuts.


263-263: LGTM: Core fix for keybind shape type validation

The use of labelShapeType ensures that only valid shape types for the selected label can be activated, effectively preventing the drawing of disabled shapes through keybinds.

Let's verify the fix by checking the implementation of labelShapeType:

✅ Verification successful

LGTM: Verified shape type validation logic

The labelShapeType function in cvat-ui/src/reducers/annotation-reducer.ts correctly validates shape types by:

  • Checking if the label's type matches any valid ShapeType value
  • Returning null for tag-type labels
  • Ensuring only valid shape types are returned

This implementation confirms that the keybind fix properly prevents drawing disabled shapes by validating against the label's constraints.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for labelShapeType implementation to verify its logic
ast-grep --pattern 'export const labelShapeType = ($_) => {
  $$$
}'

Length of output: 70


Script:

#!/bin/bash
# Let's try different patterns to find labelShapeType implementation
# Try without arrow function syntax
ast-grep --pattern 'export function labelShapeType($_) {
  $$$
}'

# Search for any labelShapeType definition
ast-grep --pattern 'labelShapeType = $_'

# Use ripgrep as fallback to find any occurrence
rg "labelShapeType" -A 5

Length of output: 3176

cvat-ui/src/reducers/annotation-reducer.ts (2)

198-202: LGTM! Changes properly integrate the new shape type handling.

The initialization of activeShapeType to null and subsequent setting via labelShapeType correctly implements the fix for disabled shape types.


198-202: Verify handling of null activeShapeType in UI components.

Let's ensure that UI components and keyboard shortcuts properly handle the case when activeShapeType is null.

✅ Verification successful

Null activeShapeType is properly handled in the codebase

The code analysis shows that activeShapeType being null is a valid state and is properly handled:

  1. When activeShapeType is null, it's explicitly set for tags in setup-tag-popover.tsx with activeShapeType: undefined
  2. The shape type control mapping in annotation-reducer.ts only activates controls when activeShapeType is defined
  3. Drawing operations in annotation-actions.ts check for activeObjectType !== ObjectType.TAG before using activeShapeType
  4. UI components properly handle the null case by checking object types first

The initial concern about null handling is unfounded as the codebase has proper type checks and conditional logic in place.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for UI components that use activeShapeType
echo "Searching for activeShapeType usage in components:"
rg "activeShapeType" --type ts --type tsx

# Search for keyboard shortcut handlers
echo "Searching for keyboard shortcut handlers:"
rg -B 2 -A 2 "keydown|keyup|keypress" --type ts --type tsx

Length of output: 362


Script:

#!/bin/bash
# Search for activeShapeType usage without file type restrictions
echo "Searching for activeShapeType usage:"
rg "activeShapeType" -l

# Search for keyboard shortcut handlers without file type restrictions
echo "Searching for keyboard shortcut handlers:"
rg "keydown|keyup|keypress" -l

# Get content of files containing activeShapeType for detailed analysis
echo "Content of files with activeShapeType:"
rg "activeShapeType" -C 5

Length of output: 22558

cvat-ui/src/actions/annotation-actions.ts (1)

1084-1084: LGTM! Type change addresses the keybind issue.

The change to allow null for activeShapeType is a good solution that prevents drawing of disabled shape types through keybinds, directly addressing the issue mentioned in #8673.

Let's verify the usage of this parameter across the codebase:

✅ Verification successful

Based on the search results, I can now generate the final response:

All call sites handle nullable activeShapeType correctly

The verification shows that all usages of rememberObject properly handle the activeShapeType parameter:

  • Tag-related calls correctly pass undefined or omit the parameter
  • Shape-related calls pass specific shape types
  • Label-list component handles type transitions safely with conditional logic
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of rememberObject's activeShapeType parameter
# to ensure all call sites handle the nullable type correctly

# Search for rememberObject calls
rg -A 5 'rememberObject\(' 

# Search for related type definitions
ast-grep --pattern 'type $_ = { $$$; activeShapeType?: $_; $$$; }'

Length of output: 7644

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.

Project coverage is 74.21%. Comparing base (1e7ff33) to head (c87ca6b).

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8685      +/-   ##
===========================================
+ Coverage    74.20%   74.21%   +0.01%     
===========================================
  Files          401      401              
  Lines        43504    43510       +6     
  Branches      3950     3950              
===========================================
+ Hits         32280    32293      +13     
+ Misses       11224    11217       -7     
Components Coverage Δ
cvat-ui 78.55% <87.50%> (+0.04%) ⬆️
cvat-server 70.51% <ø> (ø)

@klakhov klakhov merged commit 0b528df into develop Nov 13, 2024
34 checks passed
@klakhov klakhov deleted the kl/fix-remember-object branch November 14, 2024 06:45
@cvat-bot cvat-bot bot mentioned this pull request Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Keybinds in UI allow drawing disabled shape types
3 participants