使用彈出式視窗對話框

內建圖示會使用彈出式氣泡式 UI 顯示額外資訊或編輯器。自訂圖示也可以複製這項行為,以維持一致的外觀和感受。

如果圖示應顯示泡泡,您必須實作 IHasBubble 介面。

顯示或隱藏氣泡

使用氣泡的圖示應實作 setBubbleVisible 方法,以便顯示或隱藏氣泡。

// 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);
}

處理拖曳區塊

圖示變更位置時,氣泡不會自動移動。您必須更新氣泡的位置,或將其隱藏。這項操作可在 IIcon 介面的 onLocationChange 方法中執行。

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

返回氣泡的可見度

IHasBubble 介面也要求您實作 bubbleIsVisible 方法,該方法會傳回氣泡是否可見。

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