AparokshaUI/adwaita-swift

Spawning windows from outside an app (e.g. for CLI)

llsc12 opened this issue · 2 comments

I'm making a CLI program that the user runs, and I'd like to spawn windows when the user runs a particular command. How can I accomplish this? My program has its own entrypoint rather than having an App struct as per usual. Thank you!

You can start an app manually using the static main function (it's possible to have different apps with different windows).

import Adwaita

@main
enum Main {

    static func main() {
        print("App 1")
        App1.main()
        print("App 2")
        App2.main()
        print("Quit")
    }

}

struct App1: App {

    let id = "io.github.AparokshaUI.AdwaitaTemplate"
    var app: GTUIApp!

    var scene: Scene {
        Window(id: "main", open: 2) { window in
            Text("Hi")
                .padding(20)
                .topToolbar {
                    HeaderBar.empty()
                }
        }
    }

}

struct App2: App {

    let id = "io.github.AparokshaUI.AdwaitaTemplate"
    var app: GTUIApp!

    var scene: Scene {
        Window(id: "main") { window in
            Text(Loc.helloWorld)
                .padding(20)
                .topToolbar {
                    HeaderBar.empty()
                }
        }
    }

}

As this feels a bit hacky and is not suited for every use case, I'll add support for spawning individual windows on a specific app.

There's now a more flexible way to do this since 8b27a08:

import Adwaita

@main
enum Main {

    static func main() {
        print("App 1")
        let app1 = Test.setupApp()
        app1.run(automaticSetup: false) {
            app1.addWindow("test-1")
        }
        print("App 2")
        let app2 = Test.setupApp()
        app2.run(automaticSetup: false) {
            app2.addWindow("test-2")
        }
        print("Quit")
    }

}

struct Test: App {

    let id = "io.github.AparokshaUI.AdwaitaTemplate"
    var app: GTUIApp!

    var scene: Scene {
        Window(id: "test-1") { window in
            Text("Hi")
                .padding(20)
                .topToolbar {
                    HeaderBar.empty()
                }
        }
        Window(id: "test-2") { window in
            Text(Loc.helloWorld)
                .padding(20)
                .topToolbar {
                    HeaderBar.empty()
                }
        }
    }

}