macbackend/Sources/Demo/Demo.swift
david-swift badc0c12f6
All checks were successful
Deploy Docs / publish (push) Successful in 3m3s
SwiftLint / SwiftLint (push) Successful in 3s
Add support for pickers
2024-12-09 21:15:52 +01:00

142 lines
4.0 KiB
Swift

//
// Demo.swift
// MacBackend
//
// Created by david-swift on 25.09.23.
//
// swiftlint:disable missing_docs no_magic_numbers closure_body_length
import Foundation
import MacBackend
@main
struct Demo: App {
var app = MacApp(id: "dev.aparoksha.Demo")
@State("bool")
private var bool = false
@State(blockUpdates: true)
private var width = 500
@State(blockUpdates: true)
private var height = 400
@State private var elements: [Element] = [.init()]
@State private var selectedElement: String?
@State private var alert = false
@State private var menuPresented = false
@State private var selection = "Hello"
var scene: Scene {
Window(id: "secondary") { _ in
Text(selection)
.centeredToolbar {
Picker("Test", items: ["Hello", "World"], selection: $selection)
.segmented()
}
}
.frame(width: .constant(800), height: .constant(600))
Window("Main", id: "main") { _ in
NavigationSplitView {
List(elements, selection: $selectedElement) { element in
Label(element.id, icon: .system(name: "play.fill"))
}
} detail: {
VStack {
Button(selectedElement ?? "World") {
let element = Element()
elements.append(element)
selectedElement = element.id
}
Button(alert.description) {
menuPresented = true
}
.menu(isPresented: $menuPresented) {
MenuButton(alert.description) {
print("Hi")
}
}
}
.alert("Hello", isPresented: $alert)
.cancelButton("Cancel") {
print("Cancel")
}
.destructiveButton("Destructive", default: true) {
print("Destructive")
}
.toolbar {
Button("World", icon: .system(name: "globe")) {
print("World")
}
}
}
}
.frame(width: $width, height: $height)
MenuBar {
Menu("Test") {
MenuButton(bool.description) {
bool.toggle()
}
.keyboardShortcut("h")
.selected(bool)
Divider()
Menu("Test") {
MenuButton("Hi") {}
Menu("Hi?") { }
}
MenuButton("Hello") {
print("Hello")
}
.enabled(bool)
.keyboardShortcut("s")
MenuButton("World") {
print("World")
}
Menu("Actions") {
MenuButton("Quit") {
app.quit()
}
}
}
Menu("Quit") {
MenuButton("Quit") {
app.quit()
}
.keyboardShortcut("q")
}
} app: {
MenuButton("About Demo") {
app.showAboutWindow()
}
Divider()
ServicesMenu("Services")
Divider()
MenuButton("Hide Demo") {
app.hide()
}
.keyboardShortcut("h")
MenuButton("Hide Others") {
app.hideOthers()
}
.keyboardShortcut(.init("h", alt: true))
MenuButton("Show All") {
app.showAll()
}
Divider()
MenuButton("Quit Demo") {
app.quit()
}
.keyboardShortcut("q")
}
}
}
struct Element: Identifiable {
var id: String = UUID().uuidString
}
// swiftlint:enable missing_docs no_magic_numbers closure_body_length