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.
@@ -6679,6 +6679,17 @@
6679
6679
  * Groups eligible neighbours around stable representatives without coupling membership to its renderer.
6680
6680
  */
6681
6681
  function ClusterItems(params) {
6682
+ const steps = ClusterItemsInSteps(params, Infinity);
6683
+ let next = steps.next();
6684
+ while (!next.done) {
6685
+ next = steps.next();
6686
+ }
6687
+ return next.value;
6688
+ }
6689
+ /*
6690
+ * ClusterItems as steps, pausing after every so many representatives tried, so a caller can spread one run over frames.
6691
+ */
6692
+ function* ClusterItemsInSteps(params, every = 32) {
6682
6693
  const eligible = new Map();
6683
6694
  for (const item of params.items) {
6684
6695
  if (!params.eligible || params.eligible(item)) {
@@ -6688,12 +6699,21 @@
6688
6699
  const assigned = new Set();
6689
6700
  const result = [];
6690
6701
  const minimum = Math.max(2, params.minSize || 2);
6702
+ const free = (other) => {
6703
+ const key = params.id(other);
6704
+ return eligible.has(key) && !assigned.has(key);
6705
+ };
6706
+ let tried = 0;
6691
6707
  for (const [id, item] of eligible) {
6692
6708
  if (assigned.has(id)) {
6693
6709
  continue;
6694
6710
  }
6711
+ if (++tried > every) {
6712
+ tried = 1;
6713
+ yield;
6714
+ }
6695
6715
  const members = new Map([[id, item]]);
6696
- for (const neighbour of params.neighbours(item)) {
6716
+ for (const neighbour of params.neighbours(item, free)) {
6697
6717
  const key = params.id(neighbour);
6698
6718
  if (eligible.has(key) && !assigned.has(key)) {
6699
6719
  members.set(key, eligible.get(key));
@@ -7218,6 +7238,10 @@
7218
7238
  const ICON_STACK_SCALE = 0.6;
7219
7239
  // How far an angled leader runs sideways before the label starts.
7220
7240
  const ANGLED_LEADER_RUN = 28;
7241
+ // While labels move on screen, clusters are worked out afresh at most this often, and again once they settle.
7242
+ const CLUSTER_MOVING_INTERVAL = 400;
7243
+ // Milliseconds of each frame a fresh clustering may take; the rest of it runs on the next frames.
7244
+ const CLUSTER_STEP_BUDGET = 4;
7221
7245
  // How far a pin's white body reaches past the icon it wraps, before its tip.
7222
7246
  const PIN_RIM = 7;
7223
7247
  // A pin is drawn in one fixed look for now: a white body with a faint edge and a soft shadow.
@@ -7540,6 +7564,8 @@
7540
7564
  */
7541
7565
  class PlacementMemory {
7542
7566
  constructor() {
7567
+ // The last clusters worked out from scratch, by label id, for frames that may reuse them.
7568
+ this.clusters = null;
7543
7569
  this.previous = new Map();
7544
7570
  this.nextOrder = 0;
7545
7571
  }
@@ -7661,6 +7687,16 @@
7661
7687
  function pinLook(look) {
7662
7688
  return look.iconBackground ? look : { ...look, iconBackground: PIN_COLOR };
7663
7689
  }
7690
+ /*
7691
+ * Whether a label draws a pin: it needs an icon to wrap, and a photo keeps its own shape.
7692
+ */
7693
+ function HasPin(options) {
7694
+ return ((options === null || options === void 0 ? void 0 : options.leaderStyle) === "pin" &&
7695
+ options.leader !== false &&
7696
+ (options.presentation || "text") !== "text" &&
7697
+ !options.iconPhoto);
7698
+ }
7699
+ EntityLabel.HasPin = HasPin;
7664
7700
  function iconLook(options) {
7665
7701
  return {
7666
7702
  icon: options.icon || undefined,
@@ -7683,20 +7719,80 @@
7683
7719
  presentation === "text" ? null : iconLook(options)
7684
7720
  ]);
7685
7721
  }
7722
+ function summariseCluster(cluster) {
7723
+ var _a, _b, _c;
7724
+ let chosen = cluster.representative;
7725
+ const iconic = (chosen.options.presentation || "text") !== "text";
7726
+ // Members in the order they would win representation; the seed is the nearest.
7727
+ let ordered = [chosen, ...cluster.members.filter(member => member !== chosen)];
7728
+ if (iconic && chosen.options.cluster.representative !== "nearest") {
7729
+ ordered = ordered.map((member, i) => ({ member, i, priority: EntityLabel.IconPriority(member.options) }))
7730
+ .sort((a, b) => b.priority - a.priority || a.i - b.i).map(entry => entry.member);
7731
+ chosen = ordered[0];
7732
+ }
7733
+ const summary = {
7734
+ chosen: chosen.id,
7735
+ members: cluster.members.map(frame => frame.id),
7736
+ entityIds: Array.from(new Set(cluster.members.map(frame => frame.entityId).filter(Boolean)))
7737
+ };
7738
+ if (iconic && chosen.options.iconScale !== 0) {
7739
+ const seen = new Set();
7740
+ const icons = [];
7741
+ for (const member of ordered) {
7742
+ const look = iconLook(member.options);
7743
+ const key = JSON.stringify(look);
7744
+ if (seen.has(key)) {
7745
+ continue;
7746
+ }
7747
+ seen.add(key);
7748
+ if (member !== ordered[0])
7749
+ icons.push(look);
7750
+ if (seen.size >= ICON_STACK_LIMIT) {
7751
+ break;
7752
+ }
7753
+ }
7754
+ if (icons.length)
7755
+ summary.icons = icons;
7756
+ }
7757
+ if (chosen.options.presentation !== "icon") {
7758
+ const values = new Set();
7759
+ const stableMembers = [...cluster.members].sort((a, b) => (a.entityKey || a.entityId || a.id).localeCompare(b.entityKey || b.entityId || b.id));
7760
+ for (const member of stableMembers) {
7761
+ 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();
7762
+ if (value) {
7763
+ values.add(value);
7764
+ }
7765
+ }
7766
+ if (values.size > 1) {
7767
+ // A value long enough to wrap fills the box on its own, so only one is listed.
7768
+ 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()); });
7769
+ const limit = wrapped ? 1 : 2;
7770
+ const rows = Array.from(values).slice(0, limit);
7771
+ if (values.size > limit)
7772
+ rows.push(`+${values.size - limit} more…`);
7773
+ summary.text = rows.join("\n");
7774
+ }
7775
+ }
7776
+ return summary;
7777
+ }
7686
7778
  /*
7687
- * Resolves clustering and stable text placement before depth fading; highlighted labels receive priority.
7779
+ * Frames in the order they win: priority first, then nearest, with every coordinate known.
7688
7780
  */
7689
- function Layout(input, memory = new PlacementMemory()) {
7690
- var _a, _b, _c, _d, _e, _f, _g;
7691
- const frames = input.filter(frame => [frame.x, frame.y, frame.width, frame.height, frame.depth].every(Number.isFinite))
7781
+ function sortFrames(input) {
7782
+ return input.filter(frame => [frame.x, frame.y, frame.width, frame.height, frame.depth].every(Number.isFinite))
7692
7783
  .sort((a, b) => (b.priority || 0) - (a.priority || 0) || a.depth - b.depth || a.id.localeCompare(b.id));
7693
- const result = new Map();
7784
+ }
7785
+ /*
7786
+ * The frames clustering considers, by presentation, and the ones hidden as another label of the same entity.
7787
+ */
7788
+ function clusterGroups(frames) {
7789
+ var _a, _b;
7694
7790
  const entityKeys = new Set();
7695
7791
  const groups = new Map();
7792
+ const duplicates = new Set();
7696
7793
  for (const frame of frames) {
7697
- const duplicate = frame.entityKey && entityKeys.has(frame.entityKey);
7698
- result.set(frame.id, { frame, visible: !duplicate, opacity: frame.opacity, zIndex: 0, members: [], entityIds: frame.entityId ? [frame.entityId] : [] });
7699
- if (duplicate) {
7794
+ if (frame.entityKey && entityKeys.has(frame.entityKey)) {
7795
+ duplicates.add(frame.id);
7700
7796
  continue;
7701
7797
  }
7702
7798
  if (frame.entityKey)
@@ -7711,6 +7807,13 @@
7711
7807
  }
7712
7808
  groups.get(key).push(frame);
7713
7809
  }
7810
+ return { groups, duplicates };
7811
+ }
7812
+ /*
7813
+ * Works clusters out from scratch, as steps a caller can spread over frames or run straight through.
7814
+ */
7815
+ function* clusterInSteps(groups, every = 32) {
7816
+ const clustered = [];
7714
7817
  for (const group of groups.values()) {
7715
7818
  // Summary and badge dimensions are output, never the next frame's membership input.
7716
7819
  const candidates = group.map(frame => frame.extendedGeometry && frame.canFlip &&
@@ -7724,70 +7827,76 @@
7724
7827
  rank.set(frame.id, i);
7725
7828
  looks.set(frame.id, clusterLook(frame.options));
7726
7829
  });
7727
- const clusters = ClusterItems({
7830
+ const clusters = yield* ClusterItemsInSteps({
7728
7831
  items: candidates,
7729
7832
  id: frame => frame.id,
7730
7833
  minSize: group[0].options.cluster.minSize,
7731
- neighbours: frame => {
7834
+ neighbours: (frame, free) => {
7732
7835
  var _a;
7733
- 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));
7734
- return compatibleMembers(frame, nearby, other => other.group || "", other => looks.get(other.id));
7836
+ // Labels an earlier cluster took cannot join, so they are dropped before the pairwise checks.
7837
+ const nearby = index.Query(frame, (_a = frame.options.cluster.pixelRange) !== null && _a !== void 0 ? _a : (frame.options.presentation === "icon" ? 24 : 8))
7838
+ .filter(other => other === frame || free(other))
7839
+ .sort((a, b) => rank.get(a.id) - rank.get(b.id));
7840
+ // A pin stands for one thing, so it clusters only with pins that look the same.
7841
+ const group = (other) => HasPin(other.options) ? `look:${looks.get(other.id)}` : other.group || "";
7842
+ return compatibleMembers(frame, nearby, group, other => looks.get(other.id));
7735
7843
  }
7736
- });
7737
- for (const cluster of clusters) {
7738
- let chosen = cluster.representative;
7739
- const iconic = (chosen.options.presentation || "text") !== "text";
7740
- // Members in the order they would win representation; the seed is the nearest.
7741
- let ordered = [chosen, ...cluster.members.filter(member => member !== chosen)];
7742
- if (iconic && chosen.options.cluster.representative !== "nearest") {
7743
- ordered = ordered.map((member, i) => ({ member, i, priority: EntityLabel.IconPriority(member.options) }))
7744
- .sort((a, b) => b.priority - a.priority || a.i - b.i).map(entry => entry.member);
7745
- chosen = ordered[0];
7746
- }
7747
- const representative = result.get(chosen.id);
7748
- representative.members = cluster.members.map(frame => frame.id);
7749
- if (iconic && chosen.options.iconScale !== 0) {
7750
- const seen = new Set();
7751
- const icons = [];
7752
- for (const member of ordered) {
7753
- const look = iconLook(member.options);
7754
- const key = JSON.stringify(look);
7755
- if (seen.has(key)) {
7756
- continue;
7757
- }
7758
- seen.add(key);
7759
- if (member !== ordered[0])
7760
- icons.push(look);
7761
- if (seen.size >= ICON_STACK_LIMIT) {
7762
- break;
7763
- }
7764
- }
7765
- if (icons.length)
7766
- representative.icons = icons;
7767
- }
7768
- representative.entityIds = Array.from(new Set(cluster.members.map(frame => frame.entityId).filter(Boolean)));
7769
- if (chosen.options.presentation !== "icon") {
7770
- const values = new Set();
7771
- const stableMembers = [...cluster.members].sort((a, b) => (a.entityKey || a.entityId || a.id).localeCompare(b.entityKey || b.entityId || b.id));
7772
- for (const member of stableMembers) {
7773
- 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();
7774
- if (value) {
7775
- values.add(value);
7776
- }
7777
- }
7778
- if (values.size > 1) {
7779
- // A value long enough to wrap fills the box on its own, so only one is listed.
7780
- 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()); });
7781
- const limit = wrapped ? 1 : 2;
7782
- const rows = Array.from(values).slice(0, limit);
7783
- if (values.size > limit)
7784
- rows.push(`+${values.size - limit} more…`);
7785
- representative.text = rows.join("\n");
7786
- }
7844
+ }, every);
7845
+ clusters.forEach(cluster => clustered.push({ representative: cluster.representative.id, members: cluster.members.map(member => member.id) }));
7846
+ }
7847
+ return clustered;
7848
+ }
7849
+ /*
7850
+ * Runs a clustering job to the end at once.
7851
+ */
7852
+ function finishClustering(steps) {
7853
+ let next = steps.next();
7854
+ while (!next.done) {
7855
+ next = steps.next();
7856
+ }
7857
+ return next.value;
7858
+ }
7859
+ function Layout(input, memory = new PlacementMemory(), reuseClusters = false) {
7860
+ var _a, _b, _c;
7861
+ const frames = sortFrames(input);
7862
+ const { groups, duplicates } = clusterGroups(frames);
7863
+ const result = new Map();
7864
+ for (const frame of frames) {
7865
+ result.set(frame.id, { frame, visible: !duplicates.has(frame.id), opacity: frame.opacity, zIndex: 0, members: [], entityIds: frame.entityId ? [frame.entityId] : [] });
7866
+ }
7867
+ if (!reuseClusters || !memory.clusters) {
7868
+ memory.clusters = finishClustering(clusterInSteps(groups, Infinity));
7869
+ }
7870
+ for (const group of groups.values()) {
7871
+ // The kept membership, less anything gone, hovered or moved out of the group since.
7872
+ const byId = new Map(group.map(frame => [frame.id, frame]));
7873
+ const minSize = Math.max(2, (_a = group[0].options.cluster.minSize) !== null && _a !== void 0 ? _a : 2);
7874
+ const clusters = memory.clusters.map(entry => {
7875
+ var _a;
7876
+ const members = entry.members.map(id => byId.get(id)).filter(Boolean);
7877
+ if (members.length < minSize) {
7878
+ return null;
7787
7879
  }
7788
- for (const frame of cluster.members) {
7789
- if (frame !== chosen)
7790
- result.get(frame.id).visible = false;
7880
+ return { entry, whole: members.length === entry.members.length, representative: (_a = byId.get(entry.representative)) !== null && _a !== void 0 ? _a : members[0], members };
7881
+ }).filter(Boolean);
7882
+ for (const cluster of clusters) {
7883
+ // A cluster that kept every member keeps its summary, worked out once, sparing a pass over its members.
7884
+ let summary = cluster.whole ? cluster.entry.summary : undefined;
7885
+ if (!summary) {
7886
+ summary = summariseCluster(cluster);
7887
+ if (cluster.whole)
7888
+ cluster.entry.summary = summary;
7889
+ }
7890
+ const representative = result.get(summary.chosen);
7891
+ representative.members = summary.members;
7892
+ if (summary.icons)
7893
+ representative.icons = summary.icons;
7894
+ representative.entityIds = summary.entityIds;
7895
+ if (summary.text != null)
7896
+ representative.text = summary.text;
7897
+ for (const id of summary.members) {
7898
+ if (id !== summary.chosen)
7899
+ result.get(id).visible = false;
7791
7900
  }
7792
7901
  }
7793
7902
  }
@@ -7806,7 +7915,7 @@
7806
7915
  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) };
7807
7916
  const overlap = foreground.HasOverlap(box) || foregroundLeaders.HasOverlap(box) || Boolean(stem && foreground.HasOverlap(stem));
7808
7917
  if (overlap) {
7809
- 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));
7918
+ 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));
7810
7919
  }
7811
7920
  placement.zIndex = zIndex--;
7812
7921
  foreground.Add(frame);
@@ -7826,21 +7935,23 @@
7826
7935
  const accepted = [seed];
7827
7936
  const byGroup = new Map([[group(seed), 1]]);
7828
7937
  const byLook = new Map([[look(seed), 1]]);
7829
- const byBoth = new Map([[JSON.stringify([group(seed), look(seed)]), 1]]);
7938
+ // Joined with a character no group id or look holds, not serialised, since this runs per neighbour.
7939
+ const both = (itemGroup, itemLook) => `${itemGroup}\u0000${itemLook}`;
7940
+ const byBoth = new Map([[both(group(seed), look(seed)), 1]]);
7830
7941
  for (const item of nearby) {
7831
7942
  if (item === seed) {
7832
7943
  continue;
7833
7944
  }
7834
- const itemGroup = group(item), itemLook = look(item), both = JSON.stringify([itemGroup, itemLook]);
7945
+ const itemGroup = group(item), itemLook = look(item), key = both(itemGroup, itemLook);
7835
7946
  // Accepted members in another group with another look are the ones this item may not sit beside.
7836
- const conflicting = accepted.length - (byGroup.get(itemGroup) || 0) - (byLook.get(itemLook) || 0) + (byBoth.get(both) || 0);
7947
+ const conflicting = accepted.length - (byGroup.get(itemGroup) || 0) - (byLook.get(itemLook) || 0) + (byBoth.get(key) || 0);
7837
7948
  if (conflicting > 0) {
7838
7949
  continue;
7839
7950
  }
7840
7951
  accepted.push(item);
7841
7952
  byGroup.set(itemGroup, (byGroup.get(itemGroup) || 0) + 1);
7842
7953
  byLook.set(itemLook, (byLook.get(itemLook) || 0) + 1);
7843
- byBoth.set(both, (byBoth.get(both) || 0) + 1);
7954
+ byBoth.set(key, (byBoth.get(key) || 0) + 1);
7844
7955
  }
7845
7956
  return accepted;
7846
7957
  }
@@ -7871,10 +7982,20 @@
7871
7982
  this.viewer = viewer;
7872
7983
  this.element = document.createElement("div");
7873
7984
  this.coordinates = { x: 0, y: 0, scaleX: 1, scaleY: 1 };
7985
+ // The canvas size, read once a frame: reading the canvas after placing a label lays the page out again.
7986
+ this.viewport = { width: 0, height: 0 };
7874
7987
  this.participants = new Set();
7875
7988
  // Participants whose failure has been logged, so a label that throws every frame logs once.
7876
7989
  this.failed = new WeakSet();
7877
7990
  this.placementMemory = new PlacementMemory();
7991
+ // What the clusters were last worked out from: the labels taking part and where they sat, and when.
7992
+ this.clustered = null;
7993
+ // Labels added or removed, which can change any cluster's look or membership.
7994
+ this.clustersDirty = true;
7995
+ // A fresh clustering spread over frames, while the last one stays on show.
7996
+ this.clusterJob = null;
7997
+ // Labels hidden in a cluster last frame, with the frame they were laid out from, for reused clusters to skip.
7998
+ this.sleeping = new Map();
7878
7999
  this.modelBoxes = new ModelBoxes();
7879
8000
  this.forwardingWheel = false;
7880
8001
  this.pendingDrag = null;
@@ -8051,9 +8172,12 @@
8051
8172
  }
8052
8173
  Add(participant) {
8053
8174
  this.participants.add(participant);
8175
+ this.clustersDirty = true;
8054
8176
  this.viewer.scene.requestRender();
8055
8177
  return () => {
8056
8178
  this.participants.delete(participant);
8179
+ this.sleeping.delete(participant);
8180
+ this.clustersDirty = true;
8057
8181
  if (!this.participants.size) {
8058
8182
  this.remove();
8059
8183
  this.element.removeEventListener("wheel", this.ForwardWheel);
@@ -8104,8 +8228,9 @@
8104
8228
  }
8105
8229
  }
8106
8230
  Update() {
8107
- var _a, _b;
8231
+ var _a, _b, _c;
8108
8232
  this.coordinates = this.MeasureCoordinates();
8233
+ this.viewport = { width: this.viewer.canvas.clientWidth, height: this.viewer.canvas.clientHeight };
8109
8234
  this.UpdateHorizon();
8110
8235
  const rect = (_b = (_a = this.viewer.canvas).getBoundingClientRect) === null || _b === void 0 ? void 0 : _b.call(_a);
8111
8236
  const pointer = this.pointerClient;
@@ -8121,21 +8246,77 @@
8121
8246
  console.error("EntityLabel: model boxes failed to resolve.", e);
8122
8247
  }
8123
8248
  // This runs inside Cesium's frame, where one throwing label would stop rendering for the whole scene.
8249
+ const project = (participant) => {
8250
+ try {
8251
+ return participant.project();
8252
+ }
8253
+ catch (e) {
8254
+ this.logFailure(participant, e);
8255
+ return null;
8256
+ }
8257
+ };
8258
+ // A label hidden in a cluster needs no position while its cluster is reused, which is most frames.
8259
+ const mayReuse = Boolean(this.clustered) && !this.clustersDirty && Date.now() - this.clustered.at < CLUSTER_MOVING_INTERVAL;
8124
8260
  const projected = new Map();
8125
8261
  for (const participant of this.participants) {
8126
- let frame = null;
8262
+ const asleep = mayReuse && !((_c = participant.wake) === null || _c === void 0 ? void 0 : _c.call(participant)) ? this.sleeping.get(participant) : undefined;
8263
+ projected.set(participant, asleep !== null && asleep !== void 0 ? asleep : project(participant));
8264
+ }
8265
+ let frames = Array.from(projected.values()).filter(Boolean);
8266
+ const decision = this.DecideClusters(frames);
8267
+ if (decision !== "reuse" && mayReuse) {
8268
+ // Clusters are worked out from where every label is now, sleepers included.
8269
+ for (const participant of this.participants) {
8270
+ if (this.sleeping.has(participant))
8271
+ projected.set(participant, project(participant));
8272
+ }
8273
+ frames = Array.from(projected.values()).filter(Boolean);
8274
+ }
8275
+ if (decision === "refresh") {
8276
+ this.clusterJob = clusterInSteps(clusterGroups(sortFrames(frames)).groups);
8277
+ }
8278
+ this.StepClusters();
8279
+ const layout = Layout(frames, this.placementMemory, decision !== "now");
8280
+ const placements = new Map(layout.map(item => [item.frame.id, item]));
8281
+ const participantsById = new Map();
8282
+ for (const [participant, frame] of projected) {
8283
+ if (frame)
8284
+ participantsById.set(frame.id, participant);
8285
+ }
8286
+ this.sleeping = new Map();
8287
+ for (const placement of layout) {
8288
+ for (const id of placement.members) {
8289
+ const participant = id !== placement.frame.id ? participantsById.get(id) : undefined;
8290
+ if (participant)
8291
+ this.sleeping.set(participant, projected.get(participant));
8292
+ }
8293
+ }
8294
+ // Writes, then reads, then writes, each across all labels; interleaved, each read lays them out again.
8295
+ const measures = [];
8296
+ for (const [participant, frame] of projected) {
8127
8297
  try {
8128
- frame = participant.project();
8298
+ const measure = participant.apply(frame ? placements.get(frame.id) : undefined);
8299
+ if (measure)
8300
+ measures.push([participant, measure]);
8129
8301
  }
8130
8302
  catch (e) {
8131
8303
  this.logFailure(participant, e);
8132
8304
  }
8133
- projected.set(participant, frame);
8134
8305
  }
8135
- const placements = new Map(Layout(Array.from(projected.values()).filter(Boolean), this.placementMemory).map(item => [item.frame.id, item]));
8136
- for (const [participant, frame] of projected) {
8306
+ const places = [];
8307
+ for (const [participant, measure] of measures) {
8137
8308
  try {
8138
- participant.apply(frame ? placements.get(frame.id) : undefined);
8309
+ const place = measure();
8310
+ if (place)
8311
+ places.push([participant, place]);
8312
+ }
8313
+ catch (e) {
8314
+ this.logFailure(participant, e);
8315
+ }
8316
+ }
8317
+ for (const [participant, place] of places) {
8318
+ try {
8319
+ place();
8139
8320
  }
8140
8321
  catch (e) {
8141
8322
  this.logFailure(participant, e);
@@ -8148,6 +8329,55 @@
8148
8329
  console.error("EntityLabel: model boxes failed to draw.", e);
8149
8330
  }
8150
8331
  }
8332
+ /*
8333
+ * How this frame gets its clusters: kept from last time, refreshed by a job over frames, or worked out now.
8334
+ * They are kept while no clustering label has moved, and while labels move (camera or entities) until a moment
8335
+ * old or while a refresh is under way. Labels coming or going, or none clustered yet, work them out now.
8336
+ */
8337
+ DecideClusters(frames) {
8338
+ // Where each clustering label sits, to a few pixels, so drawing alone never counts as movement.
8339
+ const signature = frames
8340
+ .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); })
8341
+ .map(frame => `${frame.id}@${Math.round(frame.x / 4)},${Math.round(frame.y / 4)}`)
8342
+ .join("|");
8343
+ const now = Date.now();
8344
+ if (!this.clustered || this.clustersDirty) {
8345
+ this.clusterJob = null;
8346
+ this.clustered = { signature, at: now };
8347
+ this.clustersDirty = false;
8348
+ return "now";
8349
+ }
8350
+ if (this.clustered.signature === signature) {
8351
+ return "reuse";
8352
+ }
8353
+ if (this.clusterJob || now - this.clustered.at < CLUSTER_MOVING_INTERVAL) {
8354
+ // Another frame is due either way, so the scene gets its fresh clusters once it settles.
8355
+ this.viewer.scene.requestRender();
8356
+ return "reuse";
8357
+ }
8358
+ this.clustered = { signature, at: now };
8359
+ return "refresh";
8360
+ }
8361
+ /*
8362
+ * Advances a spread clustering for this frame's share of time, handing its result over once it finishes.
8363
+ */
8364
+ StepClusters() {
8365
+ if (!this.clusterJob) {
8366
+ return;
8367
+ }
8368
+ const deadline = performance.now() + CLUSTER_STEP_BUDGET;
8369
+ let next = this.clusterJob.next();
8370
+ while (!next.done && performance.now() < deadline) {
8371
+ next = this.clusterJob.next();
8372
+ }
8373
+ if (next.done) {
8374
+ this.placementMemory.clusters = next.value;
8375
+ this.clusterJob = null;
8376
+ }
8377
+ else {
8378
+ this.viewer.scene.requestRender();
8379
+ }
8380
+ }
8151
8381
  logFailure(participant, error) {
8152
8382
  if (this.failed.has(participant)) {
8153
8383
  return;
@@ -9452,8 +9682,7 @@
9452
9682
  target.addEventListener(type, event => event.stopPropagation());
9453
9683
  }
9454
9684
  };
9455
- // A pin needs an icon to wrap; a photo keeps its own shape, and plain text keeps a straight stem.
9456
- const pin = this.options.leaderStyle === "pin" && look.leader !== false && presentation !== "text" && !look.iconPhoto;
9685
+ const pin = HasPin(look);
9457
9686
  const normalLeader = (_m = look.leader) !== null && _m !== void 0 ? _m : (presentation === "text" || pin);
9458
9687
  const angled = this.options.leaderStyle === "angled";
9459
9688
  let leader = normalLeader;
@@ -9514,11 +9743,9 @@
9514
9743
  makeClickable(iconStack);
9515
9744
  iconContainer.append(iconStack, icon);
9516
9745
  if (pin) {
9517
- // The shadow sits on an unturned holder so it falls the same way whichever way the tip points.
9518
9746
  pinHolder = document.createElement("span");
9519
9747
  pinHolder.className = "entity-label-pin";
9520
- pinHolder.style.cssText = "position:absolute;left:50%;top:50%;z-index:-1;pointer-events:none;" +
9521
- "filter:drop-shadow(1px 2px 2px rgba(0, 0, 0, 0.35));";
9748
+ pinHolder.style.cssText = "position:absolute;left:50%;top:50%;z-index:-1;pointer-events:none;";
9522
9749
  pinBody = document.createElement("span");
9523
9750
  pinBody.style.cssText = `position:absolute;inset:0;background:${PIN_COLOR};border:1px solid rgba(0, 0, 0, 0.14);` +
9524
9751
  "box-sizing:border-box;border-radius:50% 50% 0 50%;";
@@ -9600,7 +9827,10 @@
9600
9827
  border-radius: 9px;
9601
9828
  color: #ffffff;
9602
9829
  text-shadow: 0 0 5px rgba(0, 0, 0, 0.8);
9830
+ will-change: transform;
9603
9831
  `);
9832
+ // Each label, leader and ring gets its own compositor layer: in one shared layer, moving any of them
9833
+ // repainted all of them, shadows included, on every camera move.
9604
9834
  this.label = ele;
9605
9835
  ele.style.width = "max-content";
9606
9836
  layer.element.appendChild(ele);
@@ -9619,6 +9849,7 @@
9619
9849
  width: 2px;
9620
9850
  background: rgba(255, 255, 255, 0.8);
9621
9851
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
9852
+ will-change: transform;
9622
9853
  `);
9623
9854
  this.line = lineEle;
9624
9855
  lineEle.className = "entity-label-leader";
@@ -9636,6 +9867,7 @@
9636
9867
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
9637
9868
  top: -2px;
9638
9869
  left: -2px;
9870
+ will-change: transform;
9639
9871
  `);
9640
9872
  lineEleNode.className = "entity-label-anchor";
9641
9873
  // SVG preserves fractional strokes in scaled containers where CSS borders round to pixels.
@@ -9668,13 +9900,13 @@
9668
9900
  }
9669
9901
  const fitLeader = (anchorX, anchorY, desired, width, height, radius = 0) => {
9670
9902
  const above = Math.max(0, anchorY - height);
9671
- const below = Math.max(0, this.viewer.canvas.clientHeight - anchorY - height);
9903
+ const below = Math.max(0, layer.viewport.height - anchorY - height);
9672
9904
  // Keep a short visible stem before switching sides; never aim it at the cursor or a box corner.
9673
9905
  const upwards = above >= Math.max(radius ? radius + 4 : 0, Math.min(8, desired)) || above >= below;
9674
9906
  const length = Math.min(desired, upwards ? above : below);
9675
9907
  const endY = anchorY + (upwards ? -length : length);
9676
9908
  return {
9677
- x: Math.max(0, Math.min(anchorX, this.viewer.canvas.clientWidth - width)),
9909
+ x: Math.max(0, Math.min(anchorX, layer.viewport.width - width)),
9678
9910
  y: upwards ? endY - height : endY, endY
9679
9911
  };
9680
9912
  };
@@ -9683,17 +9915,17 @@
9683
9915
  * so the stem rises to the label's middle and then runs out to whichever side has the room.
9684
9916
  */
9685
9917
  const fitAngledLeader = (anchorX, anchorY, desired, width, height, radius = 0) => {
9686
- const canvas = this.viewer.canvas;
9918
+ const canvas = layer.viewport;
9687
9919
  const above = Math.max(0, anchorY - height / 2);
9688
- const below = Math.max(0, canvas.clientHeight - anchorY - height / 2);
9920
+ const below = Math.max(0, canvas.height - anchorY - height / 2);
9689
9921
  const upwards = above >= Math.max(radius ? radius + 4 : 0, Math.min(8, desired)) || above >= below;
9690
9922
  const length = Math.min(desired, upwards ? above : below);
9691
9923
  const endY = anchorY + (upwards ? -length : length);
9692
9924
  const run = Math.max(ANGLED_LEADER_RUN, radius + 4);
9693
- const rightwards = anchorX + run + width <= canvas.clientWidth || anchorX - run - width < 0;
9925
+ const rightwards = anchorX + run + width <= canvas.width || anchorX - run - width < 0;
9694
9926
  return {
9695
- x: Math.max(0, Math.min(rightwards ? anchorX + run : anchorX - run - width, canvas.clientWidth - width)),
9696
- y: Math.max(0, Math.min(endY - height / 2, canvas.clientHeight - height)), endY,
9927
+ x: Math.max(0, Math.min(rightwards ? anchorX + run : anchorX - run - width, canvas.width - width)),
9928
+ y: Math.max(0, Math.min(endY - height / 2, canvas.height - height)), endY,
9697
9929
  run, side: (rightwards ? "right" : "left")
9698
9930
  };
9699
9931
  };
@@ -9717,13 +9949,13 @@
9717
9949
  * Off to the side it is only clamped back on screen, and its tip turns to keep pointing at the anchor.
9718
9950
  */
9719
9951
  const fitPin = (anchorX, anchorY, stem, width, height, shape) => {
9720
- const canvas = this.viewer.canvas;
9952
+ const canvas = layer.viewport;
9721
9953
  const reach = stem + shape.tip;
9722
9954
  const above = anchorY - reach - shape.y;
9723
9955
  const below = anchorY + reach - shape.y;
9724
- const upwards = above >= 0 || (below + height > canvas.clientHeight && anchorY >= canvas.clientHeight - anchorY);
9956
+ const upwards = above >= 0 || (below + height > canvas.height && anchorY >= canvas.height - anchorY);
9725
9957
  return {
9726
- x: Math.max(0, Math.min(anchorX - shape.x, canvas.clientWidth - width)),
9958
+ x: Math.max(0, Math.min(anchorX - shape.x, canvas.width - width)),
9727
9959
  y: upwards ? above : below
9728
9960
  };
9729
9961
  };
@@ -9841,6 +10073,43 @@
9841
10073
  this.viewer.scene.requestRender();
9842
10074
  }).finally(settle);
9843
10075
  }
10076
+ /*
10077
+ * The label's size when last laid out. A label hidden in a cluster leaves layout altogether, so a
10078
+ * frame reads its size from here; a shown one refreshes it when it changes, a new one on its first frame.
10079
+ */
10080
+ let size = null;
10081
+ const measureSize = () => {
10082
+ size = {
10083
+ width: ele.offsetWidth,
10084
+ height: ele.offsetHeight,
10085
+ textWidth: (sourceText === null || sourceText === void 0 ? void 0 : sourceText.offsetWidth) || 0,
10086
+ textHeight: (sourceText === null || sourceText === void 0 ? void 0 : sourceText.offsetHeight) || 0,
10087
+ iconWidth: (iconElement === null || iconElement === void 0 ? void 0 : iconElement.offsetWidth) || 0,
10088
+ iconHeight: (iconElement === null || iconElement === void 0 ? void 0 : iconElement.offsetHeight) || 0,
10089
+ naturalTextWidth: (sourceNaturalText === null || sourceNaturalText === void 0 ? void 0 : sourceNaturalText.offsetWidth) || 0,
10090
+ pin: pin ? measurePin() : undefined
10091
+ };
10092
+ };
10093
+ // Whether the label was out of layout last frame, so staying hidden writes nothing.
10094
+ let hiddenLast = false;
10095
+ // Set by this frame's writes that change the label's size, so it is measured before it is placed.
10096
+ let sizeStale = true;
10097
+ // Set when the size changed on its own; a shown label reads it at the start of the next frame, while layout
10098
+ // is still clean, rather than forcing a layout mid-frame.
10099
+ let resizedSince = false;
10100
+ // The cluster badge's own size beside its icon, read with the label's.
10101
+ let badge = null;
10102
+ // Images, fonts and photo transitions change the size outside any write here. Without an observer the size
10103
+ // is read every frame instead.
10104
+ const resized = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(() => {
10105
+ if (this.disposed || this.counter !== counter) {
10106
+ resized.disconnect();
10107
+ return;
10108
+ }
10109
+ resizedSince = true;
10110
+ this.viewer.scene.requestRender();
10111
+ });
10112
+ resized === null || resized === void 0 ? void 0 : resized.observe(ele);
9844
10113
  let cursorMotion = this.cursorMotion;
9845
10114
  const project = () => {
9846
10115
  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;
@@ -9852,6 +10121,11 @@
9852
10121
  (this.hoverFade.from === 0 || Date.now() - this.hoverFade.start >= 120)) {
9853
10122
  return null;
9854
10123
  }
10124
+ // A new label is still in layout, so its first frame reads its size, as does a shown one that resized.
10125
+ if (!size || (resizedSince && !hiddenLast)) {
10126
+ measureSize();
10127
+ resizedSince = false;
10128
+ }
9855
10129
  // Moving point/model positions must be sampled at the current clock time.
9856
10130
  if (!this.prePositioned && visual instanceof Cesium.Entity && visual.position) {
9857
10131
  const position = getValue(this.viewer, visual.position);
@@ -9899,8 +10173,8 @@
9899
10173
  y: ringPoint.y + (boxTarget.y - ringPoint.y) * boxBlend
9900
10174
  } : undefined;
9901
10175
  const start = boxAnchor || anchor;
9902
- const onScreen = anchor.x >= 0 && anchor.y >= 0 && anchor.x < this.viewer.canvas.clientWidth && anchor.y < this.viewer.canvas.clientHeight;
9903
- if (leader && (start.x < 0 || start.y < 0 || start.x >= this.viewer.canvas.clientWidth || start.y >= this.viewer.canvas.clientHeight))
10176
+ const onScreen = anchor.x >= 0 && anchor.y >= 0 && anchor.x < layer.viewport.width && anchor.y < layer.viewport.height;
10177
+ if (leader && (start.x < 0 || start.y < 0 || start.x >= layer.viewport.width || start.y >= layer.viewport.height))
9904
10178
  return null;
9905
10179
  if (onScreen && !this.imageAsked && this.groupId && !HasIcon(this.options) && !this.options.photo) {
9906
10180
  this.imageAsked = true;
@@ -9944,8 +10218,8 @@
9944
10218
  if (cursorAmount > 0 && !pinned) {
9945
10219
  // A leader-less label starts centred on its anchor; preserve that box as it lifts away.
9946
10220
  if (!normalLeader) {
9947
- x -= ele.offsetWidth / layer.coordinates.scaleX / 2;
9948
- y += ele.offsetHeight / layer.coordinates.scaleY / 2;
10221
+ x -= size.width / layer.coordinates.scaleX / 2;
10222
+ y += size.height / layer.coordinates.scaleY / 2;
9949
10223
  }
9950
10224
  x += (cursorMotion.position.x + pixelOffset.x - x) * cursorAmount;
9951
10225
  y += (cursorMotion.position.y + pixelOffset.y - y) * cursorAmount;
@@ -9995,17 +10269,16 @@
9995
10269
  x = hover.x + pixelOffset.x;
9996
10270
  y = hover.y + pixelOffset.y - 24;
9997
10271
  }
9998
- // Hidden participants remain measurable; visibility never removes their layout boxes.
9999
10272
  const coordinates = layer.coordinates;
10000
- const width = ele.offsetWidth / coordinates.scaleX;
10001
- const height = ele.offsetHeight / coordinates.scaleY;
10273
+ const width = size.width / coordinates.scaleX;
10274
+ const height = size.height / coordinates.scaleY;
10002
10275
  let clusterBounds;
10003
10276
  if ((_u = this.options.cluster) === null || _u === void 0 ? void 0 : _u.enabled) {
10004
- const textWidth = (sourceText === null || sourceText === void 0 ? void 0 : sourceText.offsetWidth) || 0;
10005
- const textHeight = (sourceText === null || sourceText === void 0 ? void 0 : sourceText.offsetHeight) || 0;
10277
+ const textWidth = size.textWidth;
10278
+ const textHeight = size.textHeight;
10006
10279
  // A photo is never part of the footprint, so its arrival cannot change which cluster a label joins.
10007
- const iconWidth = look.iconPhoto ? 0 : (iconElement === null || iconElement === void 0 ? void 0 : iconElement.offsetWidth) || 0;
10008
- const iconHeight = look.iconPhoto ? 0 : (iconElement === null || iconElement === void 0 ? void 0 : iconElement.offsetHeight) || 0;
10280
+ const iconWidth = look.iconPhoto ? 0 : size.iconWidth;
10281
+ const iconHeight = look.iconPhoto ? 0 : size.iconHeight;
10009
10282
  const gap = sourceText && iconWidth ? 6 : 0;
10010
10283
  const above = presentation === "icon-text-above";
10011
10284
  // Match the fixed content gap and label padding, excluding the count badge.
@@ -10024,7 +10297,7 @@
10024
10297
  let leaderRun;
10025
10298
  let leaderSide;
10026
10299
  if (leader && pin)
10027
- ({ x, y } = fitPin(anchorX, anchorY, leaderLength, width, height, measurePin()));
10300
+ ({ x, y } = fitPin(anchorX, anchorY, leaderLength, width, height, size.pin));
10028
10301
  else if (leader && angled) {
10029
10302
  const fit = fitAngledLeader(anchorX, anchorY, leaderLength, width, height);
10030
10303
  ({ x, y } = fit);
@@ -10033,17 +10306,17 @@
10033
10306
  else if (leader)
10034
10307
  ({ x, y } = fitLeader(anchorX, anchorY, leaderLength, width, height));
10035
10308
  const bounds = clusterBounds || { x, y, width, height };
10036
- if (!leader && (bounds.x + bounds.width < 0 || bounds.y + bounds.height < 0 || bounds.x > this.viewer.canvas.clientWidth || bounds.y > this.viewer.canvas.clientHeight)) {
10309
+ if (!leader && (bounds.x + bounds.width < 0 || bounds.y + bounds.height < 0 || bounds.x > layer.viewport.width || bounds.y > layer.viewport.height)) {
10037
10310
  return null;
10038
10311
  }
10039
10312
  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 });
10040
10313
  const opacity = this.prePositioned || hoverOnly || MAX_DISTANCE === Infinity ? 1 : 1 - distance / MAX_DISTANCE;
10041
10314
  return {
10042
10315
  canFlip: leader && presentation !== "icon" && !pin, hasLeader: leader,
10043
- textWrapped: ((sourceNaturalText === null || sourceNaturalText === void 0 ? void 0 : sourceNaturalText.offsetWidth) || 0) > TEXT_MAX_WIDTH,
10316
+ textWrapped: size.naturalTextWidth > TEXT_MAX_WIDTH,
10044
10317
  transient: this.Transient || Boolean(hover) || cursorAmount > 0,
10045
10318
  extendedGeometry: visual instanceof Cesium.Entity && Boolean(visual.polygon || visual.polyline || visual.corridor),
10046
- viewportWidth: this.viewer.canvas.clientWidth,
10319
+ viewportWidth: layer.viewport.width,
10047
10320
  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,
10048
10321
  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,
10049
10322
  iconRadius: icon === null || icon === void 0 ? void 0 : icon.radius, modelBox: boxed || undefined, boxBlend, follows: !pinned && Boolean(transientPivot || cursorMotion.target),
@@ -10054,10 +10327,19 @@
10054
10327
  let displacement = { x: 0, y: 0 };
10055
10328
  let nudge = { x: 0, y: 0, fromX: 0, fromY: 0, targetX: 0, targetY: 0, start: 0 };
10056
10329
  const apply = (placement) => {
10057
- 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;
10330
+ var _a, _b, _c, _d, _e;
10058
10331
  const show = (placement === null || placement === void 0 ? void 0 : placement.visible) && placement.opacity > 0;
10332
+ if (!show && hiddenLast) {
10333
+ return;
10334
+ }
10335
+ if (show && hiddenLast) {
10336
+ sizeStale = true;
10337
+ }
10338
+ hiddenLast = !show;
10059
10339
  shownOnce = shownOnce || Boolean(show);
10060
10340
  clickEntityIds = show ? placement.entityIds : [];
10341
+ // Out of layout while hidden, so thousands of clustered labels cost the browser nothing to lay out.
10342
+ ele.style.display = show ? "flex" : "none";
10061
10343
  ele.style.visibility = show ? "visible" : "hidden";
10062
10344
  lineEle.style.display = show && leader ? "block" : "none";
10063
10345
  if (runEle)
@@ -10096,6 +10378,7 @@
10096
10378
  const displayText = (_c = placement.text) !== null && _c !== void 0 ? _c : originalText;
10097
10379
  if (textElement && textElement.textContent !== displayText) {
10098
10380
  textElement.textContent = displayText;
10381
+ sizeStale = true;
10099
10382
  // Measure the summary's footprint next frame without replacing each member's source text.
10100
10383
  this.viewer.scene.requestRender();
10101
10384
  }
@@ -10110,10 +10393,10 @@
10110
10393
  const count = placement.members.length ? String(placement.members.length) : "";
10111
10394
  if (countElement.textContent !== count) {
10112
10395
  countElement.textContent = count;
10396
+ sizeStale = true;
10113
10397
  // The badge participates in layout, so its new width needs another measurement.
10114
10398
  this.viewer.scene.requestRender();
10115
10399
  }
10116
- fitIconBackground === null || fitIconBackground === void 0 ? void 0 : fitIconBackground();
10117
10400
  const stack = JSON.stringify(placement.icons || []);
10118
10401
  if (iconStack && stack !== displayedStack) {
10119
10402
  displayedStack = stack;
@@ -10129,6 +10412,7 @@
10129
10412
  built[0].icon.style.gridColumn = "1 / span 2";
10130
10413
  }
10131
10414
  iconStack.replaceChildren(...built.map(({ icon }) => icon));
10415
+ sizeStale = true;
10132
10416
  iconStack.style.display = built.length ? "inline-grid" : "none";
10133
10417
  // The glyph stays measurable, since cluster membership is taken from its footprint.
10134
10418
  iconElement.style.visibility = built.length ? "hidden" : "";
@@ -10137,255 +10421,301 @@
10137
10421
  // The stack widens the label, which the next frame has to measure.
10138
10422
  this.viewer.scene.requestRender();
10139
10423
  }
10140
- for (const fit of stackedFits)
10141
- fit();
10142
- if (badgeOnIcon) {
10143
- const badgeWidth = countElement.offsetWidth;
10144
- const iconWidth = iconElement.offsetWidth;
10145
- const narrow = iconWidth < iconElement.offsetHeight * 0.6;
10146
- const clearance = badgeWidth ? (narrow ? badgeWidth / 2 + 2 : Math.max(0, badgeWidth / 2 - iconWidth / 4)) : 0;
10147
- countElement.style.left = `calc(100% + ${clearance}px)`;
10148
- // Reserve the badge overhang so adjacent text and viewport edges remain clear.
10149
- const overhang = countElement.offsetHeight / 2;
10150
- iconContainer.style.marginTop = badgeBelow ? "0px" : `${overhang}px`;
10151
- iconContainer.style.marginBottom = badgeBelow ? `${overhang}px` : "0px";
10152
- iconContainer.style.marginRight = `${badgeWidth / 2 + clearance}px`;
10153
- }
10154
- // Summary and badge changes must fit on their first visible frame, including after resize.
10155
- const ring = this.options.anchorRing;
10156
- const pointer = (layer.pointer || (cursorMotion.target ? cursorMotion.position : undefined) || (this.Transient && !this.Persistent ?
10157
- (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));
10158
- const iconRing = frame.iconRadius ? Math.max(ICON_RING_MIN, frame.iconRadius + ICON_RING_PADDING) : 0;
10159
- const boxed = leader ? frame.modelBox : undefined;
10160
- const hovered = pointer && (boxed ? hullContains(boxed.hull, pointer) :
10161
- 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));
10162
- if (look.iconPhoto && growPhoto) {
10163
- // A rectangle leans on the leader's side of the label.
10164
- if (photoShape === "rect") {
10165
- const align = !leader ? "center" : placement.textSide === "left" ? "flex-end" : "flex-start";
10166
- if (content.style.alignItems !== align) {
10167
- content.style.alignItems = align;
10168
- }
10169
- }
10170
- // Grown in layout rather than scaled, so the border keeps its width, and the label, whose bottom
10171
- // sits on the leader, grows up and sideways without changing the leader's length.
10172
- growPhoto(Boolean(hovered) && !((_k = placement.members) === null || _k === void 0 ? void 0 : _k.length));
10173
- }
10174
- 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));
10175
- const targetRadius = normalRadius + (15.4 - normalRadius) * cursorMotion.amount;
10176
- // Around a glyph the ring keeps the glyph's size and only fades its stroke in, so it never covers the icon.
10177
- // A ring around the cursor marks nothing, so a riding label keeps only its leader.
10178
- const targetOuter = frame.follows ? 0 : iconRing || targetRadius || 2.5;
10179
- const targetInner = frame.follows ? 0 : iconRing ? (targetRadius > 0 ? iconRing - 2 : iconRing) : Math.max(0, targetRadius - 2);
10180
- const now = Date.now();
10181
- const duration = (_p = ring === null || ring === void 0 ? void 0 : ring.transitionDuration) !== null && _p !== void 0 ? _p : 200;
10182
- const sharedRing = this.Transient && !this.Persistent && this.hoverFade.to === 1 ? this.hoverRingState : undefined;
10183
- let ringAnimation = sharedRing ? sharedRing.animation : this.ringAnimation;
10184
- if (!ringAnimation) {
10185
- const outer = frame.follows ? 0 : iconRing || 2.5;
10186
- const inner = frame.follows ? 0 : iconRing;
10187
- ringAnimation = { target: outer, targetInner: inner, start: now, outer, inner, fromOuter: outer, fromInner: inner };
10188
- }
10189
- const animateRing = () => {
10190
- const progress = duration > 0 ? Math.min(1, Math.max(0, (now - ringAnimation.start) / duration)) : 1;
10191
- const eased = progress * progress * (3 - 2 * progress);
10192
- ringAnimation.outer = ringAnimation.fromOuter + (ringAnimation.target - ringAnimation.fromOuter) * eased;
10193
- ringAnimation.inner = ringAnimation.fromInner + (ringAnimation.targetInner - ringAnimation.fromInner) * eased;
10194
- return progress;
10195
- };
10196
- animateRing();
10197
- if (ringAnimation.target !== targetOuter || ringAnimation.targetInner !== targetInner) {
10198
- ringAnimation = {
10199
- ...ringAnimation,
10200
- target: targetOuter,
10201
- targetInner,
10202
- start: now,
10203
- fromOuter: ringAnimation.outer,
10204
- fromInner: ringAnimation.inner
10424
+ /*
10425
+ * Everything above writes the label's content. The layer runs what follows in two passes over all
10426
+ * its labels, measuring every one before placing any, so no label reads layout another just changed.
10427
+ */
10428
+ return () => {
10429
+ if (sizeStale || !resized) {
10430
+ fitIconBackground === null || fitIconBackground === void 0 ? void 0 : fitIconBackground();
10431
+ for (const fit of stackedFits)
10432
+ fit();
10433
+ badge = badgeOnIcon ? {
10434
+ width: countElement.offsetWidth,
10435
+ height: countElement.offsetHeight,
10436
+ iconWidth: iconElement.offsetWidth,
10437
+ iconHeight: iconElement.offsetHeight
10438
+ } : null;
10439
+ measureSize();
10440
+ sizeStale = false;
10441
+ }
10442
+ const pinShape = leader && pin ? size.pin : undefined;
10443
+ const measuredWidth = size.width / coordinates.scaleX;
10444
+ const measuredHeight = size.height / coordinates.scaleY;
10445
+ return () => {
10446
+ 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;
10447
+ if (badge) {
10448
+ const narrow = badge.iconWidth < badge.iconHeight * 0.6;
10449
+ const clearance = badge.width ? (narrow ? badge.width / 2 + 2 : Math.max(0, badge.width / 2 - badge.iconWidth / 4)) : 0;
10450
+ // Reserve the badge overhang so adjacent text and viewport edges remain clear.
10451
+ const overhang = badge.height / 2;
10452
+ const margins = [
10453
+ badgeBelow ? "0px" : `${overhang}px`, badgeBelow ? `${overhang}px` : "0px", `${badge.width / 2 + clearance}px`
10454
+ ];
10455
+ countElement.style.left = `calc(100% + ${clearance}px)`;
10456
+ if (iconContainer.style.marginTop !== margins[0] ||
10457
+ iconContainer.style.marginBottom !== margins[1] ||
10458
+ iconContainer.style.marginRight !== margins[2]) {
10459
+ [iconContainer.style.marginTop, iconContainer.style.marginBottom, iconContainer.style.marginRight] = margins;
10460
+ sizeStale = true;
10461
+ // The margins change the label's size, which the next frame measures.
10462
+ this.viewer.scene.requestRender();
10463
+ }
10464
+ }
10465
+ const ring = this.options.anchorRing;
10466
+ const pointer = (layer.pointer || (cursorMotion.target ? cursorMotion.position : undefined) || (this.Transient && !this.Persistent ?
10467
+ (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));
10468
+ const iconRing = frame.iconRadius ? Math.max(ICON_RING_MIN, frame.iconRadius + ICON_RING_PADDING) : 0;
10469
+ const boxed = leader ? frame.modelBox : undefined;
10470
+ const hovered = pointer && (boxed ? hullContains(boxed.hull, pointer) :
10471
+ 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));
10472
+ if (look.iconPhoto && growPhoto) {
10473
+ // A rectangle leans on the leader's side of the label.
10474
+ if (photoShape === "rect") {
10475
+ const align = !leader ? "center" : placement.textSide === "left" ? "flex-end" : "flex-start";
10476
+ if (content.style.alignItems !== align) {
10477
+ content.style.alignItems = align;
10478
+ }
10479
+ }
10480
+ // Grown in layout rather than scaled, so the border keeps its width, and the label,
10481
+ // whose bottom sits on the leader, grows up and sideways without changing its length.
10482
+ growPhoto(Boolean(hovered) && !((_e = placement.members) === null || _e === void 0 ? void 0 : _e.length));
10483
+ sizeStale = true;
10484
+ }
10485
+ 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));
10486
+ const targetRadius = normalRadius + (15.4 - normalRadius) * cursorMotion.amount;
10487
+ // Around a glyph the ring keeps its size and only fades its stroke in, never covering it.
10488
+ // A ring around the cursor marks nothing, so a riding label keeps only its leader.
10489
+ const targetOuter = frame.follows ? 0 : iconRing || targetRadius || 2.5;
10490
+ const targetInner = frame.follows ? 0 : iconRing ? (targetRadius > 0 ? iconRing - 2 : iconRing) : Math.max(0, targetRadius - 2);
10491
+ const now = Date.now();
10492
+ const duration = (_j = ring === null || ring === void 0 ? void 0 : ring.transitionDuration) !== null && _j !== void 0 ? _j : 200;
10493
+ const sharedRing = this.Transient && !this.Persistent && this.hoverFade.to === 1 ? this.hoverRingState : undefined;
10494
+ let ringAnimation = sharedRing ? sharedRing.animation : this.ringAnimation;
10495
+ if (!ringAnimation) {
10496
+ const outer = frame.follows ? 0 : iconRing || 2.5;
10497
+ const inner = frame.follows ? 0 : iconRing;
10498
+ ringAnimation = { target: outer, targetInner: inner, start: now, outer, inner, fromOuter: outer, fromInner: inner };
10499
+ }
10500
+ const animateRing = () => {
10501
+ const progress = duration > 0 ? Math.min(1, Math.max(0, (now - ringAnimation.start) / duration)) : 1;
10502
+ const eased = progress * progress * (3 - 2 * progress);
10503
+ ringAnimation.outer = ringAnimation.fromOuter + (ringAnimation.target - ringAnimation.fromOuter) * eased;
10504
+ ringAnimation.inner = ringAnimation.fromInner + (ringAnimation.targetInner - ringAnimation.fromInner) * eased;
10505
+ return progress;
10506
+ };
10507
+ animateRing();
10508
+ if (ringAnimation.target !== targetOuter || ringAnimation.targetInner !== targetInner) {
10509
+ ringAnimation = {
10510
+ ...ringAnimation,
10511
+ target: targetOuter,
10512
+ targetInner,
10513
+ start: now,
10514
+ fromOuter: ringAnimation.outer,
10515
+ fromInner: ringAnimation.inner
10516
+ };
10517
+ }
10518
+ // Animate the hole and stem together; no CSS transition may lag behind a moving world anchor.
10519
+ if (animateRing() < 1 && (ringAnimation.fromOuter !== targetOuter || ringAnimation.fromInner !== targetInner)) {
10520
+ this.viewer.scene.requestRender();
10521
+ }
10522
+ this.ringAnimation = ringAnimation;
10523
+ if (sharedRing) {
10524
+ sharedRing.animation = ringAnimation;
10525
+ }
10526
+ const ringShare = 1 - (frame.boxBlend || 0);
10527
+ const radius = ringShare > 0 && ringAnimation.inner > 0 ? ringAnimation.outer * ringShare : 0;
10528
+ const stem = (pinShape ? fitPin(frame.anchorX, frame.anchorY, frame.leaderLength, measuredWidth, measuredHeight, pinShape) :
10529
+ leader ? (angled ? fitAngledLeader : fitLeader)(frame.anchorX, frame.anchorY, Math.max(frame.leaderLength, radius ? radius + 4 : 0), measuredWidth, measuredHeight, radius) : undefined);
10530
+ const position = { x: (stem || frame).x, y: (stem || frame).y };
10531
+ const width = measuredWidth;
10532
+ const height = measuredHeight;
10533
+ if (leader && angled) {
10534
+ // The declutter owns the side; the fit only says which one had the room to begin with.
10535
+ const run = Math.max(ANGLED_LEADER_RUN, radius + 4);
10536
+ const side = (_l = (_k = placement.textSide) !== null && _k !== void 0 ? _k : frame.leaderSide) !== null && _l !== void 0 ? _l : "right";
10537
+ const beside = side === "left" ? frame.anchorX - run - width : frame.anchorX + run;
10538
+ position.x = Math.max(0, Math.min(beside, layer.viewport.width - width));
10539
+ }
10540
+ else if (leader && !pin && placement.textSide === "left") {
10541
+ position.x = Math.max(0, Math.min(frame.anchorX - width, layer.viewport.width - width));
10542
+ }
10543
+ const gap = (_o = (_m = this.options.pointerAvoidance) === null || _m === void 0 ? void 0 : _m.gap) !== null && _o !== void 0 ? _o : 12;
10544
+ const containsPointer = (x, y, margin) => {
10545
+ return pointer && pointer.x >= x - margin && pointer.x <= x + width + margin && pointer.y >= y - margin && pointer.y <= y + height + margin;
10546
+ };
10547
+ const passive = !selectOnClick || !this.visualRegister;
10548
+ if (passive && ((_p = this.options.pointerAvoidance) === null || _p === void 0 ? void 0 : _p.enabled) !== false && pointer &&
10549
+ (containsPointer(position.x, position.y, gap) || containsPointer(position.x + displacement.x, position.y + displacement.y, gap))) {
10550
+ if (containsPointer(position.x + displacement.x, position.y + displacement.y, 0)) {
10551
+ // Choose against the original box; keep the offset until the pointer leaves both.
10552
+ const candidates = [
10553
+ { x: 0, y: pointer.y - gap - height - position.y },
10554
+ { x: 0, y: pointer.y + gap - position.y },
10555
+ { x: pointer.x - gap - width - position.x, y: 0 },
10556
+ { x: pointer.x + gap - position.x, y: 0 }
10557
+ ].filter(offset => position.x + offset.x >= 0 && position.y + offset.y >= 0 &&
10558
+ position.x + offset.x + width <= layer.viewport.width && position.y + offset.y + height <= layer.viewport.height)
10559
+ .sort((a, b) => Math.hypot(a.x, a.y) - Math.hypot(b.x, b.y));
10560
+ displacement = candidates[0] || { x: 0, y: 0 };
10561
+ }
10562
+ }
10563
+ else {
10564
+ displacement = { x: 0, y: 0 };
10565
+ }
10566
+ const nudgeDuration = (_r = (_q = this.options.pointerAvoidance) === null || _q === void 0 ? void 0 : _q.transitionDuration) !== null && _r !== void 0 ? _r : 200;
10567
+ const animateNudge = () => {
10568
+ const progress = now < nudge.start ? 0 : nudgeDuration > 0 ? Math.min(1, (now - nudge.start) / nudgeDuration) : 1;
10569
+ const eased = progress * progress * (3 - 2 * progress);
10570
+ nudge.x = nudge.fromX + (nudge.targetX - nudge.fromX) * eased;
10571
+ nudge.y = nudge.fromY + (nudge.targetY - nudge.fromY) * eased;
10572
+ return progress;
10573
+ };
10574
+ animateNudge();
10575
+ if (nudge.targetX !== displacement.x || nudge.targetY !== displacement.y) {
10576
+ nudge = {
10577
+ ...nudge,
10578
+ fromX: nudge.x,
10579
+ fromY: nudge.y,
10580
+ targetX: displacement.x,
10581
+ targetY: displacement.y,
10582
+ start: now + ((_t = (_s = this.options.pointerAvoidance) === null || _s === void 0 ? void 0 : _s.delay) !== null && _t !== void 0 ? _t : 180)
10583
+ };
10584
+ }
10585
+ if (animateNudge() < 1 && (nudge.fromX !== nudge.targetX || nudge.fromY !== nudge.targetY)) {
10586
+ this.viewer.scene.requestRender();
10587
+ }
10588
+ position.x += nudge.x;
10589
+ position.y += nudge.y;
10590
+ ele.style.left = `${coordinates.x + position.x * coordinates.scaleX}px`;
10591
+ ele.style.top = `${coordinates.y + position.y * coordinates.scaleY}px`;
10592
+ for (const clickTarget of Array.from(content.querySelectorAll('[role="button"]'))) {
10593
+ 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}`);
10594
+ }
10595
+ if (leader) {
10596
+ const nodeRadius = ringAnimation.outer * (1 - (frame.boxBlend || 0));
10597
+ const anchor = this.anchor;
10598
+ anchor.style.left = `${coordinates.x + (frame.anchorX - nodeRadius) * coordinates.scaleX}px`;
10599
+ anchor.style.top = `${coordinates.y + (frame.anchorY - nodeRadius) * coordinates.scaleY}px`;
10600
+ anchor.style.width = `${nodeRadius * 2 * coordinates.scaleX}px`;
10601
+ anchor.style.height = `${nodeRadius * 2 * coordinates.scaleY}px`;
10602
+ anchor.style.boxSizing = "border-box";
10603
+ anchor.style.background = "transparent";
10604
+ const svg = anchor.firstElementChild;
10605
+ svg.setAttribute("viewBox", `0 0 ${nodeRadius * 2} ${nodeRadius * 2}`);
10606
+ const circle = svg.firstElementChild;
10607
+ circle.setAttribute("cx", String(nodeRadius));
10608
+ circle.setAttribute("cy", String(nodeRadius));
10609
+ const innerRadius = ringAnimation.inner * (1 - (frame.boxBlend || 0));
10610
+ circle.setAttribute("r", String((nodeRadius + innerRadius) / 2));
10611
+ circle.setAttribute("stroke", this.options.leaderColor || "rgba(255, 255, 255, 0.8)");
10612
+ circle.setAttribute("stroke-width", String(nodeRadius - innerRadius));
10613
+ anchor.style.boxShadow = radius ? "none" : "0 0 5px rgba(0, 0, 0, 0.3)";
10614
+ anchor.style.opacity = String(opacity * (normalLeader ? 1 : cursorMotion.amount) * (1 - (frame.boxBlend || 0)));
10615
+ anchor.style.zIndex = String(placement.zIndex);
10616
+ // The stem starts at the rim, never across the clear centre, even after displacement.
10617
+ const straightX = Math.max(position.x, Math.min(frame.anchorX, position.x + width));
10618
+ const straightY = frame.anchorY < position.y ? position.y : frame.anchorY > position.y + height ? position.y + height : frame.anchorY;
10619
+ const beside = angled && (frame.anchorX < position.x || frame.anchorX > position.x + width);
10620
+ const elbowY = position.y + height / 2;
10621
+ let endX = beside ? frame.anchorX : straightX;
10622
+ let endY = beside ? elbowY : straightY;
10623
+ if (pinShape) {
10624
+ // Aimed from wherever clamping or a nudge left it; a stem, if any, meets the tip.
10625
+ const centreX = position.x + pinShape.x, centreY = position.y + pinShape.y;
10626
+ const aim = Math.atan2(frame.anchorY - centreY, frame.anchorX - centreX);
10627
+ const reach = Math.min(pinShape.tip, Math.hypot(frame.anchorX - centreX, frame.anchorY - centreY));
10628
+ endX = centreX + Math.cos(aim) * reach;
10629
+ endY = centreY + Math.sin(aim) * reach;
10630
+ const size = `${pinShape.radius * 2 * coordinates.scaleX}px`;
10631
+ if (pinHolder.style.width !== size) {
10632
+ pinHolder.style.width = pinHolder.style.height = size;
10633
+ pinHolder.style.margin = `-${pinShape.radius * coordinates.scaleX}px`;
10634
+ }
10635
+ // The body's square corner starts lower right, a quarter turn short of down.
10636
+ const turn = Math.round((aim - Math.PI / 4) * 200) / 200;
10637
+ const rotation = `rotate(${turn}rad)`;
10638
+ if (pinBody.style.transform !== rotation) {
10639
+ pinBody.style.transform = rotation;
10640
+ // A box shadow turns with the body, so its offset is turned back to fall down
10641
+ // and right; a drop-shadow filter would not turn, but repaints every moving pin.
10642
+ const offsetX = Math.round((Math.cos(turn) + 2 * Math.sin(turn)) * 4) / 4;
10643
+ const offsetY = Math.round((2 * Math.cos(turn) - Math.sin(turn)) * 4) / 4;
10644
+ pinBody.style.boxShadow = `${offsetX}px ${offsetY}px 3px rgba(0, 0, 0, 0.35)`;
10645
+ }
10646
+ }
10647
+ const runToX = frame.anchorX < position.x ? position.x : position.x + width;
10648
+ const dx = endX - frame.anchorX, dy = endY - frame.anchorY;
10649
+ const length = Math.hypot(dx, dy);
10650
+ const leaderAngle = Math.atan2(dy, dx);
10651
+ const pointerDx = pointer ? pointer.x - frame.anchorX : 0;
10652
+ const pointerDy = pointer ? pointer.y - frame.anchorY : 0;
10653
+ const desiredAngle = Math.hypot(pointerDx, pointerDy) > 0.5 ? Math.atan2(pointerDy, pointerDx) + Math.PI - leaderAngle : 0;
10654
+ const wrappedAngle = Math.atan2(Math.sin(desiredAngle), Math.cos(desiredAngle));
10655
+ // Keep the stem inside the 126-degree arc, away from antialiased endpoints.
10656
+ const arcFraction = 0.35;
10657
+ const rotationLimit = Math.PI * arcFraction - Math.PI / 30;
10658
+ const targetOffset = hovered ? Math.max(-rotationLimit, Math.min(rotationLimit, wrappedAngle)) : ((_x = ringAnimation.arcOffset) !== null && _x !== void 0 ? _x : 0);
10659
+ const targetOpen = cursorMotion.target || (hovered && (ring === null || ring === void 0 ? void 0 : ring.enabled) !== false && targetRadius > 0) ? 1 : 0;
10660
+ const elapsed = Math.min(32, Math.max(0, now - ((_y = ringAnimation.arcTime) !== null && _y !== void 0 ? _y : now)));
10661
+ const blend = duration > 0 ? 1 - Math.exp(-elapsed / (duration / 3)) : 1;
10662
+ const approach = (value, target) => Math.abs(value - target) < 0.001 ? target : value + (target - value) * blend;
10663
+ ringAnimation.arcOffset = approach((_z = ringAnimation.arcOffset) !== null && _z !== void 0 ? _z : 0, targetOffset);
10664
+ ringAnimation.arcOpen = approach((_0 = ringAnimation.arcOpen) !== null && _0 !== void 0 ? _0 : 0, targetOpen);
10665
+ ringAnimation.arcTime = now;
10666
+ if (ringAnimation.arcOffset !== targetOffset || ringAnimation.arcOpen !== targetOpen) {
10667
+ this.viewer.scene.requestRender();
10668
+ }
10669
+ const circumference = Math.PI * (nodeRadius + ringAnimation.inner);
10670
+ const filledFraction = 1 - ringAnimation.arcOpen * (1 - arcFraction);
10671
+ circle.setAttribute("stroke-dasharray", `${circumference * filledFraction} ${circumference * (1 - filledFraction)}`);
10672
+ circle.setAttribute("transform", `rotate(${(leaderAngle + ringAnimation.arcOffset - Math.PI * filledFraction) * 180 / Math.PI} ${nodeRadius} ${nodeRadius})`);
10673
+ const cut = Math.min(radius, length);
10674
+ const startX = frame.anchorX + (length ? dx / length * cut : 0);
10675
+ const startY = frame.anchorY + (length ? dy / length * cut : 0);
10676
+ const draw = (element, fromX, fromY, toX, toY) => {
10677
+ const screenDx = (toX - fromX) * coordinates.scaleX;
10678
+ const screenDy = (toY - fromY) * coordinates.scaleY;
10679
+ element.style.width = `${2 * coordinates.scaleX}px`;
10680
+ element.style.left = `${coordinates.x + (fromX - 1) * coordinates.scaleX}px`;
10681
+ element.style.top = `${coordinates.y + fromY * coordinates.scaleY}px`;
10682
+ element.style.height = `${Math.hypot(screenDx, screenDy)}px`;
10683
+ element.style.transform = `rotate(${Math.atan2(screenDy, screenDx) - Math.PI / 2}rad)`;
10684
+ element.style.boxShadow = radius ? "none" : "0 0 5px rgba(0, 0, 0, 0.3)";
10685
+ element.style.transformOrigin = "top center";
10686
+ element.style.opacity = String(opacity * (normalLeader ? 1 : cursorMotion.amount));
10687
+ element.style.zIndex = String(placement.zIndex);
10688
+ };
10689
+ draw(lineEle, startX, startY, endX, endY);
10690
+ if (layer.modelBoxes.Hull(this.id)) {
10691
+ layer.modelBoxes.Request(this.id, {
10692
+ whole: Boolean(cursorMotion.target), from: { x: frame.anchorX, y: frame.anchorY },
10693
+ opacity, zIndex: placement.zIndex, color: this.options.leaderColor || "rgba(255, 255, 255, 0.8)", duration
10694
+ });
10695
+ }
10696
+ if (runEle) {
10697
+ // Overlap the corner by the stem's own width so the elbow has no notch in it.
10698
+ const overlap = frame.anchorX < runToX ? -1 : 1;
10699
+ runEle.style.display = beside ? "block" : "none";
10700
+ if (beside)
10701
+ draw(runEle, endX + overlap, endY, runToX, endY);
10702
+ }
10703
+ }
10205
10704
  };
10206
- }
10207
- // Animate the hole and stem together; no CSS transition may lag behind a moving world anchor.
10208
- if (animateRing() < 1 && (ringAnimation.fromOuter !== targetOuter || ringAnimation.fromInner !== targetInner)) {
10209
- this.viewer.scene.requestRender();
10210
- }
10211
- this.ringAnimation = ringAnimation;
10212
- if (sharedRing) {
10213
- sharedRing.animation = ringAnimation;
10214
- }
10215
- const ringShare = 1 - (frame.boxBlend || 0);
10216
- const radius = ringShare > 0 && ringAnimation.inner > 0 ? ringAnimation.outer * ringShare : 0;
10217
- const pinShape = leader && pin ? measurePin() : undefined;
10218
- const stem = (pinShape ? fitPin(frame.anchorX, frame.anchorY, frame.leaderLength, ele.offsetWidth / coordinates.scaleX, ele.offsetHeight / coordinates.scaleY, pinShape) :
10219
- 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);
10220
- const position = { x: (stem || frame).x, y: (stem || frame).y };
10221
- const width = ele.offsetWidth / coordinates.scaleX;
10222
- const height = ele.offsetHeight / coordinates.scaleY;
10223
- if (leader && angled) {
10224
- // The declutter owns the side; the fit only says which one had the room to begin with.
10225
- const run = Math.max(ANGLED_LEADER_RUN, radius + 4);
10226
- const side = (_r = (_q = placement.textSide) !== null && _q !== void 0 ? _q : frame.leaderSide) !== null && _r !== void 0 ? _r : "right";
10227
- const beside = side === "left" ? frame.anchorX - run - width : frame.anchorX + run;
10228
- position.x = Math.max(0, Math.min(beside, this.viewer.canvas.clientWidth - width));
10229
- }
10230
- else if (leader && !pin && placement.textSide === "left") {
10231
- position.x = Math.max(0, Math.min(frame.anchorX - width, this.viewer.canvas.clientWidth - width));
10232
- }
10233
- const gap = (_t = (_s = this.options.pointerAvoidance) === null || _s === void 0 ? void 0 : _s.gap) !== null && _t !== void 0 ? _t : 12;
10234
- const containsPointer = (x, y, margin) => {
10235
- return pointer && pointer.x >= x - margin && pointer.x <= x + width + margin && pointer.y >= y - margin && pointer.y <= y + height + margin;
10236
- };
10237
- const passive = !selectOnClick || !this.visualRegister;
10238
- if (passive && ((_u = this.options.pointerAvoidance) === null || _u === void 0 ? void 0 : _u.enabled) !== false && pointer &&
10239
- (containsPointer(position.x, position.y, gap) || containsPointer(position.x + displacement.x, position.y + displacement.y, gap))) {
10240
- if (containsPointer(position.x + displacement.x, position.y + displacement.y, 0)) {
10241
- // Choose against the original box and retain the offset until the pointer leaves both boxes.
10242
- const candidates = [
10243
- { x: 0, y: pointer.y - gap - height - position.y },
10244
- { x: 0, y: pointer.y + gap - position.y },
10245
- { x: pointer.x - gap - width - position.x, y: 0 },
10246
- { x: pointer.x + gap - position.x, y: 0 }
10247
- ].filter(offset => position.x + offset.x >= 0 && position.y + offset.y >= 0 &&
10248
- position.x + offset.x + width <= this.viewer.canvas.clientWidth && position.y + offset.y + height <= this.viewer.canvas.clientHeight)
10249
- .sort((a, b) => Math.hypot(a.x, a.y) - Math.hypot(b.x, b.y));
10250
- displacement = candidates[0] || { x: 0, y: 0 };
10251
- }
10252
- }
10253
- else {
10254
- displacement = { x: 0, y: 0 };
10255
- }
10256
- const nudgeDuration = (_w = (_v = this.options.pointerAvoidance) === null || _v === void 0 ? void 0 : _v.transitionDuration) !== null && _w !== void 0 ? _w : 200;
10257
- const animateNudge = () => {
10258
- const progress = now < nudge.start ? 0 : nudgeDuration > 0 ? Math.min(1, (now - nudge.start) / nudgeDuration) : 1;
10259
- const eased = progress * progress * (3 - 2 * progress);
10260
- nudge.x = nudge.fromX + (nudge.targetX - nudge.fromX) * eased;
10261
- nudge.y = nudge.fromY + (nudge.targetY - nudge.fromY) * eased;
10262
- return progress;
10263
10705
  };
10264
- animateNudge();
10265
- if (nudge.targetX !== displacement.x || nudge.targetY !== displacement.y) {
10266
- nudge = {
10267
- ...nudge,
10268
- fromX: nudge.x,
10269
- fromY: nudge.y,
10270
- targetX: displacement.x,
10271
- targetY: displacement.y,
10272
- start: now + ((_y = (_x = this.options.pointerAvoidance) === null || _x === void 0 ? void 0 : _x.delay) !== null && _y !== void 0 ? _y : 180)
10273
- };
10274
- }
10275
- if (animateNudge() < 1 && (nudge.fromX !== nudge.targetX || nudge.fromY !== nudge.targetY)) {
10276
- this.viewer.scene.requestRender();
10277
- }
10278
- position.x += nudge.x;
10279
- position.y += nudge.y;
10280
- ele.style.left = `${coordinates.x + position.x * coordinates.scaleX}px`;
10281
- ele.style.top = `${coordinates.y + position.y * coordinates.scaleY}px`;
10282
- for (const clickTarget of Array.from(content.querySelectorAll('[role="button"]'))) {
10283
- 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}`);
10284
- }
10285
- if (leader) {
10286
- const nodeRadius = ringAnimation.outer * (1 - (frame.boxBlend || 0));
10287
- const anchor = this.anchor;
10288
- anchor.style.left = `${coordinates.x + (frame.anchorX - nodeRadius) * coordinates.scaleX}px`;
10289
- anchor.style.top = `${coordinates.y + (frame.anchorY - nodeRadius) * coordinates.scaleY}px`;
10290
- anchor.style.width = `${nodeRadius * 2 * coordinates.scaleX}px`;
10291
- anchor.style.height = `${nodeRadius * 2 * coordinates.scaleY}px`;
10292
- anchor.style.boxSizing = "border-box";
10293
- anchor.style.background = "transparent";
10294
- const svg = anchor.firstElementChild;
10295
- svg.setAttribute("viewBox", `0 0 ${nodeRadius * 2} ${nodeRadius * 2}`);
10296
- const circle = svg.firstElementChild;
10297
- circle.setAttribute("cx", String(nodeRadius));
10298
- circle.setAttribute("cy", String(nodeRadius));
10299
- const innerRadius = ringAnimation.inner * (1 - (frame.boxBlend || 0));
10300
- circle.setAttribute("r", String((nodeRadius + innerRadius) / 2));
10301
- circle.setAttribute("stroke", this.options.leaderColor || "rgba(255, 255, 255, 0.8)");
10302
- circle.setAttribute("stroke-width", String(nodeRadius - innerRadius));
10303
- anchor.style.boxShadow = radius ? "none" : "0 0 5px rgba(0, 0, 0, 0.3)";
10304
- anchor.style.opacity = String(opacity * (normalLeader ? 1 : cursorMotion.amount) * (1 - (frame.boxBlend || 0)));
10305
- anchor.style.zIndex = String(placement.zIndex);
10306
- // The stem starts at the rim, never across the transparent centre, even after passive displacement.
10307
- const straightX = Math.max(position.x, Math.min(frame.anchorX, position.x + width));
10308
- const straightY = frame.anchorY < position.y ? position.y : frame.anchorY > position.y + height ? position.y + height : frame.anchorY;
10309
- const beside = angled && (frame.anchorX < position.x || frame.anchorX > position.x + width);
10310
- const elbowY = position.y + height / 2;
10311
- let endX = beside ? frame.anchorX : straightX;
10312
- let endY = beside ? elbowY : straightY;
10313
- if (pinShape) {
10314
- // Aimed from wherever clamping or a nudge left it; a stem, if any, meets the tip.
10315
- const centreX = position.x + pinShape.x, centreY = position.y + pinShape.y;
10316
- const aim = Math.atan2(frame.anchorY - centreY, frame.anchorX - centreX);
10317
- const reach = Math.min(pinShape.tip, Math.hypot(frame.anchorX - centreX, frame.anchorY - centreY));
10318
- endX = centreX + Math.cos(aim) * reach;
10319
- endY = centreY + Math.sin(aim) * reach;
10320
- const size = `${pinShape.radius * 2 * coordinates.scaleX}px`;
10321
- if (pinHolder.style.width !== size) {
10322
- pinHolder.style.width = pinHolder.style.height = size;
10323
- pinHolder.style.margin = `-${pinShape.radius * coordinates.scaleX}px`;
10324
- }
10325
- // The body's square corner starts at the lower right, a quarter turn short of pointing down.
10326
- pinBody.style.transform = `rotate(${aim - Math.PI / 4}rad)`;
10327
- }
10328
- const runToX = frame.anchorX < position.x ? position.x : position.x + width;
10329
- const dx = endX - frame.anchorX, dy = endY - frame.anchorY;
10330
- const length = Math.hypot(dx, dy);
10331
- const leaderAngle = Math.atan2(dy, dx);
10332
- const pointerDx = pointer ? pointer.x - frame.anchorX : 0;
10333
- const pointerDy = pointer ? pointer.y - frame.anchorY : 0;
10334
- const desiredAngle = Math.hypot(pointerDx, pointerDy) > 0.5 ? Math.atan2(pointerDy, pointerDx) + Math.PI - leaderAngle : 0;
10335
- const wrappedAngle = Math.atan2(Math.sin(desiredAngle), Math.cos(desiredAngle));
10336
- // Keep the stem inside the 126-degree arc, away from antialiased endpoints.
10337
- const arcFraction = 0.35;
10338
- const rotationLimit = Math.PI * arcFraction - Math.PI / 30;
10339
- const targetOffset = hovered ? Math.max(-rotationLimit, Math.min(rotationLimit, wrappedAngle)) : ((_2 = ringAnimation.arcOffset) !== null && _2 !== void 0 ? _2 : 0);
10340
- const targetOpen = cursorMotion.target || (hovered && (ring === null || ring === void 0 ? void 0 : ring.enabled) !== false && targetRadius > 0) ? 1 : 0;
10341
- const elapsed = Math.min(32, Math.max(0, now - ((_3 = ringAnimation.arcTime) !== null && _3 !== void 0 ? _3 : now)));
10342
- const blend = duration > 0 ? 1 - Math.exp(-elapsed / (duration / 3)) : 1;
10343
- const approach = (value, target) => Math.abs(value - target) < 0.001 ? target : value + (target - value) * blend;
10344
- ringAnimation.arcOffset = approach((_4 = ringAnimation.arcOffset) !== null && _4 !== void 0 ? _4 : 0, targetOffset);
10345
- ringAnimation.arcOpen = approach((_5 = ringAnimation.arcOpen) !== null && _5 !== void 0 ? _5 : 0, targetOpen);
10346
- ringAnimation.arcTime = now;
10347
- if (ringAnimation.arcOffset !== targetOffset || ringAnimation.arcOpen !== targetOpen) {
10348
- this.viewer.scene.requestRender();
10349
- }
10350
- const circumference = Math.PI * (nodeRadius + ringAnimation.inner);
10351
- const filledFraction = 1 - ringAnimation.arcOpen * (1 - arcFraction);
10352
- circle.setAttribute("stroke-dasharray", `${circumference * filledFraction} ${circumference * (1 - filledFraction)}`);
10353
- circle.setAttribute("transform", `rotate(${(leaderAngle + ringAnimation.arcOffset - Math.PI * filledFraction) * 180 / Math.PI} ${nodeRadius} ${nodeRadius})`);
10354
- const cut = Math.min(radius, length);
10355
- const startX = frame.anchorX + (length ? dx / length * cut : 0);
10356
- const startY = frame.anchorY + (length ? dy / length * cut : 0);
10357
- const draw = (element, fromX, fromY, toX, toY) => {
10358
- const screenDx = (toX - fromX) * coordinates.scaleX;
10359
- const screenDy = (toY - fromY) * coordinates.scaleY;
10360
- element.style.width = `${2 * coordinates.scaleX}px`;
10361
- element.style.left = `${coordinates.x + (fromX - 1) * coordinates.scaleX}px`;
10362
- element.style.top = `${coordinates.y + fromY * coordinates.scaleY}px`;
10363
- element.style.height = `${Math.hypot(screenDx, screenDy)}px`;
10364
- element.style.transform = `rotate(${Math.atan2(screenDy, screenDx) - Math.PI / 2}rad)`;
10365
- element.style.boxShadow = radius ? "none" : "0 0 5px rgba(0, 0, 0, 0.3)";
10366
- element.style.transformOrigin = "top center";
10367
- element.style.opacity = String(opacity * (normalLeader ? 1 : cursorMotion.amount));
10368
- element.style.zIndex = String(placement.zIndex);
10369
- };
10370
- draw(lineEle, startX, startY, endX, endY);
10371
- if (layer.modelBoxes.Hull(this.id)) {
10372
- layer.modelBoxes.Request(this.id, {
10373
- whole: Boolean(cursorMotion.target), from: { x: frame.anchorX, y: frame.anchorY },
10374
- opacity, zIndex: placement.zIndex, color: this.options.leaderColor || "rgba(255, 255, 255, 0.8)", duration
10375
- });
10376
- }
10377
- if (runEle) {
10378
- // Overlap the corner by the stem's own width so the elbow has no notch in it.
10379
- const overlap = frame.anchorX < runToX ? -1 : 1;
10380
- runEle.style.display = beside ? "block" : "none";
10381
- if (beside)
10382
- draw(runEle, endX + overlap, endY, runToX, endY);
10383
- }
10384
- }
10385
10706
  };
10386
10707
  ele.style.display = "flex";
10387
10708
  ele.style.visibility = "hidden";
10388
- this.renderRemoval = layer.Add({ project, apply });
10709
+ // Hovering, riding the cursor or a highlight lift a label out of its cluster, which a stale frame misses.
10710
+ const wake = () => {
10711
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
10712
+ return Boolean(this.Transient ||
10713
+ ((_a = this.cursorMotion) === null || _a === void 0 ? void 0 : _a.target) ||
10714
+ ((_b = this.cursorMotion) === null || _b === void 0 ? void 0 : _b.amount) > 0 ||
10715
+ ((_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)) ||
10716
+ ((_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));
10717
+ };
10718
+ this.renderRemoval = layer.Add({ project, apply, wake });
10389
10719
  leaveLayer === null || leaveLayer === void 0 ? void 0 : leaveLayer();
10390
10720
  // If terrain changes then we'll re-call createLabel as the position may have changed.
10391
10721
  let terrTimeout = null;
@@ -60279,7 +60609,7 @@ void main() {
60279
60609
  StyleUtils.ApplyTypeStyle = ApplyTypeStyle;
60280
60610
  })(exports.StyleUtils || (exports.StyleUtils = {}));
60281
60611
 
60282
- const VERSION = "7.8.1";
60612
+ const VERSION = "7.8.2";
60283
60613
  /**
60284
60614
  * Updates the environment instance used by bruce-cesium to one specified.
60285
60615
  * This can be used to ensure that the instance a parent is referencing is shared between bruce-cesium, bruce-models, and the parent app.