Skip to content

Animation & Gestures

In PAM Desktop, PHP describes motion and the host runs it. Animations, gesture tracking and the transforms that follow the pointer are sampled by the host’s frame clock and never wait for PHP; PHP hears only the result: an animation completed, a gesture began or ended, a drag settled. A slow handler therefore never makes an animation stutter, and an application with nothing moving has no frame clock running at all.

animate() makes an element animate towards its new values whenever its opacity, translation, scale or rotation changes between two renders:

use Pam\Desktop\AnimationEasing;
View::make()
->style(new Style(
width: 56,
height: 56,
borderRadius: 16,
backgroundColor: 0xFF2563EB,
translationX: $this->moved ? 360.0 : 0.0,
rotation: $this->moved ? 180.0 : 0.0,
opacity: $this->moved ? 0.6 : 1.0,
))
->animate(600, AnimationEasing::EaseInOut);

animate($durationMs = 180, $easing = AnimationEasing::EaseInOut) accepts 1 to 10000 ms. A transition starts from what is on screen, so interrupting one does not jump. An element never transitions on the frame that mounts it.

transition() takes a CSS transition list, with spring(mass stiffness damping) as a timing function:

View::make()->transition('transform 300ms spring(1 260 18), opacity 120ms ease-out 40ms');

It animates opacity, transform (or translate, scale, rotate), background-color, color, border-color and border-radius, or all. The same list works in a stylesheet and applies to state styles, so hover fades run entirely in the host:

.row { background-color: #00000000; transition: background-color 120ms ease-out; }
.row:hover { background-color: #2A2D2E; }

motion() plays an entrance when the element appears:

use Pam\Desktop\MotionPreset;
Text::make('Saved')->motion(MotionPreset::SlideUp, 500);

The presets are FadeIn, ScaleIn, SlideUp, SlideDown, Success and Shake, up to 2000 ms.

An Animation is a declarative program of phases, each animating properties with a curve or a spring:

use Pam\Desktop\Animation\Animation;
use Pam\Desktop\Animation\Easing;
// A spring that replays each time $this->bounces changes.
View::make()
->style(new Style(width: 56, height: 56, borderRadius: 28, backgroundColor: 0xFFEF4444))
->animation(
Animation::sequence(
Animation::set(['scale' => 0.4]),
Animation::spring(['scale' => 1.0], stiffness: 180.0, damping: 8.0),
)->key($this->bounces),
);
// An endless rotation.
View::make()
->style(new Style(width: 56, height: 56, borderRadius: 12, borderWidth: 6, borderColor: 0xFF10B981))
->animation(Animation::timing(['rotate' => 360], 1400, Easing::Linear)->repeat());
Builder Meaning
Animation::timing($to, $duration, $easing, $delay) Animates every property in $to with a curve.
Animation::spring($to, $stiffness, $damping, $mass, $velocity, $delay) A physical spring.
Animation::set($values), Animation::wait($ms) Jump at once; pause.
Animation::sequence(...$parts) Chains steps.
->then($next), ->with($other) Runs after, or in parallel.
->delay($ms), ->repeat($times = -1) Delay; repeat (-1 is forever).
->key($value) The replay identity.

Animatable properties are opacity, translateX, translateY, scale, scaleX, scaleY, rotate and borderRadius. A program replays only when its identity changes (the program itself, or its key()); rendering the same program again does not restart it.

Easing has Linear, Ease, EaseIn, EaseOut, EaseInOut, their Quad and Cubic variants, EaseOutBack and Easing::bezier($x1, $y1, $x2, $y2).

A shimmer is two properties on a box:

use Pam\Desktop\PropKey;
View::make()
->style(new Style(height: 18, borderRadius: 9, backgroundColor: 0xFFE5E7EB, widthPercent: 70))
->property(PropKey::ShimmerEnabled, true)
->property(PropKey::ShimmerGradientColor, 0xCCFFFFFF);

When the system asks for reduced motion, press feedback, animated programmatic scrolls and gesture settle animations land at their end state at once, and prefers-reduced-motion turns implicit transitions into instant changes. The setting is followed while the application runs: gtk-enable-animations on Linux, Reduce Motion on macOS, the animation setting on Windows.

GestureDetector wraps content and recognizes one kind of gesture:

use Pam\Desktop\GestureEvent;
use Pam\Desktop\GestureType;
use Pam\Desktop\UI\GestureDetector;
GestureDetector::make(GestureType::Swipe, Text::make('Swipe me'))
->onEnd(function (GestureEvent $event): void {
$this->status = sprintf('Swiped %d, %d', $event->translationX, $event->translationY);
});
GestureType On desktop
Tap A click.
LongPress The primary button held (500 ms by default) without moving.
Pan A drag with the primary button, after 8 units of travel by default.
Swipe A drag of at least 48 units, decided on release.
Pinch A trackpad pinch, or Control + wheel.
Rotation A two-finger trackpad rotation.

direction(), minimumDistance(), minimumDuration() and gestureEnabled() tune recognition. onBegin, onUpdate, onEnd and onCancel receive a GestureEvent with the position, translation, velocity, scale and rotation. Updates are coalesced to at most one per host turn.

A Drag attached to a pan detector moves a view with the pointer, drives other properties from that movement and settles on a snap point, all in the host:

use Pam\Desktop\Animation\Drag;
use Pam\Desktop\Animation\Spring;
use Pam\Desktop\GestureSettleEvent;
GestureDetector::make(GestureType::Pan, $panel)
->drag(
Drag::vertical()
->bounds(min: 0)
->snapPoints(0, '100%')
->threshold(120, velocity: 900)
->settle(new Spring(230, 22, 0.72))
->drive('', 'scale', [0, '50%'], [1, 0.955]),
)
->onSettle(function (GestureSettleEvent $event): void {
if ($event->snapIndex === 1) {
$this->open = false;
}
});

bounds(), rubberBand(), target(), snapPoints(), threshold(), settle(), settleAt(), drive() and group() configure it. GestureDetector::snapTo($index, $request) settles on a snap point from code.

Swipeable is a ready-made drag contract that reveals action panels behind a row:

use Pam\Desktop\UI\Swipeable;
Swipeable::make(
Row::make(Text::make($message))
->style(new Style(height: 56, paddingLeft: 16, alignItems: Align::Center, backgroundColor: 0xFFFFFFFF)),
)
->rightActions(Row::make($archive, $delete), 160)
->group('inbox')
->onOpen(fn (string $side) => $this->status = "Opened ({$side})")
->toElement()
->key($message);

Opening one row of a group closes the others. The row content must paint an opaque background so the panels stay hidden while closed.

nativeTransform() lets the user pan, zoom and rotate content with no round trip to PHP. Nest a pinch detector in a pan detector to get both:

GestureDetector::make(
GestureType::Pan,
GestureDetector::make(
GestureType::Pinch,
Image::make('asset://assets/photo.jpg')->fit(ImageFit::Cover)->style(new Style(width: 400, height: 300)),
)->nativeTransform(maximumScale: 5, focalZoom: true, bounds: true),
)
->nativeTransform(maximumScale: 5, bounds: true)
->onEnd(fn (GestureEvent $event) => $this->scale = $event->nativeScale ?? 1.0)
->cursor('grab')
->style(new Style(width: 400, height: 300, borderRadius: 16, overflow: Overflow::Hidden));

minimumScale, maximumScale, focalZoom, bounds, contentAspect, resetOnEnd and resetKey configure it.