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

Improve Slider Scrolling #8321

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

knollengewaechs
Copy link
Contributor

@knollengewaechs knollengewaechs commented Jan 10, 2025

URL of deployed dev instance (used for testing):

Steps to test:

  • With at least two sliders (number and log slider, such as gamma correction and opacity in the layer setting tab, try the following:
    • the sliders only scroll on focus (e.g. after being clicked)
    • in the case that the layer settings tab is scrollable, e.g. for an annotation with many layers, scrolling the slider should not scroll the layer settings tab.
    • double clicking on the gamma correction value resets it to 1.0 (not 0.33 as before).
      (- you can also test this with double clicking the slider for the brush size in the tool bar (having the brush tool active): it should be reset to 50)

TODOs:

  • fix the following: focus slider, lose focus. then you can slide just a bit. I dont know why this is, isFocused is false in that case!

Issues:


(Please delete unneeded items, merge only when none are left open)

Copy link
Contributor

coderabbitai bot commented Jan 10, 2025

📝 Walkthrough

Walkthrough

The pull request introduces improvements to the Slider component in the frontend, focusing on enhancing wheel event handling, adding a reset-to-default functionality, and improving overall slider interaction. The changes include adding an optional onResetToDefault callback, restructuring wheel event handling with performance optimizations, and implementing focus-based scrolling. These modifications aim to address issues with slider interactions, particularly for log-based sliders like the gamma slider.

Changes

File Change Summary
frontend/javascripts/components/slider.tsx - Added onResetToDefault optional prop
- Improved wheel event handling with useCallback
- Added focus-based event listener
- Updated double-click handling
frontend/javascripts/oxalis/view/components/setting_input_views.tsx - Added resetToDefaultValue method to LogSliderSetting
- Integrated onResetToDefault prop for slider reset
CHANGELOG.unreleased.md - Updated changelog with improvements to slider scrolling behavior

Assessment against linked issues

Objective Addressed Explanation
Fix scrolling to NaN
Fix default values of log sliders
Only scroll sliders on focus

Poem

🐰 A slider's tale, with wheel so fine,
Scrolling gently, line by line,
Focus tracked, default restored,
Performance optimized, users adored!
Code rabbit hops with pure delight 🎉


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7c10dce and 7f6ae32.

📒 Files selected for processing (1)
  • CHANGELOG.unreleased.md (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.unreleased.md
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: circleci_build

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.

if (sliderElement) {
sliderElement.addEventListener("wheel", handleWheelEvent, { passive: false });
}
}, [handleWheelEvent]);
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 a little surprised about this solution.
In my view the dependency that makes sense would be sliderRef.current. I read that components are not rerendered if refs change, which makes sense because the concepts behind refs is to reference objects that are not needed for rendering (according to react.dev. I thought that hooks would still be updated, but this must be the wrong assumption.

@knollengewaechs knollengewaechs marked this pull request as ready for review January 22, 2025 11:14
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 (1)
frontend/javascripts/components/slider.tsx (1)

50-71: Consider simplifying the wheel event handler conditions.

The wheel event handler implementation is correct but could be more maintainable.

Consider this refactoring to improve readability:

 const handleWheelEvent = useCallback(
   (event: { preventDefault: () => void; deltaY: number }) => {
-    // differentiate between single value and range slider
-    if (onWheelDisabled || value == null || min == null || max == null || !isFocused.current)
-      return _.noop;
+    if (onWheelDisabled || !isFocused.current || value == null || min == null || max == null) {
+      return;
+    }
+
+    event.preventDefault();
+    const diff = getWheelStepFromEvent(ensuredStep, event.deltaY, wheelStep);
+
     if (range === false || range == null) {
-      event.preventDefault();
-      const newValue = value - getWheelStepFromEvent(ensuredStep, event.deltaY, wheelStep);
+      const newValue = value - diff;
       const clampedNewValue = clamp(min, newValue, max);
       if (onChange != null) onChange(clampedNewValue);
     } else if (range === true || typeof range === "object") {
-      event.preventDefault();
-      const diff = getWheelStepFromEvent(ensuredStep, event.deltaY, wheelStep);
       const newLowerValue = Math.round(value[0] + diff);
       const newUpperValue = Math.round(value[1] - diff);
       const clampedNewLowerValue = clamp(min, newLowerValue, Math.min(newUpperValue, max));
       const clampedNewUpperValue = clamp(newLowerValue, newUpperValue, max);
       if (onChange != null) onChange([clampedNewLowerValue, clampedNewUpperValue]);
     }
   },
   [value, min, max, onChange, range, onWheelDisabled],
 );
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a04bca6 and 0f5ced1.

📒 Files selected for processing (2)
  • frontend/javascripts/components/slider.tsx (4 hunks)
  • frontend/javascripts/oxalis/view/components/setting_input_views.tsx (2 hunks)
🔇 Additional comments (4)
frontend/javascripts/components/slider.tsx (3)

5-5: LGTM! Clean prop and import additions.

The new optional callback prop and React hooks imports are appropriate for implementing the reset and focus-based scrolling functionality.

Also applies to: 13-13


47-48: LGTM! Proper focus management implementation.

Using useRef for focus tracking is the correct approach as it persists across renders without causing re-renders.


87-106: LGTM! Well-implemented reset functionality.

The double-click handler correctly implements the reset-to-default functionality with proper type handling. The focus/blur handlers and touchAction style are appropriate additions.

frontend/javascripts/oxalis/view/components/setting_input_views.tsx (1)

197-200: LGTM! Clean implementation of reset functionality.

The resetToDefaultValue method is properly implemented with null checks, and the onResetToDefault prop is correctly connected to the Slider component.

Also applies to: 218-218

frontend/javascripts/components/slider.tsx Outdated Show resolved Hide resolved
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

🧹 Nitpick comments (1)
CHANGELOG.unreleased.md (1)

16-16: Consider more concise wording.

The phrase "In order to" can be shortened for better readability.

-- Improved the scrolling behaviour of sliders: In order to scroll them, sliders need to be focused. It is also prevented that the parent element is scrolled together with the slider itself. [#8321](https://github.com/scalableminds/webknossos/pull/8321)
++ Improved the scrolling behaviour of sliders: Sliders must be focused to scroll them. Additionally, parent element scrolling is prevented when using the slider. [#8321](https://github.com/scalableminds/webknossos/pull/8321)
🧰 Tools
🪛 LanguageTool

[style] ~16-~16: Consider a shorter alternative to avoid wordiness.
Context: ...ved the scrolling behaviour of sliders: In order to scroll them, sliders need to be focused...

(IN_ORDER_TO_PREMIUM)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0f5ced1 and 80a47d7.

📒 Files selected for processing (2)
  • CHANGELOG.unreleased.md (1 hunks)
  • frontend/javascripts/components/slider.tsx (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • frontend/javascripts/components/slider.tsx
🧰 Additional context used
🪛 LanguageTool
CHANGELOG.unreleased.md

[style] ~16-~16: Consider a shorter alternative to avoid wordiness.
Context: ...ved the scrolling behaviour of sliders: In order to scroll them, sliders need to be focused...

(IN_ORDER_TO_PREMIUM)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: circleci_build
🔇 Additional comments (1)
CHANGELOG.unreleased.md (1)

16-16: LGTM! The changelog entry is well-structured.

The entry correctly documents the slider improvements, follows the changelog format, and includes the appropriate PR reference.

🧰 Tools
🪛 LanguageTool

[style] ~16-~16: Consider a shorter alternative to avoid wordiness.
Context: ...ved the scrolling behaviour of sliders: In order to scroll them, sliders need to be focused...

(IN_ORDER_TO_PREMIUM)

Copy link
Member

@philippotto philippotto left a comment

Choose a reason for hiding this comment

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

cool! the code looks good and testing went smoothly mostly, too. I noticed two things, though:

  • double clicking the opacity slider for a segmentation layer, resets the value to 100. however, the default value is 20 for segmentation layers. can you have a look whether it's easy to fix this?

  • I already noticed this on master and maybe it's unrelated to the new slider wrapper, but could you please doublecheck this: when dragging the slider and moving on the y axis, text in the UI is selected. this is not critical, but maybe related to the following problem: sometimes the cursor changes into a "grab" pointer and the slider knob does not move anymore. if I release the mouse button, the knob is still moved when I move the mouse. i have to click again to get rid of it. can you try to reproduce this and afterwards temporarily remove the custom slider component and test whether the problem still exists? I can provoke this by clicking and holding for a moment and only then start to move my mouse.

https://github.com/user-attachments/assets/036f8861-6083-4a46-863e-c9981903e7d9
https://github.com/user-attachments/assets/4da1f440-1b60-4da7-9edd-b7ae339fddf1

by the way, the one moment where the slider jumps to 255, I did a doubleclick, so this is fine :)


const handleWheelEvent = useCallback(
(event: { preventDefault: () => void; deltaY: number }) => {
// differentiate between single value and range slider
Copy link
Member

Choose a reason for hiding this comment

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

the comment should probably moved to the other if.

const handleWheelEvent = useCallback(
(event: { preventDefault: () => void; deltaY: number }) => {
// differentiate between single value and range slider
if (onWheelDisabled || value == null || min == null || max == null || !isFocused.current)
Copy link
Member

Choose a reason for hiding this comment

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

you could add || onChange == null and then remove the onChange-ifs below.

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.

Follow-up for slider improvements
2 participants