I want to implement two different types of Adornments :
- Blue Adornment when the block is selected
- And skyblue when the block is hovered.
The Adornment is slightly bigger than the Block shape. And I want to hide it when the cursor leave the Adornment Rectangle. (not the block area)
The issue is : For some reason The mouseLeave
event seems to work only when I move the cursor slowly unless I add background
or fill
attributes.
Here is what I tried.
I created an Adornment generator method :
const adornmentGenerator = (stroke) =>
$(
go.Adornment,
"Spot",
{
name: "MY_ADORNMENT",
// background: "transparent",
mouseLeave: function (_e, obj) {
// Work better with background ?!
obj.part.adornedPart.removeAdornment("mouseHover");
}
},
$(go.Placeholder, {
isActionable: true
}),
$(
go.Shape,
"Rectangle",
{
name: "MY_RECTANGLE",
// Or transparent ? However adding a fill won't show the block cursor
fill: null,
stroke,
strokeWidth: 2,
stretch: go.GraphObject.None
}
/* new go.Binding("stroke", "", (isSelected, obj) => {
console.log("obj", obj);
return obj.part.isSelected ? "blue" : "skyblue";
}).ofObject() */
)
);
I have now the two Adornment types with different strokes
const selectionAdornmentTemplate = adornmentGenerator("blue");
const hoverAdornment = adornmentGenerator("skyblue");
In the Block Template I have different events with addAdornment
and removeAdornment
:
mouseOver: function (_e, activeNode) {
// Show blue adornment
if (activeNode.part.isSelected) {
return;
}
// Show skyblue adornment
hoverAdornment.adornedObject = activeNode;
activeNode.addAdornment("mouseHover", hoverAdornment);
},
click: function (_e, activeNode) {
if (!activeNode.part.isSelected) {
// Show skyblue adornment
// after deselecting the block
hoverAdornment.adornedObject = activeNode;
activeNode.addAdornment("mouseHover", hoverAdornment);
}
},
selectionChanged: function (activeNode) {
activeNode.part.removeAdornment("mouseHover");
},
Any idea how can I enhance this ? Full example here