Skip to content
eloamPublic

About

Experimental SwiftUI-style declarative UI in C# for Avalonia — no XAML.

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

Martlet

Status: experimental License: MIT .NET 10 Avalonia 12 Platforms

Martlet is a declarative UI framework for .NET: you write your user interface in C#, in the style of SwiftUI and Jetpack Compose, and Avalonia renders it with its own controls. No XAML, no view models, no bindings to wire: a component is some state and a Body. Its packages and its namespaces are named MartletUI.

MartletApp.Run<CounterApp>("Counter", 400, 300);

public sealed partial class CounterApp : Component
{
    [State] partial int Count { get; set; }

    public override View Body =>
        HStack(() =>
        {
            Button("−", () => Count--).Style(ButtonStyle.Secondary);
            Text($"{Count}").Font(TextStyle.Title);
            Button("+", () => Count++);
            if (Count != 0)
                Button("Reset", () => Count = 0).Style(ButtonStyle.Ghost);
        })
        .Padding(24);
}

A stack takes its children as arguments, HStack(a, b), or as a block, as above: each view statement of the block is a child, and if, switch and local variables decide which views are shown, as in a SwiftUI @ViewBuilder.

Controls bind to state with the generated Bind property: Bind.Name is a Binding<string> that the field reads and writes, so there is no event handler to write.

public sealed partial class SignUp : Component
{
    [State] partial string Name { get; set; } = "";
    [State] partial bool Newsletter { get; set; }

    public override View Body =>
        VStack(
            TextField(Bind.Name, "Your name"),               // reads and writes Name
            Checkbox(Bind.Newsletter, "Send me the news"),   // reads and writes Newsletter
            Text(Name.Length == 0 ? "Hello!" : $"Hello, {Name}!"))
        .Padding(24);
}

The sample app with the Standard light theme   The sample app with the custom Sable dark theme

Warning

Martlet is experimental. The API can change at any time and the packages are not published to NuGet. Do not use it in production yet.

Features

  • Concise C# views. Views are immutable records built with factory functions (Text, Button, VStack…) and chainable modifiers (.Padding(16), .Background(…)). The common case fits on one line.
  • Fine-grained reactivity. Properties marked [State] are generated at compile time. A change re-evaluates only the components and the view methods that read it, whether it comes from an action, after an await or from a background thread.
  • Generated two-way bindings. TextField(Bind.Draft), Checkbox(todo.Bind.Done): each [State] property gets its binding, like $draft in SwiftUI.
  • Real Avalonia controls. A reconciler maps each view to an Avalonia control and keeps it from one update to the next, so focus, text selection, input methods and scrolling inside fields come from Avalonia.
  • Themes declared by the view. Light and dark families that follow the system, named colors and styles, and .Theme("Sable") on any view or component. No visual value is hard-coded.
  • Control templates. Replace the whole look of a button, checkbox or text field with a view, keeping its behavior.
  • View builder blocks. VStack(() => { … }) takes if, switch and local variables, and Show(…) adds any view; the generator intercepts each view statement at compile time, so the code runs as written, and the IDE offers fixes such as "Convert to block".
  • View lifecycle. .OnAppear, .OnDisappear and .Task, whose task is cancelled when the view disappears.
  • Compile-time diagnostics. Analyzers report misuses (MVU001–MVU018) with the fix in the message.
  • Hot reload. Edit a Body and save: the window updates in about a second and keeps its state.
  • Escape hatch. Any Avalonia control can be embedded with Native(() => new Calendar()).

Getting started

Prerequisites

Run the sample app

git clone https://github.com/eloam/MartletUI.git
cd MartletUI
dotnet run --project samples/TodoDemo

For hot reload, run dotnet watch --project samples/TodoDemo and edit any Body.

Use it in your app

Until the packages are on NuGet, reference the projects from source. The runtime brings the core; the generators and the code fixes are added as analyzers, and the build rules of the core package are imported (they allow the code that the generator writes for view builder blocks):

<ItemGroup>
  <ProjectReference Include="..\MartletUI\src\MartletUI\MartletUI.csproj" />
  <ProjectReference Include="..\MartletUI\src\MartletUI.Generators\MartletUI.Generators.csproj"
                    OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  <ProjectReference Include="..\MartletUI\src\MartletUI.CodeFixes\MartletUI.CodeFixes.csproj"
                    OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<Import Project="..\MartletUI\src\MartletUI.Core\buildTransitive\MartletUI.Core.targets" />

Then import the namespaces once, in a GlobalUsings.cs file:

global using MartletUI;
global using static MartletUI.Views.Ui;

Documentation

Topic Description
Reactive state [State], StateList, bindings, scopes, the generator and its diagnostics
Writing views Controls, stacks, modifiers, actions, async code, errors, drawing and embedding Avalonia controls
Theming Themes and families, the catalog, named tokens and control templates
Windows and menus Window options, zoom, the menu bar and keyboard shortcuts
Architecture How the reconciler works, the projects and the repository layout

Supported platforms

Platform Status
macOS Supported, including the system menu bar
Windows Supported by Avalonia; not yet verified with this version of the framework
Linux Supported by Avalonia; not yet verified with this version of the framework
iOS, Android, browser Planned: Avalonia runs there, the framework still needs a mobile and a browser host

Building from source

dotnet build Martlet.sln
dotnet test Martlet.sln

The build treats every warning of the framework and its tests as an error (the samples keep them as warnings, so that they can be edited freely). Tests open real Avalonia windows without a screen (Avalonia.Headless), so they run on any machine.

Roadmap

Continuous integration on Windows and Linux, ScrollView and virtualized lists, navigation and modal sheets, input controls (Toggle, Picker, Slider, multiline text), images, animations, accessibility, then mobile and browser hosts. The roadmap gives the milestones and what has shipped.

Contributing

Contributions are welcome. Please read CONTRIBUTING.md before opening a pull request.

License

Martlet is licensed under the MIT license.

About

Experimental SwiftUI-style declarative UI in C# for Avalonia — no XAML.

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages