diff --git a/src/view/view_input.cpp b/src/view/view_input.cpp index ab86669..b47a58b 100644 --- a/src/view/view_input.cpp +++ b/src/view/view_input.cpp @@ -12,7 +12,8 @@ namespace { /** Travel past this is a drag, so letting go is not also a press. */ constexpr float kDragSlop = 8; -// Long enough that a finger starting a scroll has passed the slop by then. +// Long enough that a finger starting a scroll has passed the slop by then, +// and a tap has lifted: a finger still down and unmoved is resting. constexpr double kFingerRestMs = 250; Json keyJson(const KeyEvent& event) { @@ -529,6 +530,18 @@ void View::pointer(const PointerEvent& event) { return; } // A control owns one finger until release; another cannot steal its drag. + // A finger that has rested unmoved longer than a tap is the hand holding + // the device, though, and a new finger takes the controls from it. + const bool resting = + uiTouch_ && !press_.moved && nowMs_ - press_.startMs >= kFingerRestMs; + if (begins && resting) { + PointerEvent cancel = event; + cancel.kind = PointerEvent::Kind::Cancel; + cancel.id = *uiTouch_; + cancel.x = press_.startX * uiScale_; + cancel.y = press_.startY * uiScale_; + pointer(cancel); + } if (begins && !uiTouch_) uiTouch_ = event.id; if (!uiTouch_ || *uiTouch_ != event.id) return; if (ends) uiTouch_.reset(); @@ -694,6 +707,7 @@ void View::pointer(const PointerEvent& event) { press_ = Press{}; press_.startX = x; press_.startY = y; + press_.startMs = nowMs_; press_.touch = event.touch; // The innermost box that presses owns the press; the nearest that drags // hears its travel. @@ -722,7 +736,6 @@ void View::pointer(const PointerEvent& event) { press_.scrollSampleX = press_.scrollStartX; press_.scrollSampleY = press_.scrollStartY; press_.scrollSampleMs = nowMs_; - press_.startMs = nowMs_; // Stopping a coast is part of scrolling: the finger taps nothing. press_.moved = fingerScroll == coasting; } else { diff --git a/tests/view_test.cpp b/tests/view_test.cpp index a19d8ce..bba50e3 100644 --- a/tests/view_test.cpp +++ b/tests/view_test.cpp @@ -235,6 +235,70 @@ TEST_CASE("a click on a box tells its press it was no finger") { Json::array({Json{{"touch", false}}})); } +/** Two 40-point pressable boxes, 1 above 2, at the top-left corner. */ +Stream twoPressableBoxes() { + return Stream() + .create(1, "bureau-box", + {{"width", 40}, {"height", 40}, {"onPress", true}}) + .create(2, "bureau-box", + {{"width", 40}, {"height", 40}, {"onPress", true}}) + .append(0, 1) + .append(0, 2); +} + +bureau::PointerEvent touchAt(bureau::PointerEvent::Kind kind, int id, float x, + float y) { + bureau::PointerEvent event{kind, x, y}; + event.touch = true; + event.id = id; + return event; +} + +TEST_CASE("a finger presses beside a palm resting on the controls") { + Fixture fixture; + fixture.commit(twoPressableBoxes()); + fixture.view.frame(0); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Down, 1, 10, 10)); + fixture.view.frame(600); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Down, 2, 10, 60)); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Up, 2, 10, 60)); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Up, 1, 10, 10)); + REQUIRE(fixture.handled("onPress").size() == 1); + CHECK(fixture.handled("onPress").front().node == 2); +} + +TEST_CASE("a second finger during a tap leaves the tap its press") { + Fixture fixture; + fixture.commit(twoPressableBoxes()); + fixture.view.frame(0); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Down, 1, 10, 10)); + fixture.view.frame(100); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Down, 2, 10, 60)); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Up, 2, 10, 60)); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Up, 1, 10, 10)); + REQUIRE(fixture.handled("onPress").size() == 1); + CHECK(fixture.handled("onPress").front().node == 1); +} + +TEST_CASE("a finger dragging a control keeps it from a second finger") { + Fixture fixture; + fixture.commit(Stream() + .create(1, "bureau-box", + {{"width", 40}, {"height", 40}, {"onDrag", true}}) + .create(2, "bureau-box", + {{"width", 40}, {"height", 40}, {"onPress", true}}) + .append(0, 1) + .append(0, 2)); + fixture.view.frame(0); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Down, 1, 10, 10)); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Move, 1, 30, 10)); + fixture.view.frame(600); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Down, 2, 10, 60)); + fixture.view.pointer(touchAt(bureau::PointerEvent::Kind::Up, 2, 10, 60)); + CHECK(fixture.handled("onPress").empty()); + CHECK(fixture.handled("onDrag").size() == 1); +} + TEST_CASE("a click with Shift and Ctrl held tells its press so") { Fixture fixture; fixture.commit(pressableBox());