restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
import AppKit
|
||||
import HelderCore
|
||||
import SwiftUI
|
||||
|
||||
/// The pane under the bar: code, Markdown preview, image, or a placeholder.
|
||||
struct EditorPane: View {
|
||||
let editor: EditorStore
|
||||
let presentation: EditorPresentation
|
||||
/// View → Zoom.
|
||||
var fontScale: CGFloat = 1
|
||||
|
||||
var body: some View {
|
||||
let path = presentation.path
|
||||
let showsCode = presentation.kind == .text && presentation.placeholder == nil && presentation.paneMode != .preview
|
||||
ZStack {
|
||||
// Kept alive while hidden, so a trip to Preview keeps each file's undo history and colours.
|
||||
if presentation.kind == .text {
|
||||
CodeEditorView(configuration: configuration(path, isShown: showsCode), actions: actions)
|
||||
.opacity(showsCode ? 1 : 0)
|
||||
.allowsHitTesting(showsCode)
|
||||
}
|
||||
if presentation.kind == .image {
|
||||
ImagePreview(data: editor.image(for: path), failed: editor.isUnreadableImage(path), name: PathUtils.basename(path))
|
||||
.overlay(RightClickCatcher { fileMenu(path, at: $0) })
|
||||
} else if let placeholder = presentation.placeholder {
|
||||
EditorPlaceholderView(placeholder: placeholder)
|
||||
.background(Palette.editor)
|
||||
} else if presentation.paneMode == .preview {
|
||||
MarkdownPreviewView(editor: editor, path: path, text: editor.text(for: path))
|
||||
.overlay(RightClickCatcher { fileMenu(path, at: $0) })
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
.background(Palette.editor)
|
||||
}
|
||||
|
||||
/// Views without lines refer to the file's first line.
|
||||
private func fileMenu(_ path: String, at anchor: CGPoint) {
|
||||
let request = editor.contextMenuRequest(reference: AgentReference(path: path, startLine: 1), code: nil, anchor: anchor)
|
||||
editor.presentOverlay?(.contextMenu(request))
|
||||
}
|
||||
|
||||
private func configuration(_ path: String, isShown: Bool) -> CodeEditorConfiguration {
|
||||
let diff = editor.diff(for: path)
|
||||
let wrap: Bool = switch editor.editorConfig.wordWrap {
|
||||
case .on: true
|
||||
case .off: false
|
||||
case .markdown: presentation.isMarkdown
|
||||
}
|
||||
let marked = presentation.paneMode == .actual ? diff : nil
|
||||
return CodeEditorConfiguration(
|
||||
documentID: path, path: path, text: editor.text(for: path), revision: editor.revision(for: path),
|
||||
language: FileType.language(for: path), isEditable: presentation.isEditable, wrap: wrap,
|
||||
tabSize: editor.editorConfig.tabSize,
|
||||
marksKey: marked.map { "actual:" + Self.stamp($0) } ?? "none",
|
||||
makeMarks: { marked.map { ChangeMarks.actual(DiffViews.actual($0)) } ?? [:] },
|
||||
opensDiffOnClick: presentation.opensDiffOnClick, knownSymbols: editor.knownSymbols,
|
||||
symbolsVersion: editor.knownSymbolsVersion, flash: editor.flash,
|
||||
focusRequest: editor.textFocusRequest, selectAllRequest: editor.selectAllRequest, fontScale: fontScale,
|
||||
isShown: isShown)
|
||||
}
|
||||
|
||||
/// Cheap identity of a diff: counts and byte lengths, never a full comparison.
|
||||
private static func stamp(_ diff: Diff) -> String {
|
||||
"\(diff.added).\(diff.removed).\(diff.rows.count).\(diff.original.utf8.count).\(diff.updated.utf8.count)"
|
||||
}
|
||||
|
||||
private var actions: CodeEditorActions {
|
||||
let editor = editor
|
||||
return CodeEditorActions(
|
||||
edited: { editor.edit($0) },
|
||||
cursorMoved: { editor.updateCursor(line: $0, column: $1) },
|
||||
selectionChanged: { editor.updateSelection(lines: $0, code: $1) },
|
||||
markedLineClicked: { editor.openDiff(atLine: $0) },
|
||||
contextMenu: { reference, code, anchor in
|
||||
editor.presentOverlay?(.contextMenu(editor.contextMenuRequest(reference: reference, code: code, anchor: anchor)))
|
||||
},
|
||||
symbolClicked: { editor.lookupSymbol($0) },
|
||||
passOnSelection: { anchor in
|
||||
guard let request = editor.passOnSelectionRequest(anchor: anchor) else { return false }
|
||||
editor.presentOverlay?(.passOn(request))
|
||||
return true
|
||||
},
|
||||
command: { command in
|
||||
switch command {
|
||||
case .save: Task { try? await editor.save() }
|
||||
case .close: Task { await editor.requestCloseActive() }
|
||||
case .cycleMode: editor.togglePreview()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A short line in place of the code, with a note under it.
|
||||
struct EditorPlaceholderView: View {
|
||||
let placeholder: EditorPlaceholder
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 10) {
|
||||
Text(placeholder.title)
|
||||
.typeRole(Typography.body)
|
||||
.foregroundStyle(color)
|
||||
if let note = placeholder.note {
|
||||
Text(note).typeRole(Typography.path).foregroundStyle(Palette.text4)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
|
||||
private var color: Color {
|
||||
switch placeholder {
|
||||
case .noUpdated: Palette.removedText
|
||||
case .cannotDisplay: Palette.text4
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An image file, centred on the editor surface; read-only.
|
||||
struct ImagePreview: View {
|
||||
let data: Data?
|
||||
let failed: Bool
|
||||
let name: String
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
if let data, let image = NSImage(data: data) {
|
||||
Image(nsImage: image)
|
||||
.resizable()
|
||||
.interpolation(.high)
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.background(Checkerboard())
|
||||
.frame(maxWidth: image.size.width, maxHeight: image.size.height)
|
||||
.clipShape(RoundedRectangle(cornerRadius: Metrics.radiusSmall))
|
||||
.overlay(RoundedRectangle(cornerRadius: Metrics.radiusSmall).stroke(Palette.raised, lineWidth: Metrics.hairline))
|
||||
.accessibilityLabel(name)
|
||||
.padding(24)
|
||||
} else if failed || data != nil {
|
||||
Text("Can’t preview this image").typeRole(Typography.body).foregroundStyle(Palette.text4)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
}
|
||||
|
||||
/// 10pt squares behind an image, so a transparent PNG or SVG reads on the dark ground.
|
||||
private struct Checkerboard: View {
|
||||
var body: some View {
|
||||
Canvas { context, size in
|
||||
context.fill(Path(CGRect(origin: .zero, size: size)), with: .color(Palette.panel))
|
||||
let square: CGFloat = 10
|
||||
for row in 0..<Int((size.height / square).rounded(.up)) {
|
||||
for column in 0..<Int((size.width / square).rounded(.up)) where (row + column).isMultiple(of: 2) {
|
||||
context.fill(Path(CGRect(x: CGFloat(column) * square, y: CGFloat(row) * square, width: square, height: square)),
|
||||
with: .color(Palette.raised))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes only right-clicks, so the view under it keeps scrolling, selecting and following links.
|
||||
struct RightClickCatcher: NSViewRepresentable {
|
||||
/// Receives the click in top-left window coordinates.
|
||||
let action: (CGPoint) -> Void
|
||||
|
||||
func makeNSView(context: Context) -> CatcherView { CatcherView() }
|
||||
|
||||
func updateNSView(_ view: CatcherView, context: Context) {
|
||||
view.action = action
|
||||
}
|
||||
|
||||
final class CatcherView: NSView {
|
||||
var action: (CGPoint) -> Void = { _ in }
|
||||
|
||||
override func hitTest(_ point: NSPoint) -> NSView? {
|
||||
NSApp.currentEvent?.type == .rightMouseDown ? super.hitTest(point) : nil
|
||||
}
|
||||
|
||||
override func rightMouseDown(with event: NSEvent) {
|
||||
let height = window?.contentView?.bounds.height ?? 0
|
||||
action(CGPoint(x: event.locationInWindow.x, y: height - event.locationInWindow.y))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user