2.8 KiB
2.8 KiB
Hello World
This is a beginner tutorial. We will create a simple "Hello, world!" app using Adwaita.
Create the Swift Package
- Open your terminal client and navigate to a directory you want to create the package in (e.g.
~/Documents/). - Create a new folder for the package using
mkdir HelloWorld. - Enter the newly created folder using
cd HelloWorld. - Run
swift package init --type executablefor creating a new Swift package. - Open the Swift package. If you are using GNOME Builder, click on
Select a Folder…in the view that appears after opening Builder and open theHelloWorldfolder.
Add the Dependency
- Open the
Package.swiftfile. - Add the following line of code after
name: "HelloWorld",:
dependencies: [.package(url: "https://github.com/AparokshaUI/Adwaita", from: "0.1.1")],
- Add the dependency to the executable target:
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "HelloWorld",
dependencies: [.product(name: "Adwaita", package: "Adwaita")]
),
]
- On macOS you may need to set the platform version. Add the following after
name: "HelloWorld",:
platforms: [.macOS(.v10_15)],
Create the App
- Navigate to
Sources/main.swift. On macOS, you may have to change the file name to something else, e.g.HelloWorld.swift. - An app that uses the Adwaita framework has a structure that conforms to the
Appprotocol. Thesceneproperty returns one or more windows which provide content for display. An@mainattribute marks it as the entry point of the app. Replaceprint("Hello, world!")by your first app:
import Adwaita
@main
struct HelloWorld: App {
let id = "io.github.david_swift.HelloWorld"
var app: GTUIApp!
var scene: Scene {
Window(id: "content") { _ in
Text("Hello, world!")
.padding()
}
}
}
Test the App
- Run the executable Swift package (in GNOME Builder, press the play button, on the command line, use
swift run). You can see that one important component of a window in GNOME is missing: The header bar.
Add a Header Bar
- If you add another view inside of the
Window's body, the views get aligned vertically:
import Adwaita
@main
struct HelloWorld: App {
let id = "io.github.david_swift.HelloWorld"
var app: GTUIApp!
var scene: Scene {
Window(id: "content") { _ in
// Add the header bar:
HeaderBar.empty()
Text("Hello, world!")
.padding()
}
}
}
- Run the app.