組み込みアイコンは、ポップアップ バブル スタイルの 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;
}