| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use super::layout::PanelId; |
| 4 | |
| 5 | /// Tracks what each panel was last told about its active state, so a group |
| 6 | /// emits exactly one notification per real edge. |
| 7 | /// |
| 8 | /// This is the enforcement point for the `Panel::set_active` contract: called |
| 9 | /// with the frame-end net state when a panel becomes, or stops being, the |
| 10 | /// displayed tab of its group — exactly one notification per edge, delivered |
| 11 | /// on the next tick after the change, never same-value repeats nor |
| 12 | /// false-then-true flips within one frame. A panel removed from its group is |
| 13 | /// NOT told `false`; `Panel::on_removed` is the deactivation signal instead. A |
| 14 | /// hidden panel occupying the active slot still receives `true`, even though |
| 15 | /// rendering falls back to the first visible panel. |
| 16 | /// |
| 17 | /// `TabGroup` owns one of these, and is the only thing that does: the skin in |
| 18 | /// `crates/component` draws tab bars and keeps no active-state map of its own. |
| 19 | #[derive(Default)] |
| 20 | pub(crate) struct ActiveTracker { |
| 21 | notified: HashMap<PanelId, bool>, |
| 22 | sync_scheduled: bool, |
| 23 | } |
| 24 | |
| 25 | impl ActiveTracker { |
| 26 | /// Compare the group's current membership and displayed panel against |
| 27 | /// what each panel was last told, and return only the real edges, |
| 28 | /// deactivations before the activation. |
| 29 | /// |
| 30 | /// `displayed` names at most one panel, so at most one panel can newly |
| 31 | /// become active per call. Collecting deactivations separately from the |
| 32 | /// single possible activation (rather than sorting the combined list) |
| 33 | /// keeps "deactivations are delivered first" a structural property of |
| 34 | /// this function instead of an incidental property of a sort key, and is |
| 35 | /// what guarantees no panel ever observes two panels active in its group |
| 36 | /// at the same time. |
| 37 | /// |
| 38 | /// `displayed`, when `Some`, is expected to name a member of `panels`. |
| 39 | /// This is not a hard invariant enforced here — a `displayed` id absent |
| 40 | /// from `panels` is simply never matched by the `should_be_active` check |
| 41 | /// below, so it degrades gracefully to "no panel in this group is |
| 42 | /// displayed" rather than panicking. Callers should not rely on that |
| 43 | /// degradation as a feature; it exists so a caller computing `displayed` |
| 44 | /// from slightly stale state doesn't crash the tracker. |
| 45 | pub(crate) fn reconcile( |
| 46 | &mut self, |
| 47 | panels: &[PanelId], |
| 48 | displayed: Option<PanelId>, |
| 49 | ) -> Vec<(PanelId, bool)> { |
| 50 | // A panel that left the group is forgotten, not deactivated. |
| 51 | self.notified.retain(|panel, _| panels.contains(panel)); |
| 52 | |
| 53 | let mut deactivations = Vec::new(); |
| 54 | let mut activation = None; |
| 55 | |
| 56 | for panel in panels { |
| 57 | let should_be_active = displayed == Some(*panel); |
| 58 | let was_active = self.notified.get(panel).copied(); |
| 59 | if was_active == Some(should_be_active) { |
| 60 | continue; |
| 61 | } |
| 62 | if !should_be_active && was_active.is_none() { |
| 63 | // Never announce `false` to a panel that was never told `true`. |
| 64 | self.notified.insert(*panel, false); |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | self.notified.insert(*panel, should_be_active); |
| 69 | if should_be_active { |
| 70 | debug_assert!( |
| 71 | activation.is_none(), |
| 72 | "`displayed` names at most one panel, so at most one activation per reconcile" |
| 73 | ); |
| 74 | activation = Some((*panel, true)); |
| 75 | } else { |
| 76 | deactivations.push((*panel, false)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | deactivations.extend(activation); |
| 81 | deactivations |
| 82 | } |
| 83 | |
| 84 | /// Record what a panel was last told, without emitting anything. Used |
| 85 | /// when a panel moves between groups while displayed, so the move does |
| 86 | /// not look like an edge to the destination group. |
| 87 | pub(crate) fn seed(&mut self, panel: PanelId, active: bool) { |
| 88 | self.notified.insert(panel, active); |
| 89 | } |
| 90 | |
| 91 | /// What this tracker last told `panel`, if anything. A pure read: it does |
| 92 | /// not remove or otherwise change the entry. This is how a caller reads a |
| 93 | /// departing panel's state *before* pruning it (e.g. before removing the |
| 94 | /// panel from the group that owns this tracker), so the value can be |
| 95 | /// handed to [`Self::seed`] on the destination tracker when the panel |
| 96 | /// moves to another group — the source tracker's own next `reconcile` |
| 97 | /// prunes the entry on its own, so this type does not also need a |
| 98 | /// remove-and-return accessor. |
| 99 | pub(crate) fn last_notified(&self, panel: PanelId) -> Option<bool> { |
| 100 | self.notified.get(&panel).copied() |
| 101 | } |
| 102 | |
| 103 | #[cfg(test)] |
| 104 | pub(crate) fn tracks(&self, panel: PanelId) -> bool { |
| 105 | self.notified.contains_key(&panel) |
| 106 | } |
| 107 | |
| 108 | /// Returns whether a sync needs scheduling; the caller defers the actual |
| 109 | /// `reconcile` to frame end so within-frame churn nets out to one |
| 110 | /// delivery per edge. |
| 111 | pub(crate) fn schedule_sync(&mut self) -> bool { |
| 112 | let needed = !self.sync_scheduled; |
| 113 | self.sync_scheduled = true; |
| 114 | needed |
| 115 | } |
| 116 | |
| 117 | /// Marks the deferred sync as delivered, so a future change schedules a |
| 118 | /// new one. Callers should invoke this alongside `reconcile` (typically |
| 119 | /// just before it) when the scheduled sync runs. |
| 120 | pub(crate) fn sync_finished(&mut self) { |
| 121 | self.sync_scheduled = false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #[cfg(test)] |
| 126 | mod tests { |
| 127 | use super::*; |
| 128 | |
| 129 | fn panel(n: u64) -> PanelId { |
| 130 | PanelId::from_u64(n) |
| 131 | } |
| 132 | |
| 133 | #[test] |
| 134 | fn the_first_reconcile_announces_the_displayed_panel_only() { |
| 135 | let mut tracker = ActiveTracker::default(); |
| 136 | let changes = tracker.reconcile(&[panel(1), panel(2)], Some(panel(1))); |
| 137 | |
| 138 | assert_eq!(changes, vec![(panel(1), true)]); |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn switching_reports_the_old_panel_false_then_the_new_panel_true() { |
| 143 | let mut tracker = ActiveTracker::default(); |
| 144 | tracker.reconcile(&[panel(1), panel(2)], Some(panel(1))); |
| 145 | |
| 146 | let changes = tracker.reconcile(&[panel(1), panel(2)], Some(panel(2))); |
| 147 | |
| 148 | assert_eq!(changes, vec![(panel(1), false), (panel(2), true)]); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn reselecting_the_displayed_panel_is_silent() { |
| 153 | let mut tracker = ActiveTracker::default(); |
| 154 | tracker.reconcile(&[panel(1)], Some(panel(1))); |
| 155 | |
| 156 | assert!(tracker.reconcile(&[panel(1)], Some(panel(1))).is_empty()); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn a_removed_panel_is_forgotten_rather_than_told_false() { |
| 161 | let mut tracker = ActiveTracker::default(); |
| 162 | tracker.reconcile(&[panel(1), panel(2)], Some(panel(1))); |
| 163 | |
| 164 | let changes = tracker.reconcile(&[panel(2)], Some(panel(2))); |
| 165 | |
| 166 | assert_eq!( |
| 167 | changes, |
| 168 | vec![(panel(2), true)], |
| 169 | "panel 1 left the group; on_removed is its deactivation signal" |
| 170 | ); |
| 171 | assert!(!tracker.tracks(panel(1))); |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn seeding_a_moved_panel_prevents_a_spurious_reactivation() { |
| 176 | let mut tracker = ActiveTracker::default(); |
| 177 | tracker.seed(panel(1), true); |
| 178 | |
| 179 | assert!( |
| 180 | tracker.reconcile(&[panel(1)], Some(panel(1))).is_empty(), |
| 181 | "a panel dragged in while displayed must not be told true twice" |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn no_panel_displayed_deactivates_the_previous_one() { |
| 187 | let mut tracker = ActiveTracker::default(); |
| 188 | tracker.reconcile(&[panel(1)], Some(panel(1))); |
| 189 | |
| 190 | let changes = tracker.reconcile(&[panel(1)], None); |
| 191 | |
| 192 | assert_eq!(changes, vec![(panel(1), false)]); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn last_notified_reads_a_panels_state_for_handoff_to_another_tracker() { |
| 197 | // Mirrors `TabPanel::detach_panel` reading the outgoing panel's |
| 198 | // notified state, and `on_drop` handing it to the destination |
| 199 | // group's tracker via `seed`, when a panel is dragged between tab |
| 200 | // groups while displayed. |
| 201 | let mut source = ActiveTracker::default(); |
| 202 | source.reconcile(&[panel(1), panel(2)], Some(panel(1))); |
| 203 | |
| 204 | assert_eq!(source.last_notified(panel(1)), Some(true)); |
| 205 | assert_eq!(source.last_notified(panel(2)), Some(false)); |
| 206 | assert_eq!( |
| 207 | source.last_notified(panel(3)), |
| 208 | None, |
| 209 | "a panel never reconciled has no recorded state" |
| 210 | ); |
| 211 | |
| 212 | let mut destination = ActiveTracker::default(); |
| 213 | destination.seed(panel(1), source.last_notified(panel(1)).unwrap()); |
| 214 | |
| 215 | assert!( |
| 216 | destination |
| 217 | .reconcile(&[panel(1)], Some(panel(1))) |
| 218 | .is_empty(), |
| 219 | "seeded from the source's last_notified value, the move must not look like a fresh edge" |
| 220 | ); |
| 221 | } |
| 222 | } |