macbackend/Sources/Core/Model/MacApp.swift
david-swift 3d2ef52cc8
Some checks failed
Deploy Docs / publish (push) Successful in 3m4s
SwiftLint / SwiftLint (push) Failing after 9s
Extract core target
2024-12-04 21:17:21 +01:00

103 lines
2.4 KiB
Swift

//
// MacApp.swift
// MacBackend
//
// Created by david-swift on 31.07.2024.
//
import AppKit
@_exported import Meta
@_exported import MetaSQLite
/// The Meta app storage for the macOS backend.
public class MacApp: AppStorage {
/// The scene element type of the macOS backend.
public typealias SceneElementType = MacSceneElement
/// The app storage.
public var storage: StandardAppStorage = .init()
/// The application.
let app = NSApplication.shared
/// The menu bar.
let mainMenu = NSMenu()
/// The "<App>" menu.
let appItem = NSMenuItem(title: "", action: nil, keyEquivalent: "")
/// The "Window" menu.
let windowsItem = NSMenuItem(title: "Window", action: nil, keyEquivalent: "")
/// The "Help" menu.
let helpItem = NSMenuItem(title: "Help", action: nil, keyEquivalent: "")
/// Initialize the app storage.
/// - Parameter id: The identifier.
public init(id: String) {
app.mainMenu = mainMenu
DatabaseInformation.setPath(
URL.applicationSupportDirectory
.appendingPathComponent(id)
.appendingPathComponent("database.sqlite")
.path
)
}
/// The app menu.
func appMenu() {
let appMenu = NSMenu()
mainMenu.addItem(appItem)
appItem.submenu = appMenu
}
/// The windows menu.
func windowsMenu() {
let windowsMenu = NSMenu()
mainMenu.addItem(windowsItem)
windowsItem.submenu = windowsMenu
app.windowsMenu = windowsMenu
}
/// The help menu.
func helpMenu() {
let helpMenu = NSMenu()
mainMenu.addItem(helpItem)
helpItem.submenu = helpMenu
app.helpMenu = helpMenu
}
/// Execute the app.
/// - Parameter setup: Set the scene elements up.
public func run(setup: @escaping () -> Void) {
appMenu()
setup()
windowsMenu()
helpMenu()
app.run()
}
/// Present the about window.
public func showAboutWindow() {
app.orderFrontStandardAboutPanel()
}
/// Quit the app.
@objc
public func quit() {
app.terminate(nil)
}
/// Hide the app.
public func hide() {
app.hide(nil)
}
/// Hide other apps.
public func hideOthers() {
app.hideOtherApplications(nil)
}
/// Show all the apps.
public func showAll() {
app.unhideAllApplications(nil)
}
}