Skip to content

Examples

Vinícius Costa edited this page Apr 12, 2026 · 2 revisions

Minimal counter app

import Aparoksha

@main
struct CounterApp: App {

    let app = Aparoksha(
        id: "com.example.counter",
        name: "Counter",
        settings: "Settings",
        about: "About Counter",
        developer: "Your Name",
        version: "0.1.0",
        quit: "Quit Counter",
        website: .init(string: "https://example.com")!,
        websiteLabel: "Website",
        issues: .init(string: "https://example.com/issues")!,
        issuesLabel: "Issues",
        services: "Services"
    )
    .hideMenu(
        hideApp: "Hide Counter",
        hideOthers: "Hide Others",
        showAll: "Show All"
    )

    var scene: Scene {
        Window("Counter", icon: .headphones, id: "main") {
            ContentView()
        }
    }
}

struct ContentView: WindowView {

    @State private var count = 0

    var view: Body {
        VStack {
            Text("Counter: \(count)")
                .padding(10)

            HStack {
                Button("-1") {
                    count -= 1
                }

                Button("+1") {
                    count += 1
                }
            }
        }
        .padding(20)
    }
}

Alert with text input

import Aparoksha

@main
struct AlertDemoApp: App {

    let app = Aparoksha(
        id: "com.example.alertdemo",
        name: "Alert Demo",
        settings: "Settings",
        about: "About Alert Demo",
        developer: "Your Name",
        version: "0.1.0",
        quit: "Quit Alert Demo",
        website: .init(string: "https://example.com")!,
        websiteLabel: "Website",
        issues: .init(string: "https://example.com/issues")!,
        issuesLabel: "Issues",
        services: "Services"
    )
    .hideMenu(
        hideApp: "Hide Alert Demo",
        hideOthers: "Hide Others",
        showAll: "Show All"
    )

    var scene: Scene {
        Window("Alert Demo", icon: .headphones, id: "main") {
            ContentView()
        }
    }
}

struct ContentView: WindowView {

    @State private var showAlert = false
    @State private var input = ""

    var view: Body {
        VStack {
            Text("Current text: \(input)")
                .padding(10)

            Button("Rename") {
                showAlert = true
            }
        }
        .padding(20)
        .alert(
            visible: $showAlert,
            title: "New name",
            content: "Enter a new name below.",
            closeLabel: "Cancel"
        ) {
            print("Alert closed")
        }
        .primaryResponse("Save", style: .suggested) {
            print("New value: \(input)")
        }
        .textField("Name", text: $input)
    }
}

Multi-window app with menu bar

import Aparoksha

@main
struct MultiWindowDemo: App {

    let app = Aparoksha(
        id: "com.example.multiwindow",
        name: "Multi Window Demo",
        settings: "Settings",
        about: "About Multi Window Demo",
        developer: "Your Name",
        version: "0.1.0",
        quit: "Quit Multi Window Demo",
        website: .init(string: "https://example.com")!,
        websiteLabel: "Website",
        issues: .init(string: "https://example.com/issues")!,
        issuesLabel: "Issues",
        services: "Services"
    )
    .hideMenu(
        hideApp: "Hide Multi Window Demo",
        hideOthers: "Hide Others",
        showAll: "Show All"
    )

    var scene: Scene {
        MenuBar {
            Submenu("File") {
                MenuButton("About") {
                    print("About clicked")
                }
            }
        }

        Window("Main Window", icon: .headphones, id: "main") {
            MainView()
        }

        Window("Secondary Window", icon: .headphones, id: "second") {
            Text("Hello from the second window")
                .padding(10)
        }
        .frame(width: .constant(320), height: .constant(200))
        .leadingToolbarItem("Action", icon: .airplane) {
            print("Toolbar clicked")
        }
    }
}

struct MainView: WindowView {

    @State(blockUpdates: true) private var width = 700
    @State(blockUpdates: true) private var height = 450

    var view: Body {
        VStack {
            Text("Main window")
                .padding(10)
        }
        .padding(20)
    }

    func window(_ window: Window) -> Window {
        window.frame(width: $width, height: $height)
    }
}

Sidebar navigation

import Aparoksha

@main
struct NavigationDemoApp: App {

    let app = Aparoksha(
        id: "com.example.navigationdemo",
        name: "Navigation Demo",
        settings: "Settings",
        about: "About Navigation Demo",
        developer: "Your Name",
        version: "0.1.0",
        quit: "Quit Navigation Demo",
        website: .init(string: "https://example.com")!,
        websiteLabel: "Website",
        issues: .init(string: "https://example.com/issues")!,
        issuesLabel: "Issues",
        services: "Services"
    )
    .hideMenu(
        hideApp: "Hide Navigation Demo",
        hideOthers: "Hide Others",
        showAll: "Show All"
    )

    var scene: Scene {
        Window("Navigation Demo", icon: .headphones, id: "main") {
            ContentView()
        }
    }
}

struct ContentView: WindowView {

    @State private var selectedItem: SidebarItem = .home

    var view: Body {
        FlatNavigation(SidebarItem.allCases, selection: $selectedItem) {
            switch selectedItem {
            case .home:
                VStack {
                    Text("Home page")
                }
                .padding(20)

            case .settings:
                VStack {
                    Text("Settings page")
                }
                .padding(20)
            }
        }
    }
}

enum SidebarItem: String, FlatNavigationItem, CaseIterable, Equatable, Codable {

    case home
    case settings

    var id: Self { self }

    var description: String {
        rawValue.capitalized
    }

    var icon: Icon {
        switch self {
        case .home:
            return .navigation
        case .settings:
            return .warning
        }
    }
}

How to use these examples

Use these examples as a starting point, not as a guarantee that every API surface will remain stable. If a signature changes upstream, update your code against the current demo app and package definitions.

Clone this wiki locally