aboutsummaryrefslogtreecommitdiff
path: root/Map/MapApp.swift
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-07-07 21:52:16 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-07-07 21:52:16 +0200
commit129bca332b68bd6106079f58bd0de77baf21ec68 (patch)
treee77fc44ca4a5a9a0cb9b653859d7546154472911 /Map/MapApp.swift
parent9cbc1f2bf737e862a01c6859029c3683d6067def (diff)
Take Notes into account in smart labels
Diffstat (limited to 'Map/MapApp.swift')
-rw-r--r--Map/MapApp.swift67
1 files changed, 66 insertions, 1 deletions
diff --git a/Map/MapApp.swift b/Map/MapApp.swift
index 54eb9be..5061807 100644
--- a/Map/MapApp.swift
+++ b/Map/MapApp.swift
@@ -12,6 +12,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see https://map.tranquil.systems.
+import AppKit
import Sparkle
import SwiftUI
@@ -23,7 +24,7 @@ struct MapApp: App {
var body: some Scene {
DocumentGroup(newDocument: MapDocument(text: nil)) { file in
- MapEditor(document: file.$document, url: file.fileURL)
+ LazyMapEditor(document: file.$document, url: file.fileURL)
.focusedSceneValue(\.document, file.$document)
.focusedSceneValue(\.fileURL, file.fileURL)
}
@@ -38,3 +39,67 @@ struct MapApp: App {
}
}
}
+
+struct LazyMapEditor: View {
+ @Binding var document: MapDocument
+ var url: URL?
+
+ @FocusedBinding(\.document) var focusedDocument: MapDocument?
+ @FocusedValue(\.fileURL) var focusedFileURL: URL?
+ @State private var shouldRender = false
+ @State private var renderTask: Task<Void, Never>?
+
+ var isActiveDocument: Bool {
+ guard let focusedDocument else { return false }
+ // Use URL for identification if available, otherwise fall back to text comparison
+ if let documentURL = url,
+ let focusedURL = focusedFileURL
+ {
+ return documentURL == focusedURL
+ }
+ // For new documents without URLs, use text comparison as fallback
+ return focusedDocument.text == document.text
+ }
+
+ var body: some View {
+ if isActiveDocument && shouldRender {
+ MapEditor(document: $document, url: url)
+ } else {
+ Rectangle()
+ .fill(Color(NSColor.windowBackgroundColor))
+ .overlay(
+ VStack(spacing: 16) {
+ Text("document.inactive")
+ .font(.title2)
+ .foregroundColor(.secondary)
+ if let url = url {
+ Text(url.lastPathComponent)
+ .font(.caption)
+ .foregroundColor(.secondary)
+ }
+ }
+ )
+ .onChange(of: isActiveDocument) { _, newValue in
+ renderTask?.cancel()
+ renderTask = nil
+
+ if newValue {
+ renderTask = Task {
+ try? await Task.sleep(nanoseconds: 50_000_000)
+ if !Task.isCancelled {
+ await MainActor.run {
+ shouldRender = true
+ }
+ }
+ }
+ } else {
+ shouldRender = false
+ }
+ }
+ .onAppear {
+ // Initialize focus state properly - don't render by default
+ shouldRender = false
+ }
+ }
+ }
+}