UsoUIElements 0.1.0
Data Centric implimentation of Unity's UI Elements (UI Toolkit)
Loading...
Searching...
No Matches
GWG.UsoUIElements.Utilities.UsoUiHelper Class Reference

A static utility class that provides helper methods and extensions for working with Unity's UI Elements system. This class offers functionality for field validation styling, LINQ-style query operations, element creation, manipulation, and traversal. More...

Static Public Member Functions

static void AddClass (this VisualElement ele, string className)
 Adds a single CSS class to a VisualElement with null-checking for safety. This method provides a safe way to add CSS classes with proper error handling.
static T AddClass< T > (this T visualElement, params string[] classes)
 Adds multiple CSS classes to a VisualElement and returns the element for method chaining. This method provides a fluent interface for applying multiple CSS classes in a single operation.
static void AddClickListener (this VisualElement ele, EventCallback< ClickEvent > callback)
 Adds a click event listener to a VisualElement with null-checking for safety. This method provides a convenient way to register click event handlers.
static T AddTo< T > (this T child, VisualElement parent)
 Adds the specified child element to a parent element and returns the child for method chaining. This method enables fluent interface patterns for element hierarchy construction.
static int CountWhere< T > (this UQueryBuilder< T > query, Func< T, bool > predicate)
 Extends UQueryBuilder with LINQ-style Count functionality to count elements that satisfy a specified condition. Converts the query to a list and applies the predicate function to count matching elements.
static VisualElement CreateChild (this VisualElement parent, params string[] classes)
 Creates a new child VisualElement, applies optional CSS classes, and adds it to the specified parent element. This method provides a fluent interface for rapid UI construction.
static T CreateChild< T > (this VisualElement parent, params string[] classes)
 Creates a new child element of the specified type, applies optional CSS classes, and adds it to the specified parent element. This generic method allows creation of specific VisualElement types with fluent interface support.
static VisualElement FindChildByName (this VisualElement ele, string name)
 Recursively searches for a child element with the specified name within the visual hierarchy. This method performs a depth-first search through all descendants of the starting element.
static T FirstOrDefault< T > (this UQueryBuilder< T > query)
 Extends UQueryBuilder with LINQ-style FirstOrDefault functionality to return the first element or a default value. Converts the query to a list and returns the first element, or null if the sequence is empty.
static VisualElement GetDocumentRoot (this VisualElement ele)
 Recursively traverses up the visual hierarchy to find and return the root element of the document. This method climbs the parent chain until it reaches an element with no parent.
static bool IsVisible (this VisualElement ele)
 Determines whether a VisualElement is currently visible by checking its display style. Returns true if the element's display style is not set to None.
static IEnumerable< T > OrderBy< T, TKey > (this UQueryBuilder< T > query, Func< T, TKey > keySelector, Comparer< TKey > @default)
 Extends UQueryBuilder with LINQ-style OrderBy functionality to sort elements in ascending order according to a key. Converts the query to a list and applies standard LINQ OrderBy with the provided key selector and comparer.
static void RemoveClass (this VisualElement ele, string className)
 Removes a CSS class from a VisualElement with null-checking for safety. This method provides a safe way to remove CSS classes with proper error handling.
static void SetFieldStatus (VisualElement element, FieldStatusTypes fieldStatus)
 Sets the field validation status for a visual element by applying the appropriate CSS class. Removes any existing validation classes and applies the class corresponding to the specified status.
static IEnumerable< T > SortByNumericValue< T > (this UQueryBuilder< T > query, Func< T, float > keySelector)
 Extends UQueryBuilder with functionality to sort elements by a numeric key in ascending order. This is a specialized version of OrderBy optimized for numeric sorting operations.
static void ToggleClass (this VisualElement ele, string className)
 Toggles a CSS class on a VisualElement (adds if not present, removes if present) with null-checking for safety. This method provides a convenient way to toggle CSS classes for state changes.
static void ToggleVisibility (this VisualElement ele)
 Toggles the visibility of a VisualElement between visible (Flex) and hidden (None) display states. This method provides a convenient way to show/hide elements with a single method call.
static T WithManipulator< T > (this T visualElement, IManipulator manipulator)
 Adds a manipulator to a VisualElement and returns the element for method chaining. This method enables fluent interface patterns for adding interaction manipulators.

Static Public Attributes

static List< string > FieldValidationClasses
 A list of CSS class names used for field validation status styling. These classes correspond to different validation states and are used for visual feedback.

Detailed Description

A static utility class that provides helper methods and extensions for working with Unity's UI Elements system. This class offers functionality for field validation styling, LINQ-style query operations, element creation, manipulation, and traversal.

The UsoUiHelper class serves as the central utility hub for the Uso UI Elements system, providing:

  • Field validation status management through CSS class manipulation
  • LINQ-style extension methods for UQueryBuilder operations
  • Fluent API extensions for creating and styling visual elements
  • Convenience methods for common UI element operations like visibility toggling and event handling
  • Hierarchical traversal methods for finding elements within the visual tree

All methods are designed to follow fluent interface patterns where applicable, allowing for method chaining. The class integrates with the broader Uso UI validation system through standardized CSS class naming conventions.

Member Function Documentation

◆ AddClass()

void GWG.UsoUIElements.Utilities.UsoUiHelper.AddClass ( this VisualElement ele,
string className )
static

Adds a single CSS class to a VisualElement with null-checking for safety. This method provides a safe way to add CSS classes with proper error handling.

Parameters
eleThe VisualElement to add the class to.
classNameThe CSS class name to add.
Exceptions
ArgumentNullExceptionThrown when the element parameter is null.

This method provides explicit null-checking and clear error messages for debugging purposes. It's designed for scenarios where you need guaranteed safety and clear error reporting.

◆ AddClass< T >()

T GWG.UsoUIElements.Utilities.UsoUiHelper.AddClass< T > ( this T visualElement,
params string[] classes )
static

Adds multiple CSS classes to a VisualElement and returns the element for method chaining. This method provides a fluent interface for applying multiple CSS classes in a single operation.

Template Parameters
TThe type of the VisualElement.
Parameters
visualElementThe VisualElement to add classes to.
classesArray of CSS class names to add to the element.
Returns
The VisualElement with applied classes, allowing for continued method chaining.

This method iterates through the provided class names and applies each non-null and non-empty class to the element. It supports the fluent interface pattern for streamlined element styling during creation. Null or empty class names are safely ignored without throwing exceptions.

Type Constraints
T :VisualElement 

◆ AddClickListener()

void GWG.UsoUIElements.Utilities.UsoUiHelper.AddClickListener ( this VisualElement ele,
EventCallback< ClickEvent > callback )
static

Adds a click event listener to a VisualElement with null-checking for safety. This method provides a convenient way to register click event handlers.

Parameters
eleThe VisualElement to add the click listener to.
callbackThe event callback to execute when the element is clicked.
Exceptions
ArgumentNullExceptionThrown when the element parameter is null.

This method simplifies the common pattern of adding click event handlers to UI elements. It provides explicit null-checking and a more readable method name than the generic RegisterCallback.

◆ AddTo< T >()

T GWG.UsoUIElements.Utilities.UsoUiHelper.AddTo< T > ( this T child,
VisualElement parent )
static

Adds the specified child element to a parent element and returns the child for method chaining. This method enables fluent interface patterns for element hierarchy construction.

Template Parameters
TThe type of the child VisualElement.
Parameters
childThe child element to be added to the parent.
parentThe parent VisualElement to add the child to.
Returns
The child element, allowing for continued method chaining.

This extension method supports fluent interface patterns by allowing element creation and parenting to be chained with other operations. It's designed to work seamlessly with other fluent methods in this class to enable readable and concise UI construction code.

Type Constraints
T :VisualElement 

◆ CountWhere< T >()

int GWG.UsoUIElements.Utilities.UsoUiHelper.CountWhere< T > ( this UQueryBuilder< T > query,
Func< T, bool > predicate )
static

Extends UQueryBuilder with LINQ-style Count functionality to count elements that satisfy a specified condition. Converts the query to a list and applies the predicate function to count matching elements.

Template Parameters
TThe type of VisualElement being queried.
Parameters
queryThe UQueryBuilder containing the sequence of elements to be processed.
predicateA function to test each element for a condition.
Returns
The number of elements that satisfy the condition specified by the predicate.

This method enables conditional counting operations on UI element queries, useful for validation scenarios, state analysis, or determining the presence of elements with specific characteristics. The predicate function is applied to each element, and only elements that return true are counted.

Type Constraints
T :VisualElement 

◆ CreateChild()

VisualElement GWG.UsoUIElements.Utilities.UsoUiHelper.CreateChild ( this VisualElement parent,
params string[] classes )
static

Creates a new child VisualElement, applies optional CSS classes, and adds it to the specified parent element. This method provides a fluent interface for rapid UI construction.

Parameters
parentThe parent VisualElement to add the new child to.
classesOptional array of CSS class names to apply to the new child element.
Returns
The newly created child VisualElement.

This method streamlines the common pattern of creating, styling, and parenting UI elements in a single operation. It uses the fluent interface pattern to allow for readable and concise UI construction code. The method applies classes and parents the element in one atomic operation for convenience.

◆ CreateChild< T >()

T GWG.UsoUIElements.Utilities.UsoUiHelper.CreateChild< T > ( this VisualElement parent,
params string[] classes )
static

Creates a new child element of the specified type, applies optional CSS classes, and adds it to the specified parent element. This generic method allows creation of specific VisualElement types with fluent interface support.

Template Parameters
TThe specific type of VisualElement to create, must have a parameterless constructor.
Parameters
parentThe parent VisualElement to add the new child to.
classesOptional array of CSS class names to apply to the new child element.
Returns
The newly created child element of type T.

This generic version of CreateChild allows for creating specific UI element types (like Button, Label, TextField, etc.) while maintaining the fluent interface pattern. It's particularly useful when you need to manipulate the created element further and want to maintain the specific type rather than working with the base VisualElement type. The return type is the specific element type, allowing immediate access to type-specific properties and methods.

Type Constraints
T :VisualElement 
T :new() 

◆ FindChildByName()

VisualElement GWG.UsoUIElements.Utilities.UsoUiHelper.FindChildByName ( this VisualElement ele,
string name )
static

Recursively searches for a child element with the specified name within the visual hierarchy. This method performs a depth-first search through all descendants of the starting element.

Parameters
eleThe starting VisualElement to begin the search from.
nameThe name of the child element to find.
Returns
The first child VisualElement with the matching name, or null if not found.
Exceptions
ArgumentNullExceptionThrown when the element parameter is null.
ArgumentExceptionThrown when the name parameter is null or empty.

This method performs a recursive depth-first search through the entire visual tree starting from the specified element. It checks immediate children first, then recursively searches within each child's hierarchy. The search returns the first matching element found, so element names should be unique within the search scope for predictable results. This method is useful for finding elements by their programmatically assigned names when direct references are not available.

◆ FirstOrDefault< T >()

T GWG.UsoUIElements.Utilities.UsoUiHelper.FirstOrDefault< T > ( this UQueryBuilder< T > query)
static

Extends UQueryBuilder with LINQ-style FirstOrDefault functionality to return the first element or a default value. Converts the query to a list and returns the first element, or null if the sequence is empty.

Template Parameters
TThe type of VisualElement being queried.
Parameters
queryThe UQueryBuilder containing the elements to search.
Returns
The first element in the sequence, or null if no elements are found.

This extension method provides safe access to the first element in a query result without throwing exceptions when the sequence is empty. It's particularly useful for optional element lookups where the absence of an element is a valid scenario that should be handled gracefully.

Type Constraints
T :VisualElement 

◆ GetDocumentRoot()

VisualElement GWG.UsoUIElements.Utilities.UsoUiHelper.GetDocumentRoot ( this VisualElement ele)
static

Recursively traverses up the visual hierarchy to find and return the root element of the document. This method climbs the parent chain until it reaches an element with no parent.

Parameters
eleThe starting VisualElement to begin the traversal from.
Returns
The root VisualElement of the document hierarchy.
Exceptions
ArgumentNullExceptionThrown when the element parameter is null.

This method uses recursion to traverse up the visual tree until it finds the root element. It's useful for accessing document-level properties or performing operations that require knowledge of the complete hierarchy context. The root element is typically the UIDocument's root visual element.

◆ IsVisible()

bool GWG.UsoUIElements.Utilities.UsoUiHelper.IsVisible ( this VisualElement ele)
static

Determines whether a VisualElement is currently visible by checking its display style. Returns true if the element's display style is not set to None.

Parameters
eleThe VisualElement to check for visibility.
Returns
True if the element is visible (display is not None); otherwise, false.
Exceptions
ArgumentNullExceptionThrown when the element parameter is null.

This method checks the resolved display style to determine actual visibility state. It's useful for conditional logic that depends on element visibility without directly checking CSS properties. Note that this only checks the display property and doesn't account for other factors like opacity or parent visibility.

◆ OrderBy< T, TKey >()

IEnumerable< T > GWG.UsoUIElements.Utilities.UsoUiHelper.OrderBy< T, TKey > ( this UQueryBuilder< T > query,
Func< T, TKey > keySelector,
Comparer< TKey > @ default )
static

Extends UQueryBuilder with LINQ-style OrderBy functionality to sort elements in ascending order according to a key. Converts the query to a list and applies standard LINQ OrderBy with the provided key selector and comparer.

Template Parameters
TThe type of VisualElement being queried.
TKeyThe type of the key used for sorting.
Parameters
queryThe UQueryBuilder containing the elements to be sorted.
keySelectorA function to extract a sort key from each element.
defaultThe Comparer to use for comparing keys.
Returns
An ordered sequence of elements sorted by the specified key.

This extension method bridges the gap between Unity's UQueryBuilder system and standard LINQ operations. It materializes the query results into a list before applying the ordering operation. The method allows for custom comparison logic through the comparer parameter.

Type Constraints
T :VisualElement 

◆ RemoveClass()

void GWG.UsoUIElements.Utilities.UsoUiHelper.RemoveClass ( this VisualElement ele,
string className )
static

Removes a CSS class from a VisualElement with null-checking for safety. This method provides a safe way to remove CSS classes with proper error handling.

Parameters
eleThe VisualElement to remove the class from.
classNameThe CSS class name to remove.
Exceptions
ArgumentNullExceptionThrown when the element parameter is null.

This method provides explicit null-checking and clear error messages for debugging purposes. It safely removes the specified class if it exists, with no effect if the class is not present.

◆ SetFieldStatus()

void GWG.UsoUIElements.Utilities.UsoUiHelper.SetFieldStatus ( VisualElement element,
FieldStatusTypes fieldStatus )
static

Sets the field validation status for a visual element by applying the appropriate CSS class. Removes any existing validation classes and applies the class corresponding to the specified status.

Parameters
elementThe VisualElement to apply the status styling to.
fieldStatusThe field status type to apply.

This method first removes all validation classes from the element to ensure a clean state, then applies the appropriate class based on the fieldStatus parameter. The Default status results in no additional classes being applied, allowing the element to use its base styling.

The method is central to the Uso UI validation system and ensures consistent visual feedback across all form elements that implement field validation.

◆ SortByNumericValue< T >()

IEnumerable< T > GWG.UsoUIElements.Utilities.UsoUiHelper.SortByNumericValue< T > ( this UQueryBuilder< T > query,
Func< T, float > keySelector )
static

Extends UQueryBuilder with functionality to sort elements by a numeric key in ascending order. This is a specialized version of OrderBy optimized for numeric sorting operations.

Template Parameters
TThe type of VisualElement being queried.
Parameters
queryThe UQueryBuilder containing the elements to be sorted.
keySelectorA function to extract a numeric key from each element.
Returns
An ordered sequence of elements sorted by the numeric key in ascending order.

This method provides a convenient way to sort UI elements by numeric properties such as positions, sizes, or custom numeric attributes. It uses the default float comparer for consistent sorting behavior. Common use cases include sorting elements by their layout positions or custom numeric data attributes.

Type Constraints
T :VisualElement 

◆ ToggleClass()

void GWG.UsoUIElements.Utilities.UsoUiHelper.ToggleClass ( this VisualElement ele,
string className )
static

Toggles a CSS class on a VisualElement (adds if not present, removes if present) with null-checking for safety. This method provides a convenient way to toggle CSS classes for state changes.

Parameters
eleThe VisualElement to toggle the class on.
classNameThe CSS class name to toggle.
Exceptions
ArgumentNullExceptionThrown when the element parameter is null.

This method is particularly useful for implementing toggle behaviors, hover effects, or state changes where a CSS class represents a binary state. It provides explicit null-checking for safety.

◆ ToggleVisibility()

void GWG.UsoUIElements.Utilities.UsoUiHelper.ToggleVisibility ( this VisualElement ele)
static

Toggles the visibility of a VisualElement between visible (Flex) and hidden (None) display states. This method provides a convenient way to show/hide elements with a single method call.

Parameters
eleThe VisualElement to toggle visibility for.
Exceptions
ArgumentNullExceptionThrown when the element parameter is null.

This method toggles between DisplayStyle.None (hidden) and DisplayStyle.Flex (visible). It's commonly used for implementing show/hide functionality, collapsible sections, or modal dialogs. The method preserves the element's layout properties when visible by using Flex display mode.

◆ WithManipulator< T >()

T GWG.UsoUIElements.Utilities.UsoUiHelper.WithManipulator< T > ( this T visualElement,
IManipulator manipulator )
static

Adds a manipulator to a VisualElement and returns the element for method chaining. This method enables fluent interface patterns for adding interaction manipulators.

Template Parameters
TThe type of the VisualElement.
Parameters
visualElementThe VisualElement to add the manipulator to.
manipulatorThe IManipulator to add to the element.
Returns
The VisualElement with the added manipulator, allowing for continued method chaining.

This method supports fluent interface patterns by allowing manipulator addition to be chained with other element configuration operations. Manipulators handle complex interactions like dragging, resizing, or custom gesture recognition.

Type Constraints
T :VisualElement 

Member Data Documentation

◆ FieldValidationClasses

List<string> GWG.UsoUIElements.Utilities.UsoUiHelper.FieldValidationClasses
static
Initial value:
= new List<string>()
{
"uso-field-label--error",
"uso-field-label--warning",
"uso-field-label--success",
"uso-field-label--info",
"uso-field-label--disabled"
}

A list of CSS class names used for field validation status styling. These classes correspond to different validation states and are used for visual feedback.

The list contains predefined CSS class names for all supported field validation states:

  • uso-field-label–error: Applied when a field has validation errors
  • uso-field-label–warning: Applied when a field has validation warnings
  • uso-field-label–success: Applied when a field passes validation successfully
  • uso-field-label–info: Applied when a field displays informational status
  • uso-field-label–disabled: Applied when a field is disabled

These classes should have corresponding styles defined in the project's CSS to provide appropriate visual feedback.