Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions kit/primitives/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
4 changes: 4 additions & 0 deletions src/view/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ class View {
void paintContext(int id, PaintState state);
void paintBox(int id, const UiRect& rect, PaintState& state,
const std::optional<AnimationSample>& animated);
/** How far from its place a gliding box (`glideMs`) is drawn this frame,
* in UI pixels. */
std::pair<float, float> 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);
Expand Down
13 changes: 13 additions & 0 deletions src/view/view_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +66,15 @@ struct View::Live {
* how far it had run. */
bool animationHeld = false;
std::optional<double> 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. */
Expand Down
2 changes: 2 additions & 0 deletions src/view/view_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
94 changes: 80 additions & 14 deletions src/view/view_paint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<style::Color> emboss;
Expand Down Expand Up @@ -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<int>(std::floor(
(clip.x + state.dx) * state.scale)));
const int top = std::max(outer.y, static_cast<int>(std::floor(
(clip.y + state.dy) * state.scale)));
const int left = std::max(
outer.x,
static_cast<int>(std::floor((clip.x + state.clipDx) * state.scale)));
const int top = std::max(
outer.y,
static_cast<int>(std::floor((clip.y + state.clipDy) * state.scale)));
const int right = std::min(
outer.x + outer.width,
static_cast<int>(std::ceil((clip.x + clip.width + state.dx) *
static_cast<int>(std::ceil((clip.x + clip.width + state.clipDx) *
state.scale)));
const int bottom = std::min(
outer.y + outer.height,
static_cast<int>(std::ceil((clip.y + clip.height + state.dy) *
static_cast<int>(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)});
Expand Down Expand Up @@ -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<float> animatedOpacity;
// This frame's sample, once, with the box's delay and duration: its
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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;
Expand All @@ -365,6 +391,46 @@ void View::paintNode(int id, PaintState& parent) {
paintFocusMark(id, rect, state);
}

std::pair<float, float> 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<float>((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<AnimationSample>& animated) {
const Node& box = *node(id);
Expand Down
Loading
Loading