rdi-core 0.2.29

Platform-agnostic animation core for rusty-desktop-icons (curves, duration, spec, error, backend trait).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
//! Worker thread, control-message dispatch and tick loop.
//!
//! The engine owns a single [`DesktopBackend`](crate::DesktopBackend)
//! trait object and runs it on a dedicated OS thread. All commands flow
//! through a crossbeam MPSC channel; during an animation the same channel
//! delivers `AnimStop` plus read-only queries, which are drained between
//! ticks. An animation's active set is fixed at `StartAnimation`.
//!
//! COM apartment threading (Windows) is honoured automatically: the
//! backend is created on this worker thread and never moves.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration as StdDuration, Instant};

use crossbeam_channel::{Receiver, RecvTimeoutError, Sender};
use tracing::{debug, info, info_span, trace, warn};

use crate::backend::DesktopBackend;
use crate::events::{
    FinishReason, IconAnimationState, StartContext, StopMode, TickContext,
};
use crate::handle::{AnimationHandle, HandleInner, PreObservers};
use crate::spec::{AnimationOptions, FolderFlagOp, IconAnimationSpec};
use crate::{DesktopError, IconId, IconSnapshot, Point, TimelineCloseMode};

pub(crate) enum StartMode {
    Animation,
    Cancel,
    Timeline { runtime: crate::timeline::Runtime, ready: Sender<()> },
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Default tick period — 10 ms (100 Hz), matching the legacy C
/// `POS_CHANGE_PER_N = 10_000 µs` exactly.
///
/// This rate is only smooth **because the Windows shell backend pumps
/// its worker thread's message queue after every commit** (see
/// `pump_thread_messages` in `crates/rdi-platform-windows/src/backend.rs`
/// for the full explanation). Without the pump, cross-STA `SendMessage`
/// traffic from Explorer back into the worker piles up and Explorer's
/// UI thread stalls until the animation ends.
///
/// Callers can override via [`AnimationOptions::tick_hz`].
const DEFAULT_TICK: StdDuration = StdDuration::from_millis(10);

/// Default position-error tolerance in pixels — matches legacy
/// `POSITION_ERROR_VALUE = 10`.
const DEFAULT_TOLERANCE_PX: i32 = 10;

// ---------------------------------------------------------------------------
// Worker messages
// ---------------------------------------------------------------------------

pub(crate) enum WorkerMsg {
    PrepareScene {
        scene: crate::Scene,
        resp: Sender<Result<crate::RenderSession, DesktopError>>,
    },
    // --- Sync request/reply commands.
    ListIcons {
        resp: Sender<Result<Vec<IconSnapshot>, DesktopError>>,
    },
    GetFlags {
        resp: Sender<Result<u32, DesktopError>>,
    },
    ApplyFlags {
        mask: u32,
        values: u32,
        resp: Sender<Result<(), DesktopError>>,
    },
    SetPositions {
        moves: Vec<(IconId, Point)>,
        resp: Sender<Result<Vec<IconId>, DesktopError>>,
    },
    ListMonitors {
        resp: Sender<Result<Vec<crate::MonitorInfo>, DesktopError>>,
    },
    DesktopInfo {
        resp: Sender<Result<crate::DesktopInfo, DesktopError>>,
    },
    RenderOverlaySnapshot {
        width_px: u32,
        height_px: u32,
        dpi_scale: f32,
        positions: Vec<(IconId, Point)>,
        render_options: crate::OverlayRenderOptions,
        resp: Sender<Result<crate::SnapshotFrame, DesktopError>>,
    },

    // --- Animation lifecycle.
    StartAnimation {
        specs: Vec<IconAnimationSpec>,
        options: AnimationOptions,
        observers: PreObservers,
        resp: Sender<Result<AnimationHandle, DesktopError>>,
        preparation: Option<(Sender<()>, Receiver<StartMode>)>,
    },

    // --- In-flight animation control (received between ticks).
    AnimStop {
        mode: StopMode,
        owner: std::sync::Weak<HandleInner>,
    },

    Shutdown,
}

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

pub(crate) fn worker_main(
    tx: Sender<WorkerMsg>,
    rx: Receiver<WorkerMsg>,
    mut backend: Box<dyn DesktopBackend>,
) {
    // `tx` is held for the thread's lifetime so a clone can be handed
    // to each `AnimationHandle`. Without it the channel would appear
    // disconnected the moment the controller drops its sender.
    while let Ok(msg) = rx.recv() {
        match msg {
            WorkerMsg::Shutdown => break,
            WorkerMsg::PrepareScene { scene, resp } => {
                let prepared = (|| {
                    let scene = crate::scene::EvaluatedScene::new(scene)?;
                    let icons = backend.list_icons()?;
                    if scene.scene.icons.iter().any(|entry| !icons.iter().any(|icon| icon.id == entry.animation.id)) {
                        return Err(DesktopError::BackendUnavailable("scene contains an unavailable desktop icon".into()));
                    }
                    let renderer = backend.prepare_scene_renderer(scene.scene.canvas, &scene.plans(), scene.scene.render_options)?;
                    Ok((scene, renderer))
                })();
                match prepared {
                    Err(error) => { let _ = resp.send(Err(error)); }
                    Ok((scene, mut renderer)) => {
                        let (requests, frames) = crossbeam_channel::unbounded();
                        let session = crate::RenderSession { tx: requests, worker: std::thread::current().id(), duration: scene.duration };
                        if resp.send(Ok(session)).is_err() { continue; }
                        let handle = HandleInner::new(tx.clone());
                        loop {
                            crossbeam_channel::select! {
                                recv(frames) -> request => match request {
                                    Ok(crate::scene::RenderRequest::Frame(seconds, reply)) => {
                                        let result = scene.sample(seconds).and_then(|frame| renderer.render(&frame, seconds));
                                        let _ = reply.send(result);
                                    }
                                    Ok(crate::scene::RenderRequest::Close(reply)) => {
                                        drop(renderer);
                                        let _ = reply.send(());
                                        break;
                                    }
                                    Err(_) => break,
                                },
                                recv(rx) -> message => match message {
                                    Ok(message) => match handle_control(message, &mut Vec::new(), &handle, 0) {
                                        Control::ControlSync(command) => run_sync_command(command, &mut *backend),
                                        Control::Shutdown => return,
                                        _ => {}
                                    },
                                    Err(_) => return,
                                }
                            }
                        }
                    }
                }
            }
            WorkerMsg::ListIcons { resp } => {
                let _ = resp.send(backend.list_icons());
            }
            WorkerMsg::GetFlags { resp } => {
                let _ = resp.send(backend.get_flags());
            }
            WorkerMsg::ApplyFlags { mask, values, resp } => {
                let _ = resp.send(backend.apply_flags(mask, values));
            }
            WorkerMsg::SetPositions { moves, resp } => {
                let _ = resp.send(backend.set_positions(&moves));
            }
            WorkerMsg::ListMonitors { resp } => {
                let _ = resp.send(backend.list_monitors());
            }
            WorkerMsg::DesktopInfo { resp } => {
                run_sync_command(SyncCommand::DesktopInfo(resp), &mut *backend);
            }
            WorkerMsg::RenderOverlaySnapshot {
                width_px,
                height_px,
                dpi_scale,
                positions,
                render_options,
                resp,
            } => {
                let _ = resp.send(backend.render_overlay_snapshot(
                    width_px,
                    height_px,
                    dpi_scale,
                    &positions,
                    render_options,
                ));
            }
            WorkerMsg::StartAnimation {
                specs,
                options,
                observers,
                resp,
                preparation,
            } => {
                let handle_inner = HandleInner::new(tx.clone());
                // Install pre-observers atomically *before* handing the
                // handle back to the caller so the worker can fire events
                // through them from the very first tick.
                handle_inner.install_pre_observers(observers);
                let handle = AnimationHandle::from_inner(handle_inner.clone());
                let _ = resp.send(Ok(handle));
                if let WorkerFate::Shutdown =
                    run_animation(&mut *backend, &rx, specs, options, handle_inner, preparation)
                {
                    break;
                }
            }
            // Stray stop arriving while no animation is running: ignore.
            WorkerMsg::AnimStop { .. } => {}
        }
    }
}

/// Whether the worker should keep running after an animation ends or shut
/// down entirely (because a `Shutdown` message was seen mid-animation).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum WorkerFate {
    Continue,
    Shutdown,
}

// ---------------------------------------------------------------------------
// Per-icon animation state (worker-owned)
// ---------------------------------------------------------------------------

struct AnimState {
    id: IconId,
    origin: Point,
    current: Point,
    target: Point,
    duration: StdDuration,
    start_at: Instant,
    curve_x: crate::curve::Curve,
    curve_y: crate::curve::Curve,
    effect: Option<crate::Effect>,
    is_final: bool,
}

impl AnimState {
    fn position_at(&self, progress: f32) -> Point {
        crate::scene::position_at(self.origin, self.target, &self.curve_x, &self.curve_y, progress)
    }

    fn timeline_t(&self, position: f64, elapsed: f64) -> f32 {
        crate::scene::progress_at(self.duration.as_secs_f64(), position, elapsed)
    }

    fn t_at(&self, now: Instant) -> f32 {
        let dur_s = self.duration.as_secs_f32();
        if dur_s <= 0.0 {
            return 1.0;
        }
        let elapsed = now.saturating_duration_since(self.start_at).as_secs_f32();
        (elapsed / dur_s).clamp(0.0, 1.0)
    }

    fn snapshot(&self, now: Instant) -> IconAnimationState {
        IconAnimationState {
            id: self.id.clone(),
            origin: self.origin,
            current: self.current,
            target: self.target,
            t: if self.is_final { 1.0 } else { self.t_at(now) },
            is_final: self.is_final,
        }
    }
}

// ---------------------------------------------------------------------------
// Animation execution
// ---------------------------------------------------------------------------

fn run_animation(
    backend: &mut dyn DesktopBackend,
    rx: &Receiver<WorkerMsg>,
    mut specs: Vec<IconAnimationSpec>,
    options: AnimationOptions,
    handle: Arc<HandleInner>,
    preparation: Option<(Sender<()>, Receiver<StartMode>)>,
) -> WorkerFate {
    let mut fate = WorkerFate::Continue;
    let mut timeline = None;
    let mut timeline_ready = None;
    let span = info_span!("animation", icons = specs.len());
    let _guard = span.enter();

    // --- 1. Snapshot the desktop
    let icons = match backend.list_icons() {
        Ok(v) => v,
        Err(e) => {
            finalize_with_error(&handle, &e);
            return fate;
        }
    };
    if options.snap_to_grid && specs.iter().any(|spec| icons.iter().any(|icon| icon.id == spec.id)) {
        let resolution = backend.desktop_info(&icons).and_then(|desktop| {
            crate::resolve_grid_targets(&mut specs, &icons, &desktop)
        });
        if let Err(error) = resolution {
            finalize_with_error(&handle, &error);
            return fate;
        }
    }
    let mut by_id: HashMap<IconId, Point> =
        icons.into_iter().map(|s| (s.id, s.position)).collect();

    // --- 2. Build the active set + missing list
    let started_at = Instant::now();
    let mut active: Vec<AnimState> = Vec::new();
    let mut missing: Vec<IconId> = Vec::new();
    for spec in specs {
        if let Some(effect) = &spec.effect {
            if let Err(error) = effect.validate() {
                finalize_with_error(&handle, &error);
                return fate;
            }
        }
        match by_id.remove(&spec.id) {
            Some(origin) => match spec.duration.resolve(origin, spec.target) {
                Ok(dur) => active.push(AnimState {
                    id: spec.id,
                    origin,
                    current: origin,
                    target: spec.target,
                    duration: dur,
                    start_at: started_at,
                    curve_x: spec.curve_x,
                    curve_y: spec.curve_y,
                    effect: spec.effect,
                    is_final: false,
                }),
                Err(e) => {
                    finalize_with_error(&handle, &e);
                    return fate;
                }
            },
            None => missing.push(spec.id),
        }
    }

    let total_icons = active.len();
    let missing_count = missing.len();
    info!(
        animating = total_icons,
        missing = missing_count,
        "animation starting"
    );

    // --- 3. Publish initial state and fire on_start
    {
        let mut state = handle.lock_state();
        state.running = true;
        state.progress = 0.0;
        state.missing = missing;
        state.per_icon = active.iter().map(|a| a.snapshot(started_at)).collect();
        state.finish_reason = None;
    }
    // --- 5. Try to open an overlay session
    let mut plans: Vec<crate::IconRenderPlan> = active
        .iter()
        .map(|entry| {
            let mut plan = crate::IconRenderPlan::placeholder(entry.id.clone(), entry.origin, entry.target);
            plan.effect = entry.effect.clone();
            plan
        })
        .collect();

    let mut stationary: Vec<_> = by_id.into_iter().collect();
    stationary.sort_by(|left, right| left.0.as_str().cmp(right.0.as_str()));
    plans.extend(stationary.into_iter().map(|(id, position)| {
        let mut plan = crate::IconRenderPlan::placeholder(id, position, position);
        plan.stationary = true;
        plan
    }));

    let requires_overlay = preparation.is_some() || active.iter().any(|entry| entry.effect.is_some());
    if options.force_fallback && requires_overlay {
        finalize_with_error(&handle, &DesktopError::InvalidEffect("preparation and effects require an overlay".into()));
        return fate;
    }

    let overlay_open = if options.force_fallback {
        eprint_fallback_banner(
            "AnimationOptions::force_fallback is true — running without overlay",
        );
        false
    } else {
        match backend.begin_overlay_session(&plans, options.render_options) {
            Ok(()) => {
                debug!(plans = plans.len(), "overlay session opened");
                true
            }
            Err(DesktopError::OverlayUnavailable(reason)) if !requires_overlay => {
                eprint_fallback_banner(&format!(
                    "backend reported OverlayUnavailable: {reason}"
                ));
                false
            }
            Err(e) => {
                finalize_with_error(&handle, &e);
                return fate;
            }
        }
    };

    if let Some((ready, start)) = preparation {
        let _ = ready.send(());
        loop {
            crossbeam_channel::select! {
                recv(start) -> requested => {
                    match requested {
                        Ok(StartMode::Animation) => break,
                        Ok(StartMode::Timeline { runtime, ready }) => {
                            timeline = Some(runtime);
                            timeline_ready = Some(ready);
                            break;
                        }
                        _ => {}
                    }
                    backend.discard_overlay_session();
                    publish_finish_state(&handle, &active, FinishReason::Stopped(StopMode::LeaveInPlace), None, None);
                    return fate;
                }
                recv(rx) -> message => {
                    let control = message.map(|msg| handle_control(msg, &mut active, &handle, 0)).unwrap_or(Control::Shutdown);
                    match control {
                        Control::Continue => {},
                        Control::ControlSync(command) => run_sync_command(command, backend),
                        other => {
                            backend.discard_overlay_session();
                            publish_finish_state(&handle, &active, FinishReason::Stopped(StopMode::LeaveInPlace), None, None);
                            return if matches!(other, Control::Shutdown) { WorkerFate::Shutdown } else { fate };
                        }
                    }
                }
                default(StdDuration::from_millis(50)) => {
                    if let Err(error) = backend.validate_prepared_session() {
                        backend.discard_overlay_session();
                        finalize_with_error(&handle, &error);
                        return fate;
                    }
                }
            }
        }
    }

    if overlay_open {
        if let Err(error) = backend.validate_prepared_session() {
            backend.discard_overlay_session();
            finalize_with_error(&handle, &error);
            return fate;
        }
    }

    if let Some(op) = options.before_flags {
        let _ = apply_flag_op(backend, op);
    }

    if overlay_open {
        let initial: Vec<_> = active.iter().map(|entry| crate::IconFrame {
            id: entry.id.clone(), position: entry.origin, progress: 0.0, elapsed_seconds: 0.0,
        }).collect();
        if let Err(error) = backend.commit_visual_frame(&initial) {
            let cancelled = matches!(error, DesktopError::OverlayCancelled(_));
            let positions: Vec<_> = active.iter_mut().map(|entry| {
                if cancelled { entry.current = entry.target; entry.is_final = true; }
                (entry.id.clone(), entry.current)
            }).collect();
            let outcome = backend.finalize_overlay_session(&positions).ok();
            if let Some(op) = options.after_flags { let _ = apply_flag_op(backend, op); }
            let reason = if cancelled { FinishReason::Stopped(StopMode::TeleportToTarget) }
                else { FinishReason::Error(error.to_string()) };
            publish_finish_state(&handle, &active, reason, outcome, timeline.as_mut());
            return fate;
        }
    }
    let started_at = Instant::now();
    for entry in &mut active { entry.start_at = started_at; }
    if let Some(runtime) = &mut timeline {
        runtime.activate(active.iter().map(|entry| entry.duration.as_secs_f64()).fold(0.0, f64::max));
        runtime.publish(0.0, active.iter().map(|entry| {
            let mut snapshot = entry.snapshot(started_at);
            snapshot.t = 0.0;
            snapshot
        }).collect());
        if let Some(ready) = timeline_ready.take() { let _ = ready.send(()); }
    }
    for callback in &handle.observers_snapshot().on_start {
        callback(&StartContext { total_icons, missing_icons: missing_count, started_at });
    }

    // --- 6. Fallback path: no overlay, direct teleport
    
    if !overlay_open {
        let targets: Vec<(IconId, Point)> = active
            .iter()
            .map(|a| (a.id.clone(), a.target))
            .collect();

        // Apply targets and update the in-memory state so the finish
        // observers see the right per-icon state.
        let commit_result = backend.set_positions(&targets);

        if let Some(op) = options.after_flags {
            let _ = apply_flag_op(backend, op);
        }

        // Mark every remaining icon final.
        let vanished: Vec<IconId> = match commit_result {
            Ok(missing_now) => missing_now,
            Err(e) => {
                finalize_with_error(&handle, &e);
                return fate;
            }
        };
        if !vanished.is_empty() {
            warn!(
                unresolved = vanished.len(),
                "fallback commit could not resolve every icon"
            );
        }
        for a in active.iter_mut() {
            a.current = a.target;
            a.is_final = true;
        }

        // `moved_ids` stays empty: the fallback path issues no
        // confirmation poll, so nothing was confirmed by the Shell.
        let outcome = crate::FinalCommitOutcome {
            moved_ids: Vec::new(),
            missing_ids: vanished,
        };
        publish_finish_state(&handle, &active, FinishReason::Completed, Some(outcome), None);
        return fate;
    }

    // --- 7. Overlay tick loop.
    let period = options
        .tick_hz
        .map(|hz| StdDuration::from_secs_f64(1.0 / hz.max(1) as f64))
        .unwrap_or(DEFAULT_TICK);
    let tolerance = options.position_tolerance_px.unwrap_or(DEFAULT_TOLERANCE_PX);
    let tolerance_abs = tolerance.abs();

    let mut finish = FinishReason::Completed;
    let mut next_tick = Instant::now() + period;
    let mut finalized_count: usize = 0;
    let timeline_rx = timeline.as_ref().map(|runtime| runtime.rx.clone()).unwrap_or_else(crossbeam_channel::never);

    'outer: loop {
        let mut dirty = timeline.as_ref().is_none_or(|runtime| runtime.playing());
        // --- 7a. Drain queued control messages until the next tick.
        loop {
            let now = Instant::now();
            if now >= next_tick {
                break;
            }
            let message = crossbeam_channel::select! {
                recv(timeline_rx) -> request => {
                    if let Some(runtime) = &mut timeline {
                        let close = match request {
                            Ok(request) => runtime.control(request, Instant::now(), backend),
                            Err(_) => Some(TimelineCloseMode::RestoreOrigins),
                        };
                        if let Some(mode) = close {
                            for entry in &mut active {
                                entry.current = match mode {
                                    TimelineCloseMode::RestoreOrigins => entry.origin,
                                    TimelineCloseMode::LeaveInPlace => entry.current,
                                    TimelineCloseMode::TeleportToTarget => entry.target,
                                };
                            }
                            finish = FinishReason::Stopped(if mode == TimelineCloseMode::TeleportToTarget {
                                StopMode::TeleportToTarget
                            } else { StopMode::LeaveInPlace });
                            runtime.acknowledge();
                            break 'outer;
                        }
                        dirty = true;
                    }
                    break;
                }
                recv(rx) -> message => message.map_err(|_| RecvTimeoutError::Disconnected),
                default(next_tick.saturating_duration_since(now)) => Err(RecvTimeoutError::Timeout),
            };
            match message {
                Ok(msg) => match handle_control(msg, &mut active, &handle, tolerance_abs) {
                    Control::Continue => {}
                    Control::Stop(reason) => {
                        finish = reason;
                        break 'outer;
                    }
                    Control::Shutdown => {
                        if timeline.is_some() {
                            for entry in &mut active { entry.current = entry.origin; }
                        }
                        finish = FinishReason::Stopped(StopMode::LeaveInPlace);
                        fate = WorkerFate::Shutdown;
                        break 'outer;
                    }
                    Control::ControlSync(sync) => {
                        // Read-only commands are serviced here so they
                        // stay serial with respect to the tick loop.
                        // Writes are rejected in `handle_control` — see
                        // the `AnimationBusy` arms there.
                        run_sync_command(sync, backend);
                    }
                },
                Err(RecvTimeoutError::Timeout) => break,
                Err(RecvTimeoutError::Disconnected) => {
                    // All external senders and even the worker's own `tx`
                    // must be gone for this to happen — treat as shutdown.
                    finish = FinishReason::Stopped(StopMode::LeaveInPlace);
                    fate = WorkerFate::Shutdown;
                    break 'outer;
                }
            }
        }

        if active.is_empty() && timeline.is_none() {
            break 'outer;
        }

        // NOTE: `set_positions` is deliberately not called here. The
        // overlay is the source of visible truth for the entire
        // animation; `finalize_overlay_session` is what eventually
        // hands the final positions to Explorer.
        let now = Instant::now();
        let timeline_sample = timeline.as_mut().map(|runtime| runtime.sample(now));
        let mut newly_final: Vec<IconId> = Vec::new();
        let mut frame: Vec<(IconId, Point)> = Vec::with_capacity(active.len());

        for a in active.iter_mut() {
            if let Some((position, elapsed)) = timeline_sample {
                let progress = a.timeline_t(position, elapsed);
                a.current = if progress <= 0.0 { a.origin } else if progress >= 1.0 { a.target }
                    else { a.position_at(progress) };
                frame.push((a.id.clone(), a.current));
                continue;
            }
            if a.is_final {
                // Keep already-final icons at their target for the
                // remainder of the animation so overlay copies don't
                // vanish visually.
                frame.push((a.id.clone(), a.target));
                continue;
            }
            let t = a.t_at(now);
            let new_pos = a.position_at(t);

            let close_enough = (new_pos.x - a.target.x).abs() < tolerance_abs
                && (new_pos.y - a.target.y).abs() < tolerance_abs;

            if t >= 1.0 || (close_enough && a.effect.is_none()) {
                a.current = a.target;
                a.is_final = true;
                newly_final.push(a.id.clone());
                frame.push((a.id.clone(), a.target));
            } else {
                a.current = new_pos;
                frame.push((a.id.clone(), new_pos));
            }
        }

        if !frame.is_empty() || timeline.is_some() {
            let visual_frame: Vec<_> = active.iter().map(|entry| crate::IconFrame {
                id: entry.id.clone(), position: entry.current,
                progress: timeline_sample.map_or_else(
                    || if entry.is_final { 1.0 } else { entry.t_at(now) },
                    |(position, elapsed)| entry.timeline_t(position, elapsed)),
                elapsed_seconds: timeline_sample.map_or_else(
                    || now.saturating_duration_since(started_at).as_secs_f32(),
                    |(_, elapsed)| elapsed as f32),
            }).collect();
            let submitted = if dirty { backend.commit_visual_frame(&visual_frame) }
                else { backend.poll_overlay_session() };
            match submitted {
                Ok(()) => {}
                Err(DesktopError::OverlayCancelled(reason)) => {
                    // Graceful cancellation — the backend detected a
                    // shell disruption (Explorer restarted / display
                    // topology changed / DPI changed / shell view
                    // lost). Because the first-frame teleport
                    // already moved the real icons to their target
                    // positions, snapping in-memory state to targets
                    // and finishing with `Stopped(TeleportToTarget)`
                    // accurately reflects the visible outcome. The
                    // engine still runs `finalize_overlay_session`
                    // below to unhide the real icons and destroy
                    // the overlay window.
                    warn!(%reason, "overlay animation cancelled");
                    for a in active.iter_mut() {
                        if !a.is_final {
                            a.current = a.target;
                            a.is_final = true;
                        }
                    }
                    finish = FinishReason::Stopped(StopMode::TeleportToTarget);
                    break 'outer;
                }
                Err(e) => {
                    finish = FinishReason::Error(format!(
                        "commit_overlay_frame failed: {e}"
                    ));
                    break 'outer;
                }
            }
            if let (Some(runtime), Some((position, elapsed))) = (&mut timeline, timeline_sample) {
                runtime.capture_pending(backend, elapsed);
                runtime.publish(position, active.iter().zip(&visual_frame).map(|(entry, frame)| {
                    let mut snapshot = entry.snapshot(now);
                    snapshot.t = frame.progress;
                    snapshot
                }).collect());
            }
        }

        if let Some(runtime) = &timeline {
            next_tick = Instant::now() + if runtime.playing() { period } else { StdDuration::from_millis(50) };
            continue;
        }

        finalized_count += newly_final.len();

        // --- 7c. Publish snapshot and fire observers.
        let progress = mean_progress(&active, now);
        trace!(progress, newly_final = newly_final.len(), "tick");
        {
            let mut state = handle.lock_state();
            state.progress = progress;
            state.per_icon = active.iter().map(|a| a.snapshot(now)).collect();
        }

        let observers = handle.observers_snapshot();
        for id in &newly_final {
            for cb in &observers.on_icon_complete {
                cb(id);
            }
        }
        let elapsed = now.saturating_duration_since(started_at);
        let active_icons = active.iter().filter(|a| !a.is_final).count();
        for cb in &observers.on_tick {
            cb(&TickContext {
                elapsed,
                progress,
                active_icons,
                finalized_icons: finalized_count,
            });
        }

        // --- 7d. Termination check.
        if active.iter().all(|a| a.is_final) {
            break 'outer;
        }

        next_tick += period;
        if next_tick < Instant::now() { next_tick = Instant::now(); }
    }

    // --- 8. Finalize the overlay session with the true final positions
    let final_positions: Vec<(IconId, Point)> = active
        .iter()
        .map(|a| (a.id.clone(), a.current))
        .collect();

    let final_commit = match backend.finalize_overlay_session(&final_positions) {
        Ok(outcome) => {
            if !outcome.missing_ids.is_empty() {
                // The backend could not resolve these ids at commit
                // time, so they are NOT at their targets.
                warn!(
                    unresolved = outcome.missing_ids.len(),
                    "final commit could not resolve every icon; they were left \
                     wherever the Shell last had them"
                );
            }
            Some(outcome)
        }
        Err(e) => {
            // Finalization failure is bad — the overlay may still be up
            // and the real icons hidden. Surface it as an Error finish
            // reason, but still attempt to restore the flags below.
            if timeline.is_some() || matches!(finish, FinishReason::Completed) {
                finish = FinishReason::Error(format!(
                    "finalize_overlay_session failed: {e}"
                ));
            } else {
                warn!(
                    error = %e,
                    ?finish,
                    "finalize_overlay_session failed on top of an existing failure"
                );
            }
            None
        }
    };

    // --- 9. Apply after-flags
    if let Some(op) = options.after_flags {
        let _ = apply_flag_op(backend, op);
    }

    // --- 10. Publish finish state and notify waiters
    info!(?finish, ticks_finalized = finalized_count, "animation finished");
    publish_finish_state(&handle, &active, finish, final_commit, timeline.as_mut());

    fate
}

/// Publish the terminal state of an animation onto its handle and fire
/// the `on_finish` observers.
///
/// Ordering matters: (1) record `finish_reason` while `running ==
/// true` so callbacks that inspect the handle see the outcome, (2) fire
/// `on_finish`, and (3) *only then* flip `running` and notify waiters.
/// Doing (3) before (2) would let a caller blocked in `handle.wait()`
/// wake up before the observer callbacks have fired — see the
/// `observers_fire_in_expected_order` integration test.
fn publish_finish_state(
    handle: &Arc<HandleInner>,
    active: &[AnimState],
    finish: FinishReason,
    final_commit: Option<crate::FinalCommitOutcome>,
    timeline: Option<&mut crate::timeline::Runtime>,
) {
    {
        let mut state = handle.lock_state();
        state.progress = 1.0;
        state.finish_reason = Some(finish.clone());
        state.final_commit = final_commit;
        for entry in state.per_icon.iter_mut() {
            for a in active {
                if a.id == entry.id {
                    entry.current = a.current;
                    entry.is_final = a.is_final;
                    entry.t = if a.is_final { 1.0 } else { entry.t };
                    break;
                }
            }
        }
    }

    if let Some(runtime) = timeline { runtime.finish(); }

    let observers = handle.observers_snapshot();
    for cb in &observers.on_finish {
        cb(&finish);
    }

    {
        let mut state = handle.lock_state();
        state.running = false;
    }
    handle.finish_cv.notify_all();
}

/// Report why the engine is taking the fallback path.
///
/// `warn!` rather than `error!` because the animation still completes —
/// the icons just teleport instead of gliding.
fn eprint_fallback_banner(reason: &str) {
    warn!(
        reason,
        "overlay renderer unavailable — icons will teleport to their targets \
         without animation"
    );
}

// ---------------------------------------------------------------------------
// Control-message dispatch
// ---------------------------------------------------------------------------

enum Control {
    Continue,
    Stop(FinishReason),
    Shutdown,
    /// A **read-only** sync request (list_icons, get_flags,
    /// list_monitors, render_overlay_snapshot) arrived mid-animation.
    /// The engine services it out of band via [`run_sync_command`] so
    /// it stays serial with respect to the tick loop. Mutating
    /// requests never reach this variant.
    ControlSync(SyncCommand),
}

enum SyncCommand {
    ListIcons(Sender<Result<Vec<IconSnapshot>, DesktopError>>),
    GetFlags(Sender<Result<u32, DesktopError>>),
    ListMonitors(Sender<Result<Vec<crate::MonitorInfo>, DesktopError>>),
    DesktopInfo(Sender<Result<crate::DesktopInfo, DesktopError>>),
    RenderOverlaySnapshot {
        width_px: u32,
        height_px: u32,
        dpi_scale: f32,
        positions: Vec<(IconId, Point)>,
        render_options: crate::OverlayRenderOptions,
        resp: Sender<Result<crate::SnapshotFrame, DesktopError>>,
    },
}

fn run_sync_command(cmd: SyncCommand, backend: &mut dyn DesktopBackend) {
    match cmd {
        SyncCommand::ListIcons(resp) => {
            let _ = resp.send(backend.list_icons());
        }
        SyncCommand::GetFlags(resp) => {
            let _ = resp.send(backend.get_flags());
        }
        SyncCommand::ListMonitors(resp) => {
            let _ = resp.send(backend.list_monitors());
        }
        SyncCommand::DesktopInfo(resp) => {
            let result = backend.list_icons().and_then(|icons| backend.desktop_info(&icons));
            let _ = resp.send(result);
        }
        SyncCommand::RenderOverlaySnapshot {
            width_px,
            height_px,
            dpi_scale,
            positions,
            render_options,
            resp,
        } => {
            let _ = resp.send(backend.render_overlay_snapshot(
                width_px,
                height_px,
                dpi_scale,
                &positions,
                render_options,
            ));
        }
    }
}

fn handle_control(
    msg: WorkerMsg,
    active: &mut Vec<AnimState>,
    handle: &Arc<HandleInner>,
    tolerance_abs: i32,
) -> Control {
    // `tolerance_abs` is currently unused inside message handling but is
    // reserved for future control messages (e.g. re-tune tolerance mid-flight).
    let _ = tolerance_abs;

    match msg {
        WorkerMsg::PrepareScene { resp, .. } => {
            let _ = resp.send(Err(DesktopError::AnimationBusy));
            Control::Continue
        }
        WorkerMsg::AnimStop { mode, owner } => {
            if !owner.ptr_eq(&Arc::downgrade(handle)) { return Control::Continue; }
            if matches!(mode, StopMode::TeleportToTarget) {
                // Snap every remaining icon to its target in memory —
                // the overlay will pick this up on the next commit and
                // `finalize_overlay_session` will push it to the Shell.
                for a in active.iter_mut() {
                    if !a.is_final {
                        a.current = a.target;
                        a.is_final = true;
                    }
                }
            }
            Control::Stop(FinishReason::Stopped(mode))
        }
        WorkerMsg::Shutdown => Control::Shutdown,

        // Read-only commands arrive during an animation too — hand
        // them over via the ControlSync variant so the tick loop can
        // service them without duplicating handler logic.
        WorkerMsg::ListIcons { resp } => Control::ControlSync(SyncCommand::ListIcons(resp)),
        WorkerMsg::GetFlags { resp } => Control::ControlSync(SyncCommand::GetFlags(resp)),
        WorkerMsg::ListMonitors { resp } => {
            Control::ControlSync(SyncCommand::ListMonitors(resp))
        }
        WorkerMsg::DesktopInfo { resp } => Control::ControlSync(SyncCommand::DesktopInfo(resp)),

        // Shell writes are refused for the duration of the animation.
        // A mid-flight `apply_flags` can clear `FWF_HIDEICONS` and
        // un-hide the real icons behind the overlay; a mid-flight
        // `set_positions` moves icons the final commit then overwrites.
        // Callers bracket animations with `AnimationOptions::
        // before_flags` / `after_flags` instead.
        WorkerMsg::ApplyFlags { resp, .. } => {
            let _ = resp.send(Err(DesktopError::AnimationBusy));
            Control::Continue
        }
        WorkerMsg::SetPositions { resp, .. } => {
            let _ = resp.send(Err(DesktopError::AnimationBusy));
            Control::Continue
        }
        WorkerMsg::RenderOverlaySnapshot {
            width_px,
            height_px,
            dpi_scale,
            positions,
            render_options,
            resp,
        } => Control::ControlSync(SyncCommand::RenderOverlaySnapshot {
            width_px,
            height_px,
            dpi_scale,
            positions,
            render_options,
            resp,
        }),
        WorkerMsg::StartAnimation { resp, .. } => {
            let _ = resp.send(Err(DesktopError::AnimationBusy));
            Control::Continue
        }
    }
}

fn apply_flag_op(
    backend: &mut dyn DesktopBackend,
    op: FolderFlagOp,
) -> Result<(), DesktopError> {
    match op {
        FolderFlagOp::Set(flags) => backend.apply_flags(flags, 0xFFFF_FFFF),
        FolderFlagOp::Exactly(flags) => backend.apply_flags(build_true_mask(flags), flags),
    }
}

/// Reproduce the legacy `buildTrueMask` helper: given a set of bits, return
/// the smallest `2^n − 1` mask that covers them.
///
/// Used by the `Exactly` variant of [`FolderFlagOp`].
pub(crate) fn build_true_mask(flags: u32) -> u32 {
    if flags == 0 {
        return 0;
    }
    let highest = 31 - flags.leading_zeros();
    if highest == 31 {
        u32::MAX
    } else {
        (1u32 << (highest + 1)) - 1
    }
}

fn finalize_with_error(handle: &Arc<HandleInner>, err: &DesktopError) {
    let reason = FinishReason::Error(err.to_string());
    // Same ordering as the normal termination path: publish
    // `finish_reason`, fire `on_finish`, *then* flip `running` and
    // notify waiters. See the comment in `run_animation`'s section 7.
    {
        let mut state = handle.lock_state();
        state.finish_reason = Some(reason.clone());
    }
    let observers = handle.observers_snapshot();
    for cb in &observers.on_finish {
        cb(&reason);
    }
    {
        let mut state = handle.lock_state();
        state.running = false;
    }
    handle.finish_cv.notify_all();
}

fn mean_progress(active: &[AnimState], now: Instant) -> f32 {
    if active.is_empty() {
        return 1.0;
    }
    let sum: f32 = active
        .iter()
        .map(|a| if a.is_final { 1.0 } else { a.t_at(now) })
        .sum();
    sum / active.len() as f32
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn build_true_mask_matches_legacy() {
        assert_eq!(build_true_mask(0), 0);
        assert_eq!(build_true_mask(0b0001), 0b0001);
        assert_eq!(build_true_mask(0b0100), 0b0111);
        assert_eq!(build_true_mask(0b1010_0000), 0b1111_1111);
        assert_eq!(build_true_mask(1 << 31), u32::MAX);
    }

    #[test]
    fn mean_progress_empty_is_one() {
        assert_eq!(mean_progress(&[], Instant::now()), 1.0);
    }
}