Skip to content

Game.UI.Widgets.IContainerWidget

Assembly: Assembly-CSharp
Namespace: Game.UI.Widgets

Type: interface

Base: None (interface)

Summary:
Defines a simple container contract for widgets that can hold child widgets. Implementors expose a mutable IList so callers can enumerate and modify the collection of child widgets. This interface is used by UI systems in Cities: Skylines 2 mods to treat composite widgets uniformly (layout containers, panels, etc.). Implementers are responsible for managing the lifecycle, layout and rendering of the children; the interface only expresses the children collection.


Fields

  • This interface declares no fields.
    No backing fields are defined on the interface itself. Concrete implementations may have fields to store the children (for example, a List).

Properties

  • public IList<IWidget> children { get; }
    A read-only property that exposes the collection of child widgets. Although the property getter is read-only, the returned IList instance is expected to be mutable (so callers can Add, Remove, Clear, etc.). Implementations should ensure the returned collection is safe to modify according to the component's expected usage (for example, perform layout invalidation when children are added/removed).

Notes and recommendations: - Prefer returning a List or other IList implementation that supports fast enumeration and add/remove operations. - If your UI system updates in a separate loop, ensure modifications to the list are done on the main/UI thread or are properly synchronized. - Consider providing helper methods on concrete types for common operations (AddChild, RemoveChild) to encapsulate layout/invalidation logic instead of relying on external code to manipulate the IList directly.

Constructors

  • Interfaces do not declare constructors.
    Concrete classes implementing IContainerWidget must provide their own constructors and initialize the children collection (for example, new List()).

Methods

  • This interface declares no methods.
    All behavior exposed by IContainerWidget is via the children property. Lifecycle and rendering methods are typically defined by the IWidget interface or by concrete widget base classes.

Usage Example

using System.Collections.Generic;
using Game.UI.Widgets;

public class PanelWidget : IWidget, IContainerWidget
{
    // Backing field for children collection
    private readonly List<IWidget> _children = new List<IWidget>();

    // Expose as IList<IWidget> per the interface
    public IList<IWidget> children => _children;

    // Example IWidget members (simplified)
    public virtual void Update() 
    {
        // Update self...

        // Update children
        foreach (var child in _children)
        {
            child.Update();
        }
    }

    public virtual void AddChild(IWidget widget)
    {
        if (widget == null) return;
        _children.Add(widget);
        // Invalidate layout or mark for redraw here
    }

    public virtual bool RemoveChild(IWidget widget)
    {
        var removed = _children.Remove(widget);
        if (removed)
        {
            // Invalidate layout or mark for redraw here
        }
        return removed;
    }
}

// Example usage
var panel = new PanelWidget();
panel.AddChild(new SomeConcreteWidget());
panel.children.Add(new AnotherWidget()); // direct IList manipulation is allowed

Notes: - Replace IWidget and concrete widget implementations with the actual interfaces/classes from the UI framework you are using. - Ensure you handle layout invalidation and rendering synchronization when children are modified.