aboutsummaryrefslogtreecommitdiff
path: root/QuickLook/PreviewViewController.swift
blob: ecacee09cb688fbe9f3fcb39927d63eb1b5c4321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import Cocoa
import Quartz
import SwiftUI
import WmapParser
import SwiftTreeSitter
import SwiftTreeSitterLayer
import TreeSitterWmap

class PreviewViewController: NSViewController, QLPreviewingController {

  override var nibName: NSNib.Name? {
    return NSNib.Name("PreviewViewController")
  }

  override func loadView() {
    super.loadView()
  }

  func preparePreviewOfFile(at url: URL) async throws {
    // Read the .wmap file content
    let content = try String(contentsOf: url, encoding: .utf8)

    // Get the user's preview style preference from shared UserDefaults
    let sharedDefaults =
      UserDefaults(suiteName: "group.systems.tranquil.Map") ?? UserDefaults.standard
    let previewStyleString =
      sharedDefaults.string(forKey: "quickLookPreviewStyle") ?? QuickLookPreviewStyle.map.rawValue
    let previewStyle = QuickLookPreviewStyle(rawValue: previewStyleString) ?? .map

    await MainActor.run {
      // Clear any existing subviews and force cleanup
      view.subviews.forEach { subview in
        if let hostingView = subview as? NSHostingView<AnyView> {
          hostingView.rootView = AnyView(EmptyView())
        }
        subview.removeFromSuperview()
      }

      // Force memory cleanup
      autoreleasepool {
        let previewContent: AnyView

        switch previewStyle {
        case .plainText:
          previewContent = AnyView(createPlainTextView(content: content))

        case .highlightedText:
          previewContent = AnyView(
            createHighlightedTextView(content: content))

        case .map:
          let parsedMap = WmapParser.parse(content)
          previewContent = AnyView(MapPreviewView(parsedMap: parsedMap))
        }

        let hostingView = NSHostingView(rootView: previewContent)

        hostingView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(hostingView)

        NSLayoutConstraint.activate([
          hostingView.topAnchor.constraint(equalTo: view.topAnchor),
          hostingView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
          hostingView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
          hostingView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])

        view.needsLayout = true
        view.layoutSubtreeIfNeeded()
      }  // autoreleasepool
    }
  }

  private func createPlainTextView(content: String) -> some View {
    ScrollView([.horizontal, .vertical]) {
      Text(content)
        .font(.monospaced(.body)())
        .multilineTextAlignment(.leading)
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
        .padding()
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(Color.white)
  }

  private func createHighlightedTextView(content: String) -> some View {
    QuickLookTextEditor(content: content)
      .frame(maxWidth: .infinity, maxHeight: .infinity)
      .background(Color.white)
  }

  deinit {
    // Cleanup when view controller is deallocated
    view.subviews.forEach { subview in
      if let hostingView = subview as? NSHostingView<AnyView> {
        hostingView.rootView = AnyView(EmptyView())
      }
      subview.removeFromSuperview()
    }
  }
}

// A SwiftUI view specifically for QuickLook preview
struct MapPreviewView: View {
  let parsedMap: WmapParser.Map
  @State private var selectedEvolution: StageType = .general

  var body: some View {
    GeometryReader { geometry in
      let mapSize = Dimensions.Map.size
      let padding = Dimensions.Map.padding
      let totalWidth = mapSize.width + 2 * padding
      let totalHeight = mapSize.height + 2 * padding

      // Calculate scale to fit the available space
      let scaleX = geometry.size.width / totalWidth
      let scaleY = geometry.size.height / totalHeight
      let scale = min(scaleX, scaleY, 1.0)  // Don't scale up, only down

      ScrollView([.horizontal, .vertical]) {
        MapRenderView(
          parsedMap: parsedMap,
          evolution: $selectedEvolution
        )
        .drawingGroup(opaque: false)
        .scaleEffect(scale)
        .frame(
          width: totalWidth * scale,
          height: totalHeight * scale
        )
      }
      .frame(maxWidth: .infinity, maxHeight: .infinity)
      .background(Color.white)
    }
  }
}

// A read-only text editor for QuickLook that shows syntax highlighting
struct QuickLookTextEditor: NSViewRepresentable {
  let content: String

  func makeNSView(context: Context) -> NSScrollView {
    let scrollView = NSTextView.scrollableTextView()
    let textView = scrollView.documentView as! NSTextView

    // Configure the scroll view
    scrollView.hasVerticalScroller = true
    scrollView.hasHorizontalScroller = true
    scrollView.autohidesScrollers = false

    // Configure the text view for read-only display
    textView.isEditable = false
    textView.isSelectable = true
    textView.backgroundColor = NSColor.textBackgroundColor
    textView.font = NSFont.monospacedSystemFont(ofSize: 13, weight: .regular)
    textView.string = content
    textView.isVerticallyResizable = true
    textView.isHorizontallyResizable = true
    textView.textContainer?.widthTracksTextView = false
    textView.textContainer?.containerSize = NSSize(
      width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)

    // Apply syntax highlighting using tree-sitter
    if let textStorage = textView.textStorage {
      applyTreeSitterHighlighting(textStorage: textStorage, content: content)
    }

    return scrollView
  }

  func updateNSView(_ nsView: NSScrollView, context: Context) {
    // No updates needed for read-only view
  }

  private func applyTreeSitterHighlighting(textStorage: NSTextStorage, content: String) {
    // Set up tree-sitter language layer
    guard let wmapConfiguration = try? LanguageConfiguration(tree_sitter_wmap(), name: "wmap")
    else { return }

    let languageConfiguration = LanguageLayer.Configuration(languageProvider: { name in
      switch name {
      case "wmap": return wmapConfiguration
      default: return nil
      }
    })

    guard
      let rootLayer = try? LanguageLayer(
        languageConfig: wmapConfiguration, configuration: languageConfiguration)
    else { return }

    // Update layer with content
    rootLayer.replaceContent(with: content)

    // Set default text color
    let fullRange = NSRange(location: 0, length: textStorage.length)
    textStorage.addAttribute(.foregroundColor, value: NSColor.textColor, range: fullRange)

    // Apply tree-sitter highlighting
    guard let highlights = try? rootLayer.highlights(in: fullRange, provider: content.predicateTextProvider)
    else { return }

    for highlight in highlights {
      let color = NSColor.Theme.Syntax.colorForSyntax(highlight.name)
      textStorage.addAttribute(.foregroundColor, value: color, range: highlight.range)
    }
  }

}