ใช้ลูกโป่งป๊อปอัปที่มีไอคอน

ไอคอนในตัวจะใช้ UI แบบลูกโป่งที่ปรากฏขึ้นเพื่อแสดงข้อมูลหรือตัวแก้ไขเพิ่มเติม ไอคอนที่กำหนดเองยังเลียนแบบพฤติกรรมนี้ได้ด้วยเพื่อรักษารูปลักษณ์ที่สม่ำเสมอ

หากไอคอนควรแสดงลูกโป่ง คุณต้องใช้อินเทอร์เฟซ IHasBubble

แสดงหรือซ่อนบับเบิล

ไอคอนที่ใช้บับเบิลควรใช้เมธอด setBubbleVisible เพื่อแสดงหรือซ่อนบับเบิล

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

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

จัดการกับการลากบล็อก

เมื่อไอคอนเปลี่ยนตำแหน่ง ลูกโป่งจะไม่เคลื่อนที่ไปโดยอัตโนมัติ คุณจะต้องอัปเดตตำแหน่งของลูกโป่งหรือซ่อนลูกโป่ง ซึ่งทำได้ในเมธอด onLocationChange ของอินเทอร์เฟซ IIcon

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

แสดงผลของลูกโป่ง

นอกจากนี้ อินเทอร์เฟซ IHasBubble ยังกำหนดให้คุณต้องใช้เมธอด isBubbleVisible ซึ่งแสดงผลว่าลูกโป่งนั้นมองเห็นได้หรือไม่

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