-
Notifications
You must be signed in to change notification settings - Fork 33
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
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a7eb389
Change btn invoking multitouch to control on windows
p-malecki dcbf394
Stop multitouching on mouse leave
p-malecki 03196a0
Add temp changes from #543
p-malecki eea6173
Add multitouching from device's frame
p-malecki 4b90903
Minor changes
p-malecki f475f77
Merge branch 'main' into @p-malecki/multitouch-fixes
p-malecki e8ade6c
Fix flashing old position of touch points at mouse enter
p-malecki bb610cb
Remove log
p-malecki 5000c54
Minor changes
p-malecki 3635b8f
Rename touch marker to indicator
p-malecki 072e04a
Disable multitouch when focus is lost, add useHasFocus
p-malecki 75190c5
Use addEventListener instead of useHasFocus
p-malecki 4933995
Set useCapture to true in listeners
p-malecki 7441830
Remove unused state
p-malecki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,8 +135,8 @@ function DeviceFrame({ device, isFrameDisabled }: DeviceFrameProps) { | |
); | ||
} | ||
|
||
function TouchPointMarker({ isPressing }: { isPressing: boolean }) { | ||
return <div className={`touch-marker ${isPressing ? "pressed" : ""}`}></div>; | ||
function TouchPointIndicator({ isPressing }: { isPressing: boolean }) { | ||
return <div className={`touch-indicator ${isPressing ? "pressed" : ""}`}></div>; | ||
} | ||
|
||
type ButtonGroupLeftProps = { | ||
|
@@ -178,6 +178,8 @@ type InspectStackData = { | |
type Props = { | ||
isInspecting: boolean; | ||
setIsInspecting: (isInspecting: boolean) => void; | ||
isPressing: boolean; | ||
setIsPressing: (isPressing: boolean) => void; | ||
zoomLevel: ZoomLevelType; | ||
onZoomChanged: (zoomLevel: ZoomLevelType) => void; | ||
}; | ||
|
@@ -197,11 +199,18 @@ function calculateMirroredTouchPosition(touchPoint: Point, anchorPoint: Point) { | |
return { x: clampedX, y: clampedY }; | ||
} | ||
|
||
function Preview({ isInspecting, setIsInspecting, zoomLevel, onZoomChanged }: Props) { | ||
function Preview({ | ||
isInspecting, | ||
setIsInspecting, | ||
isPressing, | ||
setIsPressing, | ||
zoomLevel, | ||
onZoomChanged, | ||
}: Props) { | ||
const wrapperDivRef = useRef<HTMLDivElement>(null); | ||
const [isPressing, setIsPressing] = useState(false); | ||
const [isMultiTouching, setIsMultiTouching] = useState(false); | ||
const [isPanning, setIsPanning] = useState(false); | ||
const [isTouchAreaActive, setIsTouchAreaActive] = useState(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const [touchPoint, setTouchPoint] = useState<Point>({ x: 0.5, y: 0.5 }); | ||
const [anchorPoint, setAnchorPoint] = useState<Point>({ x: 0.5, y: 0.5 }); | ||
const previewRef = useRef<HTMLImageElement>(null); | ||
|
@@ -367,17 +376,29 @@ function Preview({ isInspecting, setIsInspecting, zoomLevel, onZoomChanged }: Pr | |
} | ||
} | ||
|
||
function onMouseEnter(e: MouseEvent<HTMLDivElement>) { | ||
e.preventDefault(); | ||
wrapperDivRef.current!.focus(); | ||
|
||
if (isPressing) { | ||
if (isMultiTouching) { | ||
sendMultiTouch(e, "Down"); | ||
} else { | ||
sendTouch(e, "Down"); | ||
} | ||
} | ||
setTouchPoint(getTouchPosition(e)); | ||
} | ||
|
||
function onMouseLeave(e: MouseEvent<HTMLDivElement>) { | ||
e.preventDefault(); | ||
if (isPressing) { | ||
if (isMultiTouching) { | ||
setIsMultiTouching(false); | ||
setIsPanning(false); | ||
sendMultiTouch(e, "Up"); | ||
} else { | ||
sendTouch(e, "Up"); | ||
setIsPressing(false); | ||
} | ||
setIsPressing(false); | ||
} | ||
|
||
if (isInspecting) { | ||
|
@@ -391,6 +412,18 @@ function Preview({ isInspecting, setIsInspecting, zoomLevel, onZoomChanged }: Pr | |
e.preventDefault(); | ||
} | ||
|
||
useEffect(() => { | ||
function onBlurChange() { | ||
if (!document.hasFocus()) { | ||
setIsPanning(false); | ||
setIsMultiTouching(false); | ||
setIsPressing(false); | ||
} | ||
} | ||
addEventListener("blur", onBlurChange, true); | ||
return () => removeEventListener("blur", onBlurChange, true); | ||
}, []); | ||
|
||
useEffect(() => { | ||
function dispatchPaste(e: ClipboardEvent) { | ||
if (document.activeElement === wrapperDivRef.current) { | ||
|
@@ -402,7 +435,6 @@ function Preview({ isInspecting, setIsInspecting, zoomLevel, onZoomChanged }: Pr | |
} | ||
} | ||
} | ||
|
||
addEventListener("paste", dispatchPaste); | ||
return () => { | ||
removeEventListener("paste", dispatchPaste); | ||
|
@@ -461,6 +493,7 @@ function Preview({ isInspecting, setIsInspecting, zoomLevel, onZoomChanged }: Pr | |
onMouseDown, | ||
onMouseMove, | ||
onMouseUp, | ||
onMouseEnter, | ||
onMouseLeave, | ||
onContextMenu, | ||
}; | ||
|
@@ -473,8 +506,8 @@ function Preview({ isInspecting, setIsInspecting, zoomLevel, onZoomChanged }: Pr | |
}); | ||
|
||
const mirroredTouchPosition = calculateMirroredTouchPosition(touchPoint, anchorPoint); | ||
const normalTouchMarkerSize = 33; | ||
const smallTouchMarkerSize = 9; | ||
const normalTouchIndicatorSize = 33; | ||
const smallTouchIndicatorSize = 9; | ||
|
||
return ( | ||
<> | ||
|
@@ -501,29 +534,29 @@ function Preview({ isInspecting, setIsInspecting, zoomLevel, onZoomChanged }: Pr | |
style={{ | ||
"--x": `${touchPoint.x * 100}%`, | ||
"--y": `${touchPoint.y * 100}%`, | ||
"--size": `${normalTouchMarkerSize}px`, | ||
"--size": `${normalTouchIndicatorSize}px`, | ||
}}> | ||
<TouchPointMarker isPressing={isPressing} /> | ||
<TouchPointIndicator isPressing={isPressing} /> | ||
</div> | ||
)} | ||
{isMultiTouching && ( | ||
<div | ||
style={{ | ||
"--x": `${anchorPoint.x * 100}%`, | ||
"--y": `${anchorPoint.y * 100}%`, | ||
"--size": `${smallTouchMarkerSize}px`, | ||
"--size": `${smallTouchIndicatorSize}px`, | ||
}}> | ||
<TouchPointMarker isPressing={false} /> | ||
<TouchPointIndicator isPressing={false} /> | ||
</div> | ||
)} | ||
{isMultiTouching && ( | ||
<div | ||
style={{ | ||
"--x": `${mirroredTouchPosition.x * 100}%`, | ||
"--y": `${mirroredTouchPosition.y * 100}%`, | ||
"--size": `${normalTouchMarkerSize}px`, | ||
"--size": `${normalTouchIndicatorSize}px`, | ||
}}> | ||
<TouchPointMarker isPressing={isPressing} /> | ||
<TouchPointIndicator isPressing={isPressing} /> | ||
</div> | ||
)} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.