TaffySharp 0.1.0
dotnet add package TaffySharp --version 0.1.0
NuGet\Install-Package TaffySharp -Version 0.1.0
<PackageReference Include="TaffySharp" Version="0.1.0" />
<PackageVersion Include="TaffySharp" Version="0.1.0" />
<PackageReference Include="TaffySharp" />
paket add TaffySharp --version 0.1.0
#r "nuget: TaffySharp, 0.1.0"
#:package TaffySharp@0.1.0
#addin nuget:?package=TaffySharp&version=0.1.0
#tool nuget:?package=TaffySharp&version=0.1.0
TaffySharp
TaffySharp is a UI layout engine implementing CSS Flexbox, CSS Grid and block layout (with floats).
It is a pure C# port of taffy (Rust, MIT), ported from
v0.15.1 (commit 0064bd5). The port is released under the MIT license.
Chorizite/TaffySharp on GitHub is a native binding to the Rust library; this project is an unrelated, fully managed port with no native dependency.
Targets netstandard2.0, netstandard2.1, net8.0, and net10.0, so it can be used from .NET Framework 4.6.2+, .NET Core 2.0+, .NET 5+, Unity 2021.2+, and Mono.
Installation
dotnet add package TaffySharp
Usage
using TaffySharp;
TaffyTree tree = new();
NodeId child = tree.NewLeaf(new Style
{
Size = new Size<Dimension>(Dimension.Percent(0.5f), Dimension.Auto),
});
NodeId root = tree.NewWithChildren(new Style
{
Display = Display.Flex,
Size = new Size<Dimension>(Dimension.Length(100f), Dimension.Length(100f)),
JustifyContent = AlignContent.Center,
}, child);
tree.ComputeLayout(root, new Size<AvailableSpace>(AvailableSpace.Definite(100f), AvailableSpace.Definite(100f)));
Layout layout = tree.Layout(child); // Location = (25, 0), Size = 50 x 100
Leaves with intrinsic content (text, images) get a node context and a measure function:
TaffyTree<string> tree = new();
NodeId text = tree.NewLeafWithContext(new Style(), "Hello");
tree.ComputeLayoutWithMeasure(text, Size.MaxContent, (inputs, node, context, style) =>
Compute.ComputeLeafLayout(inputs, style, (_, _) => 0f, (known, available) =>
new Size<float>(known.Width ?? context!.Length * 8f, known.Height ?? 16f)));
The API mirrors taffy's: Style has the same properties (in PascalCase), percentages are fractions (0.5f is 50%),
and the low-level Compute.* functions and tree interfaces let you run the algorithms on your own tree type.
Not ported: CSS string parsing (taffy's parse feature) and serde support.
Rect.FromLength/FromPercent and Size.FromLengths/FromPercent build Dimension values (for Size, MinSize,
MaxSize). The other length types have their own helpers: Rect.LengthPercentageFromLength for Padding and Border,
Rect.LengthPercentageAutoFromLength for Margin and Inset, Size.LengthPercentageFromLengths for Gap
(plus the ...FromPercent variants).
Things to know
Styles are stored by reference.
NewLeaf,NewWithChildrenandSetStylekeep theStyleinstance you pass. Use one instance per node (orstyle.Clone()): two nodes sharing an instance both change when it is mutated, but only the node you pass toMarkDirtyis re-laid out. To change a style in place, mutatetree.Style(node)and calltree.MarkDirty(node), or pass a new instance toSetStyle.Positiondefaults toStatic, as in CSS (older taffy versions defaulted to relative). An absolute box is positioned against its containing block, the nearest ancestor with a non-staticPosition(or the root), and itsLayout.Locationis relative to that ancestor, not to its parent. A renderer that sums parent offsets must usetree.HoistedChildren(node), which lists the out-of-flow boxes each containing block positioned.Nesting depth is limited by the thread's stack. Layout is recursive and uses about 8 KB of stack per nesting level, so a 1 MB thread handles roughly 100-120 levels. A deeper tree overflows the stack, which terminates the process. Lay out deep trees (generated UIs, nested comment threads, tree views) on a thread with a larger stack:
Thread thread = new(() => tree.ComputeLayout(root, Size.MaxContent), 16 * 1024 * 1024); thread.Start(); thread.Join();
Credits
Algorithm and original implementation: taffy by the Taffy Authors and Visly Inc. (MIT).
The conformance tests in tests/TaffySharp.Tests/Generated are converted from taffy's Chrome-generated XML fixtures
by tests/tools/convert_generated.py.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.3)
-
.NETStandard 2.1
- No dependencies.
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 106 | 11/1/2026 |