Class UserSwitcher
An avatar trigger that opens an account menu.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class UserSwitcher : BaseVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder
Remarks
UserSwitcher is composed rather than invented: the trigger is a UserAvatar with an
optional name and caret, and the popover is the ordinary menu family —
MenuBuilder anchoring a Menu of MenuItem rows, grouped
by a MenuSection. It therefore inherits the design system's overlay, dismissal and
keyboard behaviour for free. It goes through MenuBuilder rather than
MenuTrigger only because the builder is the half of the family that takes a
placement, which is what the registry's align prop maps onto.
A row's selected state is a real selectable item, so the checkmark, its alignment and its disabled treatment are the menu's, not a bespoke glyph. A disabled row is disabled outright and carries a warning marker plus the account's warning as a tooltip on the whole row, not just the marker.
Optionally it features a profile header — avatar, name, email, caption — above the switchable list, and can collapse that list behind a single expander row labelled sectionLabel. Compose extra rows such as settings or sign-out by simply adding them to the UserSwitcher: its content container is the tail of the menu, after the generated cards.
A long list scrolls rather than growing without bound. The cap is
maxVisibleAccounts rows of the medium control height, measured off a hidden probe
element sized from --appui-alias-control-sizing-md-height — UI Toolkit cannot read a px
length back through a float custom property, and hard-coding the number would break density
rescaling. Past that cap a SearchBar appears above the list to filter it.
Width is not a design token. The registry's popover is w-64 (256px), or w-72 (288px) with
a profile header, and the App UI sizing ramp tops out well below that. menuWidth is
therefore a plain pixel property, defaulting to the registry's two widths, exactly as
width is. Everything else is a token.
Examples
A titlebar switcher with a featured profile and an extra sign-out row.
var switcher = new UserSwitcher
{
profile = new UserProfile { name = "Ada Lovelace", email = "ada@unity3d.com", caption = "Analytical Engines" },
accounts = new List<UserAccount>
{
new UserAccount { id = "personal", name = "Personal" },
new UserAccount { id = "engines", name = "Analytical Engines" },
new UserAccount { id = "locked", name = "Difference Engines", disabled = true, warning = "No seat on this org." },
},
activeAccountId = "engines",
};
switcher.accountSelected += id => Debug.Log($"switched to {id}");
switcher.Add(new MenuItem { label = "Sign out", icon = "sign-out" });
UXML declaration — the accounts themselves are data, so they are set from C#.
<appui:UserSwitcher show-name="true" section-label="Organizations" collapsible="true" size="Md" />
Constructors
UserSwitcher()
Default constructor.
Declaration
public UserSwitcher()
Fields
accountAvatarUssClassName
The styling class of an account row's avatar.
Declaration
public const string accountAvatarUssClassName = "appui-userswitcher__account-avatar"
Field Value
| Type | Description |
|---|---|
| string |
accountWarningUssClassName
The styling class of a disabled account row's warning marker.
Declaration
public const string accountWarningUssClassName = "appui-userswitcher__account-warning"
Field Value
| Type | Description |
|---|---|
| string |
accountsUssClassName
The styling class of the card holding the switchable list.
Declaration
public const string accountsUssClassName = "appui-userswitcher__accounts"
Field Value
| Type | Description |
|---|---|
| string |
caretUssClassName
The styling class of the trigger's caret.
Declaration
public const string caretUssClassName = "appui-userswitcher__caret"
Field Value
| Type | Description |
|---|---|
| string |
defaultMenuWidth
The popover width, in pixels, with no profile header. The registry's w-64.
Declaration
public const float defaultMenuWidth = 256
Field Value
| Type | Description |
|---|---|
| float |
defaultProfileMenuWidth
The popover width, in pixels, with a profile header. The registry's w-72.
Declaration
public const float defaultProfileMenuWidth = 288
Field Value
| Type | Description |
|---|---|
| float |
defaultSectionLabel
The default heading of the switchable list.
Declaration
public const string defaultSectionLabel = "Accounts"
Field Value
| Type | Description |
|---|---|
| string |
emptyUssClassName
The styling class of the "no matches" message.
Declaration
public const string emptyUssClassName = "appui-userswitcher__empty"
Field Value
| Type | Description |
|---|---|
| string |
expanderUssClassName
The styling class of the expander row shown while collapsible is set.
Declaration
public const string expanderUssClassName = "appui-userswitcher__expander"
Field Value
| Type | Description |
|---|---|
| string |
extraUssClassName
The styling class of the container extra rows are appended to.
Declaration
public const string extraUssClassName = "appui-userswitcher__extra"
Field Value
| Type | Description |
|---|---|
| string |
listUssClassName
The styling class of the scrolling list of account rows.
Declaration
public const string listUssClassName = "appui-userswitcher__list"
Field Value
| Type | Description |
|---|---|
| string |
menuUssClassName
The styling class of the popover menu.
Declaration
public const string menuUssClassName = "appui-userswitcher__menu"
Field Value
| Type | Description |
|---|---|
| string |
noMatchesLabel
The message shown when the filter matches no account.
Declaration
public const string noMatchesLabel = "No matches."
Field Value
| Type | Description |
|---|---|
| string |
profileCaptionUssClassName
The styling class of the profile header's caption.
Declaration
public const string profileCaptionUssClassName = "appui-userswitcher__profile-caption"
Field Value
| Type | Description |
|---|---|
| string |
profileEmailUssClassName
The styling class of the profile header's email.
Declaration
public const string profileEmailUssClassName = "appui-userswitcher__profile-email"
Field Value
| Type | Description |
|---|---|
| string |
profileNameUssClassName
The styling class of the profile header's name.
Declaration
public const string profileNameUssClassName = "appui-userswitcher__profile-name"
Field Value
| Type | Description |
|---|---|
| string |
profileTextUssClassName
The styling class of the profile header's text column.
Declaration
public const string profileTextUssClassName = "appui-userswitcher__profile-text"
Field Value
| Type | Description |
|---|---|
| string |
profileUssClassName
The styling class of the profile header card.
Declaration
public const string profileUssClassName = "appui-userswitcher__profile"
Field Value
| Type | Description |
|---|---|
| string |
rowProbeUssClassName
The styling class of the hidden element the switcher measures a row's height from.
Declaration
public const string rowProbeUssClassName = "appui-userswitcher__row-probe"
Field Value
| Type | Description |
|---|---|
| string |
searchUssClassName
The styling class of the search field's inset wrapper.
Declaration
public const string searchUssClassName = "appui-userswitcher__search"
Field Value
| Type | Description |
|---|---|
| string |
triggerLabelUssClassName
The styling class of the trigger's label.
Declaration
public const string triggerLabelUssClassName = "appui-userswitcher__trigger-label"
Field Value
| Type | Description |
|---|---|
| string |
triggerUssClassName
The styling class of the trigger.
Declaration
public const string triggerUssClassName = "appui-userswitcher__trigger"
Field Value
| Type | Description |
|---|---|
| string |
triggerWithNameUssClassName
The styling class carried by the trigger while it also shows a name.
Declaration
public const string triggerWithNameUssClassName = "appui-userswitcher__trigger--with-name"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The UserSwitcher main styling class.
Declaration
public const string ussClassName = "appui-userswitcher"
Field Value
| Type | Description |
|---|---|
| string |
Properties
accountRows
The account rows currently in the menu, in order.
Declaration
public IEnumerable<MenuItem> accountRows { get; }
Property Value
| Type | Description |
|---|---|
| IEnumerable<MenuItem> |
accounts
The accounts available to switch between.
Declaration
public IEnumerable<UserAccount> accounts { get; set; }
Property Value
| Type | Description |
|---|---|
| IEnumerable<UserAccount> |
Remarks
Assigning copies the sequence, so a later mutation of the caller's list does not silently desynchronise the menu. Call Refresh() after mutating an account in place.
activeAccount
The account the switcher currently considers active, which is the first one when activeAccountId names none.
Declaration
public UserAccount activeAccount { get; }
Property Value
| Type | Description |
|---|---|
| UserAccount |
activeAccountId
The id of the active account. Defaults to the first account.
Declaration
[CreateProperty]
[UxmlAttribute("active-account-id")]
[Header("User Switcher")]
public string activeAccountId { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
collapsible
Whether the switchable list hides behind a single expander row.
Declaration
[CreateProperty]
[UxmlAttribute("collapsible")]
public bool collapsible { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
contentContainer
Extra menu rows land after the generated cards — settings, sign out, and anything else.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
expanded
Whether the collapsible list is currently open. Always true when collapsible is false, since the list is then never hidden.
Declaration
public bool expanded { get; }
Property Value
| Type | Description |
|---|---|
| bool |
maxVisibleAccounts
How many account rows are visible before the list scrolls.
Declaration
[CreateProperty]
[UxmlAttribute("max-visible-accounts")]
public int maxVisibleAccounts { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
Remarks
Past this count the list also gains a SearchBar to filter it, on the grounds that a list long enough to scroll is long enough to search.
menu
The menu the switcher fills and shows.
Declaration
public Menu menu { get; }
Property Value
| Type | Description |
|---|---|
| Menu |
menuWidth
The popover width, in pixels. Unset picks the registry's default for the current shape: defaultProfileMenuWidth with a profile header, defaultMenuWidth without.
Declaration
[CreateProperty]
[UxmlAttribute("menu-width")]
public Optional<float> menuWidth { get; set; }
Property Value
| Type | Description |
|---|---|
| Optional<float> |
placement
Where the popover sits relative to the trigger. The registry's align prop.
Declaration
[CreateProperty]
[UxmlAttribute("placement")]
public PopoverPlacement placement { get; set; }
Property Value
| Type | Description |
|---|---|
| PopoverPlacement |
profile
The profile to feature. When set, the trigger avatar reflects this person rather than the active account, and a header renders above the switchable list.
Declaration
public UserProfile profile { get; set; }
Property Value
| Type | Description |
|---|---|
| UserProfile |
searchVisible
Whether the list currently shows a search field, which happens once there are more accounts than maxVisibleAccounts.
Declaration
public bool searchVisible { get; }
Property Value
| Type | Description |
|---|---|
| bool |
sectionLabel
The heading of the switchable list, and the label of the expander when collapsible is set.
Declaration
[CreateProperty]
[UxmlAttribute("section-label")]
public string sectionLabel { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
showAccountAvatars
Whether each account row carries an avatar. Turn it off for lists where an avatar adds no meaning, such as organizations.
Declaration
[CreateProperty]
[UxmlAttribute("show-account-avatars")]
public bool showAccountAvatars { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
showName
Whether the trigger shows the active name and a caret next to the avatar.
Declaration
[CreateProperty]
[UxmlAttribute("show-name")]
public bool showName { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
The sidebar or header style. Left false, the trigger is avatar-only, which suits a compact titlebar.
size
The trigger avatar's size.
Declaration
[CreateProperty]
[UxmlAttribute("size")]
public Size size { get; set; }
Property Value
| Type | Description |
|---|---|
| Size |
triggerElement
The trigger element, so a caller can focus it or read its accessible tooltip.
Declaration
public VisualElement triggerElement { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
triggerLabel
Overrides the label shown in the trigger while showName is set.
Declaration
[CreateProperty]
[UxmlAttribute("trigger-label")]
public string triggerLabel { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Remarks
Use it to label the trigger with something other than the active account or profile name — the current organization, say — while the menu header still features the signed-in person.
Methods
CloseMenu()
Dismiss the account menu, if it is open.
Declaration
public void CloseMenu()
OpenMenu()
Open the account menu.
Declaration
public void OpenMenu()
Refresh()
Rebuild the trigger and the menu from the current accounts and profile.
Declaration
public void Refresh()
Remarks
Assigning accounts or profile already does this. Call it by hand after mutating an existing UserAccount in place.
Events
accountSelected
Raised with an account id when a row is chosen. A disabled row never raises it.
Declaration
public event Action<string> accountSelected
Event Type
| Type | Description |
|---|---|
| Action<string> |