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 Timeline Rendering on Zoom #3918

Merged
merged 2 commits into from
Oct 23, 2024
Merged

Conversation

acieslewicz
Copy link
Contributor

@acieslewicz acieslewicz commented Oct 22, 2024

Short description

This should fix the timeline disappearing when Zooming in on the waveform. Resolves #3782

Implementation details

Instead of having virtualAppend initially call renderIfVisible with start = 0. Gets the current scroll window position and utilized it as the start instead. This means that the correct markers are rendered when the timeline is reinitialized after a zoom. Also removed the deferring of the initial call as it was causing timeline flickering while zooming.

How to test it

Create a window with a timeline. Change the cursor position to be something other than 0. Zoom in and out.

Screenshots

before-change.mp4
after-change.mp4

Checklist

  • This PR is covered by e2e tests
  • It introduces no breaking API changes

Summary by CodeRabbit

  • New Features
    • Improved responsiveness of the timeline by optimizing visibility checks to execute immediately based on the current scroll position.
  • Bug Fixes
    • Enhanced error handling to ensure operations are only performed when the wavesurfer instance is initialized.

Uses current scroll position instead of 0 when initially rendering timeline. Previously, the initial render
would show ticks starting from 0 regardless of scroll position.

Closes katspaugh#3782
Fix timeline flickering on zooming by removing rendering deferral
Copy link

coderabbitai bot commented Oct 22, 2024

Walkthrough

The changes in this pull request modify the TimelinePlugin class in src/plugins/timeline.ts, specifically enhancing the virtualAppend method. The update eliminates the use of setTimeout for visibility checks, allowing immediate execution based on the current scroll position of the wavesurfer instance. The error handling for the wavesurfer instance remains unchanged, and the subscription to the scroll event is retained. This change aims to improve the responsiveness of the timeline during interactions.

Changes

File Path Change Summary
src/plugins/timeline.ts Modified virtualAppend method to remove setTimeout, enabling immediate visibility checks based on the current scroll position. Error handling for wavesurfer instance remains unchanged.

Assessment against linked issues

Objective Addressed Explanation
Timeline should update correctly when zooming (3782)

🐰 In the timeline, we hop and play,
No more delays, we’re here to stay!
With scrolls so swift, our view's a delight,
Zoom in, zoom out, everything's bright!
Hooray for the changes, let’s dance and cheer,
The timeline's alive, let’s spread the good cheer! 🎉


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.

Copy link

@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

🧹 Outside diff range and nitpick comments (1)
src/plugins/timeline.ts (1)

164-174: Consider adding end-to-end tests for zoom operations.

As mentioned in the PR objectives, this fix isn't covered by end-to-end tests. Given that this is a UI interaction bug, it would be valuable to add tests that verify timeline visibility during zoom operations.

Consider adding the following test scenarios:

  1. Timeline visibility check after zoom in/out
  2. Timeline marker positions at different zoom levels
  3. Cross-browser compatibility tests

Would you like me to help create these end-to-end tests or open a GitHub issue to track this task?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2682460 and 887676c.

📒 Files selected for processing (1)
  • src/plugins/timeline.ts (1 hunks)
🧰 Additional context used
🔇 Additional comments (1)
src/plugins/timeline.ts (1)

164-174: Verify the fix across different zoom levels and browser environments.

Since the issue was specifically reported in Firefox and involves zoom operations, we should verify the fix across different scenarios.

Comment on lines +164 to +174
if (!this.wavesurfer) return
const scrollLeft = this.wavesurfer.getScroll()
const scrollRight = scrollLeft + this.wavesurfer.getWidth()

renderIfVisible(scrollLeft, scrollRight)

this.subscriptions.push(
this.wavesurfer.on('scroll', (_start, _end, scrollLeft, scrollRight) => {
renderIfVisible(scrollLeft, scrollRight)
}),
)
Copy link

@coderabbitai coderabbitai bot Oct 22, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Implementation aligns with PR objectives but needs additional error handling.

The changes correctly address the timeline disappearing issue by using the current scroll position instead of zero, and removing the setTimeout deferral that was causing flickering. However, there are a few considerations:

  1. The getScroll() and getWidth() calls should be wrapped in try-catch blocks as they might throw if wavesurfer is in an invalid state.
  2. The scroll event subscription could leak if the renderIfVisible function throws.

Consider applying these defensive programming improvements:

 if (!this.wavesurfer) return
-const scrollLeft = this.wavesurfer.getScroll()
-const scrollRight = scrollLeft + this.wavesurfer.getWidth()
+let scrollLeft = 0
+let scrollRight = 0
+try {
+  scrollLeft = this.wavesurfer.getScroll()
+  scrollRight = scrollLeft + this.wavesurfer.getWidth()
+} catch (error) {
+  console.warn('Timeline: Failed to get scroll position', error)
+  return
+}

 renderIfVisible(scrollLeft, scrollRight)

-this.subscriptions.push(
-  this.wavesurfer.on('scroll', (_start, _end, scrollLeft, scrollRight) => {
-    renderIfVisible(scrollLeft, scrollRight)
-  }),
-)
+try {
+  this.subscriptions.push(
+    this.wavesurfer.on('scroll', (_start, _end, scrollLeft, scrollRight) => {
+      renderIfVisible(scrollLeft, scrollRight)
+    }),
+  )
+} catch (error) {
+  console.warn('Timeline: Failed to subscribe to scroll events', error)
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!this.wavesurfer) return
const scrollLeft = this.wavesurfer.getScroll()
const scrollRight = scrollLeft + this.wavesurfer.getWidth()
renderIfVisible(scrollLeft, scrollRight)
this.subscriptions.push(
this.wavesurfer.on('scroll', (_start, _end, scrollLeft, scrollRight) => {
renderIfVisible(scrollLeft, scrollRight)
}),
)
if (!this.wavesurfer) return
let scrollLeft = 0
let scrollRight = 0
try {
scrollLeft = this.wavesurfer.getScroll()
scrollRight = scrollLeft + this.wavesurfer.getWidth()
} catch (error) {
console.warn('Timeline: Failed to get scroll position', error)
return
}
renderIfVisible(scrollLeft, scrollRight)
try {
this.subscriptions.push(
this.wavesurfer.on('scroll', (_start, _end, scrollLeft, scrollRight) => {
renderIfVisible(scrollLeft, scrollRight)
}),
)
} catch (error) {
console.warn('Timeline: Failed to subscribe to scroll events', error)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was instead thinking of:

const scrollLeft = this.wavesurfer?.getScroll() || 0
const scrollRight = scrollLeft + (this.wavesurfer?.getWidth() || 0)

However, shouldn't if (!this.wavesurfer) return already guard against this, or am I missing something?

Copy link

Choose a reason for hiding this comment

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

@acieslewicz, you're absolutely right. The check if (!this.wavesurfer) return ensures that this.wavesurfer is defined, so calling getScroll() and getWidth() should be safe. Using optional chaining with fallback values, as you've suggested, is a clean and effective way to handle potential undefined values. Your approach makes sense, and the try-catch blocks may not be necessary here.

Thanks for clarifying!

Copy link
Owner

@katspaugh katspaugh left a comment

Choose a reason for hiding this comment

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

Awesome, thanks so much!

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.

Timeline disappear when zooming
2 participants