VisualElement Event for entering 'Preview' mode in UI Builder?

I created a custom element, but I can’t find a way to get notified when UI Builder enters Preview mode.

I expected AttachToPanelEvent to fire in this instance, but it doesn’t:

public CustomElement()
{
    RegisterCallback<AttachToPanelEvent>(e => Debug.Log("attach"));
    RegisterCallback<DetachFromPanelEvent>(e => Debug.Log("detach"));
}

What’s the alternative solution?

Use case: I need to reset state of a toggle button to its default state. Currently, the button state remains after leaving Preview mode.

The UI builder preview mode just enable the dispatch of events in the viewport, while not in preview event are intercepted to be able to select/resize elements and they never reach the elements. I think what you are experiencing is the desired behavior as it allow modifying the state of the UI (especially expand some foldouts) and then adjust the layout values.

But if that is the expected behavior we would also need a way to restore the default state. I will report both concern together to see if we need change related to this.

1 Like

Good to know. A little more about my use case since it’s trivial:

I subclassed TextField to add a “unmask password” Button (absolute positioned to the fields right side so it draws over the field). This button toggles the isPasswordField property, and changes its background image accordingly. Toggling this button in preview mode for testing purposes can then leave the text field unmasked when exiting preview mode.

In this instance, the behaviour of live modifications is undesirable. Knowing this I could handle resetting the isPasswordField state to its original state but for that I’d need to handle imaginary OnEnter/ExitPreviewMode events.