summaryrefslogtreecommitdiff
path: root/macos/Sources/Features/App Intents/QuickTerminalIntent.swift
blob: 2e6c9850c23e3d1c7f94a1e536c46cb645419988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import AppKit
import AppIntents

struct QuickTerminalIntent: AppIntent {
    static var title: LocalizedStringResource = "Open the Quick Terminal"
    static var description = IntentDescription("Open the Quick Terminal. If it is already open, then do nothing.")

    @available(macOS 26.0, *)
    static var supportedModes: IntentModes = .background

    @MainActor
    func perform() async throws -> some IntentResult & ReturnsValue<[TerminalEntity]> {
        guard await requestIntentPermission() else {
            throw GhosttyIntentError.permissionDenied
        }
        
        guard let delegate = NSApp.delegate as? AppDelegate else {
            throw GhosttyIntentError.appUnavailable
        }

        // This is safe to call even if it is already shown.
        let c = delegate.quickController
        c.animateIn()

        // Grab all our terminals
        let terminals = c.surfaceTree.root?.leaves().map {
            TerminalEntity($0)
        } ?? []

        return .result(value: terminals)
    }
}