adds first version of the app
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
import AppKit
|
||||
import PlakServices
|
||||
import PlakUI
|
||||
|
||||
@MainActor
|
||||
final class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
private var store: PageStore?
|
||||
private var windowController: PageWindowController?
|
||||
private var shortcut: GlobalShortcut?
|
||||
private var statusMenu: StatusMenu?
|
||||
|
||||
func applicationDidFinishLaunching(_ notification: Notification) {
|
||||
FontRegistry.registerBundledFonts()
|
||||
NSApp.mainMenu = MainMenu.make()
|
||||
let store: PageStore
|
||||
do {
|
||||
store = try PageStore()
|
||||
} catch {
|
||||
// No store means nothing can flush over the unreadable page file on quit.
|
||||
showUnreadable(error)
|
||||
NSApp.terminate(nil)
|
||||
return
|
||||
}
|
||||
self.store = store
|
||||
let controller = PageWindowController(store: store)
|
||||
windowController = controller
|
||||
controller.onHide = { [weak self] in self?.hidePlak() }
|
||||
open(atEnd: true)
|
||||
shortcut = GlobalShortcut { [weak self] in self?.togglePlak() }
|
||||
statusMenu = StatusMenu(
|
||||
show: { [weak self] in self?.showPlak() },
|
||||
hide: { [weak self] in self?.hidePlak() }
|
||||
)
|
||||
}
|
||||
|
||||
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
||||
open(atEnd: true)
|
||||
return true
|
||||
}
|
||||
|
||||
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
|
||||
false
|
||||
}
|
||||
|
||||
func applicationWillResignActive(_ notification: Notification) {
|
||||
store?.flush()
|
||||
}
|
||||
|
||||
func applicationWillTerminate(_ notification: Notification) {
|
||||
store?.flush()
|
||||
}
|
||||
|
||||
private func togglePlak() {
|
||||
if NSApp.isActive, let window = windowController?.window, window.isVisible, window.isKeyWindow {
|
||||
hidePlak()
|
||||
} else {
|
||||
showPlak()
|
||||
}
|
||||
}
|
||||
|
||||
private func showPlak() {
|
||||
open(atEnd: false)
|
||||
}
|
||||
|
||||
/// Only hiding the app gives focus back to the previous app; an ordered-out window leaves Plak active.
|
||||
private func hidePlak() {
|
||||
NSApp.hide(nil)
|
||||
}
|
||||
|
||||
private func open(atEnd: Bool) {
|
||||
if NSApp.isHidden { NSApp.unhideWithoutActivation() }
|
||||
// NSApp.activate() is cooperative since macOS 14 and loses to the app in front on ⌘P.
|
||||
(NSApp as ForcedActivation).activate(ignoringOtherApps: true)
|
||||
if atEnd {
|
||||
windowController?.showAtEnd()
|
||||
} else {
|
||||
windowController?.reveal()
|
||||
}
|
||||
}
|
||||
|
||||
private func showUnreadable(_ error: Error) {
|
||||
let alert = NSAlert()
|
||||
alert.alertStyle = .critical
|
||||
alert.messageText = "Plak cannot read the page file"
|
||||
var path = PageStore.defaultURL.path
|
||||
if case let .unreadable(unreadable)? = error as? PageStoreError { path = unreadable }
|
||||
alert.informativeText = "\(path) is not readable as text. Plak quits and leaves the file as it is."
|
||||
alert.addButton(withTitle: "Quit")
|
||||
NSApp.activate()
|
||||
alert.runModal()
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls the deprecated activate(ignoringOtherApps:) without the deprecation warning.
|
||||
@MainActor
|
||||
private protocol ForcedActivation {
|
||||
func activate(ignoringOtherApps flag: Bool)
|
||||
}
|
||||
|
||||
extension NSApplication: ForcedActivation {}
|
||||
@@ -0,0 +1,28 @@
|
||||
import AppKit
|
||||
import Carbon.HIToolbox
|
||||
|
||||
/// The system-wide ⌘P that shows or hides Plak from any app; no Accessibility permission needed.
|
||||
final class GlobalShortcut {
|
||||
private let onPress: @MainActor () -> Void
|
||||
private var hotKey: EventHotKeyRef?
|
||||
private var handler: EventHandlerRef?
|
||||
|
||||
init(onPress: @escaping @MainActor () -> Void) {
|
||||
self.onPress = onPress
|
||||
var pressed = EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: UInt32(kEventHotKeyPressed))
|
||||
InstallEventHandler(GetApplicationEventTarget(), { _, _, userData in
|
||||
guard let userData else { return OSStatus(eventNotHandledErr) }
|
||||
let shortcut = Unmanaged<GlobalShortcut>.fromOpaque(userData).takeUnretainedValue()
|
||||
// Carbon calls the handler on the main thread, from the app's run loop.
|
||||
MainActor.assumeIsolated { shortcut.onPress() }
|
||||
return noErr
|
||||
}, 1, &pressed, Unmanaged.passUnretained(self).toOpaque(), &handler)
|
||||
let id = EventHotKeyID(signature: OSType(0x506C_616B), id: 1)
|
||||
RegisterEventHotKey(UInt32(kVK_ANSI_P), UInt32(cmdKey), id, GetApplicationEventTarget(), 0, &hotKey)
|
||||
}
|
||||
|
||||
deinit {
|
||||
if let hotKey { UnregisterEventHotKey(hotKey) }
|
||||
if let handler { RemoveEventHandler(handler) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import AppKit
|
||||
import PlakUI
|
||||
|
||||
/// The app, Edit, View and Window menus; their actions go up the responder chain.
|
||||
enum MainMenu {
|
||||
static func make() -> NSMenu {
|
||||
let main = NSMenu()
|
||||
main.addItem(submenu(appMenu()))
|
||||
main.addItem(submenu(fileMenu()))
|
||||
main.addItem(submenu(editMenu()))
|
||||
main.addItem(submenu(viewMenu()))
|
||||
let window = windowMenu()
|
||||
main.addItem(submenu(window))
|
||||
NSApp.windowsMenu = window
|
||||
return main
|
||||
}
|
||||
|
||||
private static func submenu(_ menu: NSMenu) -> NSMenuItem {
|
||||
let item = NSMenuItem(title: menu.title, action: nil, keyEquivalent: "")
|
||||
item.submenu = menu
|
||||
return item
|
||||
}
|
||||
|
||||
private static func appMenu() -> NSMenu {
|
||||
let menu = NSMenu(title: "Plak")
|
||||
menu.addItem(withTitle: "About Plak", action: #selector(NSApplication.orderFrontStandardAboutPanel(_:)), keyEquivalent: "")
|
||||
menu.addItem(.separator())
|
||||
menu.addItem(withTitle: "Hide Plak", action: #selector(NSApplication.hide(_:)), keyEquivalent: "h")
|
||||
let others = menu.addItem(withTitle: "Hide Others", action: #selector(NSApplication.hideOtherApplications(_:)), keyEquivalent: "h")
|
||||
others.keyEquivalentModifierMask = [.command, .option]
|
||||
menu.addItem(withTitle: "Show All", action: #selector(NSApplication.unhideAllApplications(_:)), keyEquivalent: "")
|
||||
menu.addItem(.separator())
|
||||
menu.addItem(withTitle: "Quit Plak", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
|
||||
return menu
|
||||
}
|
||||
|
||||
private static func fileMenu() -> NSMenu {
|
||||
let menu = NSMenu(title: "File")
|
||||
menu.addItem(withTitle: "Close Window", action: #selector(NSWindow.performClose(_:)), keyEquivalent: "w")
|
||||
return menu
|
||||
}
|
||||
|
||||
private static func editMenu() -> NSMenu {
|
||||
let menu = NSMenu(title: "Edit")
|
||||
menu.addItem(withTitle: "Undo", action: Selector(("undo:")), keyEquivalent: "z")
|
||||
let redo = menu.addItem(withTitle: "Redo", action: Selector(("redo:")), keyEquivalent: "z")
|
||||
redo.keyEquivalentModifierMask = [.command, .shift]
|
||||
menu.addItem(.separator())
|
||||
menu.addItem(withTitle: "Cut", action: #selector(NSText.cut(_:)), keyEquivalent: "x")
|
||||
menu.addItem(withTitle: "Copy", action: #selector(NSText.copy(_:)), keyEquivalent: "c")
|
||||
menu.addItem(withTitle: "Paste", action: #selector(NSText.paste(_:)), keyEquivalent: "v")
|
||||
return menu
|
||||
}
|
||||
|
||||
private static func viewMenu() -> NSMenu {
|
||||
let menu = NSMenu(title: "View")
|
||||
menu.addItem(withTitle: "Bigger", action: #selector(PageViewController.makeTextBigger(_:)), keyEquivalent: "+")
|
||||
// ⌘= is the same key as ⌘+ without Shift.
|
||||
let equals = menu.addItem(withTitle: "Bigger", action: #selector(PageViewController.makeTextBigger(_:)), keyEquivalent: "=")
|
||||
equals.isHidden = true
|
||||
equals.allowsKeyEquivalentWhenHidden = true
|
||||
menu.addItem(withTitle: "Smaller", action: #selector(PageViewController.makeTextSmaller(_:)), keyEquivalent: "-")
|
||||
menu.addItem(withTitle: "Default Size", action: #selector(PageViewController.makeTextStandardSize(_:)), keyEquivalent: "0")
|
||||
return menu
|
||||
}
|
||||
|
||||
private static func windowMenu() -> NSMenu {
|
||||
let menu = NSMenu(title: "Window")
|
||||
menu.addItem(withTitle: "Minimize", action: #selector(NSWindow.performMiniaturize(_:)), keyEquivalent: "m")
|
||||
menu.addItem(withTitle: "Zoom", action: #selector(NSWindow.performZoom(_:)), keyEquivalent: "")
|
||||
return menu
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import AppKit
|
||||
|
||||
/// The menu bar icon with Show, Hide and Quit.
|
||||
@MainActor
|
||||
final class StatusMenu: NSObject {
|
||||
private let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
|
||||
private let show: () -> Void
|
||||
private let hide: () -> Void
|
||||
|
||||
init(show: @escaping () -> Void, hide: @escaping () -> Void) {
|
||||
self.show = show
|
||||
self.hide = hide
|
||||
super.init()
|
||||
let image = NSImage(systemSymbolName: "note.text", accessibilityDescription: "Plak")
|
||||
image?.isTemplate = true
|
||||
item.button?.image = image
|
||||
let menu = NSMenu()
|
||||
menu.addItem(withTitle: "Show Plak", action: #selector(showPlak), keyEquivalent: "p").target = self
|
||||
menu.addItem(withTitle: "Hide Plak", action: #selector(hidePlak), keyEquivalent: "").target = self
|
||||
menu.addItem(.separator())
|
||||
menu.addItem(withTitle: "Quit Plak", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
|
||||
item.menu = menu
|
||||
}
|
||||
|
||||
@objc private func showPlak() {
|
||||
show()
|
||||
}
|
||||
|
||||
@objc private func hidePlak() {
|
||||
hide()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import AppKit
|
||||
|
||||
MainActor.assumeIsolated {
|
||||
let app = NSApplication.shared
|
||||
let delegate = AppDelegate()
|
||||
app.delegate = delegate
|
||||
app.setActivationPolicy(.accessory)
|
||||
app.run()
|
||||
}
|
||||
Reference in New Issue
Block a user