Browse the documentation

Desktop apps

The Desktop sample is the same to-do app in Windows Forms, WPF and WinUI 3, each an ordinary .NET project whose sources are Typhon modules.

samples/Desktop contains the same small to-do app three times, once for each .NET desktop framework. Each app has a text box with an Add button, a list of check boxes, a status line, a button that removes finished items, and a button that loads examples on a worker thread while the window stays responsive. Each app is an ordinary .NET project whose sources are .ty files.

FolderFrameworkWindow
WinFormsWindows FormsA Form subclass that builds its controls in code.
WpfWPFXAML compiled to BAML during the build, loaded and wired in Typhon.
WinUIWinUI 3 with the Windows App SDK, unpackagedXAML parsed at run time and wired in Typhon. One C# base class implements an interface that WinUI’s control styles need.
SharedNoneThe model in plain Typhon, which each app compiles with its own files.

Classes and events

A Typhon class can derive from a .NET class, and super().__init__() calls the base constructor. Assignments to inherited properties set them, and += subscribes a bound method or a lambda to a .NET event. An excerpt of the Windows Forms window:

class TodoForm(Form):
    def __init__(self) -> None:
        super().__init__()
        self.todos = TodoList()
        self.Text = "Typhon To-Do (Windows Forms)"
        self.ClientSize = Size(420, 360)

        self.add_button = Button()
        self.add_button.Text = "Add"
        self.add_button.Click += self.on_add

A method named like a virtual .NET member overrides it with the access that C# requires, for example def OnLaunched(self, args) in the WinUI app. A parameter that is a .NET int in the base method needs an int32 annotation. For WPF and WinUI, the Typhon class loads the XAML and looks up the named elements with FindName, and typing.cast gives the result its type: self.add_button = cast(Button, view.FindName("AddButton")).

UI thread

The desktop frameworks need a UI thread in a single-threaded COM apartment, which the CLR sets up from [STAThread] on the entry point. Typhon generates Main itself, so @STAThread goes on the function that starts the UI, and Typhon puts the attribute on Main as well:

@STAThread
def main(args: list[str]) -> None:
    Application.Run(TodoForm())


if __name__ == "__main__":
    main(sys.argv[1:])

An async def method is a .NET Task method. In the sample, titles = await Task.Run(example_titles) runs a slow function on the thread pool, and the code after await continues on the UI thread.

Running the sample

dotnet run --project samples/Desktop/WinForms
dotnet run --project samples/Desktop/Wpf
dotnet run --project samples/Desktop/WinUI

The projects build on Windows, Linux and macOS, but the apps run only on Windows. WinUI 3 also needs the Windows App Runtime 2.7 or later on the machine that runs it. With --smoke, an app types, clicks and ticks through its window off-screen, prints what the window shows and exits. Published as self-contained Windows builds, the Windows Forms and WPF apps printed the same lines, except for the framework named in the first line:

Windows Forms on .NET 11.0.0, UI thread: ApartmentState.STA
added two: ['[ ] Buy milk', '[ ] Write a Typhon sample'] - 0 of 2 done
Add enabled for a blank title: False
ticked the first: ['[x] Buy milk', '[ ] Write a Typhon sample'] - 1 of 2 done
removed done: ['[ ] Write a Typhon sample'] - 0 of 1 done
loading: Loading… - Load enabled: False
loaded examples: ['[ ] Write a Typhon sample', '[ ] Read docs/language.md', '[ ] Try typhon run', '[ ] Port a script'] - 0 of 4 done
back on the UI thread after await: True

The sample’s README describes the limits it runs into. For example, a module cannot define a function with its own name.