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 multitouch and touch indicator behaviour on lost focus #551

Merged
merged 14 commits into from
Sep 19, 2024

Conversation

p-malecki
Copy link
Contributor

@p-malecki p-malecki commented Sep 18, 2024

This PR brings the following changes:

  • Enhances multitouch and touch gesture functionalities by pausing when the mouse leaves touch-area and resuming upon re-entry
  • Makes touch indicator visible only when touch-area is in focus, preventing unnecessary indicator display when users switch focus to a different window
  • Renames touch marker to touch indicator
Screen.Recording.2024-09-17.at.15.53.16.mov

Copy link

vercel bot commented Sep 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-native-ide ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 19, 2024 9:23am

Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

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

Left a couple of comments inline

const [isMultiTouching, setIsMultiTouching] = useState(false);
const [isPanning, setIsPanning] = useState(false);
const [isTouchAreaActive, setIsTouchAreaActive] = useState(false);
Copy link
Member

Choose a reason for hiding this comment

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

do you really need this one to be expressed as state? Given you only read it in mouse event handler. Maybe you could just check document.hasFocus there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, it was unnecessary to use state there, so I've used listeners and document.hasFocus instead.

const onFocus = () => setFocus(true);
const onBlur = () => setFocus(false);

window.addEventListener("focus", onFocus);
Copy link
Member

Choose a reason for hiding this comment

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

you may want to pass true for useCapture argument here, as if there are some elements beneath in the tree that set that listener, you'll never get it at the document level: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#usecapture

Comment on lines 423 to 430
useEffect(() => {
if (!focus) {
setIsPanning(false);
setIsMultiTouching(false);
setIsPressing(false);
}
setIsTouchAreaActive(focus);
}, [focus]);
Copy link
Member

Choose a reason for hiding this comment

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

Feels a bit weird to express this the way you did. It should be enough to have a single effect here instead of state-like variable that uses useState and useEffect under the hood just to use its output in useEffect.

Would be more readable if you just had listeners setup here, or you can modify the helper method to be

useDocuemntFocusEffect((isDocumentFocused) => {
  if (!isDocumentFocused) {
    // .... 
  }
})```
This way it wouldn't require additional state variable in this component that you never access except in the deps array for this effect here

Copy link
Member

@kmagiera kmagiera left a comment

Choose a reason for hiding this comment

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

Looks good 👍 There's one state that got added and is not used after the changes, so you may want to clean it up before merging

const [isMultiTouching, setIsMultiTouching] = useState(false);
const [isPanning, setIsPanning] = useState(false);
const [isTouchAreaActive, setIsTouchAreaActive] = useState(false);
Copy link
Member

Choose a reason for hiding this comment

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

isTouchAreaActive is not used, delete it?

@p-malecki p-malecki merged commit 13cb9ce into main Sep 19, 2024
3 checks passed
@p-malecki p-malecki deleted the @p-malecki/multitouch-fixes branch September 19, 2024 09:25
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