Skip to main content

Architecture

Sway turns a widget tree into pixels in stages that mirror Flutter's. All of it lives in Sway.Widgets, apart from the host at the bottom:

Widget tree immutable configuration returned by Build()
│ diff by type and key
▼
Element tree long-lived instances: State, inherited lookups, dirty tracking
▼
RenderObject tree constraints down, sizes up; paint; hit test
▼
Skia canvas SKCanvas on an OpenGL surface (or a CPU bitmap when headless)
▼
Platform host window, input routing, clipboard, cursors, system theme

Foundation​

Foundation/Widget/ defines Widget, StatelessWidget, StatefulWidget and State, InheritedWidget, ParentDataWidget, the render-object widgets, and an element class for each. Element.UpdateChildren reconciles child lists from both ends and then by key, so a State follows its widget when siblings reorder. BuildOwner keeps the dirty list and rebuilds the shallowest elements first.

The rest of Foundation/ is plain value types and helpers: Geometry/ (Offset, Size, Rect, EdgeInsets, Alignment, BoxConstraints, Colors), Painting/ (decorations, borders, shadows, gradients), Animation/, Curves/, Physics/ (springs and friction), TextStyle/ and TextEditState/.

Rendering​

Rendering/RenderObject/RenderObject.cs is the base of the render tree. MarkNeedsLayout flags the chain up to the root, and a layout pass only re-lays-out flagged nodes: a child with unchanged constraints and no dirty flag returns immediately. RenderBox adds the constraint protocol and hit testing. The layout widgets live in RenderShifted/ (padding, align, clip, opacity, transform, listener), RenderFlex/ (Row, Column) and RenderLayouts/ (Stack, Wrap, Grid, filters).

Text goes through RenderParagraph. Runs are measured with HarfBuzz (TextShaper), broken into lines, split into directional runs by Bidi, and drawn per run in visual order. RenderEditable does the same for text fields and adds the caret, selection and inner scrolling on top of TextEditState. Shaped text and line layouts are cached.

Repaint boundaries​

Layout is incremental, but painting walks the tree each frame. RepaintBoundary is the opt-in way to avoid repainting a subtree that hasn't changed: RenderRepaintBoundary records its child into a picture and replays it until something inside calls MarkNeedsPaint, which walks up to the nearest boundary. Wrap list rows, sidebars or cards that sit beside something animating. It does not help a subtree that changes every frame, and drawing more than a small margin outside the child's box is cut off.

Binding and frames​

Widgets/Binding.cs (WidgetsBinding) owns the build and pipeline owners and runs a frame:

  1. Run due timers and frame callbacks (tickers, fling).
  2. Rebuild dirty elements.
  3. Flush layout.
  4. Paint the tree into the canvas.
  5. Re-run hover testing, since layout may have moved things under a still pointer.

The host only draws when NeedsFrame is true: something was marked dirty, a timer is due, or an animation requested another frame. Pointer movement requests a frame only when it changed something (hover, cursor, an active drag). The clock can be frozen and advanced by hand (UseManualClock, AdvanceClock), which makes animated screenshots and tests deterministic.

Input​

GestureBinding hit tests the render tree, delivers pointer events to the objects under the pointer, and tracks hover for MouseRegion. GestureArena decides which recognizer wins when several compete (a tap against a drag inside a scroll view): the first claimant wins, otherwise the deepest on pointer-up. Touch input (TouchInput) adds drag-to-scroll with fling.

FocusManager holds the primary focus, dispatches key events up the focus chain, implements Tab traversal, and scrolls the focused widget into view. Text editing, including IME composition on Android, runs through EditableText and TextEditState.

Router (Widgets/Navigation/) matches URL patterns to pages and keeps a page stack. It reads and writes the current location through AppLocation.Source, an ILocationSource: the browser address bar on the web, a MemoryLocationSource elsewhere. WidgetsBinding.HandleBack() is how a host reports a back gesture.

Localizations and LocalizationsDelegate<T> (Widgets/Localization/) load resources for a Locale and provide them through the tree. Design-system app widgets register their own delegates.

Platform hosts​

Sway.Widgets has no window, input or OS dependency. It talks to a host through a small surface: WidgetsBinding accepts pointer, key and text events and draws a frame onto any SKCanvas; the clipboard and cursor are delegates; ISystemThemeSource (installed through SystemTheme.Source) supplies the OS light/dark preference and accent colour; IFilePickerSource backs FilePicker; ILocationSource backs Router; and GlContext exposes the GL loader to Sway.Gl.

HostResponsibilities
Sway.Platform.DesktopApp.Run owns the Silk.NET window, the OpenGL/Skia surface and input wiring. KeyMap translates Silk.NET keys to the DOM-style names that KeyEvent.Key carries ("Enter", "ArrowLeft"). DesktopSystemTheme reads the OS theme from the registry, defaults or gsettings. File pickers per OS.
Sway.Platform.AndroidSwayActivity and SwayView provide a GL surface, touch, the soft keyboard and IME, the back gesture, and the system theme.
Sway.Platform.WebSwayView is a Blazor component on SkiaSharp.Views.Blazor (WebGL), with pointer, keyboard, clipboard, theme, file input and the History API.
Sway.Platform.HeadlessHeadless.Screenshot renders to a PNG with a software surface and no host at all.

A new platform is a new Sway.Platform.* project that creates a surface, forwards input to WidgetsBinding, calls DrawFrame whenever NeedsFrame is set, and provides an ISystemThemeSource. Nothing in Sway.Widgets changes.

Design systems​

Design systems sit on top of the neutral building blocks in Widgets/ (Interactive, EditableText, Icon, Overlay, the animation widgets) and know nothing about each other. Each ships an app widget, a theme object carried by an InheritedWidget, and its components.

Where to look in the code​

Paths are relative to Sway.Widgets unless noted.

AreaFiles
Widget, Element, StateFoundation/Widget/
Constraints, geometry, decorationsFoundation/Geometry/, Foundation/Painting/
Render objects and layoutRendering/RenderObject/, RenderBox/, RenderShifted/, RenderFlex/, RenderLayouts/
TextRendering/TextShaper.cs, Bidi/, FontCache.cs, RenderParagraph/, RenderEditable.cs, Foundation/TextEditState/
Frames, clock and gesturesWidgets/Binding.cs, Widgets/Gestures/
Focus and keyboardWidgets/Focus/, Sway.Platform.Desktop/KeyMap.cs
Scrolling and lazy listsWidgets/Scrolling/
AnimationFoundation/Animation/, Foundation/Curves/, Foundation/Physics/, Widgets/Animated/
Navigation and localizationWidgets/Navigation/, Widgets/Localization/, Platform/Location/
Platform seamsPlatform/ (file picker, location, system theme, GL context)
Repaint cachingWidgets/Basic/RepaintBoundary.cs, Rendering/RenderShifted/RenderRepaintBoundary.cs
Material 3Sway.Extras.Material3/ (ColorScheme.cs, ThemeData.cs, MaterialApp.cs, one file per component)
IBM CarbonSway.Extras.Ibm/ (CarbonColors.cs, CarbonTheme.cs, DataTable.cs, ...)
FioriSway.Extras.Fiori/Foundations/, Components/, Patterns/
ShopifySway.Extras.Shopify/
OpenGLSway.Gl/