### Install ClangSharpPInvokeGenerator Source: https://github.com/vandercat/yogasharp/blob/main/README.md Installs the global tool required to generate Yoga.Interop bindings. ```shell dotnet tool install --global ClangSharpPInvokeGenerator ``` -------------------------------- ### Create and Configure Layout Nodes Source: https://context7.com/vandercat/yogasharp/llms.txt Demonstrates initializing a root node, adding children, and triggering a layout calculation. ```csharp using Yoga; // Create a root node with explicit dimensions var root = new Node { Width = 1000f, Height = 800f, FlexDirection = FlexDirection.Column, AlignItems = Align.Center }; // Create child nodes and add to parent var header = new Node { Height = 100f, Width = 1000f }; root.Children.Add(header); // Alternative: set parent directly var content = new Node { FlexGrow = 1f, FlexShrink = 1f }; content.Parent = root; // Calculate layout with default direction root.CalculateLayout(); // Access computed layout values Console.WriteLine($"Header position: ({header.Left}, {header.Top})"); Console.WriteLine($"Header dimensions: {header.Width}x{header.Height}"); ``` -------------------------------- ### Configure Flex Grow, Shrink, and Basis Source: https://context7.com/vandercat/yogasharp/llms.txt Shows how to manage item sizing and distribution within a container using FlexGrow and FlexShrink. ```csharp using Yoga; var container = new Node { Width = 500f, Height = 100f, FlexDirection = FlexDirection.Row }; // Fixed-size item var sidebar = new Node { Width = 100f, Height = 100f, FlexShrink = 0f, // Don't shrink Parent = container }; // Flexible content area that grows to fill space var content = new Node { FlexGrow = 1f, // Take up remaining space FlexShrink = 1f, // Shrink if needed Height = 100f, Parent = container }; // Another flexible item with different grow ratio var rightPanel = new Node { FlexGrow = 2f, // Grows twice as much as content Height = 100f, Parent = container }; container.CalculateLayout(); // sidebar: 100px, content: ~133px, rightPanel: ~267px ``` -------------------------------- ### Clone Nodes and Copy Styles in C# Source: https://context7.com/vandercat/yogasharp/llms.txt Illustrates how to create independent copies of nodes using cloning and how to copy style properties from one node to another. ```csharp using Yoga; var original = new Node { Width = 200f, Height = 150f, FlexDirection = FlexDirection.Row, AlignItems = Align.Center, JustifyContent = Justify.SpaceBetween }; original.StyleSetPadding(Edge.All, 10f); original.StyleSetMargin(Edge.All, 5f); // Clone the entire node (creates independent copy) Node cloned = original.Clone(); // Copy only style properties from another node var target = new Node(); target.CopyStyleFrom(original); // Both approaches preserve style but cloned is a new node instance Console.WriteLine($"Original width: {original.Width}"); Console.WriteLine($"Cloned width: {cloned.Width}"); ``` -------------------------------- ### Resource Management and Disposal in C# Source: https://context7.com/vandercat/yogasharp/llms.txt Explains proper resource management for Yogasharp nodes, including automatic disposal using `using` statements and manual disposal via the `Dispose()` method. Also covers resetting a node's state. ```csharp using Yoga; // Using statement for automatic disposal using (var root = new Node { Width = 400f, Height = 300f }) { var child = new Node { Width = 100f, Height = 100f, Parent = root }; root.CalculateLayout(); // Use layout results... Console.WriteLine($"Child position: ({child.Left}, {child.Top})"); } // root is disposed here // Manual disposal var manualNode = new Node { Width = 200f, Height = 200f }; try { manualNode.CalculateLayout(); // Use node... } finally { manualNode.Dispose(); } // Reset node to initial state (without disposing) var resettableNode = new Node { Width = 100f }; resettableNode.FlexGrow = 1f; resettableNode.Reset(); // Clears all properties ``` -------------------------------- ### Configure Yoga Layout Settings Source: https://context7.com/vandercat/yogasharp/llms.txt Shows how to customize global layout behavior using YogaConfig, including web defaults and experimental features. ```csharp using Yoga; // Create a custom configuration var config = new YogaConfig { // Enable web CSS Flexbox compatibility UseWebDefaults = true, // Set point scale factor for rounding (useful for pixel-perfect layouts) PointScaleFactor = 2.0f, // Configure errata flags for layout behavior quirks Errata = Errata.None }; // Access the default configuration var defaultConfig = YogaConfig.Default; // Create nodes with the custom config var root = new Node(config) { Width = 800f, Height = 600f }; // Enable/check experimental features config.SetExperimentalFeatureEnabled(YogaExperimentalFeature.WebFlexBasis, true); bool isEnabled = config.IsExperimentalFeatureEnabled(YogaExperimentalFeature.WebFlexBasis); ``` -------------------------------- ### Build Yoga C++ Library Source: https://github.com/vandercat/yogasharp/blob/main/README.md Configures and compiles the Yoga C++ library using CMake and Ninja. ```shell cmake . -B YogaCpp/build -G Ninja cd YogaCpp/build ninja ``` -------------------------------- ### Calculate Layout and Access Results in C# Source: https://context7.com/vandercat/yogasharp/llms.txt Demonstrates calculating layout for a root node and its children, and accessing computed position and dimension values. Includes options for default and specific dimension calculations. ```csharp using Yoga; var root = new Node { Width = 800f, Height = 600f, FlexDirection = FlexDirection.Column, AlignItems = Align.Center, JustifyContent = Justify.Center }; var box = new Node { Width = 200f, Height = 150f, Parent = root }; box.StyleSetMargin(Edge.All, 10f); box.StyleSetPadding(Edge.All, 5f); // Calculate layout root.CalculateLayout(); // Or calculate with specific dimensions and direction root.CalculateLayout(width: 1024f, height: 768f, direction: Direction.LTR); // Access computed layout values Console.WriteLine($"Box position: ({box.Left}, {box.Top}, {box.Right}, {box.Bottom})"); Console.WriteLine($"Box size: {box.Width}x{box.Height}"); Console.WriteLine($"Box computed margin (top): {box.GetMargin(Edge.Top)}"); Console.WriteLine($"Box computed padding (left): {box.GetPadding(Edge.Left)}"); Console.WriteLine($"Box computed border (all): {box.GetBorder(Edge.All)}"); Console.WriteLine($"Had overflow: {box.HadOverflow}"); // Check layout direction Direction direction = box.Direction; ``` -------------------------------- ### Configure Alignment Properties in YogaSharp Source: https://context7.com/vandercat/yogasharp/llms.txt Demonstrates how to control cross-axis and main-axis alignment using AlignItems, JustifyContent, and AlignSelf. ```csharp using Yoga; var container = new Node { Width = 400f, Height = 400f, FlexDirection = FlexDirection.Row, // Align children along cross-axis (vertical for Row direction) AlignItems = Align.Center, // Center, FlexStart, FlexEnd, Stretch, Baseline // Distribute space along main-axis (horizontal for Row direction) JustifyContent = Justify.SpaceBetween, // Center, FlexStart, End, SpaceAround, SpaceEvenly, SpaceBetween // Align wrapped lines when wrapping is enabled AlignContent = Align.FlexStart }; // Individual child can override parent's AlignItems var specialChild = new Node { Width = 50f, Height = 50f, AlignSelf = Align.FlexEnd, // Override parent's AlignItems for this child Parent = container }; var normalChild = new Node { Width = 50f, Height = 50f, Parent = container }; container.CalculateLayout(); ``` -------------------------------- ### Manage Dirty State and Layout Updates in C# Source: https://context7.com/vandercat/yogasharp/llms.txt Shows how to track layout recalculations using the dirty state system. Includes subscribing to dirty events, checking the dirty status, manually marking nodes as dirty, and handling new layout flags. ```csharp using Yoga; var root = new Node { Width = 400f, Height = 300f }; var child = new Node { Width = 100f, Height = 100f, Parent = root }; root.CalculateLayout(); // Subscribe to dirty events root.HaveDirtied += (Node node) => { Console.WriteLine("Layout needs recalculation!"); }; // Check if layout needs recalculation bool isDirty = root.IsDirty; // Manually mark as dirty (triggers recalculation on next CalculateLayout) root.IsDirty = true; // Check for new layout (after calculation) bool hasNewLayout = root.HasNewLayout; root.HasNewLayout = false; // Reset flag after processing // Modify a property (automatically marks dirty) child.Width = 150f; // Recalculate if dirty if (root.IsDirty) { root.CalculateLayout(); } ``` -------------------------------- ### Define Flex Direction and Flow Source: https://context7.com/vandercat/yogasharp/llms.txt Illustrates how to arrange child nodes using Row, Column, and Reverse layout directions. ```csharp using Yoga; // Row layout (horizontal, left-to-right) var rowContainer = new Node { Width = 600f, Height = 100f, FlexDirection = FlexDirection.Row }; var item1 = new Node { Width = 100f, Height = 100f, Parent = rowContainer }; var item2 = new Node { Width = 100f, Height = 100f, Parent = rowContainer }; var item3 = new Node { Width = 100f, Height = 100f, Parent = rowContainer }; rowContainer.CalculateLayout(); // item1 at (0, 0), item2 at (100, 0), item3 at (200, 0) // Column layout (vertical, top-to-bottom) var columnContainer = new Node { Width = 100f, Height = 600f, FlexDirection = FlexDirection.Column }; var col1 = new Node { Width = 100f, Height = 50f, Parent = columnContainer }; var col2 = new Node { Width = 100f, Height = 50f, Parent = columnContainer }; columnContainer.CalculateLayout(); // col1 at (0, 0), col2 at (0, 50) // Reverse direction var reverseRow = new Node { Width = 600f, Height = 100f, FlexDirection = FlexDirection.RowReverse }; ``` -------------------------------- ### Set Dimensions with Units and Constraints Source: https://context7.com/vandercat/yogasharp/llms.txt Define element dimensions using fixed points, percentages of parent size, or auto-sizing. Apply min/max constraints and percentage-based min/max values. ```csharp using Yoga; var container = new Node { Width = 500f, Height = 400f, FlexDirection = FlexDirection.Column }; // Fixed dimensions var fixedBox = new Node { Width = 100f, Height = 50f, Parent = container }; // Percentage-based dimensions var percentBox = new Node { Parent = container }; percentBox.StyleSetWidthPercent(75f); // 75% of parent width percentBox.StyleSetHeightPercent(25f); // 25% of parent height // Auto dimensions (size to content) var autoBox = new Node { Parent = container }; autoBox.StyleSetWidthAuto(); autoBox.StyleSetHeightAuto(); // Min/Max constraints var constrainedBox = new Node { Width = 200f, Parent = container }; constrainedBox.StyleSetMinWidth(100f); constrainedBox.StyleSetMaxWidth(300f); constrainedBox.StyleSetMinHeight(50f); constrainedBox.StyleSetMaxHeight(200f); // Percentage min/max constrainedBox.StyleSetMinWidthPercent(20f); constrainedBox.StyleSetMaxWidthPercent(80f); container.CalculateLayout(); ``` -------------------------------- ### Configure Border Widths Source: https://context7.com/vandercat/yogasharp/llms.txt Sets border widths for elements, which affect layout calculations. ```csharp using Yoga; var box = new Node { Width = 300f, Height = 200f }; // Set border widths box.StyleSetBorder(Edge.All, 2f); // All edges box.StyleSetBorder(Edge.Top, 4f); // Top border only box.StyleSetBorder(Edge.Left, 1f); box.StyleSetBorder(Edge.Right, 1f); // Get border values float topBorder = box.StyleGetBorder(Edge.Top); box.CalculateLayout(); // Get computed border after layout float computedBorder = box.GetBorder(Edge.Top); ``` -------------------------------- ### YogaSharp CMake Build Configuration Source: https://github.com/vandercat/yogasharp/blob/main/CMakeLists.txt Defines the project structure, source files, and build properties for the Yoga library. ```cmake cmake_minimum_required(VERSION 3.21) set( CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS 1 ) set(WINDOWS_EXPORT_ALL_SYMBOLS ON) project(yoga-sharp) set(CMAKE_VERBOSE_MAKEFILE on) include(CheckIPOSupported) set(YOGA_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/YogaCpp) include(${YOGA_ROOT}/cmake/project-defaults.cmake) file(GLOB SOURCES CONFIGURE_DEPENDS ${YOGA_ROOT}/yoga/*.cpp ${YOGA_ROOT}/yoga/**/*.cpp) add_library(yoga SHARED ${SOURCES}) # Yoga conditionally uses when building for Android if (ANDROID) target_link_libraries(yoga log) endif() check_ipo_supported(RESULT result) if(result) set_target_properties(yoga PROPERTIES CMAKE_INTERPROCEDURAL_OPTIMIZATION true) endif() target_include_directories(yoga PUBLIC $ $) set_target_properties(yoga PROPERTIES PREFIX "") target_compile_options(yoga PRIVATE -static) target_compile_definitions(yoga PRIVATE $<$:DEBUG=1> ) ``` -------------------------------- ### Configure Margin and Padding Source: https://context7.com/vandercat/yogasharp/llms.txt Sets spacing around and inside elements using edge-specific methods and percentage-based values. ```csharp using Yoga; var box = new Node { Width = 200f, Height = 200f }; // Set margins (space outside the element) box.StyleSetMargin(Edge.All, 10f); // All edges box.StyleSetMargin(Edge.Top, 20f); // Top only box.StyleSetMargin(Edge.Horizontal, 15f); // Left and Right box.StyleSetMargin(Edge.Vertical, 25f); // Top and Bottom box.StyleSetMarginPercent(Edge.Left, 10f); // 10% of parent width box.StyleSetMarginAuto(Edge.Right); // Auto margin (for centering) // Set padding (space inside the element) box.StyleSetPadding(Edge.All, 8f); box.StyleSetPadding(Edge.Top, 16f); box.StyleSetPaddingPercent(Edge.All, 5f); // 5% padding // Read back values YogaValue topMargin = box.StyleGetMargin(Edge.Top); YogaValue leftPadding = box.StyleGetPadding(Edge.Left); box.CalculateLayout(); // Get computed margin/padding values after layout float computedMargin = box.GetMargin(Edge.Top); float computedPadding = box.GetPadding(Edge.Left); ``` -------------------------------- ### Implement Custom Measure Function in C# Source: https://context7.com/vandercat/yogasharp/llms.txt Implement a custom MeasureFunction for nodes that require dynamic sizing based on content, such as text. Respect width and height constraints provided by the measure mode. ```csharp using Yoga; using System.Drawing; var container = new Node { Width = 300f, Height = 200f, FlexDirection = FlexDirection.Column }; // Create a text node with custom measurement var textNode = new Node { Parent = container }; // Set custom measure function for text sizing textNode.MeasureFunction = (Node node, float width, MeasureMode widthMode, float height, MeasureMode heightMode) => { // Simulate text measurement string text = "Hello, YogaSharp!"; float charWidth = 8f; float lineHeight = 20f; float measuredWidth = text.Length * charWidth; float measuredHeight = lineHeight; // Respect constraints based on measure mode if (widthMode == MeasureMode.Exactly) { measuredWidth = width; } else if (widthMode == MeasureMode.AtMost) { measuredWidth = Math.Min(measuredWidth, width); } return new SizeF(measuredWidth, measuredHeight); }; container.CalculateLayout(); Console.WriteLine($"Text node size: {textNode.Width}x{textNode.Height}"); ``` -------------------------------- ### Implement Custom Baseline Function in C# Source: https://context7.com/vandercat/yogasharp/llms.txt Implement a custom BaselineFunction for nodes to align elements with different internal content baselines. The function should return the baseline position relative to the top of the element. ```csharp using Yoga; var container = new Node { Width = 400f, Height = 200f, FlexDirection = FlexDirection.Row, AlignItems = Align.Baseline }; var tallNode = new Node { Width = 100f, Height = 150f, Parent = container }; // Node with custom baseline var customBaselineNode = new Node { Width = 100f, Height = 80f, Parent = container }; // Set custom baseline function customBaselineNode.BaselineFunction = (Node node, float width, float height) => { // Return baseline position from top of element // E.g., text baseline might be at 70% of height return height * 0.7f; }; customBaselineNode.IsReferenceBaseline = true; container.CalculateLayout(); ``` -------------------------------- ### Manage Children with NodeList in C# Source: https://context7.com/vandercat/yogasharp/llms.txt Use the NodeList collection to add, insert, access, remove, replace, clear, and set child nodes for a parent node. Supports iteration over children. ```csharp using Yoga; var parent = new Node { Width = 400f, Height = 400f, FlexDirection = FlexDirection.Column }; // Add children var child1 = new Node { Height = 50f }; var child2 = new Node { Height = 50f }; var child3 = new Node { Height = 50f }; parent.Children.Add(child1); parent.Children.Add(child2); parent.Children.Add(child3); // Insert at specific index var insertedChild = new Node { Height = 30f }; parent.Children.Insert(1, insertedChild); // Insert at index 1 // Access by index Node firstChild = parent.Children[0]; // Get child count int count = parent.Children.Count; // Remove specific child parent.Children.Remove(child2); // Replace child at index var replacement = new Node { Height = 40f }; parent.Children[0] = replacement; // Clear all children parent.Children.Clear(); // Set multiple children at once parent.Children.Set(new[] { child1, child2, child3 }); // Iterate children foreach (Node child in parent.Children) { Console.WriteLine($"Child height: {child.Height}"); } parent.CalculateLayout(); ``` -------------------------------- ### Control Element Visibility with Display Property Source: https://context7.com/vandercat/yogasharp/llms.txt Manage element visibility and layout participation using the Display property. 'Flex' is the default and includes the element in layout, while 'None' removes it. ```csharp using Yoga; var container = new Node { Width = 400f, Height = 200f, FlexDirection = FlexDirection.Row }; var visible = new Node { Width = 100f, Height = 100f, Display = Display.Flex, // Default, participates in layout Parent = container }; var hidden = new Node { Width = 100f, Height = 100f, Display = Display.None, // Removed from layout (like CSS display:none) Parent = container }; var anotherVisible = new Node { Width = 100f, Height = 100f, Parent = container }; container.CalculateLayout(); // Only visible and anotherVisible are laid out; hidden takes no space ``` -------------------------------- ### Understanding YogaValue Struct in C# Source: https://context7.com/vandercat/yogasharp/llms.txt Details the `YogaValue` struct, which represents values with units like points, percent, auto, and undefined. Shows how to access predefined values and extract numeric values and units from style properties. ```csharp using Yoga; // Predefined values YogaValue auto = YogaValue.Auto; YogaValue undefined = YogaValue.Undefined; YogaValue zero = YogaValue.Zero; // Reading style values returns YogaValue var node = new Node(); node.StyleSetMarginPercent(Edge.Top, 10f); node.StyleSetPosition(Edge.Left, 50f); YogaValue marginTop = node.StyleGetMargin(Edge.Top); YogaValue posLeft = node.StyleGetPosition(Edge.Left); // Access value and unit Console.WriteLine($"Margin top: {marginTop.Value} {marginTop.Unit}"); // 10 Percent Console.WriteLine($"Position left: {posLeft.Value} {posLeft.Unit}"); // 50 Point // Implicit conversion to float float marginValue = marginTop; // Gets the numeric value ``` -------------------------------- ### Configure Flex Wrap Source: https://context7.com/vandercat/yogasharp/llms.txt Enables wrapping for child nodes when they exceed the container's main axis size. ```csharp using Yoga; var container = new Node { Width = 200f, Height = 400f, FlexDirection = FlexDirection.Row, FlexWrap = Wrap.Wrap, // NoWrap, Wrap, WrapReverse AlignContent = Align.FlexStart // How to align multiple lines }; // Add items that will wrap to next line for (int i = 0; i < 6; i++) { var item = new Node { Width = 80f, Height = 80f, Parent = container }; } container.CalculateLayout(); // Items arranged in 3 rows of 2 items each (80*2 < 200) ``` -------------------------------- ### Set and Calculate Layout Direction in C# Source: https://context7.com/vandercat/yogasharp/llms.txt Use the Direction property to control right-to-left (RTL) or left-to-right (LTR) layouts. Calculate the layout with a specific direction to see the effect. ```csharp using Yoga; var root = new Node { Width = 400f, Height = 200f, FlexDirection = FlexDirection.Row }; // Set text direction root.Direction = Direction.LTR; // Left-to-right (default) // root.Direction = Direction.RTL; // Right-to-left var first = new Node { Width = 100f, Height = 100f, Parent = root }; var second = new Node { Width = 100f, Height = 100f, Parent = root }; // Calculate layout with specific direction root.CalculateLayout(direction: Direction.RTL); // LTR: first at (0,0), second at (100,0) // RTL: first at (300,0), second at (200,0) Direction computedDirection = root.Direction; ``` -------------------------------- ### Control Element Positioning with PositionType Source: https://context7.com/vandercat/yogasharp/llms.txt Use PositionType to switch between normal flow and absolute positioning. Set offsets using StyleSetPosition or StyleSetPositionPercent for absolute elements. ```csharp using Yoga; var container = new Node { Width = 400f, Height = 400f }; // Normal flow positioning (default) var normalChild = new Node { Width = 100f, Height = 100f, PositionType = PositionType.Relative, // Default, follows normal flow Parent = container }; // Absolute positioning - removed from normal flow var absoluteChild = new Node { Width = 50f, Height = 50f, PositionType = PositionType.Absolute, Parent = container }; // Set position offsets for absolute element absoluteChild.StyleSetPosition(Edge.Top, 10f); absoluteChild.StyleSetPosition(Edge.Right, 10f); // Position with percentages absoluteChild.StyleSetPositionPercent(Edge.Left, 50f); // 50% from left // Static positioning (CSS static equivalent) var staticChild = new Node { PositionType = PositionType.Static, Parent = container }; container.CalculateLayout(); // Read position values YogaValue topPos = absoluteChild.StyleGetPosition(Edge.Top); ``` -------------------------------- ### Maintain Proportional Dimensions with AspectRatio Source: https://context7.com/vandercat/yogasharp/llms.txt Use the AspectRatio property to keep elements proportional. Set a ratio like 16:9 or 1:1 for squares. Height will adjust automatically if width is set. ```csharp using Yoga; var container = new Node { Width = 400f, Height = 300f, FlexDirection = FlexDirection.Column }; // 16:9 video container var videoBox = new Node { AspectRatio = 16f / 9f, // 1.77 FlexShrink = 1f, Parent = container }; videoBox.StyleSetWidthPercent(100f); // Square element var squareBox = new Node { AspectRatio = 1f, Width = 100f, // Height will automatically be 100f Parent = container }; container.CalculateLayout(); Console.WriteLine($ ``` -------------------------------- ### Handle Content Overflow with Overflow Property Source: https://context7.com/vandercat/yogasharp/llms.txt Control how content exceeding element bounds is displayed using the Overflow property. Options include Hidden, Scroll, and Visible. Check HadOverflow after layout calculation. ```csharp using Yoga; var container = new Node { Width = 200f, Height = 200f, Overflow = Overflow.Hidden, // Hidden, Scroll, Visible FlexDirection = FlexDirection.Column }; // Large content that may overflow var largeContent = new Node { Width = 300f, Height = 300f, Parent = container }; container.CalculateLayout(); // Check if content overflowed bool hadOverflow = container.HadOverflow; Console.WriteLine($"Had overflow: {hadOverflow}"); ``` -------------------------------- ### Add Spacing Between Flex Items with Gap Source: https://context7.com/vandercat/yogasharp/llms.txt Utilize StyleSetGap to define spacing between flex items. Specify gap for all directions, rows, or columns independently. ```csharp using Yoga; var container = new Node { Width = 500f, Height = 400f, FlexDirection = FlexDirection.Row, FlexWrap = Wrap.Wrap }; // Set gap between all items (row and column) container.StyleSetGap(16f, Gutter.All); // Set specific row gap (between wrapped lines) container.StyleSetGap(20f, Gutter.Row); // Set specific column gap (between items in a row) container.StyleSetGap(10f, Gutter.Column); for (int i = 0; i < 6; i++) { new Node { Width = 100f, Height = 100f, Parent = container }; } container.CalculateLayout(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.