Cómo usar burbujas de ventana emergente

Los íconos integrados usan una IU de estilo de burbuja emergente para mostrar información adicional o editores. Los íconos personalizados también pueden replicar este comportamiento para mantener un aspecto coherente.

Si tu ícono debe mostrar una burbuja, debes implementar la interfaz IHasBubble.

Oculta o muestra la burbuja

Los íconos que usan burbujas deben implementar el método setBubbleVisible para mostrar o ocultar la burbuja.

// Implement the setBubbleVisible method of the IHasBubble interface.
async setBubbleVisible(visible) {
  // State is already correct.
  if (!!this.myBubble === visible) return;

  // Wait for queued renders to finish so that the icon will be correctly
  // positioned before displaying the bubble.
  await Blockly.renderManagement.finishQueuedRenders();

  if (visible) {
    this.myBubble = new MyBubble(this.getAnchorLocation(), this.getOwnerRect());
  } else {
    this.myBubble?.dispose();
  }
}

// Implement helper methods for getting the anchor location and bounds.

// Returns the location of the middle of this icon in workspace coordinates.
getAnchorLocation() {
  const size = this.getSize();
  const midIcon = new Blockly.utils.Coordinate(size.width / 2, size.height / 2);
  return Blockly.utils.Coordinate.sum(this.workspaceLocation, midIcon);
}

// Returns the rect the bubble should avoid overlapping, i.e. the block this
// icon is appended to.
getOwnerRect() {
  const bbox = this.sourceBlock.getSvgRoot().getBBox();
  return new Blockly.utils.Rect(
      bbox.y, bbox.y + bbox.height, bbox.x, bbox.x + bbox.width);
}

Cómo arrastrar el bloque

Cuando el ícono cambia de ubicación, la burbuja no se mueve automáticamente con él. Debes actualizar la ubicación de la burbuja o ocultarla. Esto se puede hacer dentro del método onLocationChange de la interfaz IIcon.

onLocationChange(blockOrigin) {
  super.onLocationChange(blockOrigin);
  this.myBubble?.setAnchorLocation(this.getAnchorLocation());
}

Cómo restablecer la visibilidad de la burbuja

La interfaz IHasBubble también requiere que implementes un método bubbleIsVisible que muestre si la burbuja es visible o no.

isBubbleVisible() {
  return !!this.myBubble;
}