1// What comes, goes and moves (decisions/F11.md, F11e): visuals,2// canvas nodes moved and faded each frame from one pose to another with3// no layout or painting, but for a room revealing. An element given a4// transition is held by a `Moving` element, whose canvas node the visual5// moves; when its owner goes, everything of it is freed as before but6// that canvas node, kept until the transition has played backwards, and7// with `reveal` a room of its size, shrinking, in its place. Layers,8// toasts, screens and switched views move the same way, by the theme's9// durations. Under reduced motion only fades are kept, at the short10// duration, unless a motion is essential.1112import std::string;13import std::vector;14import std::option;15import std::math;16import std::time;17import pkg::alloy::window;18import pkg::alloy::draw;1920import ui;21import store;22import tree;23import layout;24import layers;25import scrolling;26import motion;2728/* --- Poses and visuals --- */2930// How a canvas node is shown, from its place: its opacity, moved by x31// toward its end (the left right to left) and y down, scaled about its32// center, and how much of its room shows.33exp type Pose = struct {34 exp opacity: f32 = 1.0,35 exp x: f32 = 0.0,36 pub y: f32 = 0.0,37 pub scale: f32 = 1.0,38 exp open: f32 = 1.0,39};4041// In its place, as laid out.42exp const REST: Pose = Pose {};4344fn mixed(a: &Pose, b: &Pose, t: f32) -> Pose => Pose {45 .opacity = a.opacity + (b.opacity - a.opacity) * t,46 .x = a.x + (b.x - a.x) * t,47 .y = a.y + (b.y - a.y) * t,48 .scale = a.scale + (b.scale - a.scale) * t,49 .open = a.open + (b.open - a.open) * t50};5152// What a visual moves: an element, or the canvas node of one gone, with53// where it was and its size.54pub type Shown = enum {55 Element: Element,56 Kept: Kept,57};5859pub type Kept = struct {60 pub id: NodeId,61 pub base: Transform,62 pub size: Extent,63 pub mirrored: bool = false, // it read right to left, its poses' x leftward64};6566// What becomes of a visual's canvas node once it ends: nothing, removed67// (one gone), or hidden (a view hidden, shown meanwhile).68exp type Finish = enum {69 Stay,70 Remove,71 Hide,72};7374pub type Visual = struct {75 pub shown: Shown,76 pub from: Pose,77 pub goal: Pose,78 pub start: u64,79 pub motion: Motion,80 pub finish: Finish,81 // the room a revealed element left, shrinking with it, and its owner82 pub room: Option<Room> = ::None,83};8485pub type Room = struct {86 pub element: Element,87 pub owner: u32,88};8990fn pose_at(self v: &Visual, now: u64) -> Pose => mixed(91 &v.from,92 &v.goal,93 v.motion.along(since(now, v.start)).at94);9596// A pose and the motion to and from it, as reduced motion allows: then97// only its fade, at the short duration, unless the motion is essential;98// none when nothing would move.99exp fn fit(self ui: &Ui, pose: Pose, motion: Motion) -> Option<Fitted> {100 if (ui.reduced_motion() && !motion.essential) {101 if (pose.opacity == 1.0) {102 return ::None;103 }104 var faded = motion;105 faded.duration = ui.theme.motion.short;106 return ::Some(Fitted { .pose = Pose { .opacity = pose.opacity }, .motion = faded });107 }108 if (pose == REST) {109 return ::None;110 }111 return ::Some(Fitted { .pose = pose, .motion = motion });112}113114exp type Fitted = struct {115 exp pose: Pose,116 exp motion: Motion,117};118119// Where the element is shown now: as its visual has it, which is taken120// away, else at rest.121fn take_pose(self ui: &var Ui, element: Element) -> Pose {122 const now = ui.time.nanoseconds;123 const visuals = &var ui.motion_state().visuals;124 for ([..visuals.length()]) |k| {125 if (visuals.get(k).shown is ::Element |e| && e == element) {126 const pose = visuals.get(k).pose_at(now);127 visuals.remove(k);128 return pose;129 }130 }131 return REST;132}133134// Moves the element from one pose to another over the motion, from where135// a visual already moving it has it.136exp fn move_element(137 self ui: &var Ui,138 element: Element,139 from: Pose,140 goal: Pose,141 motion: Motion,142 finish: Finish143) {144 ui.use_motion();145 const shown = ui.take_pose(element);146 const start = if (shown == REST) from else shown;147 ui.motion_state().visuals.push(Visual {148 .shown = Shown::Element(element),149 .from = start,150 .goal = goal,151 .start = ui.time.nanoseconds,152 .motion = motion,153 .finish = finish154 });155 ui.moving = true;156}157158// The element's place on the canvas: where painting shows it in its159// parent's coordinates, a root's at the window's scale.160fn placement(self ui: &Ui, index: u32) -> Transform {161 const node = &ui.node(index);162 const place = Transform::translate(node.shown_at.x, node.shown_at.y);163 if (node.parent is ::None) {164 return place.then(&Transform::scale(ui.scale, ui.scale));165 }166 return place;167}168169// The pose's transform in a node of the size: scaled about its center,170// then moved, x toward the end.171fn local(pose: &Pose, size: Extent, mirrored: bool) -> Transform {172 const cx = size.width / 2.0;173 const cy = size.height / 2.0;174 const x = if (mirrored) -pose.x else pose.x;175 return Transform::translate(-cx, -cy).then(&Transform::scale(pose.scale, pose.scale)).then(176 &Transform::translate(cx + x, cy + pose.y)177 );178}179180// Whether the node reads right to left, its poses' x leftward.181fn mirrored(self ui: &Ui, index: u32) -> bool => ui.node(index).direction is ::RightToLeft;182183// Shows the visual's canvas node in the pose.184fn show_pose(self ui: &var Ui, v: &Visual, pose: &Pose) {185 var id = Option<NodeId>::None;186 var transform = Transform::identity();187 var size = Extent::zero();188 match (v.shown) {189 ::Element |e| {190 if (!ui.exists(e)) {191 return;192 }193 id = ui.node(e.index).draw;194 size = ui.node(e.index).shown_size;195 transform = local(&pose, size, ui.mirrored(e.index)).then(&ui.placement(e.index));196 }197 ::Kept |&k| {198 id = ::Some(k.id);199 size = k.size;200 transform = local(&pose, size, k.mirrored).then(&k.base);201 }202 }203 const scene = if (ui.scene is ::Some |&var s|) &var s else {204 return;205 };206 const node = if (id is ::Some |n| && scene.canvas.contains(n)) n else {207 return;208 };209 scene.canvas.set_transform(node, transform);210 scene.canvas.set_opacity(node, pose.opacity);211 if (v.finish is ::Hide) {212 scene.canvas.set_hidden(node, false);213 }214 if (v.room is ::Some) {215 // what a revealed element drew, within its room216 scene.canvas.set_clip(217 node,218 ::Some(Clip::rect(Rect::sized(size.width, size.height * pose.open)))219 );220 }221}222223// Ends the visual: its node in its last pose, then as its finish says.224fn end_visual(self ui: &var Ui, v: &Visual) {225 ui.show_pose(&v, &v.goal);226 if (v.room is ::Some |room| && ui.exists(room.element)) {227 ui.free_owner(room.owner);228 }229 const scene = if (ui.scene is ::Some |&var s|) &var s else {230 return;231 };232 match (v.shown) {233 ::Element |e| {234 if (235 v.finish is ::Hide236 && ui.exists(e)237 && ui.node(e.index).draw is ::Some |id|238 && scene.canvas.contains(id)239 ) {240 scene.canvas.set_hidden(id, ui.node(e.index).hidden);241 }242 }243 ::Kept |&k| {244 if (v.finish is ::Remove && scene.canvas.contains(k.id)) {245 scene.canvas.remove(k.id);246 }247 }248 }249}250251// Before a frame's update: the rooms revealing or shrinking given their252// part this frame, laid out again, and the scrolls moving.253pub fn mark_visuals(self ui: &var Ui) {254 const now = ui.time.nanoseconds;255 ui.motion_state().revealing = false;256 for ([..ui.motion_state().visuals.length()]) |k| {257 const v = &ui.motion_state().visuals.get(k);258 const open = v.pose_at(now).open;259 var room = Option<Element>::None;260 if (v.room is ::Some |r|) {261 room = ::Some(r.element);262 } else if (v.shown is ::Element |e| && (v.from.open != 1.0 || v.goal.open != 1.0)) {263 room = ::Some(e);264 }265 if (room is ::Some |e| && ui.exists(e)) {266 if (ui.node_var(e.index).widget is ::Some |&var w| && w is Moving |&var m|) {267 m.open = open;268 }269 ui.mark_layout(e.index);270 ui.motion_state().revealing = true;271 }272 }273 ui.move_scrolls();274}275276// After a frame's painting: each visual's node in its pose now, those277// done ended.278pub fn move_visuals(self ui: &var Ui) {279 const now = ui.time.nanoseconds;280 var k: u64 = 0;281 while (k < ui.motion_state().visuals.length()) {282 const v = copy ui.motion_state().visuals.get(k);283 if (v.motion.along(since(now, v.start)).done) {284 ui.motion_state().visuals.remove(k);285 ui.end_visual(&v);286 } else {287 ui.show_pose(&v, &v.pose_at(now));288 k += 1;289 }290 }291}292293/* --- Transitions --- */294295// What holds an element given a transition, its canvas node moved and296// faded; or, with `room`, the room a revealed element left, shrinking.297pub type Moving : Widget = struct {298 pub children: &[Element],299 pub transition: Transition,300 pub owner: u32, // what it belongs to, whose going plays it backwards301 pub sizing: Sizing, // its element's share of a row's room302303 pub open: f32 = 1.0, // how much of its room shows304 pub room: Option<Extent> = ::None,305 pub placed: Option<Point> = ::None, // where it was last drawn, which a move goes from306};307308pub fn layout(self m: &var Moving, arrange: &var Arrange, limits: Limits) -> Extent {309 if (m.room is ::Some |room|) {310 return Extent { .width = room.width, .height = room.height * m.open };311 }312 var size = Extent::zero();313 if (arrange.count() > 0) {314 size = arrange.lay_out(0, limits);315 arrange.place(0, ORIGIN);316 }317 return Extent { .width = size.width, .height = size.height * m.open };318}319320exp fn paint(self m: &Moving, look: &Look, paint: &var Paint) {}321322exp fn renew(self m: &var Moving, next: Moving) -> Change => Change::Nothing;323324pub fn children(self m: &Moving) -> &[Element] => &m.children;325326pub fn sizing(self m: &Moving) -> Sizing => m.sizing;327328// A revealing element's drawing stays within its room.329pub fn clips(self m: &Moving) -> bool => m.transition.reveal;330331// Whether the node is a room a revealed element left.332pub fn is_room(self ui: &Ui, index: u32) -> bool {333 if (ui.node(index).widget is ::Some |&w| && w is Moving |&m|) {334 return m.room is ::Some;335 }336 return false;337}338339// The pose a transition comes from and goes to.340fn away(t: &Transition) -> Pose => Pose {341 .opacity = if (t.fade) 0.0 else 1.0,342 .x = t.slide.x,343 .y = t.slide.y,344 .scale = t.scale,345 .open = if (t.reveal) 0.0 else 1.0346};347348pub fn make_transition(self ui: &var Ui, element: Element, transition: Transition) -> Element {349 ui.use_motion();350 const entry = if (ui.drawn) ui.fit(351 away(&transition),352 transition.motion353 ) else Option<Fitted>::None;354 const wrapper = ui.add(Moving {355 .children = &[element],356 .transition = transition,357 .owner = ui.owner,358 .sizing = Sizing { .grow = ui.node_sizing(element.index).grow },359 .open = if (entry is ::Some |&e|) e.pose.open else 1.0360 });361 ui.node_var(wrapper.index).moving = true;362 if (entry is ::Some |&e|) {363 ui.move_element(wrapper, e.pose, REST, e.motion, Finish::Stay);364 }365 return wrapper;366}367368// The owner going: the elements of its own with a transition play it369// backwards from where they are.370pub fn leave_owner(self ui: &var Ui, owner: u32) {371 if (!ui.drawn) {372 return;373 }374 const count = ui.owners.get(owner to u64).nodes.length();375 for ([..count]) |k| {376 const index = ui.owners.get(owner to u64).nodes.get(k);377 const node = &ui.node(index);378 if (node.alive && node.moving && !node.hidden && node.draw is ::Some) {379 ui.leave(index);380 }381 }382}383384fn leave(self ui: &var Ui, index: u32) {385 var transition = Transition {};386 var owner: u32 = 0;387 if (ui.node(index).widget is ::Some |&w| && w is Moving |&m|) {388 transition = m.transition;389 owner = m.owner;390 }391 const element = ui.element(index);392 const from = ui.take_pose(element);393 const entry = if (ui.fit(away(&transition), transition.motion) is ::Some |e|) e else {394 return;395 };396 const size = ui.node(index).size;397 const shown = ui.node(index).shown_size;398 const base = ui.placement(index);399 const parent = ui.node(index).parent;400 const id = if (ui.keep_drawing(index) is ::Some |d|) d else {401 return;402 };403 var room = Option<Room>::None;404 if (entry.pose.open < 1.0 && parent is ::Some |p|) {405 room = ::Some(ui.make_room(p, index, size, from.open, owner));406 }407 ui.motion_state().visuals.push(Visual {408 .shown = Shown::Kept(Kept {409 .id = id,410 .base = base,411 .size = shown,412 .mirrored = ui.mirrored(index)413 }),414 .from = from,415 .goal = entry.pose,416 .start = ui.time.nanoseconds,417 .motion = entry.motion,418 .finish = Finish::Remove,419 .room = room420 });421 ui.moving = true;422}423424// Keeps the canvas node the element draws in for a visual, its elements425// and theirs letting go of their canvas nodes, so freeing them removes426// none: the node, if it has one.427pub fn keep_drawing(self ui: &var Ui, index: u32) -> Option<NodeId> {428 const id = ui.node(index).draw;429 ui.let_go(index);430 return id;431}432433fn let_go(self ui: &var Ui, index: u32) {434 const node = &var ui.node_var(index);435 node.draw = ::None;436 node.content = ::None;437 node.overlay = ::None;438 for ([..ui.node(index).children.length()]) |k| {439 ui.let_go(ui.node(index).children.get(k));440 }441}442443// A room of the size in the element's place among its parent's children,444// in an owner of its own under the one above the element's.445fn make_room(446 self ui: &var Ui,447 parent: u32,448 index: u32,449 size: Extent,450 open: f32,451 owner: u32452) -> Room {453 const above = if (ui.owners.get(owner to u64).parent is ::Some |o|) o else 0;454 const made = ui.new_owner(above);455 const saved = ui.owner;456 ui.owner = made;457 const room = ui.add(Moving {458 .transition = Transition {},459 .owner = made,460 .sizing = Sizing {},461 .open = open,462 .room = ::Some(size),463 .children = &[]464 });465 ui.owner = saved;466 const siblings = &var ui.node_var(parent).children;467 for ([..siblings.length()]) |k| {468 if (siblings.get(k) == index) {469 siblings.insert(k, room.index);470 break;471 }472 }473 ui.node_var(room.index).parent = ::Some(parent);474 ui.mark_layout(parent);475 return Room { .element = room, .owner = made };476}477478// A moving element placed anew: with `moves`, it goes from where it was479// drawn to its new place.480pub fn place_moving(self ui: &var Ui, index: u32) {481 const at = ui.node(index).position;482 var last = Option<Point>::None;483 var transition = Transition {};484 if (ui.node_var(index).widget is ::Some |&var w| && w is Moving |&var m|) {485 last = m.placed;486 m.placed = ::Some(at);487 transition = m.transition;488 }489 if (490 !transition.moves491 || !ui.drawn492 || ui.node(index).draw is ::None493 || ui.motion_state().revealing494 || ui.reduced_motion() && !transition.motion.essential495 ) {496 return;497 }498 if (last is ::Some |was| && (was.x != at.x || was.y != at.y)) {499 const element = ui.element(index);500 var from = ui.take_pose(element);501 // where it was drawn, from where it goes, toward the end502 from.x += if (ui.mirrored(index)) at.x - was.x else was.x - at.x;503 from.y += was.y - at.y;504 ui.motion_state().visuals.push(Visual {505 .shown = Shown::Element(element),506 .from = from,507 .goal = REST,508 .start = ui.time.nanoseconds,509 .motion = transition.motion,510 .finish = Finish::Stay511 });512 ui.moving = true;513 }514}515516/* --- Layers, screens and views --- */517518// How a layer comes and goes: its host fading (with the scrim), its519// panel moving from a pose, over a duration; a dialog grows from 0.96, a520// popup or a menu drops 4 pixels, a tooltip fades; the toasts' corner,521// whose toasts come each by their own transition, only fades as it522// goes.523fn layer_poses(self ui: &Ui, overlay: &Overlay) -> Option<[Fitted : 2]> {524 const durations = &ui.theme.motion;525 var panel = REST;526 var motion = Motion { .duration = durations.short };527 match (overlay.place) {528 ::Corner {}529 ::Center {530 panel.scale = 0.96;531 motion.duration = durations.medium;532 }533 ::Below, ::At {534 panel.y = -4.0;535 }536 ::Above {537 panel.y = 4.0;538 }539 ::After {}540 }541 if (overlay.modal) {542 panel = Pose { .scale = 0.96 };543 motion.duration = durations.medium;544 } else if (overlay.passive) {545 panel = REST;546 }547 const host = if (ui.fit(Pose { .opacity = 0.0 }, motion) is ::Some |e|) e else {548 return ::None;549 };550 const moved = if (ui.fit(panel, motion) is ::Some |e|) e else Fitted {551 .pose = REST,552 .motion = motion553 };554 return ::Some([host, moved]);555}556557// The layer at the index, just open, comes in.558pub fn enter_layer(self ui: &var Ui, k: u64) {559 if (!ui.drawn) {560 return;561 }562 const slot = &ui.layers.get(k);563 const host = slot.host;564 const panel = ui.element(slot.panel);565 if (slot.overlay.place is ::Corner) {566 return;567 }568 if (ui.layer_poses(&slot.overlay) is ::Some |poses|) {569 ui.move_element(host, poses[0].pose, REST, poses[0].motion, Finish::Stay);570 if (poses[1].pose != REST) {571 ui.move_element(panel, poses[1].pose, REST, poses[1].motion, Finish::Stay);572 }573 }574}575576// The layer closing goes out, its canvas nodes kept until it has.577pub fn leave_layer(self ui: &var Ui, slot: &LayerSlot) {578 if (!ui.drawn || !ui.exists(slot.host)) {579 return;580 }581 const poses = if (ui.layer_poses(&slot.overlay) is ::Some |p|) p else {582 return;583 };584 const panel = Kept {585 .id = if (ui.node(slot.panel).draw is ::Some |d|) d else {586 return;587 },588 .base = ui.placement(slot.panel),589 .size = ui.node(slot.panel).shown_size,590 .mirrored = ui.mirrored(slot.panel)591 };592 const host_pose = ui.take_pose(slot.host);593 const panel_pose = ui.take_pose(ui.element(slot.panel));594 const host = Kept {595 .id = if (ui.node(slot.host.index).draw is ::Some |d|) d else {596 return;597 },598 .base = ui.placement(slot.host.index),599 .size = ui.node(slot.host.index).shown_size,600 .mirrored = ui.mirrored(slot.host.index)601 };602 ui.keep_drawing(slot.host.index);603 const now = ui.time.nanoseconds;604 ui.motion_state().visuals.push(Visual {605 .shown = Shown::Kept(host),606 .from = host_pose,607 .goal = poses[0].pose,608 .start = now,609 .motion = poses[0].motion,610 .finish = Finish::Remove611 });612 if (poses[1].pose != REST) {613 ui.motion_state().visuals.push(Visual {614 .shown = Shown::Kept(panel),615 .from = panel_pose,616 .goal = poses[1].pose,617 .start = now,618 .motion = poses[1].motion,619 .finish = Finish::Stay620 });621 }622 ui.moving = true;623}624625// A view switched to fades in over the one it replaces.626pub fn switch_in(self ui: &var Ui, shown: u32) {627 if (!ui.drawn) {628 return;629 }630 ui.use_motion();631 const motion = Motion { .duration = ui.theme.motion.medium };632 if (ui.fit(Pose { .opacity = 0.0 }, motion) is ::Some |e|) {633 ui.move_element(ui.element(shown), e.pose, REST, e.motion, Finish::Stay);634 }635}636637// A view switched from fades out: hidden once it has, or freed and its638// drawing kept until then.639pub fn switch_out(self ui: &var Ui, view: u32, freed: bool) {640 const motion = Motion { .duration = ui.theme.motion.medium };641 if (freed) {642 ui.keep_going(view, Pose { .opacity = 0.0 }, motion);643 } else if (ui.drawn) {644 ui.use_motion();645 if (ui.fit(Pose { .opacity = 0.0 }, motion) is ::Some |e|) {646 ui.move_element(ui.element(view), REST, e.pose, e.motion, Finish::Hide);647 }648 }649}650651// Keeps what the element going drew, moving it to the pose, then gone.652exp fn keep_going(self ui: &var Ui, index: u32, pose: Pose, motion: Motion) {653 if (!ui.drawn || ui.node(index).hidden || ui.node(index).draw is ::None) {654 return;655 }656 ui.use_motion();657 const entry = if (ui.fit(pose, motion) is ::Some |e|) e else {658 return;659 };660 const from = ui.take_pose(ui.element(index));661 const size = ui.node(index).shown_size;662 const base = ui.placement(index);663 if (ui.keep_drawing(index) is ::Some |id|) {664 ui.motion_state().visuals.push(Visual {665 .shown = Shown::Kept(Kept {666 .id = id,667 .base = base,668 .size = size,669 .mirrored = ui.mirrored(index)670 }),671 .from = from,672 .goal = entry.pose,673 .start = ui.time.nanoseconds,674 .motion = entry.motion,675 .finish = Finish::Remove676 });677 ui.moving = true;678 }679}680681/* --- Scrolling smoothly --- */682683// A scroll moving to an offset.684pub type ScrollMove = struct {685 pub scroll: Element,686 pub from: f32,687 pub goal: f32,688 pub start: u64,689 pub motion: Motion,690};691692pub fn scroll_smoothly(self ui: &var Ui, index: u32) {693 const targets = ui.reveal_offsets(index);694 const motion = Motion { .duration = ui.theme.motion.medium };695 if (!ui.drawn || ui.reduced_motion()) {696 for (targets.view()) |&t| {697 ui.set_scroll_offset(t.scroll, t.offset);698 }699 return;700 }701 ui.use_motion();702 const now = ui.time.nanoseconds;703 for (targets.view()) |&t| {704 const scroll = ui.element(t.scroll);705 var from = ui.scroll_offset(t.scroll);706 const moves = &var ui.motion_state().scrolls;707 for ([..moves.length()]) |k| {708 if (moves.get(k).scroll == scroll) {709 moves.remove(k);710 break;711 }712 }713 moves.push(ScrollMove {714 .scroll = scroll,715 .from = from,716 .goal = t.offset,717 .start = now,718 .motion = motion719 });720 ui.moving = true;721 }722}723724// The scrolls moving at their offsets now, those done forgotten.725fn move_scrolls(self ui: &var Ui) {726 const now = ui.time.nanoseconds;727 var k: u64 = 0;728 while (k < ui.motion_state().scrolls.length()) {729 const m = ui.motion_state().scrolls.get(k);730 const along = m.motion.along(since(now, m.start));731 if (ui.exists(m.scroll)) {732 ui.set_scroll_offset(m.scroll.index, m.from + (m.goal - m.from) * along.at);733 }734 if (along.done || !ui.exists(m.scroll)) {735 ui.motion_state().scrolls.remove(k);736 } else {737 k += 1;738 }739 }740}