Skip to main content

ListView

See the ListViewDemo class.

public void Draw()
{
// Display non-scrolling list view
Gui.Heading("NonScroll");
using (Style.Frame.BeginPadding(new(1, 1, 1, 1)))
using (Gui.BeginFrame())
{
// Uses item drawer and selection handler
Gui.ListView(
nonScrollItems,
new(itemDrawer, selectedItems, n => n.GetHashCode()),
null);
}

// Display scrolling list view
Gui.Heading("Scroll");
using (Style.Frame.BeginPadding(new(1, 1, 1, 1)))
using (Gui.BeginFrame())
Gui.ScrollingListView(100f, scrollItems, new(itemDrawer, selectedItems, n => n.GetHashCode()));
}

/// <summary>
/// Represents an item in the list.
/// </summary>
class Item
{
public string Name { get; }
public bool IsChecked { get; set; }

public Item(string name)
{
Name = name;
}
}

/// <summary>
/// Handles rendering of individual list items.
/// </summary>
class ItemDrawer : IListItemDrawer<Item>
{
readonly Gui ui;

public ItemDrawer(Gui im)
{
this.ui = im;
}

/// <summary>
/// Draws a list item with a checkbox and an index label.
/// </summary>
public void Draw(int index, Item item, Rect rect)
{
using var rects = ui.RectsBuilder
.Fixed(rect.height)
.Fit(1).BuildHorizontal(rect);

var isChecked = item.IsChecked;

// Draws a checkbox, updating item state if changed
if (ui.NextRect(rects.R0).CheckBox(ref isChecked))
{
item.IsChecked = isChecked;
}

ui.NextRect(rects.R1).Text($"{index}");
}
}