Gtk4DotNet 7.0.1-beta-1

This is a prerelease version of Gtk4DotNet.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Gtk4DotNet --version 7.0.1-beta-1                
NuGet\Install-Package Gtk4DotNet -Version 7.0.1-beta-1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Gtk4DotNet" Version="7.0.1-beta-1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gtk4DotNet --version 7.0.1-beta-1                
#r "nuget: Gtk4DotNet, 7.0.1-beta-1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Gtk4DotNet as a Cake Addin
#addin nuget:?package=Gtk4DotNet&version=7.0.1-beta-1&prerelease

// Install Gtk4DotNet as a Cake Tool
#tool nuget:?package=Gtk4DotNet&version=7.0.1-beta-1&prerelease                

Gtk4DotNet

A C# wrapper for GTK4 (.NET 8). You can create programs using the GTK4 UI system as a .NET 8.

Gtk4DotNet uses a functional declarative approach to GTK4 similar to REACT or Kotlin Compose:

return Application
    .New("org.gtk.example")
    .OnActivate(app => 
        app
            .NewWindow()
            .Title("Hello Gtk👍")
            .SideEffect(win => win
            .Child(
                Grid
                    .New()
                    .Attach(                                
                        Button
                            .NewWithLabel("Button 1")
                            .OnClicked(() => WriteLine("Button1 clicked")), 
                        0, 0, 1, 1)
                    .Attach(                                
                        Button
                            .NewWithLabel("Button 2")
                            .OnClicked(() => WriteLine("Button2 clicked")), 
                        1, 0, 1, 1)
                    .Attach(                                
                        Button
                            .NewWithLabel("Quit")
                            .OnClicked(() => win.CloseWindow()), 
                        0, 1, 2, 1)))
            .Show())
    .Run(0, 0);
}

Table of contents

  1. Prerequisites
  2. Hello World (a minimal GTK4 app)
    1. Fluent syntax

Prerequisites <a name="prerequisites"></a>

Necessary prerequisites only depending on the version of Linux

On modern Linux like Ubuntu 24.04 or Fedora 40 Gtk4DotNet apps will run out of the box (if you create a full contained single file exe), otherwise you have to install the necessary dotnet runtime.

libadwaita is only necessary if you want to create Adwaita apps, and webkitgtk6 you only need when integrating a webview.

On older/other Linux systems perhaps you have to install one of the following packages in order to make the app runnable.

sudo apt install libgtk-4-dev
sudo apt install libadwaita-1-dev
sudo apt install libwebkitgtk-6.0-dev

For example on Linux Mint 22 you only have to install

sudo apt install libwebkitgtk-6.0-dev

if you want to use webkit webview whereas for KDE neon 6.0 you have to install

sudo apt install libadwaita-1-dev
sudo apt install libwebkitgtk-6.0-dev

The necessary Gtk4DotNet Nuget package <a name="nuget"></a>

To use these features there is a nuget package Gtk4DotNet, which you have to include.

Hello World (a minimal GTK4 app) <a name="helloworld"></a>

In a newly created folder create a new console program with .NET (dotnet new console). Now add the necessary package: dotnet add package Gtk4DotNet.

In the created file Program.cs replace all code with:

using GtkDotNet;

static class First
{
    public static int Run()
    {
        var app = Application.New("de.uriegel.first");
        return app.Run(0, 0);
    }
}

It creates a GTK45 Application object and then runs the message loop. Compile the program and start it. It starts and ends immediately with the warning:

GLib-GIO-WARNING **: 08:27:42.149: Your application does not implement g_application_activate() and has no handlers connected to the 'activate' signal. It should do one of these.

When the app is being activated, you have to implement the activate method. You can do this with a injected C# callback with the help of Application.OnActivate. Let's do this:

    var app = Application.New("de.uriegel.first");  
    app.OnActivate(app => Console.WriteLine("App is being activated"));
    return app.Run(0, 0);

When you debug the program, OnActivate is being called and returns immediately. When app.Run() is being executed, the injected callback is being called and the text is being displayed in the terminal. However, the app also stops immediately. Of cource some kind of UI has to be created.

Let's create a window, this has to be done in the Application.OnActivate callback:

app.OnActivate(app =>
{
    var windows = app.NewWindow();
    windows.Show();
});

Now an empty default window is being shown and the function call Applicatio.Run() will only return when the window is being closed.

Fluent syntax <a name="fluentsyntax"></a>

To avoid creating many variables only to set them as parameters in a function, Gtk4DotNet uses a functional builder approach to create a complicated ui with fluent syntax. Many setter function returns the same instance, so that builder functions can be assigned in a row.

The above sample can be written using this approach as:

static class First
{
    public static int Run()
        => Application
            .New("de.uriegel.first")
            .OnActivate(app =>
                app
                    .NewWindow()
                        .Show())
            .Run(0, IntPtr.Zero);
}

With this approach the hierarchy of the GTK4 application is being reflected in code.

The Window is very empty. Let's add a title and change the default size:

    .NewWindow()
        .Title("Hello Gtk👍")
        .DefaultSize(600, 200)
        .Show())

// TODO refer to GTK exmples in GTK tutorial and GTK4 Rust

// TODO show image

// TODO Now complete Hello World

// TODO Adwaita e.g. color schemes.. // TODO Pipe SideEffect // TODO Lambdas in callbacks

// TODO explain static classes Object and ObjectHandle

DEPRECATED Part

Contained in this Repo are samples how to use Gtk4DotNet. All examples of the official GTK4 are transformed to C# with Gtk4DotNet.

If you want to use GTK resources

  • sudo apt install libglib2.0-dev-bin

Installation of GTK Schema

    sudo install -D ./Test/org.gtk.example.gschema.xml /usr/share/glib-2.0/schemas/
    sudo glib-compile-schemas /usr/share/glib-2.0/schemas/

Usage

Look at the sample programs (https://github.com/uriegel/Gtk4DotNet/tree/Main/Test)

Checking if memory is being freed

To check if GObjects are being freed, just run

Widget.AddWeakRef(() => Console.WriteLine("... is being freed));

If this object is finalized, then the callback will be called.

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Gtk4DotNet:

Package Downloads
WebWindowNetCore

A C# Webview Application for Windows and Linux similar to Electron based on WebView2 (Windows) and GTK WebKit (Linux)

WebWindowNetCore.Linux

A C# Webview Application for Linux similar to Electron based on GTK WebKit

Gtk4DotNet.FSharp

.NET 8 F# Bindings for GTK 4, functional declarative similar to REACT or Kotlin Compose

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.0.7-beta-7 41 3/2/2025
7.0.6-beta-6 66 2/26/2025
7.0.5-beta-5 82 2/25/2025
7.0.4-beta-4 76 2/24/2025
7.0.3-beta-3 84 2/24/2025
7.0.2-beta-2 80 2/24/2025
7.0.1-beta-1 71 2/24/2025
7.0.0-beta-0 86 2/23/2025
6.8.1 119 2/15/2025
6.8.0 85 2/15/2025
6.7.1 165 2/8/2025
6.7.0 82 2/8/2025
6.6.1 81 2/8/2025
6.6.0 159 2/3/2025
6.5.6 98 2/2/2025
6.5.5 177 9/7/2024
6.5.4 215 8/31/2024
6.5.4-beta6 96 8/31/2024
6.5.4-beta5 91 8/31/2024
6.5.4-beta4 93 8/31/2024
6.5.4-beta3 98 8/31/2024
6.5.3 288 8/23/2024
6.5.3-beta3 97 8/31/2024
6.5.3-beta2 100 8/31/2024
6.5.3-beta1 91 8/31/2024
6.5.2 144 8/19/2024
6.5.1 123 8/19/2024
6.5.0 139 8/18/2024
6.4.0 144 8/13/2024
6.3.0 133 8/12/2024
6.2.1 143 8/11/2024
6.2.0 126 8/11/2024
6.1.3 136 8/10/2024
6.1.2 133 8/9/2024
6.1.1 120 8/9/2024
6.1.0 310 4/29/2024
6.0.0 131 4/27/2024
5.4.0 155 4/20/2024
5.3.11 130 4/20/2024
5.3.10 121 4/20/2024
5.3.9 131 4/19/2024
5.3.8 141 3/26/2024
5.3.7 142 3/13/2024
5.3.6 205 1/20/2024
5.3.5 131 1/20/2024
5.3.4 179 1/7/2024
5.3.3 156 1/3/2024
5.3.2 151 1/2/2024
5.3.1 133 1/2/2024
5.3.0 142 1/2/2024
5.2.0 159 12/22/2023
5.1.0 136 12/22/2023
5.0.0 213 12/14/2023
4.0.0 132 12/13/2023
3.4.0 135 12/10/2023
3.3.1 143 12/10/2023
3.3.0 124 12/10/2023
3.2.0 132 12/9/2023
3.1.1 130 12/9/2023
3.1.0 128 12/9/2023
3.0.0 145 12/9/2023
2.0.0 145 11/28/2023
1.4.0 206 11/17/2023
1.3.3 115 11/16/2023
1.3.2 139 11/16/2023
1.3.1 124 11/16/2023
1.3.0 147 11/16/2023
1.2.1 152 10/29/2023
1.2.0 149 10/22/2023
1.1.0 209 4/24/2023
1.0.0 179 4/23/2023

Subclassing for GTK4 objects