import AIDEKit import Observation import SwiftUI /// A die in the status bar: a sample plugin for AIDE, small enough to review in a minute. @MainActor @Observable final class Die { var face = 1 func roll() { face = Int.random(in: 1...6) } } final class DicePlugin: AIDEPlugin { func activate(_ scope: PluginScope) { let die = Die() die.face = scope.handedOff.flatMap { try? JSONDecoder().decode(Int.self, from: $0) } ?? scope.storage.value(Int.self, forKey: "face") ?? 1 let storage = scope.storage let roll: @MainActor () -> Void = { die.roll() storage.set(die.face, forKey: "face") } scope.command("roll", title: "Roll Dice", run: roll) scope.statusItem("die") { DieItem(die: die, roll: roll) } self.die = die } private var die: Die? func handOff() -> Data? { die.flatMap { try? JSONEncoder().encode($0.face) } } } struct DieItem: View { let die: Die let roll: @MainActor () -> Void var body: some View { Button(action: roll) { Label("Rolled \(die.face)", systemImage: "die.face.\(die.face)") .labelStyle(.titleAndIcon) .font(.caption) } .buttonStyle(.plain) .help("Roll again") } } @_cdecl("aide_plugin_create") public func aidePluginCreate() -> UnsafeMutableRawPointer { PluginEntry.export { DicePlugin() } }