Skip to content

feat(simulator): add fit view to selection helper#872

Open
yashveer-1 wants to merge 5 commits intoCircuitVerse:mainfrom
yashveer-1:fit-to-view
Open

feat(simulator): add fit view to selection helper#872
yashveer-1 wants to merge 5 commits intoCircuitVerse:mainfrom
yashveer-1:fit-to-view

Conversation

@yashveer-1
Copy link

@yashveer-1 yashveer-1 commented Jan 26, 2026

Describe the changes you have made in this PR -

This PR adds a Fit View to Selection capability to the simulator.

  • When one or more elements are selected, the viewport recenters and zooms to fit the combined bounding box of the selection.
  • When no elements are selected, it gracefully falls back to the existing Reset View behavior.
  • The implementation is viewport-only and does not affect simulation logic.

The change reuses existing engine utilities (findDimensions, centerFocus) by constructing a temporary selection scope, keeping the behavior consistent with current canvas logic.


Screenshots of the UI changes (If any) -

111111111.mp4

Code Understanding and AI Usage

Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?

  • No, I wrote all the code myself
  • Yes, I used AI assistance (continue below)

If you used AI assistance:

  • I have reviewed every single line of the AI-generated code
  • I can explain the purpose and logic of each function/component I added
  • I have tested edge cases and understand how the code handles them
  • I have modified the AI output to follow this project's coding standards and conventions

Explain your implementation approach:

This change improves navigation in large or complex circuits by allowing users to quickly focus on the elements they are actively working on.

I considered directly mutating the global simulation scope to compute bounds, but that approach risked unintended side effects. Instead, I chose to construct a temporary selection-only scope that mirrors the structure expected by existing utilities. This allowed me to reuse findDimensions() without modifying global circuit state.

The core logic is handled by:

  • buildSelectionScope() — creates a derived scope containing only selected elements
  • fitToSelection() — computes bounds using the derived scope and delegates centering/zooming to centerFocus()

This approach keeps the change minimal, predictable, and consistent with existing engine patterns.


Checklist before requesting a review

  • I have added proper PR title and linked to the issue
  • I have performed a self-review of my code
  • I can explain the purpose of every function, class, and logic block I added
  • I understand why my changes work and have tested them thoroughly
  • I have considered potential edge cases and how my code handles them
  • If it is a core feature, I have added thorough tests
  • My code follows the project's style guidelines and conventions

Note: I have enabled Allow edits from maintainers.

Summary by CodeRabbit

  • New Features
    • Press F to zoom and center the viewport on your current selection (supports single or multiple selections). If nothing is selected, the view resets to the default perspective.
    • Viewport fitting is a visual-only operation and does not alter simulation state or element data.

✏️ Tip: You can customize this high-level summary in your review settings.

@netlify
Copy link

netlify bot commented Jan 26, 2026

Deploy Preview for circuitverse ready!

Name Link
🔨 Latest commit 8e43845
🔍 Latest deploy log https://app.netlify.com/projects/circuitverse/deploys/69776c191032d000088a2676
😎 Deploy Preview https://deploy-preview-872--circuitverse.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 42 (🔴 down 5 from production)
Accessibility: 73 (no change from production)
Best Practices: 92 (no change from production)
SEO: 82 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 26, 2026

Warning

Rate limit exceeded

@yashveer-1 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 10 minutes and 12 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Walkthrough

Adds viewport-fitting for selections: new fitToSelection(selected) computes dimensions for a derived scope created by buildSelectionScope(selectedElements) and recenters/zooms the viewport (or resets view when selection is empty or bounds are invalid). Adds getSelectedElements() to expose the current selection context. Adds an F/f key binding that calls fitToSelection with the current selection and refreshes canvas/grid state. All changes keep adjustments limited to viewport state and do not modify simulation logic.

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'feat(simulator): add fit view to selection helper' accurately and specifically describes the main change—adding a viewport fit-to-selection feature.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

🤖 Fix all issues with AI agents
In `@src/simulator/src/canvasApi.js`:
- Around line 85-104: fitToSelection can call globalScope.centerFocus(true)
after findDimensions even when findDimensions found no non-wire bounds (e.g.,
wires-only selection), causing undefined bounds; modify fitToSelection to detect
missing/invalid computed bounds after buildSelectionScope and findDimensions
(inspect whatever property findDimensions sets on the scope or a bounds return
value) and fall back to the empty-selection behavior (call
globalScope.centerFocus(false) or compute overall default bounds) instead of
calling centerFocus(true). Specifically, update fitToSelection to check the
selectionScope or the bounds result from findDimensions before invoking
globalScope.centerFocus(true), and call globalScope.centerFocus(false) when
bounds are absent.
🧹 Nitpick comments (1)
src/simulator/src/listeners.js (1)

61-80: Return a copy for multi‑selection to avoid external mutation.
Since this is exported, returning the internal array by reference risks accidental mutation by callers.

♻️ Suggested change
-        return simulationArea.multipleObjectSelections
+        return simulationArea.multipleObjectSelections.slice()

@yashveer-1
Copy link
Author

All changes are complete.

@yashveer-1
Copy link
Author

@tachyons @Nihal4777 @ThatDeparted2061 kindly review !

@naman79820
Copy link
Contributor

Hey @yashveer-1 can you please add fixes # . Assuming you accidently removed that line while creating a pr :))

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.

2 participants