|
| 1 | +import SwiftUI |
| 2 | +import AppKit |
| 3 | + |
| 4 | +@main |
| 5 | +struct ScratchPadApp: App { |
| 6 | + @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate |
| 7 | + |
| 8 | + var body: some Scene { |
| 9 | + WindowGroup { |
| 10 | + ContentViewWrapper() |
| 11 | + .frame(minWidth: 300, minHeight: 200) |
| 12 | + } |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate { |
| 17 | + var window: NSWindow? |
| 18 | + var contentRef: ContentView? |
| 19 | + |
| 20 | + func applicationDidFinishLaunching(_ notification: Notification) { |
| 21 | + if let mainWindow = NSApplication.shared.windows.first { |
| 22 | + mainWindow.delegate = self |
| 23 | + self.window = mainWindow |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + func windowShouldClose(_ sender: NSWindow) -> Bool { |
| 28 | + if sender.isDocumentEdited { |
| 29 | + let alert = NSAlert() |
| 30 | + alert.messageText = "Do you want to quit the app?" |
| 31 | + alert.informativeText = "Your notes will be lost if you don't save them elsewhere." |
| 32 | + alert.alertStyle = .warning |
| 33 | + alert.addButton(withTitle: "Quit") |
| 34 | + alert.addButton(withTitle: "Cancel") |
| 35 | + |
| 36 | + let response = alert.runModal() |
| 37 | + if response == .alertFirstButtonReturn { |
| 38 | + NSApp.terminate(nil) |
| 39 | + } |
| 40 | + return false |
| 41 | + } else { |
| 42 | + NSApp.terminate(nil) |
| 43 | + return false |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { |
| 48 | + return true |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +struct ContentViewWrapper: NSViewControllerRepresentable { |
| 53 | + func makeNSViewController(context: Context) -> NSHostingController<ContentView> { |
| 54 | + let view = ContentView() |
| 55 | + let controller = NSHostingController(rootView: view) |
| 56 | + DispatchQueue.main.async { |
| 57 | + if let window = controller.view.window { |
| 58 | + window.isDocumentEdited = false |
| 59 | + window.level = .normal // default level |
| 60 | + } |
| 61 | + } |
| 62 | + return controller |
| 63 | + } |
| 64 | + |
| 65 | + func updateNSViewController(_ nsViewController: NSHostingController<ContentView>, context: Context) {} |
| 66 | +} |
| 67 | + |
| 68 | +struct ContentView: View { |
| 69 | + @State private var text: String = "" { |
| 70 | + didSet { |
| 71 | + if let window = NSApplication.shared.windows.first { |
| 72 | + window.isDocumentEdited = !text.isEmpty |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @State private var isPinned: Bool = false |
| 78 | + |
| 79 | + var body: some View { |
| 80 | + VStack(spacing: 0) { |
| 81 | + HStack { |
| 82 | + Button(action: togglePin) { |
| 83 | + Image(systemName: isPinned ? "pin.slash" : "pin") |
| 84 | + .help(isPinned ? "Unpin Window" : "Pin Window") |
| 85 | + } |
| 86 | + .buttonStyle(PlainButtonStyle()) |
| 87 | + .padding(6) |
| 88 | + |
| 89 | + Spacer() |
| 90 | + } |
| 91 | + .background(Color(NSColor.windowBackgroundColor)) |
| 92 | + |
| 93 | + Divider() |
| 94 | + |
| 95 | + TextEditor(text: $text) |
| 96 | + .padding(8) |
| 97 | + .font(.system(size: 16, design: .monospaced)) |
| 98 | + .background(Color.white) |
| 99 | + } |
| 100 | + .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 101 | + .onChange(of: text) { oldValue, newValue in |
| 102 | + if let window = NSApplication.shared.windows.first { |
| 103 | + window.isDocumentEdited = !newValue.isEmpty |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private func togglePin() { |
| 109 | + guard let window = NSApplication.shared.windows.first else { return } |
| 110 | + isPinned.toggle() |
| 111 | + window.level = isPinned ? .floating : .normal |
| 112 | + } |
| 113 | +} |
0 commit comments