diff --git a/contract/cli/src/vocab/card.md b/contract/cli/src/vocab/card.md index 3945d812c..9ce30a53d 100644 --- a/contract/cli/src/vocab/card.md +++ b/contract/cli/src/vocab/card.md @@ -60,7 +60,7 @@ option | value time | datetime format progress | value max start end list | listStyle appearance virtualized refreshing scroll-start | reachstart reachend refresh -scroll | scrollTop navigationScroll keyboardDismissMode | scroll scrollend +scroll | scrollTop navigationScroll keyboardDismissMode zoomScale | scroll scrollend dialog | closedby | cancel close ## rules diff --git a/contract/lower/src/tags.rs b/contract/lower/src/tags.rs index cd23a1648..352b9d851 100644 --- a/contract/lower/src/tags.rs +++ b/contract/lower/src/tags.rs @@ -728,6 +728,8 @@ pub fn attr(name: &str) -> Option { "color-type" => AttrTarget::Prop(p("colorType")), "scrollTop" => AttrTarget::Prop(p("scrollTop")), "scrollLeft" => AttrTarget::Prop(p("scrollLeft")), + // UIKit's pinch zoom of a scroller's one child (iOS). + "minimumZoomScale" | "maximumZoomScale" | "zoomScale" => AttrTarget::Prop(p(name)), "swipeContent" => AttrTarget::Prop(p("swipeContent")), "swipeLeading" => AttrTarget::Prop(p("swipeLeading")), "swipeTrailing" => AttrTarget::Prop(p("swipeTrailing")), diff --git a/docs/card.md b/docs/card.md index 704400b0e..bd0b84cea 100644 --- a/docs/card.md +++ b/docs/card.md @@ -77,7 +77,7 @@ tags: attributes; on events. Every tag also takes CSS style attributes and colou time: datetime format progress: value max start end list: listStyle appearance virtualized refreshing scroll-start; on reachstart reachend refresh - scroll: scrollTop navigationScroll keyboardDismissMode; on scroll scrollend + scroll: scrollTop navigationScroll keyboardDismissMode zoomScale; on scroll scrollend dialog: closedby; on cancel close parts: use X from "exact:content"; an option is none or some(…) diff --git a/host/apple/Sources/ExactKit/IOS/NodeExtrasIOS.swift b/host/apple/Sources/ExactKit/IOS/NodeExtrasIOS.swift index 8917c5ca8..c1924e006 100644 --- a/host/apple/Sources/ExactKit/IOS/NodeExtrasIOS.swift +++ b/host/apple/Sources/ExactKit/IOS/NodeExtrasIOS.swift @@ -94,6 +94,13 @@ final class NodeExtras { var layoutOffset: CGPoint = .zero /// How far its scroller's scroll moves a sticky box (LLP 1083, `Sticky.swift`). var stickyOffset: CGPoint = .zero + /// The scale its zooming scroller draws it at, and how far that centres it (`ScrollZoomIOS.swift`). + var zoomFactor: CGFloat = 1 + var zoomShift: CGPoint = .zero + /// A zooming scroller's: the view UIKit zooms, the child drawn from it, and a `zoomScale` not yet applied. + var zoomProxy: UIView? + weak var zoomed: NodeView? + var pendingZoomScale: Double? var keyboardLift: CGFloat = 0 /// How far its surface's size stands from the laid-out size (LLP 1063). var layoutResize: CGSize = .zero @@ -302,6 +309,8 @@ extension NodeView { var clipBox: PlainView? { get { extras?.clipBox } set { if newValue != nil || extras != nil { more.clipBox = newValue } } } var layoutOffset: CGPoint { get { extras?.layoutOffset ?? .zero } set { if newValue != .zero || extras != nil { more.layoutOffset = newValue } } } var stickyOffset: CGPoint { get { extras?.stickyOffset ?? .zero } set { if newValue != .zero || extras != nil { more.stickyOffset = newValue } } } + var zoomFactor: CGFloat { get { extras?.zoomFactor ?? 1 } set { if newValue != 1 || extras != nil { more.zoomFactor = newValue } } } + var zoomShift: CGPoint { get { extras?.zoomShift ?? .zero } set { if newValue != .zero || extras != nil { more.zoomShift = newValue } } } var authoredAccentTint: UIColor? { get { extras?.authoredAccentTint } set { if newValue != nil || extras != nil { more.authoredAccentTint = newValue } } } /// How far a keyboard toolbar rides up with the keyboard (KeyboardToolbarIOS). var keyboardLift: CGFloat { get { extras?.keyboardLift ?? 0 } set { if newValue != 0 || extras != nil { more.keyboardLift = newValue } } } diff --git a/host/apple/Sources/ExactKit/IOS/NodePoolIOS.swift b/host/apple/Sources/ExactKit/IOS/NodePoolIOS.swift index f39bc14eb..04f1284ab 100644 --- a/host/apple/Sources/ExactKit/IOS/NodePoolIOS.swift +++ b/host/apple/Sources/ExactKit/IOS/NodePoolIOS.swift @@ -572,6 +572,10 @@ extension NodeView { /// list's scroll, with its delegate detached while it resets. func recycleScroll() { guard let sv = scroll else { return } + #if os(iOS) + dropZoom() + extras?.pendingZoomScale = nil + #endif let delegate = sv.delegate sv.delegate = nil sv.setContentOffset(.zero, animated: false) @@ -600,7 +604,7 @@ extension NodeView { // UIKit's setters are not free, even to the same value. if isHidden { isHidden = false } if alpha != 1 || boxFilter != nil { presentOpacity(1) } - translatePx = .zero; translatePercent = .zero; layoutOffset = .zero; layoutResize = .zero; endSurface(); scale = 1; rotate = 0; stopPressEase(); press = PressFeedback(); dropPressPaint() + translatePx = .zero; translatePercent = .zero; layoutOffset = .zero; zoomFactor = 1; zoomShift = .zero; layoutResize = .zero; endSurface(); scale = 1; rotate = 0; stopPressEase(); press = PressFeedback(); dropPressPaint() if !transform.isIdentity { transform = .identity } if !isUserInteractionEnabled { isUserInteractionEnabled = true } // With assistive technology's runtime loaded every accessibility diff --git a/host/apple/Sources/ExactKit/IOS/NodeViewIOS.swift b/host/apple/Sources/ExactKit/IOS/NodeViewIOS.swift index ab4e5caec..de9b97029 100644 --- a/host/apple/Sources/ExactKit/IOS/NodeViewIOS.swift +++ b/host/apple/Sources/ExactKit/IOS/NodeViewIOS.swift @@ -1076,6 +1076,7 @@ package final class NodeView: UIView, UITextViewDelegate, UITextFieldDelegate, U if let raw = set["scrollLeft"], let left = Double(raw), left.isFinite { pendingScrollLeft = left } if clear.contains("scrollTop") { pendingScrollTop = nil } if let raw = set["scrollTop"], let top = Double(raw), top.isFinite { pendingScrollTop = top } + if let raw = set["zoomScale"], let scale = Double(raw), scale.isFinite, scale > 0 { more.pendingZoomScale = scale } // One assignment: `props` tells the presenter of each. var next = props for k in clear { next.removeValue(forKey: k) } @@ -1323,6 +1324,9 @@ package final class NodeView: UIView, UITextViewDelegate, UITextFieldDelegate, U // Ending where its last edge is drawn, from this box's place on the // pixel grid, so the furthest offset is a whole pixel and reaches it (`PixelGridIOS.swift`). let s = presenter?.gridScale ?? 1, o = grid?.origin ?? .zero + #if os(iOS) + let content = zoomedContent + #endif let size = CGSize(width: sv.scrollsX ? max(PixelGrid.length(content.width, from: o, vertical: false, scale: s), sv.bounds.width) : sv.bounds.width, height: sv.scrollsY ? max(PixelGrid.length(content.height, from: o, vertical: true, scale: s), scrollFloor(sv)) : sv.bounds.height) if sv.contentSize != size { sv.contentSize = size } diff --git a/host/apple/Sources/ExactKit/IOS/ScrollViewIOS.swift b/host/apple/Sources/ExactKit/IOS/ScrollViewIOS.swift index ca7e96a8c..31c33aa75 100644 --- a/host/apple/Sources/ExactKit/IOS/ScrollViewIOS.swift +++ b/host/apple/Sources/ExactKit/IOS/ScrollViewIOS.swift @@ -278,6 +278,9 @@ extension Presenter { // Settle that layout before restoring or authoring an offset. if node.groupedOwner { node.scrollView?.layoutIfNeeded() } if !collections.owns(node.id) { node.restoreScrollPosition(authored: authored.contains { $0.isDescendant(of: node) }) } + #if os(iOS) + node.settleZoom() + #endif if let material = node.materialView { node.sendSubviewToBack(node.glassSlot ?? material) } } // Every capture is restored before any authored offset lands: a diff --git a/host/apple/Sources/ExactKit/IOS/ScrollZoomIOS.swift b/host/apple/Sources/ExactKit/IOS/ScrollZoomIOS.swift new file mode 100644 index 000000000..bbdcd0547 --- /dev/null +++ b/host/apple/Sources/ExactKit/IOS/ScrollZoomIOS.swift @@ -0,0 +1,101 @@ +// UIKit's pinch zoom on a scroll container with one child +// (`minimumZoomScale`, `maximumZoomScale`, `zoomScale`). UIKit zooms a view +// of its own that it alone writes: a node's transform is the host's +// (`applyTransform`), and a view two writers transform shows whichever wrote +// last. The child is drawn from the scroller's scale through +// `hostPlacement`, so its own transform, a press and an animation compose +// with it. The child keeps its layout: its box in the scroller is its size +// times the scale, what layout put around it stays as it is, and it is +// centred while the whole is smaller than the port, as a photo is. +#if os(iOS) +import UIKit + +extension NodeView { + /// The range a pinch zooms through, when the props give one. + var zoomRange: ClosedRange? { + guard let low = props["minimumZoomScale"].flatMap(Double.init), let high = props["maximumZoomScale"].flatMap(Double.init), + low.isFinite, high.isFinite, low > 0, high > low else { return nil } + return CGFloat(low)...CGFloat(high) + } + /// The content extent with the zoomed child at its scale. + var zoomedContent: CGSize { + guard let e = extras, e.zoomProxy != nil, let sv = scroll, let child = e.zoomed, let size = child.grid?.drawn.size else { return content } + let grown = sv.zoomScale - 1 + return CGSize(width: content.width + size.width * grown, height: content.height + size.height * grown) + } + + /// After a batch: the range and the scale the props give, and the child + /// where they put it. + func settleZoom() { + guard let sv = scroll else { return } + let nodes = sv.subviews.compactMap { $0 as? NodeView } + guard let range = zoomRange, nodes.count == 1, nodes[0].grid != nil else { return dropZoom() } + let opening = extras?.zoomProxy == nil + if extras?.zoomed !== nodes[0] { dropZoom() } + if extras?.zoomProxy == nil { + let proxy = UIView() + proxy.isUserInteractionEnabled = false + sv.insertSubview(proxy, at: 0) + more.zoomProxy = proxy + more.zoomed = nodes[0] + placeZoom() + } + if sv.minimumZoomScale != range.lowerBound { sv.minimumZoomScale = range.lowerBound } + if sv.maximumZoomScale != range.upperBound { sv.maximumZoomScale = range.upperBound } + let asked = extras?.pendingZoomScale.map { CGFloat($0) } + more.pendingZoomScale = nil + if !sv.isZooming, !sv.isZoomBouncing { + let scale = min(max(asked ?? sv.zoomScale, range.lowerBound), range.upperBound) + if scale != sv.zoomScale { + let offset = sv.contentOffset + sv.zoomScale = scale + // A scroller that opens zoomed opens at its start. + if opening { sv.contentOffset = offset } + } + } + placeZoom() + } + + /// The child drawn at the scroller's scale, the extent that gives, and + /// UIKit's view over where the child shows, which its next step zooms from. + func placeZoom() { + guard let sv = scroll, let e = extras, let proxy = e.zoomProxy, let child = e.zoomed, let place = child.grid?.drawn else { return } + let scale = sv.zoomScale, extent = zoomedContent + let shift = CGPoint(x: max(0, (sv.bounds.width - extent.width) / 2), y: max(0, (scrollFloor(sv) - extent.height) / 2)) + if child.zoomFactor != scale || child.zoomShift != shift { + child.zoomFactor = scale; child.zoomShift = shift + child.applyTransform() + } + fitScroll() + if proxy.bounds.size != place.size { proxy.bounds.size = place.size } + let center = CGPoint(x: place.minX + shift.x + place.width * scale / 2, y: place.minY + shift.y + place.height * scale / 2) + if proxy.center != center { proxy.center = center } + } + + /// No zoom any more: the child as layout placed it. + func dropZoom() { + guard let e = extras, let proxy = e.zoomProxy else { return } + if let sv = scroll { + if sv.zoomScale != 1 { sv.zoomScale = 1 } + sv.minimumZoomScale = 1; sv.maximumZoomScale = 1 + } + proxy.removeFromSuperview() + e.zoomProxy = nil + if let child = e.zoomed, child.zoomFactor != 1 || child.zoomShift != .zero { + child.zoomFactor = 1; child.zoomShift = .zero + child.applyTransform() + } + e.zoomed = nil + fitScroll() + } + + package func viewForZooming(in scrollView: UIScrollView) -> UIView? { + scrollView === scroll ? extras?.zoomProxy : nil + } + package func scrollViewDidZoom(_ scrollView: UIScrollView) { + guard scrollView === scroll else { return } + placeZoom() + queueScrollEvent() + } +} +#endif diff --git a/host/apple/Sources/ExactKit/PressFeedback.swift b/host/apple/Sources/ExactKit/PressFeedback.swift index 886ff7d82..5a36bcfb3 100644 --- a/host/apple/Sources/ExactKit/PressFeedback.swift +++ b/host/apple/Sources/ExactKit/PressFeedback.swift @@ -329,7 +329,11 @@ extension NodeView { /// transform: a layout transition's offset, a sticky box's scroll offset /// (LLP 1083), a keyboard toolbar's lift and a context preview's projection. var hostPlacement: CGAffineTransform { - CGAffineTransform(translationX: layoutOffset.x + stickyOffset.x, y: layoutOffset.y - keyboardLift + stickyOffset.y + controlRide).concatenating(contextTransform) + let placed = CGAffineTransform(translationX: layoutOffset.x + stickyOffset.x, y: layoutOffset.y - keyboardLift + stickyOffset.y + controlRide).concatenating(contextTransform) + // A zooming scroller's child, scaled from its box's top left (`ScrollZoomIOS.swift`); UIKit scales about the centre. + let zoom = zoomFactor, shift = zoomShift + guard zoom != 1 || shift != .zero else { return placed } + return placed.concatenating(CGAffineTransform(a: zoom, b: 0, c: 0, d: zoom, tx: (zoom - 1) * bounds.width / 2 + shift.x, ty: (zoom - 1) * bounds.height / 2 + shift.y)) } /// The border box in `viewport`'s visible space as an open top-layer box /// is told to the runner: before its own transform, which the runner diff --git a/host/web-js/src/emit.rs b/host/web-js/src/emit.rs index 57b6aaf1f..bc0d15aac 100644 --- a/host/web-js/src/emit.rs +++ b/host/web-js/src/emit.rs @@ -1181,6 +1181,14 @@ impl Em<'_> { { continue; } + // Nor has UIKit's pinch zoom. + if b.kind == BindingKind::Prop + && (b.id == PropId::MinimumZoomScale as u16 + || b.id == PropId::MaximumZoomScale as u16 + || b.id == PropId::ZoomScale as u16) + { + continue; + } if let Some(v) = style::literal(plan, plan.code(b.expr)) { if markdown && b.kind == BindingKind::Prop && b.id == PropId::Text as u16 { let source = serde_json::to_string(v.as_str().unwrap_or_default()).unwrap(); diff --git a/host/web/src/element.rs b/host/web/src/element.rs index 11c25f6c9..2c52a6f7e 100644 --- a/host/web/src/element.rs +++ b/host/web/src/element.rs @@ -939,6 +939,8 @@ pub fn props_of(node: &NodeFacts<'_>) -> SortedMap { // Read by rt.js's image fade. PropId::FadeDuration => "data-exact-fade", PropId::Loading => "loading", + // UIKit's pinch zoom; a page's is the browser's. + PropId::MinimumZoomScale | PropId::MaximumZoomScale | PropId::ZoomScale => continue, // Read by wakelock-glue.js (LLP 1125 D11). PropId::Wakelock => "data-exact-wakelock", // Read by orientationlock-glue.js (LLP 1133.029 D7). diff --git a/kernel/src/wire/codec.rs b/kernel/src/wire/codec.rs index d297302e7..4a0596fb5 100644 --- a/kernel/src/wire/codec.rs +++ b/kernel/src/wire/codec.rs @@ -735,7 +735,7 @@ mod tests { // schema. The literal makes an accidental removal of that coupling a // test failure whenever the byte snapshot above is intentionally moved. // Recomputed when the schema changes; the digest test prints the value. - assert_eq!(SCHEMA_DIGEST, 0xc44798a91fd796dc); + assert_eq!(SCHEMA_DIGEST, 0xb3941128c52632b8); } #[test] diff --git a/kernel/tables/schema.json b/kernel/tables/schema.json index 2c0e92dbe..be9a730dd 100644 --- a/kernel/tables/schema.json +++ b/kernel/tables/schema.json @@ -2002,6 +2002,23 @@ "name": "loading", "kind": "str", "_comment": "HTML's `loading` on an image, `lazy` or `eager`. Unset, the web chooses: an image a `for` repeats loads as its row comes into view, a lone one with the page. Native hosts load what a list shows and ignore it." + }, + { + "id": 312, + "name": "minimumZoomScale", + "kind": "float", + "_comment": "UIKit's `minimumZoomScale` on a scroll container with one child: with `maximumZoomScale`, the range a pinch scales that child through, about the fingers. The child keeps its layout; its box in the scroller is its size times the scale, centred while smaller than the port. iOS; other hosts ignore the three." + }, + { + "id": 313, + "name": "maximumZoomScale", + "kind": "float" + }, + { + "id": 314, + "name": "zoomScale", + "kind": "float", + "_comment": "The scale a zooming scroll container opens at, and moves to when the value changes." } ], "_enums": "Wire discriminant is the 0-based index. An unknown discriminant is a typed decode rejection, never a fallback. refused: keywords the wire carries and the compiler refuses an author, because no presenter builds them (LLP 1043.000 §3 D1); `contract vocab` lists the others. No wire reads it, so it is outside the digest.",