adwaita.docc/Basics/WindowsDoc.md

3.7 KiB

Windows

Windows in Adwaita are not actually single windows in the user interface, but rather instructions on how to create one type of window.

The simplest case

A single window app is an app having exactly one window, and when this window is closed, the app terminates. We can add multiple windows to an app as well. Whenever the last one disappears, the app terminates.

@main
struct HelloWorld: App {

    let app: AdwaitaApp = .init(id: "io.github.david_swift.HelloWorld")

    var scene: Scene {
        Window(id: "content") { _ in
            Text("Hello, world!")
                .padding()
                .topToolbar {
                    HeaderBar.empty()
                }
        }
        // Add a second window:
        Window(id: "window-2") { _ in
            Text("Window 2")
                .padding()
                .topToolbar {
                    HeaderBar.empty()
                }
        }
    }

}

Showing windows

Every app contains the property App/app. You can use this property for running functions that affect the whole app, e.g. quitting the app. Another use case is showing a window:

@main
struct HelloWorld: App {

    let app: AdwaitaApp = .init(id: "io.github.david_swift.HelloWorld")

    var scene: Scene {
        Window(id: "content") { _ in
            Text("Hello, world!")
                .padding()
                .topToolbar {
                    HeaderBar.empty()
                }
        }
        Window(id: "control") { _ in
            Button("Show Window") {
                // Show the window with the identifier "content":
                app.showWindow("content")
            }
            .padding()
            .topToolbar {
                HeaderBar.empty()
            }
        }
    }

}

"Showing" a window means creating an instance of the window type if there isn't one, or focusing the window that already exists of that type otherwise. It should be used for opening windows that cannot be presented more than once and for moving a window that is already open into the foreground.

Adding windows

You can call the AdwaitaApp/addWindow(_:) function instead of AdwaitaApp/showWindow(_:) if you want to add and focus another instance of a window type:

@main
struct HelloWorld: App {

    let app: AdwaitaApp = .init(id: "io.github.david_swift.HelloWorld")

    var scene: Scene {
        Window(id: "content") { _ in
            Text("Hello, world!")
                .padding()
                .topToolbar {
                    HeaderBar.empty()
                }
        }
        Window(id: "control") { _ in
            Button("Add Window") {
                // Add a new instance of the "content" window type
                app.addWindow("content")
            }
            .padding()
            .topToolbar {
                HeaderBar.empty()
            }
        }
    }

}

Customizing the initial number of windows

By default, every window type of the app's scene appears once when the app starts. It's possible to customize how many windows are being presented at the app's startup:

@main
struct HelloWorld: App {

    let app: AdwaitaApp = .init(id: "io.github.david_swift.HelloWorld")

    var scene: Scene {
        // Open no window of the "content" type
        Window(id: "content", open: 0) { _ in
            Text("Hello, world!")
                .padding()
                .topToolbar {
                    HeaderBar.empty()
                }
        }
        // Open two windows of the "control" type
        Window(id: "control", open: 2) { _ in
            Button("Show Window") {
                app.addWindow("content")
            }
            .topToolbar {
                HeaderBar.empty()
            }
        }
    }

}