bruce-cesium 7.8.1 → 7.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,6 +44,10 @@ var EntityLabel;
44
44
  const ICON_STACK_SCALE = 0.6;
45
45
  // How far an angled leader runs sideways before the label starts.
46
46
  const ANGLED_LEADER_RUN = 28;
47
+ // While labels move on screen, clusters are worked out afresh at most this often, and again once they settle.
48
+ const CLUSTER_MOVING_INTERVAL = 400;
49
+ // Milliseconds of each frame a fresh clustering may take; the rest of it runs on the next frames.
50
+ const CLUSTER_STEP_BUDGET = 4;
47
51
  // How far a pin's white body reaches past the icon it wraps, before its tip.
48
52
  const PIN_RIM = 7;
49
53
  // A pin is drawn in one fixed look for now: a white body with a faint edge and a soft shadow.
@@ -366,6 +370,8 @@ var EntityLabel;
366
370
  */
367
371
  class PlacementMemory {
368
372
  constructor() {
373
+ // The last clusters worked out from scratch, by label id, for frames that may reuse them.
374
+ this.clusters = null;
369
375
  this.previous = new Map();
370
376
  this.nextOrder = 0;
371
377
  }
@@ -487,6 +493,16 @@ var EntityLabel;
487
493
  function pinLook(look) {
488
494
  return look.iconBackground ? look : { ...look, iconBackground: PIN_COLOR };
489
495
  }
496
+ /*
497
+ * Whether a label draws a pin: it needs an icon to wrap, and a photo keeps its own shape.
498
+ */
499
+ function HasPin(options) {
500
+ return ((options === null || options === void 0 ? void 0 : options.leaderStyle) === "pin" &&
501
+ options.leader !== false &&
502
+ (options.presentation || "text") !== "text" &&
503
+ !options.iconPhoto);
504
+ }
505
+ EntityLabel.HasPin = HasPin;
490
506
  function iconLook(options) {
491
507
  return {
492
508
  icon: options.icon || undefined,
@@ -509,20 +525,80 @@ var EntityLabel;
509
525
  presentation === "text" ? null : iconLook(options)
510
526
  ]);
511
527
  }
528
+ function summariseCluster(cluster) {
529
+ var _a, _b, _c;
530
+ let chosen = cluster.representative;
531
+ const iconic = (chosen.options.presentation || "text") !== "text";
532
+ // Members in the order they would win representation; the seed is the nearest.
533
+ let ordered = [chosen, ...cluster.members.filter(member => member !== chosen)];
534
+ if (iconic && chosen.options.cluster.representative !== "nearest") {
535
+ ordered = ordered.map((member, i) => ({ member, i, priority: EntityLabel.IconPriority(member.options) }))
536
+ .sort((a, b) => b.priority - a.priority || a.i - b.i).map(entry => entry.member);
537
+ chosen = ordered[0];
538
+ }
539
+ const summary = {
540
+ chosen: chosen.id,
541
+ members: cluster.members.map(frame => frame.id),
542
+ entityIds: Array.from(new Set(cluster.members.map(frame => frame.entityId).filter(Boolean)))
543
+ };
544
+ if (iconic && chosen.options.iconScale !== 0) {
545
+ const seen = new Set();
546
+ const icons = [];
547
+ for (const member of ordered) {
548
+ const look = iconLook(member.options);
549
+ const key = JSON.stringify(look);
550
+ if (seen.has(key)) {
551
+ continue;
552
+ }
553
+ seen.add(key);
554
+ if (member !== ordered[0])
555
+ icons.push(look);
556
+ if (seen.size >= ICON_STACK_LIMIT) {
557
+ break;
558
+ }
559
+ }
560
+ if (icons.length)
561
+ summary.icons = icons;
562
+ }
563
+ if (chosen.options.presentation !== "icon") {
564
+ const values = new Set();
565
+ const stableMembers = [...cluster.members].sort((a, b) => (a.entityKey || a.entityId || a.id).localeCompare(b.entityKey || b.entityId || b.id));
566
+ for (const member of stableMembers) {
567
+ const value = ((_c = (_a = member.text) !== null && _a !== void 0 ? _a : (_b = member.options) === null || _b === void 0 ? void 0 : _b.text) !== null && _c !== void 0 ? _c : "").replace(/\s+/g, " ").trim();
568
+ if (value) {
569
+ values.add(value);
570
+ }
571
+ }
572
+ if (values.size > 1) {
573
+ // A value long enough to wrap fills the box on its own, so only one is listed.
574
+ const wrapped = stableMembers.some(member => { var _a, _b; return member.textWrapped && ((_b = (member.text || ((_a = member.options) === null || _a === void 0 ? void 0 : _a.text))) === null || _b === void 0 ? void 0 : _b.trim()); });
575
+ const limit = wrapped ? 1 : 2;
576
+ const rows = Array.from(values).slice(0, limit);
577
+ if (values.size > limit)
578
+ rows.push(`+${values.size - limit} more…`);
579
+ summary.text = rows.join("\n");
580
+ }
581
+ }
582
+ return summary;
583
+ }
512
584
  /*
513
- * Resolves clustering and stable text placement before depth fading; highlighted labels receive priority.
585
+ * Frames in the order they win: priority first, then nearest, with every coordinate known.
514
586
  */
515
- function Layout(input, memory = new PlacementMemory()) {
516
- var _a, _b, _c, _d, _e, _f, _g;
517
- const frames = input.filter(frame => [frame.x, frame.y, frame.width, frame.height, frame.depth].every(Number.isFinite))
587
+ function sortFrames(input) {
588
+ return input.filter(frame => [frame.x, frame.y, frame.width, frame.height, frame.depth].every(Number.isFinite))
518
589
  .sort((a, b) => (b.priority || 0) - (a.priority || 0) || a.depth - b.depth || a.id.localeCompare(b.id));
519
- const result = new Map();
590
+ }
591
+ /*
592
+ * The frames clustering considers, by presentation, and the ones hidden as another label of the same entity.
593
+ */
594
+ function clusterGroups(frames) {
595
+ var _a, _b;
520
596
  const entityKeys = new Set();
521
597
  const groups = new Map();
598
+ const duplicates = new Set();
522
599
  for (const frame of frames) {
523
- const duplicate = frame.entityKey && entityKeys.has(frame.entityKey);
524
- result.set(frame.id, { frame, visible: !duplicate, opacity: frame.opacity, zIndex: 0, members: [], entityIds: frame.entityId ? [frame.entityId] : [] });
525
- if (duplicate) {
600
+ if (frame.entityKey && entityKeys.has(frame.entityKey)) {
601
+ duplicates.add(frame.id);
526
602
  continue;
527
603
  }
528
604
  if (frame.entityKey)
@@ -537,6 +613,13 @@ var EntityLabel;
537
613
  }
538
614
  groups.get(key).push(frame);
539
615
  }
616
+ return { groups, duplicates };
617
+ }
618
+ /*
619
+ * Works clusters out from scratch, as steps a caller can spread over frames or run straight through.
620
+ */
621
+ function* clusterInSteps(groups, every = 32) {
622
+ const clustered = [];
540
623
  for (const group of groups.values()) {
541
624
  // Summary and badge dimensions are output, never the next frame's membership input.
542
625
  const candidates = group.map(frame => frame.extendedGeometry && frame.canFlip &&
@@ -550,70 +633,76 @@ var EntityLabel;
550
633
  rank.set(frame.id, i);
551
634
  looks.set(frame.id, clusterLook(frame.options));
552
635
  });
553
- const clusters = (0, point_clustering_core_1.ClusterItems)({
636
+ const clusters = yield* (0, point_clustering_core_1.ClusterItemsInSteps)({
554
637
  items: candidates,
555
638
  id: frame => frame.id,
556
639
  minSize: group[0].options.cluster.minSize,
557
- neighbours: frame => {
640
+ neighbours: (frame, free) => {
558
641
  var _a;
559
- const nearby = index.Query(frame, (_a = frame.options.cluster.pixelRange) !== null && _a !== void 0 ? _a : (frame.options.presentation === "icon" ? 24 : 8)).sort((a, b) => rank.get(a.id) - rank.get(b.id));
560
- return compatibleMembers(frame, nearby, other => other.group || "", other => looks.get(other.id));
642
+ // Labels an earlier cluster took cannot join, so they are dropped before the pairwise checks.
643
+ const nearby = index.Query(frame, (_a = frame.options.cluster.pixelRange) !== null && _a !== void 0 ? _a : (frame.options.presentation === "icon" ? 24 : 8))
644
+ .filter(other => other === frame || free(other))
645
+ .sort((a, b) => rank.get(a.id) - rank.get(b.id));
646
+ // A pin stands for one thing, so it clusters only with pins that look the same.
647
+ const group = (other) => HasPin(other.options) ? `look:${looks.get(other.id)}` : other.group || "";
648
+ return compatibleMembers(frame, nearby, group, other => looks.get(other.id));
649
+ }
650
+ }, every);
651
+ clusters.forEach(cluster => clustered.push({ representative: cluster.representative.id, members: cluster.members.map(member => member.id) }));
652
+ }
653
+ return clustered;
654
+ }
655
+ /*
656
+ * Runs a clustering job to the end at once.
657
+ */
658
+ function finishClustering(steps) {
659
+ let next = steps.next();
660
+ while (!next.done) {
661
+ next = steps.next();
662
+ }
663
+ return next.value;
664
+ }
665
+ function Layout(input, memory = new PlacementMemory(), reuseClusters = false) {
666
+ var _a, _b, _c;
667
+ const frames = sortFrames(input);
668
+ const { groups, duplicates } = clusterGroups(frames);
669
+ const result = new Map();
670
+ for (const frame of frames) {
671
+ result.set(frame.id, { frame, visible: !duplicates.has(frame.id), opacity: frame.opacity, zIndex: 0, members: [], entityIds: frame.entityId ? [frame.entityId] : [] });
672
+ }
673
+ if (!reuseClusters || !memory.clusters) {
674
+ memory.clusters = finishClustering(clusterInSteps(groups, Infinity));
675
+ }
676
+ for (const group of groups.values()) {
677
+ // The kept membership, less anything gone, hovered or moved out of the group since.
678
+ const byId = new Map(group.map(frame => [frame.id, frame]));
679
+ const minSize = Math.max(2, (_a = group[0].options.cluster.minSize) !== null && _a !== void 0 ? _a : 2);
680
+ const clusters = memory.clusters.map(entry => {
681
+ var _a;
682
+ const members = entry.members.map(id => byId.get(id)).filter(Boolean);
683
+ if (members.length < minSize) {
684
+ return null;
561
685
  }
562
- });
686
+ return { entry, whole: members.length === entry.members.length, representative: (_a = byId.get(entry.representative)) !== null && _a !== void 0 ? _a : members[0], members };
687
+ }).filter(Boolean);
563
688
  for (const cluster of clusters) {
564
- let chosen = cluster.representative;
565
- const iconic = (chosen.options.presentation || "text") !== "text";
566
- // Members in the order they would win representation; the seed is the nearest.
567
- let ordered = [chosen, ...cluster.members.filter(member => member !== chosen)];
568
- if (iconic && chosen.options.cluster.representative !== "nearest") {
569
- ordered = ordered.map((member, i) => ({ member, i, priority: EntityLabel.IconPriority(member.options) }))
570
- .sort((a, b) => b.priority - a.priority || a.i - b.i).map(entry => entry.member);
571
- chosen = ordered[0];
572
- }
573
- const representative = result.get(chosen.id);
574
- representative.members = cluster.members.map(frame => frame.id);
575
- if (iconic && chosen.options.iconScale !== 0) {
576
- const seen = new Set();
577
- const icons = [];
578
- for (const member of ordered) {
579
- const look = iconLook(member.options);
580
- const key = JSON.stringify(look);
581
- if (seen.has(key)) {
582
- continue;
583
- }
584
- seen.add(key);
585
- if (member !== ordered[0])
586
- icons.push(look);
587
- if (seen.size >= ICON_STACK_LIMIT) {
588
- break;
589
- }
590
- }
591
- if (icons.length)
592
- representative.icons = icons;
593
- }
594
- representative.entityIds = Array.from(new Set(cluster.members.map(frame => frame.entityId).filter(Boolean)));
595
- if (chosen.options.presentation !== "icon") {
596
- const values = new Set();
597
- const stableMembers = [...cluster.members].sort((a, b) => (a.entityKey || a.entityId || a.id).localeCompare(b.entityKey || b.entityId || b.id));
598
- for (const member of stableMembers) {
599
- const value = ((_e = (_c = member.text) !== null && _c !== void 0 ? _c : (_d = member.options) === null || _d === void 0 ? void 0 : _d.text) !== null && _e !== void 0 ? _e : "").replace(/\s+/g, " ").trim();
600
- if (value) {
601
- values.add(value);
602
- }
603
- }
604
- if (values.size > 1) {
605
- // A value long enough to wrap fills the box on its own, so only one is listed.
606
- const wrapped = stableMembers.some(member => { var _a, _b; return member.textWrapped && ((_b = (member.text || ((_a = member.options) === null || _a === void 0 ? void 0 : _a.text))) === null || _b === void 0 ? void 0 : _b.trim()); });
607
- const limit = wrapped ? 1 : 2;
608
- const rows = Array.from(values).slice(0, limit);
609
- if (values.size > limit)
610
- rows.push(`+${values.size - limit} more…`);
611
- representative.text = rows.join("\n");
612
- }
613
- }
614
- for (const frame of cluster.members) {
615
- if (frame !== chosen)
616
- result.get(frame.id).visible = false;
689
+ // A cluster that kept every member keeps its summary, worked out once, sparing a pass over its members.
690
+ let summary = cluster.whole ? cluster.entry.summary : undefined;
691
+ if (!summary) {
692
+ summary = summariseCluster(cluster);
693
+ if (cluster.whole)
694
+ cluster.entry.summary = summary;
695
+ }
696
+ const representative = result.get(summary.chosen);
697
+ representative.members = summary.members;
698
+ if (summary.icons)
699
+ representative.icons = summary.icons;
700
+ representative.entityIds = summary.entityIds;
701
+ if (summary.text != null)
702
+ representative.text = summary.text;
703
+ for (const id of summary.members) {
704
+ if (id !== summary.chosen)
705
+ result.get(id).visible = false;
617
706
  }
618
707
  }
619
708
  }
@@ -632,7 +721,7 @@ var EntityLabel;
632
721
  const box = { ...frame, x: frame.x + .01, y: frame.y + .01, width: Math.max(0, frame.width - .02), height: Math.max(0, frame.height - .02) };
633
722
  const overlap = foreground.HasOverlap(box) || foregroundLeaders.HasOverlap(box) || Boolean(stem && foreground.HasOverlap(stem));
634
723
  if (overlap) {
635
- placement.opacity *= Math.max(0, Math.min(1, (_g = (_f = frame.options) === null || _f === void 0 ? void 0 : _f.overlapOpacity) !== null && _g !== void 0 ? _g : 0.25));
724
+ placement.opacity *= Math.max(0, Math.min(1, (_c = (_b = frame.options) === null || _b === void 0 ? void 0 : _b.overlapOpacity) !== null && _c !== void 0 ? _c : 0.25));
636
725
  }
637
726
  ;
638
727
  placement.zIndex = zIndex--;
@@ -653,21 +742,23 @@ var EntityLabel;
653
742
  const accepted = [seed];
654
743
  const byGroup = new Map([[group(seed), 1]]);
655
744
  const byLook = new Map([[look(seed), 1]]);
656
- const byBoth = new Map([[JSON.stringify([group(seed), look(seed)]), 1]]);
745
+ // Joined with a character no group id or look holds, not serialised, since this runs per neighbour.
746
+ const both = (itemGroup, itemLook) => `${itemGroup}\u0000${itemLook}`;
747
+ const byBoth = new Map([[both(group(seed), look(seed)), 1]]);
657
748
  for (const item of nearby) {
658
749
  if (item === seed) {
659
750
  continue;
660
751
  }
661
- const itemGroup = group(item), itemLook = look(item), both = JSON.stringify([itemGroup, itemLook]);
752
+ const itemGroup = group(item), itemLook = look(item), key = both(itemGroup, itemLook);
662
753
  // Accepted members in another group with another look are the ones this item may not sit beside.
663
- const conflicting = accepted.length - (byGroup.get(itemGroup) || 0) - (byLook.get(itemLook) || 0) + (byBoth.get(both) || 0);
754
+ const conflicting = accepted.length - (byGroup.get(itemGroup) || 0) - (byLook.get(itemLook) || 0) + (byBoth.get(key) || 0);
664
755
  if (conflicting > 0) {
665
756
  continue;
666
757
  }
667
758
  accepted.push(item);
668
759
  byGroup.set(itemGroup, (byGroup.get(itemGroup) || 0) + 1);
669
760
  byLook.set(itemLook, (byLook.get(itemLook) || 0) + 1);
670
- byBoth.set(both, (byBoth.get(both) || 0) + 1);
761
+ byBoth.set(key, (byBoth.get(key) || 0) + 1);
671
762
  }
672
763
  return accepted;
673
764
  }
@@ -698,10 +789,20 @@ var EntityLabel;
698
789
  this.viewer = viewer;
699
790
  this.element = document.createElement("div");
700
791
  this.coordinates = { x: 0, y: 0, scaleX: 1, scaleY: 1 };
792
+ // The canvas size, read once a frame: reading the canvas after placing a label lays the page out again.
793
+ this.viewport = { width: 0, height: 0 };
701
794
  this.participants = new Set();
702
795
  // Participants whose failure has been logged, so a label that throws every frame logs once.
703
796
  this.failed = new WeakSet();
704
797
  this.placementMemory = new PlacementMemory();
798
+ // What the clusters were last worked out from: the labels taking part and where they sat, and when.
799
+ this.clustered = null;
800
+ // Labels added or removed, which can change any cluster's look or membership.
801
+ this.clustersDirty = true;
802
+ // A fresh clustering spread over frames, while the last one stays on show.
803
+ this.clusterJob = null;
804
+ // Labels hidden in a cluster last frame, with the frame they were laid out from, for reused clusters to skip.
805
+ this.sleeping = new Map();
705
806
  this.modelBoxes = new ModelBoxes();
706
807
  this.forwardingWheel = false;
707
808
  this.pendingDrag = null;
@@ -878,9 +979,12 @@ var EntityLabel;
878
979
  }
879
980
  Add(participant) {
880
981
  this.participants.add(participant);
982
+ this.clustersDirty = true;
881
983
  this.viewer.scene.requestRender();
882
984
  return () => {
883
985
  this.participants.delete(participant);
986
+ this.sleeping.delete(participant);
987
+ this.clustersDirty = true;
884
988
  if (!this.participants.size) {
885
989
  this.remove();
886
990
  this.element.removeEventListener("wheel", this.ForwardWheel);
@@ -931,8 +1035,9 @@ var EntityLabel;
931
1035
  }
932
1036
  }
933
1037
  Update() {
934
- var _a, _b;
1038
+ var _a, _b, _c;
935
1039
  this.coordinates = this.MeasureCoordinates();
1040
+ this.viewport = { width: this.viewer.canvas.clientWidth, height: this.viewer.canvas.clientHeight };
936
1041
  this.UpdateHorizon();
937
1042
  const rect = (_b = (_a = this.viewer.canvas).getBoundingClientRect) === null || _b === void 0 ? void 0 : _b.call(_a);
938
1043
  const pointer = this.pointerClient;
@@ -948,21 +1053,77 @@ var EntityLabel;
948
1053
  console.error("EntityLabel: model boxes failed to resolve.", e);
949
1054
  }
950
1055
  // This runs inside Cesium's frame, where one throwing label would stop rendering for the whole scene.
1056
+ const project = (participant) => {
1057
+ try {
1058
+ return participant.project();
1059
+ }
1060
+ catch (e) {
1061
+ this.logFailure(participant, e);
1062
+ return null;
1063
+ }
1064
+ };
1065
+ // A label hidden in a cluster needs no position while its cluster is reused, which is most frames.
1066
+ const mayReuse = Boolean(this.clustered) && !this.clustersDirty && Date.now() - this.clustered.at < CLUSTER_MOVING_INTERVAL;
951
1067
  const projected = new Map();
952
1068
  for (const participant of this.participants) {
953
- let frame = null;
1069
+ const asleep = mayReuse && !((_c = participant.wake) === null || _c === void 0 ? void 0 : _c.call(participant)) ? this.sleeping.get(participant) : undefined;
1070
+ projected.set(participant, asleep !== null && asleep !== void 0 ? asleep : project(participant));
1071
+ }
1072
+ let frames = Array.from(projected.values()).filter(Boolean);
1073
+ const decision = this.DecideClusters(frames);
1074
+ if (decision !== "reuse" && mayReuse) {
1075
+ // Clusters are worked out from where every label is now, sleepers included.
1076
+ for (const participant of this.participants) {
1077
+ if (this.sleeping.has(participant))
1078
+ projected.set(participant, project(participant));
1079
+ }
1080
+ frames = Array.from(projected.values()).filter(Boolean);
1081
+ }
1082
+ if (decision === "refresh") {
1083
+ this.clusterJob = clusterInSteps(clusterGroups(sortFrames(frames)).groups);
1084
+ }
1085
+ this.StepClusters();
1086
+ const layout = Layout(frames, this.placementMemory, decision !== "now");
1087
+ const placements = new Map(layout.map(item => [item.frame.id, item]));
1088
+ const participantsById = new Map();
1089
+ for (const [participant, frame] of projected) {
1090
+ if (frame)
1091
+ participantsById.set(frame.id, participant);
1092
+ }
1093
+ this.sleeping = new Map();
1094
+ for (const placement of layout) {
1095
+ for (const id of placement.members) {
1096
+ const participant = id !== placement.frame.id ? participantsById.get(id) : undefined;
1097
+ if (participant)
1098
+ this.sleeping.set(participant, projected.get(participant));
1099
+ }
1100
+ }
1101
+ // Writes, then reads, then writes, each across all labels; interleaved, each read lays them out again.
1102
+ const measures = [];
1103
+ for (const [participant, frame] of projected) {
954
1104
  try {
955
- frame = participant.project();
1105
+ const measure = participant.apply(frame ? placements.get(frame.id) : undefined);
1106
+ if (measure)
1107
+ measures.push([participant, measure]);
956
1108
  }
957
1109
  catch (e) {
958
1110
  this.logFailure(participant, e);
959
1111
  }
960
- projected.set(participant, frame);
961
1112
  }
962
- const placements = new Map(Layout(Array.from(projected.values()).filter(Boolean), this.placementMemory).map(item => [item.frame.id, item]));
963
- for (const [participant, frame] of projected) {
1113
+ const places = [];
1114
+ for (const [participant, measure] of measures) {
1115
+ try {
1116
+ const place = measure();
1117
+ if (place)
1118
+ places.push([participant, place]);
1119
+ }
1120
+ catch (e) {
1121
+ this.logFailure(participant, e);
1122
+ }
1123
+ }
1124
+ for (const [participant, place] of places) {
964
1125
  try {
965
- participant.apply(frame ? placements.get(frame.id) : undefined);
1126
+ place();
966
1127
  }
967
1128
  catch (e) {
968
1129
  this.logFailure(participant, e);
@@ -975,6 +1136,55 @@ var EntityLabel;
975
1136
  console.error("EntityLabel: model boxes failed to draw.", e);
976
1137
  }
977
1138
  }
1139
+ /*
1140
+ * How this frame gets its clusters: kept from last time, refreshed by a job over frames, or worked out now.
1141
+ * They are kept while no clustering label has moved, and while labels move (camera or entities) until a moment
1142
+ * old or while a refresh is under way. Labels coming or going, or none clustered yet, work them out now.
1143
+ */
1144
+ DecideClusters(frames) {
1145
+ // Where each clustering label sits, to a few pixels, so drawing alone never counts as movement.
1146
+ const signature = frames
1147
+ .filter(frame => { var _a, _b; return ((_b = (_a = frame.options) === null || _a === void 0 ? void 0 : _a.cluster) === null || _b === void 0 ? void 0 : _b.enabled) && !(frame.priority > 0); })
1148
+ .map(frame => `${frame.id}@${Math.round(frame.x / 4)},${Math.round(frame.y / 4)}`)
1149
+ .join("|");
1150
+ const now = Date.now();
1151
+ if (!this.clustered || this.clustersDirty) {
1152
+ this.clusterJob = null;
1153
+ this.clustered = { signature, at: now };
1154
+ this.clustersDirty = false;
1155
+ return "now";
1156
+ }
1157
+ if (this.clustered.signature === signature) {
1158
+ return "reuse";
1159
+ }
1160
+ if (this.clusterJob || now - this.clustered.at < CLUSTER_MOVING_INTERVAL) {
1161
+ // Another frame is due either way, so the scene gets its fresh clusters once it settles.
1162
+ this.viewer.scene.requestRender();
1163
+ return "reuse";
1164
+ }
1165
+ this.clustered = { signature, at: now };
1166
+ return "refresh";
1167
+ }
1168
+ /*
1169
+ * Advances a spread clustering for this frame's share of time, handing its result over once it finishes.
1170
+ */
1171
+ StepClusters() {
1172
+ if (!this.clusterJob) {
1173
+ return;
1174
+ }
1175
+ const deadline = performance.now() + CLUSTER_STEP_BUDGET;
1176
+ let next = this.clusterJob.next();
1177
+ while (!next.done && performance.now() < deadline) {
1178
+ next = this.clusterJob.next();
1179
+ }
1180
+ if (next.done) {
1181
+ this.placementMemory.clusters = next.value;
1182
+ this.clusterJob = null;
1183
+ }
1184
+ else {
1185
+ this.viewer.scene.requestRender();
1186
+ }
1187
+ }
978
1188
  logFailure(participant, error) {
979
1189
  if (this.failed.has(participant)) {
980
1190
  return;
@@ -2279,8 +2489,7 @@ var EntityLabel;
2279
2489
  target.addEventListener(type, event => event.stopPropagation());
2280
2490
  }
2281
2491
  };
2282
- // A pin needs an icon to wrap; a photo keeps its own shape, and plain text keeps a straight stem.
2283
- const pin = this.options.leaderStyle === "pin" && look.leader !== false && presentation !== "text" && !look.iconPhoto;
2492
+ const pin = HasPin(look);
2284
2493
  const normalLeader = (_m = look.leader) !== null && _m !== void 0 ? _m : (presentation === "text" || pin);
2285
2494
  const angled = this.options.leaderStyle === "angled";
2286
2495
  let leader = normalLeader;
@@ -2341,11 +2550,9 @@ var EntityLabel;
2341
2550
  makeClickable(iconStack);
2342
2551
  iconContainer.append(iconStack, icon);
2343
2552
  if (pin) {
2344
- // The shadow sits on an unturned holder so it falls the same way whichever way the tip points.
2345
2553
  pinHolder = document.createElement("span");
2346
2554
  pinHolder.className = "entity-label-pin";
2347
- pinHolder.style.cssText = "position:absolute;left:50%;top:50%;z-index:-1;pointer-events:none;" +
2348
- "filter:drop-shadow(1px 2px 2px rgba(0, 0, 0, 0.35));";
2555
+ pinHolder.style.cssText = "position:absolute;left:50%;top:50%;z-index:-1;pointer-events:none;";
2349
2556
  pinBody = document.createElement("span");
2350
2557
  pinBody.style.cssText = `position:absolute;inset:0;background:${PIN_COLOR};border:1px solid rgba(0, 0, 0, 0.14);` +
2351
2558
  "box-sizing:border-box;border-radius:50% 50% 0 50%;";
@@ -2427,7 +2634,10 @@ var EntityLabel;
2427
2634
  border-radius: 9px;
2428
2635
  color: #ffffff;
2429
2636
  text-shadow: 0 0 5px rgba(0, 0, 0, 0.8);
2637
+ will-change: transform;
2430
2638
  `);
2639
+ // Each label, leader and ring gets its own compositor layer: in one shared layer, moving any of them
2640
+ // repainted all of them, shadows included, on every camera move.
2431
2641
  this.label = ele;
2432
2642
  ele.style.width = "max-content";
2433
2643
  layer.element.appendChild(ele);
@@ -2446,6 +2656,7 @@ var EntityLabel;
2446
2656
  width: 2px;
2447
2657
  background: rgba(255, 255, 255, 0.8);
2448
2658
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
2659
+ will-change: transform;
2449
2660
  `);
2450
2661
  this.line = lineEle;
2451
2662
  lineEle.className = "entity-label-leader";
@@ -2463,6 +2674,7 @@ var EntityLabel;
2463
2674
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
2464
2675
  top: -2px;
2465
2676
  left: -2px;
2677
+ will-change: transform;
2466
2678
  `);
2467
2679
  lineEleNode.className = "entity-label-anchor";
2468
2680
  // SVG preserves fractional strokes in scaled containers where CSS borders round to pixels.
@@ -2495,13 +2707,13 @@ var EntityLabel;
2495
2707
  }
2496
2708
  const fitLeader = (anchorX, anchorY, desired, width, height, radius = 0) => {
2497
2709
  const above = Math.max(0, anchorY - height);
2498
- const below = Math.max(0, this.viewer.canvas.clientHeight - anchorY - height);
2710
+ const below = Math.max(0, layer.viewport.height - anchorY - height);
2499
2711
  // Keep a short visible stem before switching sides; never aim it at the cursor or a box corner.
2500
2712
  const upwards = above >= Math.max(radius ? radius + 4 : 0, Math.min(8, desired)) || above >= below;
2501
2713
  const length = Math.min(desired, upwards ? above : below);
2502
2714
  const endY = anchorY + (upwards ? -length : length);
2503
2715
  return {
2504
- x: Math.max(0, Math.min(anchorX, this.viewer.canvas.clientWidth - width)),
2716
+ x: Math.max(0, Math.min(anchorX, layer.viewport.width - width)),
2505
2717
  y: upwards ? endY - height : endY, endY
2506
2718
  };
2507
2719
  };
@@ -2510,17 +2722,17 @@ var EntityLabel;
2510
2722
  * so the stem rises to the label's middle and then runs out to whichever side has the room.
2511
2723
  */
2512
2724
  const fitAngledLeader = (anchorX, anchorY, desired, width, height, radius = 0) => {
2513
- const canvas = this.viewer.canvas;
2725
+ const canvas = layer.viewport;
2514
2726
  const above = Math.max(0, anchorY - height / 2);
2515
- const below = Math.max(0, canvas.clientHeight - anchorY - height / 2);
2727
+ const below = Math.max(0, canvas.height - anchorY - height / 2);
2516
2728
  const upwards = above >= Math.max(radius ? radius + 4 : 0, Math.min(8, desired)) || above >= below;
2517
2729
  const length = Math.min(desired, upwards ? above : below);
2518
2730
  const endY = anchorY + (upwards ? -length : length);
2519
2731
  const run = Math.max(ANGLED_LEADER_RUN, radius + 4);
2520
- const rightwards = anchorX + run + width <= canvas.clientWidth || anchorX - run - width < 0;
2732
+ const rightwards = anchorX + run + width <= canvas.width || anchorX - run - width < 0;
2521
2733
  return {
2522
- x: Math.max(0, Math.min(rightwards ? anchorX + run : anchorX - run - width, canvas.clientWidth - width)),
2523
- y: Math.max(0, Math.min(endY - height / 2, canvas.clientHeight - height)), endY,
2734
+ x: Math.max(0, Math.min(rightwards ? anchorX + run : anchorX - run - width, canvas.width - width)),
2735
+ y: Math.max(0, Math.min(endY - height / 2, canvas.height - height)), endY,
2524
2736
  run, side: (rightwards ? "right" : "left")
2525
2737
  };
2526
2738
  };
@@ -2544,13 +2756,13 @@ var EntityLabel;
2544
2756
  * Off to the side it is only clamped back on screen, and its tip turns to keep pointing at the anchor.
2545
2757
  */
2546
2758
  const fitPin = (anchorX, anchorY, stem, width, height, shape) => {
2547
- const canvas = this.viewer.canvas;
2759
+ const canvas = layer.viewport;
2548
2760
  const reach = stem + shape.tip;
2549
2761
  const above = anchorY - reach - shape.y;
2550
2762
  const below = anchorY + reach - shape.y;
2551
- const upwards = above >= 0 || (below + height > canvas.clientHeight && anchorY >= canvas.clientHeight - anchorY);
2763
+ const upwards = above >= 0 || (below + height > canvas.height && anchorY >= canvas.height - anchorY);
2552
2764
  return {
2553
- x: Math.max(0, Math.min(anchorX - shape.x, canvas.clientWidth - width)),
2765
+ x: Math.max(0, Math.min(anchorX - shape.x, canvas.width - width)),
2554
2766
  y: upwards ? above : below
2555
2767
  };
2556
2768
  };
@@ -2668,6 +2880,43 @@ var EntityLabel;
2668
2880
  this.viewer.scene.requestRender();
2669
2881
  }).finally(settle);
2670
2882
  }
2883
+ /*
2884
+ * The label's size when last laid out. A label hidden in a cluster leaves layout altogether, so a
2885
+ * frame reads its size from here; a shown one refreshes it when it changes, a new one on its first frame.
2886
+ */
2887
+ let size = null;
2888
+ const measureSize = () => {
2889
+ size = {
2890
+ width: ele.offsetWidth,
2891
+ height: ele.offsetHeight,
2892
+ textWidth: (sourceText === null || sourceText === void 0 ? void 0 : sourceText.offsetWidth) || 0,
2893
+ textHeight: (sourceText === null || sourceText === void 0 ? void 0 : sourceText.offsetHeight) || 0,
2894
+ iconWidth: (iconElement === null || iconElement === void 0 ? void 0 : iconElement.offsetWidth) || 0,
2895
+ iconHeight: (iconElement === null || iconElement === void 0 ? void 0 : iconElement.offsetHeight) || 0,
2896
+ naturalTextWidth: (sourceNaturalText === null || sourceNaturalText === void 0 ? void 0 : sourceNaturalText.offsetWidth) || 0,
2897
+ pin: pin ? measurePin() : undefined
2898
+ };
2899
+ };
2900
+ // Whether the label was out of layout last frame, so staying hidden writes nothing.
2901
+ let hiddenLast = false;
2902
+ // Set by this frame's writes that change the label's size, so it is measured before it is placed.
2903
+ let sizeStale = true;
2904
+ // Set when the size changed on its own; a shown label reads it at the start of the next frame, while layout
2905
+ // is still clean, rather than forcing a layout mid-frame.
2906
+ let resizedSince = false;
2907
+ // The cluster badge's own size beside its icon, read with the label's.
2908
+ let badge = null;
2909
+ // Images, fonts and photo transitions change the size outside any write here. Without an observer the size
2910
+ // is read every frame instead.
2911
+ const resized = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(() => {
2912
+ if (this.disposed || this.counter !== counter) {
2913
+ resized.disconnect();
2914
+ return;
2915
+ }
2916
+ resizedSince = true;
2917
+ this.viewer.scene.requestRender();
2918
+ });
2919
+ resized === null || resized === void 0 ? void 0 : resized.observe(ele);
2671
2920
  let cursorMotion = this.cursorMotion;
2672
2921
  const project = () => {
2673
2922
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
@@ -2679,6 +2928,11 @@ var EntityLabel;
2679
2928
  (this.hoverFade.from === 0 || Date.now() - this.hoverFade.start >= 120)) {
2680
2929
  return null;
2681
2930
  }
2931
+ // A new label is still in layout, so its first frame reads its size, as does a shown one that resized.
2932
+ if (!size || (resizedSince && !hiddenLast)) {
2933
+ measureSize();
2934
+ resizedSince = false;
2935
+ }
2682
2936
  // Moving point/model positions must be sampled at the current clock time.
2683
2937
  if (!this.prePositioned && visual instanceof Cesium.Entity && visual.position) {
2684
2938
  const position = getValue(this.viewer, visual.position);
@@ -2726,8 +2980,8 @@ var EntityLabel;
2726
2980
  y: ringPoint.y + (boxTarget.y - ringPoint.y) * boxBlend
2727
2981
  } : undefined;
2728
2982
  const start = boxAnchor || anchor;
2729
- const onScreen = anchor.x >= 0 && anchor.y >= 0 && anchor.x < this.viewer.canvas.clientWidth && anchor.y < this.viewer.canvas.clientHeight;
2730
- if (leader && (start.x < 0 || start.y < 0 || start.x >= this.viewer.canvas.clientWidth || start.y >= this.viewer.canvas.clientHeight))
2983
+ const onScreen = anchor.x >= 0 && anchor.y >= 0 && anchor.x < layer.viewport.width && anchor.y < layer.viewport.height;
2984
+ if (leader && (start.x < 0 || start.y < 0 || start.x >= layer.viewport.width || start.y >= layer.viewport.height))
2731
2985
  return null;
2732
2986
  if (onScreen && !this.imageAsked && this.groupId && !HasIcon(this.options) && !this.options.photo) {
2733
2987
  this.imageAsked = true;
@@ -2774,8 +3028,8 @@ var EntityLabel;
2774
3028
  if (cursorAmount > 0 && !pinned) {
2775
3029
  // A leader-less label starts centred on its anchor; preserve that box as it lifts away.
2776
3030
  if (!normalLeader) {
2777
- x -= ele.offsetWidth / layer.coordinates.scaleX / 2;
2778
- y += ele.offsetHeight / layer.coordinates.scaleY / 2;
3031
+ x -= size.width / layer.coordinates.scaleX / 2;
3032
+ y += size.height / layer.coordinates.scaleY / 2;
2779
3033
  }
2780
3034
  x += (cursorMotion.position.x + pixelOffset.x - x) * cursorAmount;
2781
3035
  y += (cursorMotion.position.y + pixelOffset.y - y) * cursorAmount;
@@ -2826,17 +3080,16 @@ var EntityLabel;
2826
3080
  x = hover.x + pixelOffset.x;
2827
3081
  y = hover.y + pixelOffset.y - 24;
2828
3082
  }
2829
- // Hidden participants remain measurable; visibility never removes their layout boxes.
2830
3083
  const coordinates = layer.coordinates;
2831
- const width = ele.offsetWidth / coordinates.scaleX;
2832
- const height = ele.offsetHeight / coordinates.scaleY;
3084
+ const width = size.width / coordinates.scaleX;
3085
+ const height = size.height / coordinates.scaleY;
2833
3086
  let clusterBounds;
2834
3087
  if ((_u = this.options.cluster) === null || _u === void 0 ? void 0 : _u.enabled) {
2835
- const textWidth = (sourceText === null || sourceText === void 0 ? void 0 : sourceText.offsetWidth) || 0;
2836
- const textHeight = (sourceText === null || sourceText === void 0 ? void 0 : sourceText.offsetHeight) || 0;
3088
+ const textWidth = size.textWidth;
3089
+ const textHeight = size.textHeight;
2837
3090
  // A photo is never part of the footprint, so its arrival cannot change which cluster a label joins.
2838
- const iconWidth = look.iconPhoto ? 0 : (iconElement === null || iconElement === void 0 ? void 0 : iconElement.offsetWidth) || 0;
2839
- const iconHeight = look.iconPhoto ? 0 : (iconElement === null || iconElement === void 0 ? void 0 : iconElement.offsetHeight) || 0;
3091
+ const iconWidth = look.iconPhoto ? 0 : size.iconWidth;
3092
+ const iconHeight = look.iconPhoto ? 0 : size.iconHeight;
2840
3093
  const gap = sourceText && iconWidth ? 6 : 0;
2841
3094
  const above = presentation === "icon-text-above";
2842
3095
  // Match the fixed content gap and label padding, excluding the count badge.
@@ -2855,7 +3108,7 @@ var EntityLabel;
2855
3108
  let leaderRun;
2856
3109
  let leaderSide;
2857
3110
  if (leader && pin)
2858
- ({ x, y } = fitPin(anchorX, anchorY, leaderLength, width, height, measurePin()));
3111
+ ({ x, y } = fitPin(anchorX, anchorY, leaderLength, width, height, size.pin));
2859
3112
  else if (leader && angled) {
2860
3113
  const fit = fitAngledLeader(anchorX, anchorY, leaderLength, width, height);
2861
3114
  ({ x, y } = fit);
@@ -2864,17 +3117,17 @@ var EntityLabel;
2864
3117
  else if (leader)
2865
3118
  ({ x, y } = fitLeader(anchorX, anchorY, leaderLength, width, height));
2866
3119
  const bounds = clusterBounds || { x, y, width, height };
2867
- if (!leader && (bounds.x + bounds.width < 0 || bounds.y + bounds.height < 0 || bounds.x > this.viewer.canvas.clientWidth || bounds.y > this.viewer.canvas.clientHeight)) {
3120
+ if (!leader && (bounds.x + bounds.width < 0 || bounds.y + bounds.height < 0 || bounds.x > layer.viewport.width || bounds.y > layer.viewport.height)) {
2868
3121
  return null;
2869
3122
  }
2870
3123
  const state = (_v = this.visualRegister) === null || _v === void 0 ? void 0 : _v.GetState({ entityId: (_w = this.rego) === null || _w === void 0 ? void 0 : _w.entityId, menuItemId: (_x = this.rego) === null || _x === void 0 ? void 0 : _x.menuItemId });
2871
3124
  const opacity = this.prePositioned || hoverOnly || MAX_DISTANCE === Infinity ? 1 : 1 - distance / MAX_DISTANCE;
2872
3125
  return {
2873
3126
  canFlip: leader && presentation !== "icon" && !pin, hasLeader: leader,
2874
- textWrapped: ((sourceNaturalText === null || sourceNaturalText === void 0 ? void 0 : sourceNaturalText.offsetWidth) || 0) > TEXT_MAX_WIDTH,
3127
+ textWrapped: size.naturalTextWidth > TEXT_MAX_WIDTH,
2875
3128
  transient: this.Transient || Boolean(hover) || cursorAmount > 0,
2876
3129
  extendedGeometry: visual instanceof Cesium.Entity && Boolean(visual.polygon || visual.polyline || visual.corridor),
2877
- viewportWidth: this.viewer.canvas.clientWidth,
3130
+ viewportWidth: layer.viewport.width,
2878
3131
  id: this.id, entityKey: ((_y = this.rego) === null || _y === void 0 ? void 0 : _y.entityId) ? JSON.stringify([this.rego.entityId, this.rego.menuItemId || "", this.Transient && !this.Persistent ? "hover" : ""]) : undefined,
2879
3132
  entityId: (_z = this.rego) === null || _z === void 0 ? void 0 : _z.entityId, group: this.groupId, x, y, width, height, anchorX, anchorY, depth, leaderLength, leaderRun, leaderSide, distance,
2880
3133
  iconRadius: icon === null || icon === void 0 ? void 0 : icon.radius, modelBox: boxed || undefined, boxBlend, follows: !pinned && Boolean(transientPivot || cursorMotion.target),
@@ -2885,10 +3138,19 @@ var EntityLabel;
2885
3138
  let displacement = { x: 0, y: 0 };
2886
3139
  let nudge = { x: 0, y: 0, fromX: 0, fromY: 0, targetX: 0, targetY: 0, start: 0 };
2887
3140
  const apply = (placement) => {
2888
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5;
3141
+ var _a, _b, _c, _d, _e;
2889
3142
  const show = (placement === null || placement === void 0 ? void 0 : placement.visible) && placement.opacity > 0;
3143
+ if (!show && hiddenLast) {
3144
+ return;
3145
+ }
3146
+ if (show && hiddenLast) {
3147
+ sizeStale = true;
3148
+ }
3149
+ hiddenLast = !show;
2890
3150
  shownOnce = shownOnce || Boolean(show);
2891
3151
  clickEntityIds = show ? placement.entityIds : [];
3152
+ // Out of layout while hidden, so thousands of clustered labels cost the browser nothing to lay out.
3153
+ ele.style.display = show ? "flex" : "none";
2892
3154
  ele.style.visibility = show ? "visible" : "hidden";
2893
3155
  lineEle.style.display = show && leader ? "block" : "none";
2894
3156
  if (runEle)
@@ -2927,6 +3189,7 @@ var EntityLabel;
2927
3189
  const displayText = (_c = placement.text) !== null && _c !== void 0 ? _c : originalText;
2928
3190
  if (textElement && textElement.textContent !== displayText) {
2929
3191
  textElement.textContent = displayText;
3192
+ sizeStale = true;
2930
3193
  // Measure the summary's footprint next frame without replacing each member's source text.
2931
3194
  this.viewer.scene.requestRender();
2932
3195
  }
@@ -2941,10 +3204,10 @@ var EntityLabel;
2941
3204
  const count = placement.members.length ? String(placement.members.length) : "";
2942
3205
  if (countElement.textContent !== count) {
2943
3206
  countElement.textContent = count;
3207
+ sizeStale = true;
2944
3208
  // The badge participates in layout, so its new width needs another measurement.
2945
3209
  this.viewer.scene.requestRender();
2946
3210
  }
2947
- fitIconBackground === null || fitIconBackground === void 0 ? void 0 : fitIconBackground();
2948
3211
  const stack = JSON.stringify(placement.icons || []);
2949
3212
  if (iconStack && stack !== displayedStack) {
2950
3213
  displayedStack = stack;
@@ -2960,6 +3223,7 @@ var EntityLabel;
2960
3223
  built[0].icon.style.gridColumn = "1 / span 2";
2961
3224
  }
2962
3225
  iconStack.replaceChildren(...built.map(({ icon }) => icon));
3226
+ sizeStale = true;
2963
3227
  iconStack.style.display = built.length ? "inline-grid" : "none";
2964
3228
  // The glyph stays measurable, since cluster membership is taken from its footprint.
2965
3229
  iconElement.style.visibility = built.length ? "hidden" : "";
@@ -2968,256 +3232,302 @@ var EntityLabel;
2968
3232
  // The stack widens the label, which the next frame has to measure.
2969
3233
  this.viewer.scene.requestRender();
2970
3234
  }
2971
- for (const fit of stackedFits)
2972
- fit();
2973
- if (badgeOnIcon) {
2974
- const badgeWidth = countElement.offsetWidth;
2975
- const iconWidth = iconElement.offsetWidth;
2976
- const narrow = iconWidth < iconElement.offsetHeight * 0.6;
2977
- const clearance = badgeWidth ? (narrow ? badgeWidth / 2 + 2 : Math.max(0, badgeWidth / 2 - iconWidth / 4)) : 0;
2978
- countElement.style.left = `calc(100% + ${clearance}px)`;
2979
- // Reserve the badge overhang so adjacent text and viewport edges remain clear.
2980
- const overhang = countElement.offsetHeight / 2;
2981
- iconContainer.style.marginTop = badgeBelow ? "0px" : `${overhang}px`;
2982
- iconContainer.style.marginBottom = badgeBelow ? `${overhang}px` : "0px";
2983
- iconContainer.style.marginRight = `${badgeWidth / 2 + clearance}px`;
2984
- }
2985
- // Summary and badge changes must fit on their first visible frame, including after resize.
2986
- const ring = this.options.anchorRing;
2987
- const pointer = (layer.pointer || (cursorMotion.target ? cursorMotion.position : undefined) || (this.Transient && !this.Persistent ?
2988
- (this.hoverFade.to === 0 ? this.retainedHoverPosition : (_f = this.visualRegister) === null || _f === void 0 ? void 0 : _f.GetLabelHoverPosition((_g = this.rego) === null || _g === void 0 ? void 0 : _g.entityId, (_h = this.rego) === null || _h === void 0 ? void 0 : _h.menuItemId)) : undefined));
2989
- const iconRing = frame.iconRadius ? Math.max(ICON_RING_MIN, frame.iconRadius + ICON_RING_PADDING) : 0;
2990
- const boxed = leader ? frame.modelBox : undefined;
2991
- const hovered = pointer && (boxed ? hullContains(boxed.hull, pointer) :
2992
- Math.hypot(pointer.x - frame.anchorX, pointer.y - frame.anchorY) <= Math.max((_j = ring === null || ring === void 0 ? void 0 : ring.hoverDistance) !== null && _j !== void 0 ? _j : 24, iconRing));
2993
- if (look.iconPhoto && growPhoto) {
2994
- // A rectangle leans on the leader's side of the label.
2995
- if (photoShape === "rect") {
2996
- const align = !leader ? "center" : placement.textSide === "left" ? "flex-end" : "flex-start";
2997
- if (content.style.alignItems !== align) {
2998
- content.style.alignItems = align;
2999
- }
3235
+ /*
3236
+ * Everything above writes the label's content. The layer runs what follows in two passes over all
3237
+ * its labels, measuring every one before placing any, so no label reads layout another just changed.
3238
+ */
3239
+ return () => {
3240
+ if (sizeStale || !resized) {
3241
+ fitIconBackground === null || fitIconBackground === void 0 ? void 0 : fitIconBackground();
3242
+ for (const fit of stackedFits)
3243
+ fit();
3244
+ badge = badgeOnIcon ? {
3245
+ width: countElement.offsetWidth,
3246
+ height: countElement.offsetHeight,
3247
+ iconWidth: iconElement.offsetWidth,
3248
+ iconHeight: iconElement.offsetHeight
3249
+ } : null;
3250
+ measureSize();
3251
+ sizeStale = false;
3000
3252
  }
3001
- // Grown in layout rather than scaled, so the border keeps its width, and the label, whose bottom
3002
- // sits on the leader, grows up and sideways without changing the leader's length.
3003
- growPhoto(Boolean(hovered) && !((_k = placement.members) === null || _k === void 0 ? void 0 : _k.length));
3004
- }
3005
- const normalRadius = (!leader || (ring === null || ring === void 0 ? void 0 : ring.enabled) === false ? 0 : Math.max(frame.distance <= ((_l = ring === null || ring === void 0 ? void 0 : ring.nearDistance) !== null && _l !== void 0 ? _l : 100) ? ((_m = ring === null || ring === void 0 ? void 0 : ring.nearRadius) !== null && _m !== void 0 ? _m : 10) : 0, hovered ? ((_o = ring === null || ring === void 0 ? void 0 : ring.hoverRadius) !== null && _o !== void 0 ? _o : (this.Transient && !this.Persistent ? 15.4 : 22)) : 0));
3006
- const targetRadius = normalRadius + (15.4 - normalRadius) * cursorMotion.amount;
3007
- // Around a glyph the ring keeps the glyph's size and only fades its stroke in, so it never covers the icon.
3008
- // A ring around the cursor marks nothing, so a riding label keeps only its leader.
3009
- const targetOuter = frame.follows ? 0 : iconRing || targetRadius || 2.5;
3010
- const targetInner = frame.follows ? 0 : iconRing ? (targetRadius > 0 ? iconRing - 2 : iconRing) : Math.max(0, targetRadius - 2);
3011
- const now = Date.now();
3012
- const duration = (_p = ring === null || ring === void 0 ? void 0 : ring.transitionDuration) !== null && _p !== void 0 ? _p : 200;
3013
- const sharedRing = this.Transient && !this.Persistent && this.hoverFade.to === 1 ? this.hoverRingState : undefined;
3014
- let ringAnimation = sharedRing ? sharedRing.animation : this.ringAnimation;
3015
- if (!ringAnimation) {
3016
- const outer = frame.follows ? 0 : iconRing || 2.5;
3017
- const inner = frame.follows ? 0 : iconRing;
3018
- ringAnimation = { target: outer, targetInner: inner, start: now, outer, inner, fromOuter: outer, fromInner: inner };
3019
- }
3020
- ;
3021
- const animateRing = () => {
3022
- const progress = duration > 0 ? Math.min(1, Math.max(0, (now - ringAnimation.start) / duration)) : 1;
3023
- const eased = progress * progress * (3 - 2 * progress);
3024
- ringAnimation.outer = ringAnimation.fromOuter + (ringAnimation.target - ringAnimation.fromOuter) * eased;
3025
- ringAnimation.inner = ringAnimation.fromInner + (ringAnimation.targetInner - ringAnimation.fromInner) * eased;
3026
- return progress;
3027
- };
3028
- animateRing();
3029
- if (ringAnimation.target !== targetOuter || ringAnimation.targetInner !== targetInner) {
3030
- ringAnimation = {
3031
- ...ringAnimation,
3032
- target: targetOuter,
3033
- targetInner,
3034
- start: now,
3035
- fromOuter: ringAnimation.outer,
3036
- fromInner: ringAnimation.inner
3037
- };
3038
- }
3039
- // Animate the hole and stem together; no CSS transition may lag behind a moving world anchor.
3040
- if (animateRing() < 1 && (ringAnimation.fromOuter !== targetOuter || ringAnimation.fromInner !== targetInner)) {
3041
- this.viewer.scene.requestRender();
3042
- }
3043
- this.ringAnimation = ringAnimation;
3044
- if (sharedRing) {
3045
- sharedRing.animation = ringAnimation;
3046
- }
3047
- const ringShare = 1 - (frame.boxBlend || 0);
3048
- const radius = ringShare > 0 && ringAnimation.inner > 0 ? ringAnimation.outer * ringShare : 0;
3049
- const pinShape = leader && pin ? measurePin() : undefined;
3050
- const stem = (pinShape ? fitPin(frame.anchorX, frame.anchorY, frame.leaderLength, ele.offsetWidth / coordinates.scaleX, ele.offsetHeight / coordinates.scaleY, pinShape) :
3051
- leader ? (angled ? fitAngledLeader : fitLeader)(frame.anchorX, frame.anchorY, Math.max(frame.leaderLength, radius ? radius + 4 : 0), ele.offsetWidth / coordinates.scaleX, ele.offsetHeight / coordinates.scaleY, radius) : undefined);
3052
- const position = { x: (stem || frame).x, y: (stem || frame).y };
3053
- const width = ele.offsetWidth / coordinates.scaleX;
3054
- const height = ele.offsetHeight / coordinates.scaleY;
3055
- if (leader && angled) {
3056
- // The declutter owns the side; the fit only says which one had the room to begin with.
3057
- const run = Math.max(ANGLED_LEADER_RUN, radius + 4);
3058
- const side = (_r = (_q = placement.textSide) !== null && _q !== void 0 ? _q : frame.leaderSide) !== null && _r !== void 0 ? _r : "right";
3059
- const beside = side === "left" ? frame.anchorX - run - width : frame.anchorX + run;
3060
- position.x = Math.max(0, Math.min(beside, this.viewer.canvas.clientWidth - width));
3061
- }
3062
- else if (leader && !pin && placement.textSide === "left") {
3063
- position.x = Math.max(0, Math.min(frame.anchorX - width, this.viewer.canvas.clientWidth - width));
3064
- }
3065
- const gap = (_t = (_s = this.options.pointerAvoidance) === null || _s === void 0 ? void 0 : _s.gap) !== null && _t !== void 0 ? _t : 12;
3066
- const containsPointer = (x, y, margin) => {
3067
- return pointer && pointer.x >= x - margin && pointer.x <= x + width + margin && pointer.y >= y - margin && pointer.y <= y + height + margin;
3068
- };
3069
- const passive = !selectOnClick || !this.visualRegister;
3070
- if (passive && ((_u = this.options.pointerAvoidance) === null || _u === void 0 ? void 0 : _u.enabled) !== false && pointer &&
3071
- (containsPointer(position.x, position.y, gap) || containsPointer(position.x + displacement.x, position.y + displacement.y, gap))) {
3072
- if (containsPointer(position.x + displacement.x, position.y + displacement.y, 0)) {
3073
- // Choose against the original box and retain the offset until the pointer leaves both boxes.
3074
- const candidates = [
3075
- { x: 0, y: pointer.y - gap - height - position.y },
3076
- { x: 0, y: pointer.y + gap - position.y },
3077
- { x: pointer.x - gap - width - position.x, y: 0 },
3078
- { x: pointer.x + gap - position.x, y: 0 }
3079
- ].filter(offset => position.x + offset.x >= 0 && position.y + offset.y >= 0 &&
3080
- position.x + offset.x + width <= this.viewer.canvas.clientWidth && position.y + offset.y + height <= this.viewer.canvas.clientHeight)
3081
- .sort((a, b) => Math.hypot(a.x, a.y) - Math.hypot(b.x, b.y));
3082
- displacement = candidates[0] || { x: 0, y: 0 };
3083
- }
3084
- }
3085
- else {
3086
- displacement = { x: 0, y: 0 };
3087
- }
3088
- const nudgeDuration = (_w = (_v = this.options.pointerAvoidance) === null || _v === void 0 ? void 0 : _v.transitionDuration) !== null && _w !== void 0 ? _w : 200;
3089
- const animateNudge = () => {
3090
- const progress = now < nudge.start ? 0 : nudgeDuration > 0 ? Math.min(1, (now - nudge.start) / nudgeDuration) : 1;
3091
- const eased = progress * progress * (3 - 2 * progress);
3092
- nudge.x = nudge.fromX + (nudge.targetX - nudge.fromX) * eased;
3093
- nudge.y = nudge.fromY + (nudge.targetY - nudge.fromY) * eased;
3094
- return progress;
3095
- };
3096
- animateNudge();
3097
- if (nudge.targetX !== displacement.x || nudge.targetY !== displacement.y) {
3098
- nudge = {
3099
- ...nudge,
3100
- fromX: nudge.x,
3101
- fromY: nudge.y,
3102
- targetX: displacement.x,
3103
- targetY: displacement.y,
3104
- start: now + ((_y = (_x = this.options.pointerAvoidance) === null || _x === void 0 ? void 0 : _x.delay) !== null && _y !== void 0 ? _y : 180)
3105
- };
3106
- }
3107
- if (animateNudge() < 1 && (nudge.fromX !== nudge.targetX || nudge.fromY !== nudge.targetY)) {
3108
- this.viewer.scene.requestRender();
3109
- }
3110
- position.x += nudge.x;
3111
- position.y += nudge.y;
3112
- ele.style.left = `${coordinates.x + position.x * coordinates.scaleX}px`;
3113
- ele.style.top = `${coordinates.y + position.y * coordinates.scaleY}px`;
3114
- for (const clickTarget of Array.from(content.querySelectorAll('[role="button"]'))) {
3115
- clickTarget.setAttribute("aria-label", placement.members.length ? `Select ${placement.entityIds.length} entities` : `Select ${(_0 = (_z = this.options.text) !== null && _z !== void 0 ? _z : text) !== null && _0 !== void 0 ? _0 : (_1 = this.rego) === null || _1 === void 0 ? void 0 : _1.entityId}`);
3116
- }
3117
- if (leader) {
3118
- const nodeRadius = ringAnimation.outer * (1 - (frame.boxBlend || 0));
3119
- const anchor = this.anchor;
3120
- anchor.style.left = `${coordinates.x + (frame.anchorX - nodeRadius) * coordinates.scaleX}px`;
3121
- anchor.style.top = `${coordinates.y + (frame.anchorY - nodeRadius) * coordinates.scaleY}px`;
3122
- anchor.style.width = `${nodeRadius * 2 * coordinates.scaleX}px`;
3123
- anchor.style.height = `${nodeRadius * 2 * coordinates.scaleY}px`;
3124
- anchor.style.boxSizing = "border-box";
3125
- anchor.style.background = "transparent";
3126
- const svg = anchor.firstElementChild;
3127
- svg.setAttribute("viewBox", `0 0 ${nodeRadius * 2} ${nodeRadius * 2}`);
3128
- const circle = svg.firstElementChild;
3129
- circle.setAttribute("cx", String(nodeRadius));
3130
- circle.setAttribute("cy", String(nodeRadius));
3131
- const innerRadius = ringAnimation.inner * (1 - (frame.boxBlend || 0));
3132
- circle.setAttribute("r", String((nodeRadius + innerRadius) / 2));
3133
- circle.setAttribute("stroke", this.options.leaderColor || "rgba(255, 255, 255, 0.8)");
3134
- circle.setAttribute("stroke-width", String(nodeRadius - innerRadius));
3135
- anchor.style.boxShadow = radius ? "none" : "0 0 5px rgba(0, 0, 0, 0.3)";
3136
- anchor.style.opacity = String(opacity * (normalLeader ? 1 : cursorMotion.amount) * (1 - (frame.boxBlend || 0)));
3137
- anchor.style.zIndex = String(placement.zIndex);
3138
- // The stem starts at the rim, never across the transparent centre, even after passive displacement.
3139
- const straightX = Math.max(position.x, Math.min(frame.anchorX, position.x + width));
3140
- const straightY = frame.anchorY < position.y ? position.y : frame.anchorY > position.y + height ? position.y + height : frame.anchorY;
3141
- const beside = angled && (frame.anchorX < position.x || frame.anchorX > position.x + width);
3142
- const elbowY = position.y + height / 2;
3143
- let endX = beside ? frame.anchorX : straightX;
3144
- let endY = beside ? elbowY : straightY;
3145
- if (pinShape) {
3146
- // Aimed from wherever clamping or a nudge left it; a stem, if any, meets the tip.
3147
- const centreX = position.x + pinShape.x, centreY = position.y + pinShape.y;
3148
- const aim = Math.atan2(frame.anchorY - centreY, frame.anchorX - centreX);
3149
- const reach = Math.min(pinShape.tip, Math.hypot(frame.anchorX - centreX, frame.anchorY - centreY));
3150
- endX = centreX + Math.cos(aim) * reach;
3151
- endY = centreY + Math.sin(aim) * reach;
3152
- const size = `${pinShape.radius * 2 * coordinates.scaleX}px`;
3153
- if (pinHolder.style.width !== size) {
3154
- pinHolder.style.width = pinHolder.style.height = size;
3155
- pinHolder.style.margin = `-${pinShape.radius * coordinates.scaleX}px`;
3253
+ const pinShape = leader && pin ? size.pin : undefined;
3254
+ const measuredWidth = size.width / coordinates.scaleX;
3255
+ const measuredHeight = size.height / coordinates.scaleY;
3256
+ return () => {
3257
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
3258
+ if (badge) {
3259
+ const narrow = badge.iconWidth < badge.iconHeight * 0.6;
3260
+ const clearance = badge.width ? (narrow ? badge.width / 2 + 2 : Math.max(0, badge.width / 2 - badge.iconWidth / 4)) : 0;
3261
+ // Reserve the badge overhang so adjacent text and viewport edges remain clear.
3262
+ const overhang = badge.height / 2;
3263
+ const margins = [
3264
+ badgeBelow ? "0px" : `${overhang}px`, badgeBelow ? `${overhang}px` : "0px", `${badge.width / 2 + clearance}px`
3265
+ ];
3266
+ countElement.style.left = `calc(100% + ${clearance}px)`;
3267
+ if (iconContainer.style.marginTop !== margins[0] ||
3268
+ iconContainer.style.marginBottom !== margins[1] ||
3269
+ iconContainer.style.marginRight !== margins[2]) {
3270
+ [iconContainer.style.marginTop, iconContainer.style.marginBottom, iconContainer.style.marginRight] = margins;
3271
+ sizeStale = true;
3272
+ // The margins change the label's size, which the next frame measures.
3273
+ this.viewer.scene.requestRender();
3274
+ }
3275
+ }
3276
+ const ring = this.options.anchorRing;
3277
+ const pointer = (layer.pointer || (cursorMotion.target ? cursorMotion.position : undefined) || (this.Transient && !this.Persistent ?
3278
+ (this.hoverFade.to === 0 ? this.retainedHoverPosition : (_a = this.visualRegister) === null || _a === void 0 ? void 0 : _a.GetLabelHoverPosition((_b = this.rego) === null || _b === void 0 ? void 0 : _b.entityId, (_c = this.rego) === null || _c === void 0 ? void 0 : _c.menuItemId)) : undefined));
3279
+ const iconRing = frame.iconRadius ? Math.max(ICON_RING_MIN, frame.iconRadius + ICON_RING_PADDING) : 0;
3280
+ const boxed = leader ? frame.modelBox : undefined;
3281
+ const hovered = pointer && (boxed ? hullContains(boxed.hull, pointer) :
3282
+ Math.hypot(pointer.x - frame.anchorX, pointer.y - frame.anchorY) <= Math.max((_d = ring === null || ring === void 0 ? void 0 : ring.hoverDistance) !== null && _d !== void 0 ? _d : 24, iconRing));
3283
+ if (look.iconPhoto && growPhoto) {
3284
+ // A rectangle leans on the leader's side of the label.
3285
+ if (photoShape === "rect") {
3286
+ const align = !leader ? "center" : placement.textSide === "left" ? "flex-end" : "flex-start";
3287
+ if (content.style.alignItems !== align) {
3288
+ content.style.alignItems = align;
3289
+ }
3290
+ }
3291
+ // Grown in layout rather than scaled, so the border keeps its width, and the label,
3292
+ // whose bottom sits on the leader, grows up and sideways without changing its length.
3293
+ growPhoto(Boolean(hovered) && !((_e = placement.members) === null || _e === void 0 ? void 0 : _e.length));
3294
+ sizeStale = true;
3295
+ }
3296
+ const normalRadius = (!leader || (ring === null || ring === void 0 ? void 0 : ring.enabled) === false ? 0 : Math.max(frame.distance <= ((_f = ring === null || ring === void 0 ? void 0 : ring.nearDistance) !== null && _f !== void 0 ? _f : 100) ? ((_g = ring === null || ring === void 0 ? void 0 : ring.nearRadius) !== null && _g !== void 0 ? _g : 10) : 0, hovered ? ((_h = ring === null || ring === void 0 ? void 0 : ring.hoverRadius) !== null && _h !== void 0 ? _h : (this.Transient && !this.Persistent ? 15.4 : 22)) : 0));
3297
+ const targetRadius = normalRadius + (15.4 - normalRadius) * cursorMotion.amount;
3298
+ // Around a glyph the ring keeps its size and only fades its stroke in, never covering it.
3299
+ // A ring around the cursor marks nothing, so a riding label keeps only its leader.
3300
+ const targetOuter = frame.follows ? 0 : iconRing || targetRadius || 2.5;
3301
+ const targetInner = frame.follows ? 0 : iconRing ? (targetRadius > 0 ? iconRing - 2 : iconRing) : Math.max(0, targetRadius - 2);
3302
+ const now = Date.now();
3303
+ const duration = (_j = ring === null || ring === void 0 ? void 0 : ring.transitionDuration) !== null && _j !== void 0 ? _j : 200;
3304
+ const sharedRing = this.Transient && !this.Persistent && this.hoverFade.to === 1 ? this.hoverRingState : undefined;
3305
+ let ringAnimation = sharedRing ? sharedRing.animation : this.ringAnimation;
3306
+ if (!ringAnimation) {
3307
+ const outer = frame.follows ? 0 : iconRing || 2.5;
3308
+ const inner = frame.follows ? 0 : iconRing;
3309
+ ringAnimation = { target: outer, targetInner: inner, start: now, outer, inner, fromOuter: outer, fromInner: inner };
3310
+ }
3311
+ ;
3312
+ const animateRing = () => {
3313
+ const progress = duration > 0 ? Math.min(1, Math.max(0, (now - ringAnimation.start) / duration)) : 1;
3314
+ const eased = progress * progress * (3 - 2 * progress);
3315
+ ringAnimation.outer = ringAnimation.fromOuter + (ringAnimation.target - ringAnimation.fromOuter) * eased;
3316
+ ringAnimation.inner = ringAnimation.fromInner + (ringAnimation.targetInner - ringAnimation.fromInner) * eased;
3317
+ return progress;
3318
+ };
3319
+ animateRing();
3320
+ if (ringAnimation.target !== targetOuter || ringAnimation.targetInner !== targetInner) {
3321
+ ringAnimation = {
3322
+ ...ringAnimation,
3323
+ target: targetOuter,
3324
+ targetInner,
3325
+ start: now,
3326
+ fromOuter: ringAnimation.outer,
3327
+ fromInner: ringAnimation.inner
3328
+ };
3329
+ }
3330
+ // Animate the hole and stem together; no CSS transition may lag behind a moving world anchor.
3331
+ if (animateRing() < 1 && (ringAnimation.fromOuter !== targetOuter || ringAnimation.fromInner !== targetInner)) {
3332
+ this.viewer.scene.requestRender();
3333
+ }
3334
+ this.ringAnimation = ringAnimation;
3335
+ if (sharedRing) {
3336
+ sharedRing.animation = ringAnimation;
3337
+ }
3338
+ const ringShare = 1 - (frame.boxBlend || 0);
3339
+ const radius = ringShare > 0 && ringAnimation.inner > 0 ? ringAnimation.outer * ringShare : 0;
3340
+ const stem = (pinShape ? fitPin(frame.anchorX, frame.anchorY, frame.leaderLength, measuredWidth, measuredHeight, pinShape) :
3341
+ leader ? (angled ? fitAngledLeader : fitLeader)(frame.anchorX, frame.anchorY, Math.max(frame.leaderLength, radius ? radius + 4 : 0), measuredWidth, measuredHeight, radius) : undefined);
3342
+ const position = { x: (stem || frame).x, y: (stem || frame).y };
3343
+ const width = measuredWidth;
3344
+ const height = measuredHeight;
3345
+ if (leader && angled) {
3346
+ // The declutter owns the side; the fit only says which one had the room to begin with.
3347
+ const run = Math.max(ANGLED_LEADER_RUN, radius + 4);
3348
+ const side = (_l = (_k = placement.textSide) !== null && _k !== void 0 ? _k : frame.leaderSide) !== null && _l !== void 0 ? _l : "right";
3349
+ const beside = side === "left" ? frame.anchorX - run - width : frame.anchorX + run;
3350
+ position.x = Math.max(0, Math.min(beside, layer.viewport.width - width));
3351
+ }
3352
+ else if (leader && !pin && placement.textSide === "left") {
3353
+ position.x = Math.max(0, Math.min(frame.anchorX - width, layer.viewport.width - width));
3354
+ }
3355
+ const gap = (_o = (_m = this.options.pointerAvoidance) === null || _m === void 0 ? void 0 : _m.gap) !== null && _o !== void 0 ? _o : 12;
3356
+ const containsPointer = (x, y, margin) => {
3357
+ return pointer && pointer.x >= x - margin && pointer.x <= x + width + margin && pointer.y >= y - margin && pointer.y <= y + height + margin;
3358
+ };
3359
+ const passive = !selectOnClick || !this.visualRegister;
3360
+ if (passive && ((_p = this.options.pointerAvoidance) === null || _p === void 0 ? void 0 : _p.enabled) !== false && pointer &&
3361
+ (containsPointer(position.x, position.y, gap) || containsPointer(position.x + displacement.x, position.y + displacement.y, gap))) {
3362
+ if (containsPointer(position.x + displacement.x, position.y + displacement.y, 0)) {
3363
+ // Choose against the original box; keep the offset until the pointer leaves both.
3364
+ const candidates = [
3365
+ { x: 0, y: pointer.y - gap - height - position.y },
3366
+ { x: 0, y: pointer.y + gap - position.y },
3367
+ { x: pointer.x - gap - width - position.x, y: 0 },
3368
+ { x: pointer.x + gap - position.x, y: 0 }
3369
+ ].filter(offset => position.x + offset.x >= 0 && position.y + offset.y >= 0 &&
3370
+ position.x + offset.x + width <= layer.viewport.width && position.y + offset.y + height <= layer.viewport.height)
3371
+ .sort((a, b) => Math.hypot(a.x, a.y) - Math.hypot(b.x, b.y));
3372
+ displacement = candidates[0] || { x: 0, y: 0 };
3373
+ }
3374
+ }
3375
+ else {
3376
+ displacement = { x: 0, y: 0 };
3377
+ }
3378
+ const nudgeDuration = (_r = (_q = this.options.pointerAvoidance) === null || _q === void 0 ? void 0 : _q.transitionDuration) !== null && _r !== void 0 ? _r : 200;
3379
+ const animateNudge = () => {
3380
+ const progress = now < nudge.start ? 0 : nudgeDuration > 0 ? Math.min(1, (now - nudge.start) / nudgeDuration) : 1;
3381
+ const eased = progress * progress * (3 - 2 * progress);
3382
+ nudge.x = nudge.fromX + (nudge.targetX - nudge.fromX) * eased;
3383
+ nudge.y = nudge.fromY + (nudge.targetY - nudge.fromY) * eased;
3384
+ return progress;
3385
+ };
3386
+ animateNudge();
3387
+ if (nudge.targetX !== displacement.x || nudge.targetY !== displacement.y) {
3388
+ nudge = {
3389
+ ...nudge,
3390
+ fromX: nudge.x,
3391
+ fromY: nudge.y,
3392
+ targetX: displacement.x,
3393
+ targetY: displacement.y,
3394
+ start: now + ((_t = (_s = this.options.pointerAvoidance) === null || _s === void 0 ? void 0 : _s.delay) !== null && _t !== void 0 ? _t : 180)
3395
+ };
3396
+ }
3397
+ if (animateNudge() < 1 && (nudge.fromX !== nudge.targetX || nudge.fromY !== nudge.targetY)) {
3398
+ this.viewer.scene.requestRender();
3399
+ }
3400
+ position.x += nudge.x;
3401
+ position.y += nudge.y;
3402
+ ele.style.left = `${coordinates.x + position.x * coordinates.scaleX}px`;
3403
+ ele.style.top = `${coordinates.y + position.y * coordinates.scaleY}px`;
3404
+ for (const clickTarget of Array.from(content.querySelectorAll('[role="button"]'))) {
3405
+ clickTarget.setAttribute("aria-label", placement.members.length ? `Select ${placement.entityIds.length} entities` : `Select ${(_v = (_u = this.options.text) !== null && _u !== void 0 ? _u : text) !== null && _v !== void 0 ? _v : (_w = this.rego) === null || _w === void 0 ? void 0 : _w.entityId}`);
3406
+ }
3407
+ if (leader) {
3408
+ const nodeRadius = ringAnimation.outer * (1 - (frame.boxBlend || 0));
3409
+ const anchor = this.anchor;
3410
+ anchor.style.left = `${coordinates.x + (frame.anchorX - nodeRadius) * coordinates.scaleX}px`;
3411
+ anchor.style.top = `${coordinates.y + (frame.anchorY - nodeRadius) * coordinates.scaleY}px`;
3412
+ anchor.style.width = `${nodeRadius * 2 * coordinates.scaleX}px`;
3413
+ anchor.style.height = `${nodeRadius * 2 * coordinates.scaleY}px`;
3414
+ anchor.style.boxSizing = "border-box";
3415
+ anchor.style.background = "transparent";
3416
+ const svg = anchor.firstElementChild;
3417
+ svg.setAttribute("viewBox", `0 0 ${nodeRadius * 2} ${nodeRadius * 2}`);
3418
+ const circle = svg.firstElementChild;
3419
+ circle.setAttribute("cx", String(nodeRadius));
3420
+ circle.setAttribute("cy", String(nodeRadius));
3421
+ const innerRadius = ringAnimation.inner * (1 - (frame.boxBlend || 0));
3422
+ circle.setAttribute("r", String((nodeRadius + innerRadius) / 2));
3423
+ circle.setAttribute("stroke", this.options.leaderColor || "rgba(255, 255, 255, 0.8)");
3424
+ circle.setAttribute("stroke-width", String(nodeRadius - innerRadius));
3425
+ anchor.style.boxShadow = radius ? "none" : "0 0 5px rgba(0, 0, 0, 0.3)";
3426
+ anchor.style.opacity = String(opacity * (normalLeader ? 1 : cursorMotion.amount) * (1 - (frame.boxBlend || 0)));
3427
+ anchor.style.zIndex = String(placement.zIndex);
3428
+ // The stem starts at the rim, never across the clear centre, even after displacement.
3429
+ const straightX = Math.max(position.x, Math.min(frame.anchorX, position.x + width));
3430
+ const straightY = frame.anchorY < position.y ? position.y : frame.anchorY > position.y + height ? position.y + height : frame.anchorY;
3431
+ const beside = angled && (frame.anchorX < position.x || frame.anchorX > position.x + width);
3432
+ const elbowY = position.y + height / 2;
3433
+ let endX = beside ? frame.anchorX : straightX;
3434
+ let endY = beside ? elbowY : straightY;
3435
+ if (pinShape) {
3436
+ // Aimed from wherever clamping or a nudge left it; a stem, if any, meets the tip.
3437
+ const centreX = position.x + pinShape.x, centreY = position.y + pinShape.y;
3438
+ const aim = Math.atan2(frame.anchorY - centreY, frame.anchorX - centreX);
3439
+ const reach = Math.min(pinShape.tip, Math.hypot(frame.anchorX - centreX, frame.anchorY - centreY));
3440
+ endX = centreX + Math.cos(aim) * reach;
3441
+ endY = centreY + Math.sin(aim) * reach;
3442
+ const size = `${pinShape.radius * 2 * coordinates.scaleX}px`;
3443
+ if (pinHolder.style.width !== size) {
3444
+ pinHolder.style.width = pinHolder.style.height = size;
3445
+ pinHolder.style.margin = `-${pinShape.radius * coordinates.scaleX}px`;
3446
+ }
3447
+ // The body's square corner starts lower right, a quarter turn short of down.
3448
+ const turn = Math.round((aim - Math.PI / 4) * 200) / 200;
3449
+ const rotation = `rotate(${turn}rad)`;
3450
+ if (pinBody.style.transform !== rotation) {
3451
+ pinBody.style.transform = rotation;
3452
+ // A box shadow turns with the body, so its offset is turned back to fall down
3453
+ // and right; a drop-shadow filter would not turn, but repaints every moving pin.
3454
+ const offsetX = Math.round((Math.cos(turn) + 2 * Math.sin(turn)) * 4) / 4;
3455
+ const offsetY = Math.round((2 * Math.cos(turn) - Math.sin(turn)) * 4) / 4;
3456
+ pinBody.style.boxShadow = `${offsetX}px ${offsetY}px 3px rgba(0, 0, 0, 0.35)`;
3457
+ }
3458
+ }
3459
+ const runToX = frame.anchorX < position.x ? position.x : position.x + width;
3460
+ const dx = endX - frame.anchorX, dy = endY - frame.anchorY;
3461
+ const length = Math.hypot(dx, dy);
3462
+ const leaderAngle = Math.atan2(dy, dx);
3463
+ const pointerDx = pointer ? pointer.x - frame.anchorX : 0;
3464
+ const pointerDy = pointer ? pointer.y - frame.anchorY : 0;
3465
+ const desiredAngle = Math.hypot(pointerDx, pointerDy) > 0.5 ? Math.atan2(pointerDy, pointerDx) + Math.PI - leaderAngle : 0;
3466
+ const wrappedAngle = Math.atan2(Math.sin(desiredAngle), Math.cos(desiredAngle));
3467
+ // Keep the stem inside the 126-degree arc, away from antialiased endpoints.
3468
+ const arcFraction = 0.35;
3469
+ const rotationLimit = Math.PI * arcFraction - Math.PI / 30;
3470
+ const targetOffset = hovered ? Math.max(-rotationLimit, Math.min(rotationLimit, wrappedAngle)) : ((_x = ringAnimation.arcOffset) !== null && _x !== void 0 ? _x : 0);
3471
+ const targetOpen = cursorMotion.target || (hovered && (ring === null || ring === void 0 ? void 0 : ring.enabled) !== false && targetRadius > 0) ? 1 : 0;
3472
+ const elapsed = Math.min(32, Math.max(0, now - ((_y = ringAnimation.arcTime) !== null && _y !== void 0 ? _y : now)));
3473
+ const blend = duration > 0 ? 1 - Math.exp(-elapsed / (duration / 3)) : 1;
3474
+ const approach = (value, target) => Math.abs(value - target) < 0.001 ? target : value + (target - value) * blend;
3475
+ ringAnimation.arcOffset = approach((_z = ringAnimation.arcOffset) !== null && _z !== void 0 ? _z : 0, targetOffset);
3476
+ ringAnimation.arcOpen = approach((_0 = ringAnimation.arcOpen) !== null && _0 !== void 0 ? _0 : 0, targetOpen);
3477
+ ringAnimation.arcTime = now;
3478
+ if (ringAnimation.arcOffset !== targetOffset || ringAnimation.arcOpen !== targetOpen) {
3479
+ this.viewer.scene.requestRender();
3480
+ }
3481
+ const circumference = Math.PI * (nodeRadius + ringAnimation.inner);
3482
+ const filledFraction = 1 - ringAnimation.arcOpen * (1 - arcFraction);
3483
+ circle.setAttribute("stroke-dasharray", `${circumference * filledFraction} ${circumference * (1 - filledFraction)}`);
3484
+ circle.setAttribute("transform", `rotate(${(leaderAngle + ringAnimation.arcOffset - Math.PI * filledFraction) * 180 / Math.PI} ${nodeRadius} ${nodeRadius})`);
3485
+ const cut = Math.min(radius, length);
3486
+ const startX = frame.anchorX + (length ? dx / length * cut : 0);
3487
+ const startY = frame.anchorY + (length ? dy / length * cut : 0);
3488
+ const draw = (element, fromX, fromY, toX, toY) => {
3489
+ const screenDx = (toX - fromX) * coordinates.scaleX;
3490
+ const screenDy = (toY - fromY) * coordinates.scaleY;
3491
+ element.style.width = `${2 * coordinates.scaleX}px`;
3492
+ element.style.left = `${coordinates.x + (fromX - 1) * coordinates.scaleX}px`;
3493
+ element.style.top = `${coordinates.y + fromY * coordinates.scaleY}px`;
3494
+ element.style.height = `${Math.hypot(screenDx, screenDy)}px`;
3495
+ element.style.transform = `rotate(${Math.atan2(screenDy, screenDx) - Math.PI / 2}rad)`;
3496
+ element.style.boxShadow = radius ? "none" : "0 0 5px rgba(0, 0, 0, 0.3)";
3497
+ element.style.transformOrigin = "top center";
3498
+ element.style.opacity = String(opacity * (normalLeader ? 1 : cursorMotion.amount));
3499
+ element.style.zIndex = String(placement.zIndex);
3500
+ };
3501
+ draw(lineEle, startX, startY, endX, endY);
3502
+ if (layer.modelBoxes.Hull(this.id)) {
3503
+ layer.modelBoxes.Request(this.id, {
3504
+ whole: Boolean(cursorMotion.target), from: { x: frame.anchorX, y: frame.anchorY },
3505
+ opacity, zIndex: placement.zIndex, color: this.options.leaderColor || "rgba(255, 255, 255, 0.8)", duration
3506
+ });
3507
+ }
3508
+ if (runEle) {
3509
+ // Overlap the corner by the stem's own width so the elbow has no notch in it.
3510
+ const overlap = frame.anchorX < runToX ? -1 : 1;
3511
+ runEle.style.display = beside ? "block" : "none";
3512
+ if (beside)
3513
+ draw(runEle, endX + overlap, endY, runToX, endY);
3514
+ }
3156
3515
  }
3157
- // The body's square corner starts at the lower right, a quarter turn short of pointing down.
3158
- pinBody.style.transform = `rotate(${aim - Math.PI / 4}rad)`;
3159
- }
3160
- const runToX = frame.anchorX < position.x ? position.x : position.x + width;
3161
- const dx = endX - frame.anchorX, dy = endY - frame.anchorY;
3162
- const length = Math.hypot(dx, dy);
3163
- const leaderAngle = Math.atan2(dy, dx);
3164
- const pointerDx = pointer ? pointer.x - frame.anchorX : 0;
3165
- const pointerDy = pointer ? pointer.y - frame.anchorY : 0;
3166
- const desiredAngle = Math.hypot(pointerDx, pointerDy) > 0.5 ? Math.atan2(pointerDy, pointerDx) + Math.PI - leaderAngle : 0;
3167
- const wrappedAngle = Math.atan2(Math.sin(desiredAngle), Math.cos(desiredAngle));
3168
- // Keep the stem inside the 126-degree arc, away from antialiased endpoints.
3169
- const arcFraction = 0.35;
3170
- const rotationLimit = Math.PI * arcFraction - Math.PI / 30;
3171
- const targetOffset = hovered ? Math.max(-rotationLimit, Math.min(rotationLimit, wrappedAngle)) : ((_2 = ringAnimation.arcOffset) !== null && _2 !== void 0 ? _2 : 0);
3172
- const targetOpen = cursorMotion.target || (hovered && (ring === null || ring === void 0 ? void 0 : ring.enabled) !== false && targetRadius > 0) ? 1 : 0;
3173
- const elapsed = Math.min(32, Math.max(0, now - ((_3 = ringAnimation.arcTime) !== null && _3 !== void 0 ? _3 : now)));
3174
- const blend = duration > 0 ? 1 - Math.exp(-elapsed / (duration / 3)) : 1;
3175
- const approach = (value, target) => Math.abs(value - target) < 0.001 ? target : value + (target - value) * blend;
3176
- ringAnimation.arcOffset = approach((_4 = ringAnimation.arcOffset) !== null && _4 !== void 0 ? _4 : 0, targetOffset);
3177
- ringAnimation.arcOpen = approach((_5 = ringAnimation.arcOpen) !== null && _5 !== void 0 ? _5 : 0, targetOpen);
3178
- ringAnimation.arcTime = now;
3179
- if (ringAnimation.arcOffset !== targetOffset || ringAnimation.arcOpen !== targetOpen) {
3180
- this.viewer.scene.requestRender();
3181
- }
3182
- const circumference = Math.PI * (nodeRadius + ringAnimation.inner);
3183
- const filledFraction = 1 - ringAnimation.arcOpen * (1 - arcFraction);
3184
- circle.setAttribute("stroke-dasharray", `${circumference * filledFraction} ${circumference * (1 - filledFraction)}`);
3185
- circle.setAttribute("transform", `rotate(${(leaderAngle + ringAnimation.arcOffset - Math.PI * filledFraction) * 180 / Math.PI} ${nodeRadius} ${nodeRadius})`);
3186
- const cut = Math.min(radius, length);
3187
- const startX = frame.anchorX + (length ? dx / length * cut : 0);
3188
- const startY = frame.anchorY + (length ? dy / length * cut : 0);
3189
- const draw = (element, fromX, fromY, toX, toY) => {
3190
- const screenDx = (toX - fromX) * coordinates.scaleX;
3191
- const screenDy = (toY - fromY) * coordinates.scaleY;
3192
- element.style.width = `${2 * coordinates.scaleX}px`;
3193
- element.style.left = `${coordinates.x + (fromX - 1) * coordinates.scaleX}px`;
3194
- element.style.top = `${coordinates.y + fromY * coordinates.scaleY}px`;
3195
- element.style.height = `${Math.hypot(screenDx, screenDy)}px`;
3196
- element.style.transform = `rotate(${Math.atan2(screenDy, screenDx) - Math.PI / 2}rad)`;
3197
- element.style.boxShadow = radius ? "none" : "0 0 5px rgba(0, 0, 0, 0.3)";
3198
- element.style.transformOrigin = "top center";
3199
- element.style.opacity = String(opacity * (normalLeader ? 1 : cursorMotion.amount));
3200
- element.style.zIndex = String(placement.zIndex);
3201
3516
  };
3202
- draw(lineEle, startX, startY, endX, endY);
3203
- if (layer.modelBoxes.Hull(this.id)) {
3204
- layer.modelBoxes.Request(this.id, {
3205
- whole: Boolean(cursorMotion.target), from: { x: frame.anchorX, y: frame.anchorY },
3206
- opacity, zIndex: placement.zIndex, color: this.options.leaderColor || "rgba(255, 255, 255, 0.8)", duration
3207
- });
3208
- }
3209
- if (runEle) {
3210
- // Overlap the corner by the stem's own width so the elbow has no notch in it.
3211
- const overlap = frame.anchorX < runToX ? -1 : 1;
3212
- runEle.style.display = beside ? "block" : "none";
3213
- if (beside)
3214
- draw(runEle, endX + overlap, endY, runToX, endY);
3215
- }
3216
- }
3517
+ };
3217
3518
  };
3218
3519
  ele.style.display = "flex";
3219
3520
  ele.style.visibility = "hidden";
3220
- this.renderRemoval = layer.Add({ project, apply });
3521
+ // Hovering, riding the cursor or a highlight lift a label out of its cluster, which a stale frame misses.
3522
+ const wake = () => {
3523
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
3524
+ return Boolean(this.Transient ||
3525
+ ((_a = this.cursorMotion) === null || _a === void 0 ? void 0 : _a.target) ||
3526
+ ((_b = this.cursorMotion) === null || _b === void 0 ? void 0 : _b.amount) > 0 ||
3527
+ ((_c = this.visualRegister) === null || _c === void 0 ? void 0 : _c.GetLabelHoverPosition((_d = this.rego) === null || _d === void 0 ? void 0 : _d.entityId, (_e = this.rego) === null || _e === void 0 ? void 0 : _e.menuItemId)) ||
3528
+ ((_j = (_f = this.visualRegister) === null || _f === void 0 ? void 0 : _f.GetState({ entityId: (_g = this.rego) === null || _g === void 0 ? void 0 : _g.entityId, menuItemId: (_h = this.rego) === null || _h === void 0 ? void 0 : _h.menuItemId })) === null || _j === void 0 ? void 0 : _j.highlighted));
3529
+ };
3530
+ this.renderRemoval = layer.Add({ project, apply, wake });
3221
3531
  leaveLayer === null || leaveLayer === void 0 ? void 0 : leaveLayer();
3222
3532
  // If terrain changes then we'll re-call createLabel as the position may have changed.
3223
3533
  let terrTimeout = null;