Cotton code
57 lines · 2.3 KB · SwiftRaw
import AppKit/// Cotton for the Mac: a menu-bar importer and nothing else (LSUIElement, no/// dock icon, no windows besides the popover).////// Launch arguments:///   -CottonOpenPopover YES   open the popover once launched///   -CottonCannedStatus YES  (Debug builds only) draw a canned status, no core, no network///   -CottonSnapshot <path>   (Debug builds only) draw the popover offscreen to a PNG, then quit@main@MainActorfinal class CottonApp: NSObject, NSApplicationDelegate {    private let model = AppModel()    private var statusItem: StatusItemController?    private var updates: Updates?    static func main() {        let app = NSApplication.shared        let delegate = CottonApp()        app.delegate = delegate        app.setActivationPolicy(.accessory)        app.run()    }    func applicationDidFinishLaunching(_ notification: Notification) {        // Before anything starts: a copy opened by hand hands itself to the        // launch agent, which then runs Cotton (and brings it back after a crash).        if LoginItem.registerIfInstalled() {            exit(0)        }        let controller = StatusItemController(model: model)        statusItem = controller        model.launch()        let updates = Updates()        self.updates = updates        if updates.available {            model.updatesCheck = { [weak updates] in updates?.checkNow() }        }        if UserDefaults.standard.bool(forKey: "CottonOpenPopover") {            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { controller.show() }        }        #if DEBUG        if let path = UserDefaults.standard.string(forKey: "CottonSnapshot") {            // -CottonSnapshotPage <source> opens that source's page; -CottonSnapshotDelay <seconds> waits for the            // core's status first (a check build against dev).            let delay = max(2, UserDefaults.standard.double(forKey: "CottonSnapshotDelay"))            if let page = UserDefaults.standard.string(forKey: "CottonSnapshotPage") {                model.page = Source(rawValue: page)            }            DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [model] in                Snapshot.write(PopoverView(model: model), size: NSSize(width: 360, height: 560), to: path)                model.quit()            }        }        #endif    }}