Prevent switching tab in tabview

I’m working on a small tool to manage some data. For this, I use the tabview to organize the different types of data. So for example, I have 3 tabs and each tab has their own fields with data that can be adjusted.

What I want to do is that if the user is changing some data in a tab and switches to another tab, I want to show a warning (popup) if there are still pending changes. The warning gives the option to discard the changes and move to the selected tab or to keep editing in the current tab.

I figured I could subscribe to the click events of the tabheader and stop propagation, but it seems to already have switched tabs before I can stop it.

Is this possible with the current implementation?

I also tried simply switching back to the “previous” tab by setting the selectedTabIndex, but that still actively changes tabs and considering I am doing some operations on tab open/close, I don’t want to do this.

I can probably also disable the other tabs the moment I have pending changes, but I would prefer the warning.

Make sure you supply TrickleDown.TrickleDown when registering callbacks when doing so with the intent of intercepting events. The default is during the bubble up of events, which is of course too late to prevent things that have already happened.

This simple example stops a tabview from changing tabs:

using UnityEngine;
using UnityEngine.UIElements;

public class TabViewTestingComponent : MonoBehaviour
{
	[SerializeField]
	private UIDocument _uiDocument;

	private void OnEnable()
	{
		var root = _uiDocument.rootVisualElement;
		var tabViewHeaderContainer = root.Q(name: TabView.headerContainerClassName);
		tabViewHeaderContainer.RegisterCallback<PointerDownEvent>(BlockPointerDown, TrickleDown.TrickleDown);
	}

	private void BlockPointerDown(PointerDownEvent evt)
	{
		Debug.Log("Pointer Down");
		evt.StopImmediatePropagation();
	}
}

Ah yes, I forgot about trickling down needing to be explicitly set.

This works perfectly, thanks!