Skip to content

Commit 850634c

Browse files
committed
Try to improve mobile experience
1 parent 8921ab7 commit 850634c

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

frontend/packages/common/TraceViewer.js

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,42 @@ export const TraceViewer = ({traceData,width, height, onSelect}) => {
166166
setMouseDownLocation(e.evt.layerX)
167167
}
168168
}
169+
const [lastTouchX, setLastTouchX] = useState(null)
170+
171+
const handleTouchStart = (e) => {
172+
if (e.evt.touches.length === 1) {
173+
setLastTouchX(e.evt.touches[0].clientX);
174+
}
175+
}
176+
177+
const handleTouchMove = (e) => {
178+
if (e.evt.touches.length === 1 && lastTouchX !== null) {
179+
const currentX = e.evt.touches[0].clientX;
180+
const deltaX = currentX - lastTouchX;
181+
182+
let newTimeStart = timeRange[0] - deltaX / scaleX;
183+
let newTimeEnd = newTimeStart + dataWidth / scaleX;
184+
185+
if (newTimeStart < 0) {
186+
newTimeStart = 0;
187+
newTimeEnd = dataWidth / scaleX;
188+
}
189+
if (newTimeEnd > maxTime) {
190+
newTimeStart -= newTimeEnd - maxTime;
191+
newTimeEnd = maxTime;
192+
}
193+
194+
setTimeRange([newTimeStart, newTimeEnd]);
195+
setLastTouchX(currentX);
196+
197+
e.evt.preventDefault(); // prevent scrolling
198+
}
199+
}
200+
201+
const handleTouchEnd = () => {
202+
setLastTouchX(null);
203+
}
204+
169205
const [rows, setRows] = useState([]);
170206

171207

@@ -216,10 +252,25 @@ export const TraceViewer = ({traceData,width, height, onSelect}) => {
216252
//}, []);
217253
return (
218254
<div>
219-
<div style={{maxHeight: height-60, overflowY: "auto"}} title={"CTRL+Scroll for Zooming, Drag and Drop for Moving"}>
220-
<Stage width={canvasWidth} height={window.innerHeight}
221-
onWheel={(e) => handleScroll(e)} onMouseDown={handleMouseDown} onMouseUp={handleMouseUp}
222-
onMouseMove={handleMouseMove}>
255+
<div
256+
style={{
257+
maxHeight: height - 60,
258+
overflowY: "auto",
259+
touchAction: "none", // prevent browser gestures like zoom
260+
WebkitOverflowScrolling: "touch" // keep native scrolling on parent
261+
}}
262+
title={"CTRL+Scroll for Zooming, Drag and Drop for Moving"}
263+
> <Stage
264+
width={canvasWidth}
265+
height={window.innerHeight}
266+
onWheel={(e) => handleScroll(e)}
267+
onMouseDown={handleMouseDown}
268+
onMouseUp={handleMouseUp}
269+
onMouseMove={handleMouseMove}
270+
onTouchStart={handleTouchStart}
271+
onTouchMove={handleTouchMove}
272+
onTouchEnd={handleTouchEnd}
273+
>
223274
<Layer>
224275
{
225276
threads.map((t, i) => <Group>
@@ -248,14 +299,14 @@ export const TraceViewer = ({traceData,width, height, onSelect}) => {
248299
var correctedDuration = d.start < timeRange[0] ? duration - (timeRange[0] - d.start) : duration
249300
correctedDuration = d.start + duration > timeRange[1] ? duration - (d.start + duration - timeRange[1]) : correctedDuration
250301
if (duration == 0) {
251-
return <Circle x={correctedStart * scaleX+offsetX} y={d.row * rowOffset+offsetY} radius={2} fill={"black"} onClick={()=> selectEvent(d)}></Circle>
302+
return <Circle x={correctedStart * scaleX+offsetX} y={d.row * rowOffset+offsetY} radius={2} fill={"black"} onClick={()=> selectEvent(d)} onTap={()=>selectEvent(d)}></Circle>
252303
} else {
253304
let extraText = formatExtraText(d)
254-
return (<Group onClick={() => selectEvent(d)}>
305+
return (<Group onClick={() => selectEvent(d)} onTap={()=>selectEvent(d)}>
255306
<Rect x={offsetX + correctedStart * scaleX} y={offsetY + d.row * rowOffset} height={15}
256307
width={correctedDuration * scaleX}
257308
fill={d.category=== "Ignore" ? "white":categoryColors[d.category + "::" + d.name]} stroke={"gray"} strokeWidth={0.4}
258-
onClick={() => {}}></Rect>
309+
onClick={() => {}} onTap={()=>{}}></Rect>
259310

260311
<Text x={offsetX + correctedStart * scaleX} y={offsetY + d.row * rowOffset}
261312
text={`${d.name} (${extraText})`}

0 commit comments

Comments
 (0)