diff --git a/kit/primitives/types.ts b/kit/primitives/types.ts index 9ffb381..82d8f3a 100644 --- a/kit/primitives/types.ts +++ b/kit/primitives/types.ts @@ -169,6 +169,16 @@ export interface BoxProps extends Layout, Access { animationDelayMs?: number; /** How long one run takes, where the caller sets it: a hold's fill. */ animationDurationMs?: number; + /** + * When layout moves the box in its parent, it glides there from where it + * was drawn over this many milliseconds, slowing as it arrives, with all it + * holds: a tab strip that slides to centre the open tab, a selection bar + * that runs between rows. A move during a glide sets off from where the box + * is drawn then. A box glides only when it moves: it appears in place, and + * a scroll or a move of its parent carries it at once. Under reduced motion + * it stands in its new place at once. + */ + glideMs?: number; onPress?: (press: Press) => void; /** The secondary button (a right click) let go over the box: a context * menu's. The innermost box that has one hears it. */ diff --git a/src/view/view.h b/src/view/view.h index ee0e83f..dabe8aa 100644 --- a/src/view/view.h +++ b/src/view/view.h @@ -258,6 +258,10 @@ class View { void paintContext(int id, PaintState state); void paintBox(int id, const UiRect& rect, PaintState& state, const std::optional& animated); + /** How far from its place a gliding box (`glideMs`) is drawn this frame, + * in UI pixels. */ + std::pair glideShift(Live& live, const Place& place, + double durationMs); void paintText(int id, const UiRect& rect, PaintState& state); void paintImage(int id, const UiRect& rect, PaintState& state); void paintField(int id, const UiRect& rect, PaintState& state); diff --git a/src/view/view_internal.h b/src/view/view_internal.h index d47a9f5..31cd750 100644 --- a/src/view/view_internal.h +++ b/src/view/view_internal.h @@ -28,6 +28,10 @@ struct View::Place { /** What the node is drawn and hit inside: its ancestors' overflow. */ UiRect clip; bool clipped = false; + /** Where layout put the border box in its parent's, in UI pixels: what + * a glide follows, which no scroll and no move of the parent changes. */ + float inParentX = 0; + float inParentY = 0; /** UI pixels on the screen per pixel the node lays out in: below 1 inside * a box that scales what it holds down (`scaleBelow`). */ float scale = 1; @@ -62,6 +66,15 @@ struct View::Live { * how far it had run. */ bool animationHeld = false; std::optional animationPausedMs; + /** A gliding box (`glideMs`): its place in its parent at the last paint, + * and the offset it set off from when that place last changed, which + * eases out to nothing. */ + bool glidePlaced = false; + float glideRestX = 0; + float glideRestY = 0; + float glideFromX = 0; + float glideFromY = 0; + double glideStartMs = 0; std::string count; double countStartMs = -1; /** A field's caret, in bytes of its value. */ diff --git a/src/view/view_layout.cpp b/src/view/view_layout.cpp index dcf3ee6..f5b3a2b 100644 --- a/src/view/view_layout.cpp +++ b/src/view/view_layout.cpp @@ -797,6 +797,8 @@ void View::layoutTree() { UiRect{originX + YGNodeLayoutGetLeft(built.yoga) * scale, originY + YGNodeLayoutGetTop(built.yoga) * scale, width * scale, height * scale}, space); + placed.inParentX = YGNodeLayoutGetLeft(built.yoga) * scale; + placed.inParentY = YGNodeLayoutGetTop(built.yoga) * scale; placed.scale = scale; const float left = placed.rect.x; const float top = placed.rect.y; diff --git a/src/view/view_paint.cpp b/src/view/view_paint.cpp index 561e146..1090d94 100644 --- a/src/view/view_paint.cpp +++ b/src/view/view_paint.cpp @@ -27,6 +27,11 @@ struct View::PaintState { * animation. */ float dx = 0; float dy = 0; + /** What moves the nearest box that clips this subtree, in the node's + * pixels: a clip moves with the box it is the edge of, and not with what + * moves inside it. */ + float clipDx = 0; + float clipDy = 0; float opacity = 1; /** The engraved shadow under text, inherited down the subtree. */ std::optional emboss; @@ -183,29 +188,36 @@ void View::paintNode(int id, PaintState& parent) { state.scale = parent.scale / parent.frame * place.scale; state.dx = parent.dx * parent.frame / place.scale; state.dy = parent.dy * parent.frame / place.scale; + state.clipDx = parent.clipDx * parent.frame / place.scale; + state.clipDy = parent.clipDy * parent.frame / place.scale; state.frame = place.scale; } // Hit testing reads the shifts in UI pixels. const float parentShiftX = parent.dx * parent.frame; const float parentShiftY = parent.dy * parent.frame; - const bool parentShifted = parent.dx != 0 || parent.dy != 0; + const float clipShiftX = parent.clipDx * parent.frame; + const float clipShiftY = parent.clipDy * parent.frame; + const bool parentShifted = parent.dx != 0 || parent.dy != 0 || + parent.clipDx != 0 || parent.clipDy != 0; if (parentShifted) { - paintedShifts_[id] = {parentShiftX, parentShiftY, parentShiftX, parentShiftY}; + paintedShifts_[id] = {clipShiftX, clipShiftY, parentShiftX, parentShiftY}; } if (place.clipped) { const UiRect clip = inOwnPixels(place.clip, state.frame); const Clip& outer = state.list->clips[state.clip]; - const int left = std::max(outer.x, static_cast(std::floor( - (clip.x + state.dx) * state.scale))); - const int top = std::max(outer.y, static_cast(std::floor( - (clip.y + state.dy) * state.scale))); + const int left = std::max( + outer.x, + static_cast(std::floor((clip.x + state.clipDx) * state.scale))); + const int top = std::max( + outer.y, + static_cast(std::floor((clip.y + state.clipDy) * state.scale))); const int right = std::min( outer.x + outer.width, - static_cast(std::ceil((clip.x + clip.width + state.dx) * + static_cast(std::ceil((clip.x + clip.width + state.clipDx) * state.scale))); const int bottom = std::min( outer.y + outer.height, - static_cast(std::ceil((clip.y + clip.height + state.dy) * + static_cast(std::ceil((clip.y + clip.height + state.clipDy) * state.scale))); state.list->clips.push_back( Clip{left, top, std::max(0, right - left), std::max(0, bottom - top)}); @@ -241,6 +253,14 @@ void View::paintNode(int id, PaintState& parent) { } // A Box, a Scroll or a Layer's root. + const float glideMs = number(*found, "glideMs", 0); + if (glideMs > 0) { + const auto [glideX, glideY] = glideShift(live_[id], place, glideMs); + state.dx += glideX / state.frame; + state.dy += glideY / state.frame; + } else if (const auto live = live_.find(id); live != live_.end()) { + live->second.glidePlaced = false; + } const Json* animation = prop(*found, "animation"); std::optional animatedOpacity; // This frame's sample, once, with the box's delay and duration: its @@ -303,12 +323,6 @@ void View::paintNode(int id, PaintState& parent) { sample->translateXPercent / 100 * rect.width; state.dy += sample->translateY + sample->translateYPercent / 100 * rect.height; - const bool shifted = state.dx * state.frame != parentShiftX || - state.dy * state.frame != parentShiftY; - if (shifted) { - paintedShifts_[id] = {parentShiftX, parentShiftY, state.dx * state.frame, - state.dy * state.frame}; - } animatedOpacity = sample->opacity; // A scale about the box's centre, or along its width from its left. const float sx = sample->scale * sample->scaleX; @@ -328,6 +342,12 @@ void View::paintNode(int id, PaintState& parent) { live->animation.clear(); live->animationStartMs = -1; } + const bool shifted = state.dx * state.frame != parentShiftX || + state.dy * state.frame != parentShiftY; + if (shifted) { + paintedShifts_[id] = {clipShiftX, clipShiftY, state.dx * state.frame, + state.dy * state.frame}; + } const auto look = lookStateOf(id); // A curtain lifts from `opacity={0}` with an animation running 1 to 0. const float opacity = animatedOpacity.value_or( @@ -349,6 +369,12 @@ void View::paintNode(int id, PaintState& parent) { paintBox(id, rect, state, animated); PaintState inner = state; + const bool clipsWhatItHolds = + type == kScroll || textProp(*found, "overflow") == "hidden"; + if (clipsWhatItHolds) { + inner.clipDx = state.dx; + inner.clipDy = state.dy; + } if (look.travel) { inner.dx += 1; inner.dy += 1; @@ -365,6 +391,46 @@ void View::paintNode(int id, PaintState& parent) { paintFocusMark(id, rect, state); } +std::pair View::glideShift(Live& live, const Place& place, + double durationMs) { + const float x = place.inParentX; + const float y = place.inParentY; + // A box appears where it is laid out; only a move glides. + if (!live.glidePlaced) { + live.glidePlaced = true; + live.glideRestX = x; + live.glideRestY = y; + live.glideFromX = 0; + live.glideFromY = 0; + return {0, 0}; + } + // What is left of the way: the offset it set off from, eased out. + const auto left = [&] { + const double run = + std::clamp((nowMs_ - live.glideStartMs) / durationMs, 0.0, 1.0); + const auto share = static_cast((1 - run) * (1 - run) * (1 - run)); + return std::pair{live.glideFromX * share, live.glideFromY * share}; + }; + const bool moved = x != live.glideRestX || y != live.glideRestY; + if (moved) { + // A move during a glide sets off from where the box is drawn now, so + // quick moves run on into each other. + const auto [drawnX, drawnY] = left(); + live.glideFromX = drawnX + live.glideRestX - x; + live.glideFromY = drawnY + live.glideRestY - y; + live.glideStartMs = nowMs_; + live.glideRestX = x; + live.glideRestY = y; + } + // Under reduced motion it stands in its new place at once. + const bool gliding = !reducedMotion_ && + nowMs_ - live.glideStartMs < durationMs && + (live.glideFromX != 0 || live.glideFromY != 0); + if (!gliding) return {0, 0}; + repaintAtMs_ = std::min(repaintAtMs_, nowMs_); + return left(); +} + void View::paintBox(int id, const UiRect& rect, PaintState& state, const std::optional& animated) { const Node& box = *node(id); diff --git a/tests/view_test.cpp b/tests/view_test.cpp index e78908c..de5b9bb 100644 --- a/tests/view_test.cpp +++ b/tests/view_test.cpp @@ -2047,6 +2047,197 @@ TEST_CASE("a click lands on a box where its animation draws it") { CHECK(fixture.handled("onPress").size() == 1); } +namespace { + +/** A 10 by 10 black box at `left`, that glides for 100 ms. */ +Json glidingBox(int left) { + return {{"position", "absolute"}, + {"inset", {{"left", left}, {"top", 0}}}, + {"width", 10}, + {"height", 10}, + {"fill", "db32-black"}, + {"glideMs", 100}}; +} + +Json leftInset(int left) { return {{"inset", {{"left", left}, {"top", 0}}}}; } + +} // namespace + +TEST_CASE("a gliding box appears where it is laid out, and owes no frames") { + Fixture fixture; + fixture.commit( + Stream().create(1, "bureau-box", glidingBox(100)).append(0, 1)); + REQUIRE(fixture.view.frame(0)); + CHECK(fixture.view.displayList()->quads.at(0).x0 == 100); + CHECK_FALSE(fixture.view.animating()); +} + +TEST_CASE("a box that layout moves glides there from where it was drawn") { + Fixture fixture; + fixture.commit(Stream().create(1, "bureau-box", glidingBox(0)).append(0, 1)); + REQUIRE(fixture.view.frame(0)); + fixture.commit(Stream().props(1, leftInset(100))); + REQUIRE(fixture.view.frame(1000)); + CHECK(fixture.view.displayList()->quads.at(0).x0 == 0); + REQUIRE(fixture.view.frame(1050)); + // Half its time gone, seven eighths of its way: it slows as it arrives. + CHECK(fixture.view.displayList()->quads.at(0).x0 == 88); + REQUIRE(fixture.view.frame(1100)); + CHECK(fixture.view.displayList()->quads.at(0).x0 == 100); +} + +TEST_CASE("a gliding box owes frames until it arrives") { + Fixture fixture; + fixture.commit(Stream().create(1, "bureau-box", glidingBox(0)).append(0, 1)); + REQUIRE(fixture.view.frame(0)); + fixture.commit(Stream().props(1, leftInset(100))); + REQUIRE(fixture.view.frame(1000)); + CHECK(fixture.view.animating()); + REQUIRE(fixture.view.frame(1100)); + CHECK_FALSE(fixture.view.animating()); + CHECK_FALSE(fixture.view.frame(1116)); +} + +TEST_CASE("a move during a glide sets off from where the box is drawn") { + Fixture fixture; + fixture.commit(Stream().create(1, "bureau-box", glidingBox(0)).append(0, 1)); + REQUIRE(fixture.view.frame(0)); + fixture.commit(Stream().props(1, leftInset(100))); + REQUIRE(fixture.view.frame(1000)); + REQUIRE(fixture.view.frame(1050)); + fixture.commit(Stream().props(1, leftInset(0))); + REQUIRE(fixture.view.frame(1050)); + CHECK(fixture.view.displayList()->quads.at(0).x0 == 88); + REQUIRE(fixture.view.frame(1150)); + CHECK(fixture.view.displayList()->quads.at(0).x0 == 0); +} + +TEST_CASE("a gliding box is carried at once by a move of its parent") { + Fixture fixture; + fixture.commit(Stream() + .create(1, "bureau-box", + {{"position", "absolute"}, + {"inset", {{"left", 0}, {"top", 0}}}, + {"width", 50}, + {"height", 50}}) + .create(2, "bureau-box", + {{"width", 10}, + {"height", 10}, + {"fill", "db32-black"}, + {"glideMs", 100}}) + .append(1, 2) + .append(0, 1)); + REQUIRE(fixture.view.frame(0)); + fixture.commit(Stream().props(1, leftInset(100))); + REQUIRE(fixture.view.frame(1000)); + CHECK(fixture.view.displayList()->quads.at(0).x0 == 100); +} + +TEST_CASE("a gliding box is carried at once by its scroll") { + Fixture fixture; + fixture.commit(Stream() + .create(1, "bureau-scroll", + {{"width", 100}, + {"height", 100}, + {"direction", "column"}, + {"scrollbar", "hidden"}}) + .create(2, "bureau-box", + {{"height", 300}, + {"shrink", 0}, + {"fill", "db32-black"}, + {"glideMs", 100}}) + .append(1, 2) + .append(0, 1)); + REQUIRE(fixture.view.frame(0)); + fixture.view.scrollBy(1, 0, 60); + REQUIRE(fixture.view.frame(1000)); + CHECK(fixture.view.displayList()->quads.at(0).y0 == -60); +} + +TEST_CASE("under reduced motion a moved box stands in its new place at once") { + Fixture fixture; + fixture.view.setReducedMotion(true); + fixture.commit(Stream().create(1, "bureau-box", glidingBox(0)).append(0, 1)); + REQUIRE(fixture.view.frame(0)); + fixture.commit(Stream().props(1, leftInset(100))); + REQUIRE(fixture.view.frame(1000)); + CHECK(fixture.view.displayList()->quads.at(0).x0 == 100); + CHECK_FALSE(fixture.view.animating()); +} + +TEST_CASE("the pointer hits a gliding box where it is drawn") { + Fixture fixture; + Json box = glidingBox(0); + box["onHoverChange"] = true; + fixture.commit(Stream().create(1, "bureau-box", box).append(0, 1)); + REQUIRE(fixture.view.frame(0)); + fixture.commit(Stream().props(1, leftInset(200))); + REQUIRE(fixture.view.frame(1000)); + fixture.view.pointer(pointer(bureau::PointerEvent::Kind::Move, 5, 5)); + const auto hovers = fixture.handled("onHoverChange"); + REQUIRE(hovers.size() == 1); + CHECK(hovers[0].args == Json::array({true})); +} + +namespace { + +/** A box 200 wide that clips what it holds, holding a box laid out at 100 + * that `bureau-centre-x` draws at 50, which holds a black box as wide. */ +Stream clippedShiftedBox() { + return Stream() + .create(1, "bureau-box", + {{"width", 200}, {"height", 40}, {"overflow", "hidden"}}) + .create(2, "bureau-box", + {{"position", "absolute"}, + {"inset", {{"left", 100}, {"top", 0}}}, + {"width", 100}, + {"height", 40}, + {"onHoverChange", true}, + {"animation", "bureau-centre-x"}}) + .create(3, "bureau-box", + {{"width", 100}, {"height", 40}, {"fill", "db32-black"}}) + .append(2, 3) + .append(1, 2) + .append(0, 1); +} + +} // namespace + +TEST_CASE("what a moved box holds is clipped where the clipping box is drawn") { + Fixture fixture; + fixture.commit(clippedShiftedBox()); + REQUIRE(fixture.view.frame(0)); + const auto& list = *fixture.view.displayList(); + const auto& clip = list.clips.at(list.quads.at(0).clip); + CHECK(clip.x == 0); + CHECK(clip.width == 200); +} + +TEST_CASE("the pointer meets what a moved box holds inside the clipping box") { + Fixture fixture; + fixture.commit(Stream() + .create(1, "bureau-box", + {{"width", 100}, + {"height", 40}, + {"overflow", "hidden"}}) + .create(2, "bureau-box", glidingBox(0)) + .create(3, "bureau-box", + {{"width", 10}, + {"height", 10}, + {"onHoverChange", true}}) + .append(2, 3) + .append(1, 2) + .append(0, 1)); + REQUIRE(fixture.view.frame(0)); + // Laid out at -50 and still drawn at 0, 50 to the right of its place. + fixture.commit(Stream().props(2, leftInset(-50))); + REQUIRE(fixture.view.frame(1000)); + fixture.view.pointer(pointer(bureau::PointerEvent::Kind::Move, 5, 5)); + const auto hovers = fixture.handled("onHoverChange"); + REQUIRE(hovers.size() == 1); + CHECK(hovers[0].node == 3); +} + TEST_CASE("under reduced motion an animation shows one still picture") { bureau::style::Tokens tokens; REQUIRE(tokens.load(tokenDocument()));