feat: add platform shells, CLI, formatting, plugins, tests, and benchmarks
Phase 4 — Platform shells: - calcpad-macos/: SwiftUI two-column editor with Rust FFI bridge (16 files) - calcpad-windows/: iced GUI with Windows 11 Fluent theme (7 files, 13 tests) - calcpad-web/: React 18 + CodeMirror 6 + WASM Worker + PWA (20 files) - calcpad-cli/: clap-based CLI with expression eval, pipe/stdin, JSON/CSV output, and interactive REPL with rustyline history Phase 5 — Engine modules: - formatting/: answer formatting (decimal/scientific/SI notation, thousands separators, currency), line type classification, clipboard values (93 tests) - plugins/: CalcPadPlugin trait, PluginRegistry, Rhai scripting stub (43 tests) - benches/: criterion benchmarks (single-line, 100/500-line sheets, DAG, incremental) - tests/sheet_scenarios.rs: 20 real-world integration tests - tests/proptest_fuzz.rs: 12 property-based fuzz tests 771 tests passing across workspace, 0 failures.
This commit is contained in:
111
calcpad-macos/Sources/CalcPad/Views/AnswerColumnView.swift
Normal file
111
calcpad-macos/Sources/CalcPad/Views/AnswerColumnView.swift
Normal file
@@ -0,0 +1,111 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Displays calculation results in a vertical column, one result per line,
|
||||
/// aligned to match the corresponding editor lines.
|
||||
/// Uses NSViewRepresentable wrapping NSScrollView + NSTextView for pixel-perfect
|
||||
/// line height alignment with the editor.
|
||||
struct AnswerColumnView: NSViewRepresentable {
|
||||
let results: [LineResult]
|
||||
@Binding var scrollOffset: CGFloat
|
||||
var font: NSFont
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator()
|
||||
}
|
||||
|
||||
func makeNSView(context: Context) -> NSScrollView {
|
||||
let scrollView = NSScrollView()
|
||||
scrollView.hasVerticalScroller = false
|
||||
scrollView.hasHorizontalScroller = false
|
||||
scrollView.borderType = .noBorder
|
||||
scrollView.drawsBackground = false
|
||||
// Disable user scrolling — scroll is driven by the editor
|
||||
scrollView.verticalScrollElasticity = .none
|
||||
scrollView.horizontalScrollElasticity = .none
|
||||
|
||||
let textView = NSTextView()
|
||||
textView.isEditable = false
|
||||
textView.isSelectable = true
|
||||
textView.isRichText = true
|
||||
textView.usesFontPanel = false
|
||||
textView.drawsBackground = false
|
||||
textView.backgroundColor = .clear
|
||||
|
||||
// Match editor text container settings for alignment
|
||||
textView.textContainer?.lineFragmentPadding = 4
|
||||
textView.textContainerInset = NSSize(width: 8, height: 8)
|
||||
|
||||
// Disable line wrapping to match editor behavior
|
||||
textView.isHorizontallyResizable = true
|
||||
textView.textContainer?.widthTracksTextView = false
|
||||
textView.textContainer?.containerSize = NSSize(
|
||||
width: CGFloat.greatestFiniteMagnitude,
|
||||
height: CGFloat.greatestFiniteMagnitude
|
||||
)
|
||||
textView.maxSize = NSSize(
|
||||
width: CGFloat.greatestFiniteMagnitude,
|
||||
height: CGFloat.greatestFiniteMagnitude
|
||||
)
|
||||
|
||||
scrollView.documentView = textView
|
||||
context.coordinator.textView = textView
|
||||
context.coordinator.scrollView = scrollView
|
||||
|
||||
updateContent(textView: textView)
|
||||
return scrollView
|
||||
}
|
||||
|
||||
func updateNSView(_ scrollView: NSScrollView, context: Context) {
|
||||
guard let textView = scrollView.documentView as? NSTextView else { return }
|
||||
|
||||
updateContent(textView: textView)
|
||||
|
||||
// Sync scroll position from editor
|
||||
let currentOffset = scrollView.contentView.bounds.origin.y
|
||||
if abs(currentOffset - scrollOffset) > 0.5 {
|
||||
scrollView.contentView.scroll(to: NSPoint(x: 0, y: scrollOffset))
|
||||
scrollView.reflectScrolledClipView(scrollView.contentView)
|
||||
}
|
||||
}
|
||||
|
||||
private func updateContent(textView: NSTextView) {
|
||||
let attributedString = NSMutableAttributedString()
|
||||
|
||||
let paragraphStyle = NSMutableParagraphStyle()
|
||||
paragraphStyle.alignment = .right
|
||||
|
||||
let resultColor = NSColor.secondaryLabelColor
|
||||
let errorColor = NSColor.systemRed
|
||||
|
||||
for (index, lineResult) in results.enumerated() {
|
||||
let displayText = lineResult.result ?? ""
|
||||
let color = lineResult.isError ? errorColor : resultColor
|
||||
|
||||
let attributes: [NSAttributedString.Key: Any] = [
|
||||
.font: font,
|
||||
.foregroundColor: color,
|
||||
.paragraphStyle: paragraphStyle,
|
||||
]
|
||||
|
||||
let line = NSAttributedString(string: displayText, attributes: attributes)
|
||||
attributedString.append(line)
|
||||
|
||||
// Add newline between lines (but not after the last)
|
||||
if index < results.count - 1 {
|
||||
let newline = NSAttributedString(
|
||||
string: "\n",
|
||||
attributes: [.font: font]
|
||||
)
|
||||
attributedString.append(newline)
|
||||
}
|
||||
}
|
||||
|
||||
textView.textStorage?.setAttributedString(attributedString)
|
||||
}
|
||||
|
||||
final class Coordinator {
|
||||
weak var textView: NSTextView?
|
||||
weak var scrollView: NSScrollView?
|
||||
}
|
||||
}
|
||||
137
calcpad-macos/Sources/CalcPad/Views/EditorTextView.swift
Normal file
137
calcpad-macos/Sources/CalcPad/Views/EditorTextView.swift
Normal file
@@ -0,0 +1,137 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// A SwiftUI wrapper around NSTextView for the editor pane.
|
||||
/// Uses NSViewRepresentable to bridge AppKit's NSTextView into SwiftUI,
|
||||
/// providing line-level control, scroll position access, and performance
|
||||
/// that SwiftUI's TextEditor cannot match.
|
||||
struct EditorTextView: NSViewRepresentable {
|
||||
@Binding var text: String
|
||||
@Binding var scrollOffset: CGFloat
|
||||
var font: NSFont
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(self)
|
||||
}
|
||||
|
||||
func makeNSView(context: Context) -> NSScrollView {
|
||||
let scrollView = NSScrollView()
|
||||
scrollView.hasVerticalScroller = true
|
||||
scrollView.hasHorizontalScroller = false
|
||||
scrollView.autohidesScrollers = true
|
||||
scrollView.borderType = .noBorder
|
||||
scrollView.drawsBackground = false
|
||||
|
||||
let textView = NSTextView()
|
||||
textView.isEditable = true
|
||||
textView.isSelectable = true
|
||||
textView.allowsUndo = true
|
||||
textView.isRichText = false
|
||||
textView.usesFontPanel = false
|
||||
textView.isAutomaticQuoteSubstitutionEnabled = false
|
||||
textView.isAutomaticDashSubstitutionEnabled = false
|
||||
textView.isAutomaticTextReplacementEnabled = false
|
||||
textView.isAutomaticSpellingCorrectionEnabled = false
|
||||
|
||||
// Disable line wrapping — horizontal scroll instead
|
||||
textView.isHorizontallyResizable = true
|
||||
textView.textContainer?.widthTracksTextView = false
|
||||
textView.textContainer?.containerSize = NSSize(
|
||||
width: CGFloat.greatestFiniteMagnitude,
|
||||
height: CGFloat.greatestFiniteMagnitude
|
||||
)
|
||||
textView.maxSize = NSSize(
|
||||
width: CGFloat.greatestFiniteMagnitude,
|
||||
height: CGFloat.greatestFiniteMagnitude
|
||||
)
|
||||
|
||||
// Configure font and text color
|
||||
textView.font = font
|
||||
textView.textColor = .textColor
|
||||
textView.backgroundColor = .clear
|
||||
textView.drawsBackground = false
|
||||
textView.insertionPointColor = .textColor
|
||||
|
||||
// Set the text
|
||||
textView.string = text
|
||||
|
||||
// Configure the text container for consistent line spacing
|
||||
textView.textContainer?.lineFragmentPadding = 4
|
||||
textView.textContainerInset = NSSize(width: 8, height: 8)
|
||||
|
||||
scrollView.documentView = textView
|
||||
context.coordinator.textView = textView
|
||||
context.coordinator.scrollView = scrollView
|
||||
|
||||
// Observe text changes
|
||||
textView.delegate = context.coordinator
|
||||
|
||||
// Observe scroll changes
|
||||
scrollView.contentView.postsBoundsChangedNotifications = true
|
||||
NotificationCenter.default.addObserver(
|
||||
context.coordinator,
|
||||
selector: #selector(Coordinator.scrollViewDidScroll(_:)),
|
||||
name: NSView.boundsDidChangeNotification,
|
||||
object: scrollView.contentView
|
||||
)
|
||||
|
||||
return scrollView
|
||||
}
|
||||
|
||||
func updateNSView(_ scrollView: NSScrollView, context: Context) {
|
||||
guard let textView = scrollView.documentView as? NSTextView else { return }
|
||||
|
||||
// Update font if changed
|
||||
if textView.font != font {
|
||||
textView.font = font
|
||||
// Reapply font to entire text storage
|
||||
let range = NSRange(location: 0, length: textView.textStorage?.length ?? 0)
|
||||
textView.textStorage?.addAttribute(.font, value: font, range: range)
|
||||
}
|
||||
|
||||
// Update text only if it actually changed (avoid feedback loops)
|
||||
if textView.string != text {
|
||||
let selectedRanges = textView.selectedRanges
|
||||
textView.string = text
|
||||
textView.selectedRanges = selectedRanges
|
||||
}
|
||||
|
||||
// Update scroll position if driven externally
|
||||
context.coordinator.isUpdatingScroll = true
|
||||
let currentOffset = scrollView.contentView.bounds.origin.y
|
||||
if abs(currentOffset - scrollOffset) > 0.5 {
|
||||
scrollView.contentView.scroll(to: NSPoint(x: 0, y: scrollOffset))
|
||||
scrollView.reflectScrolledClipView(scrollView.contentView)
|
||||
}
|
||||
context.coordinator.isUpdatingScroll = false
|
||||
}
|
||||
|
||||
final class Coordinator: NSObject, NSTextViewDelegate {
|
||||
var parent: EditorTextView
|
||||
weak var textView: NSTextView?
|
||||
weak var scrollView: NSScrollView?
|
||||
var isUpdatingScroll = false
|
||||
|
||||
init(_ parent: EditorTextView) {
|
||||
self.parent = parent
|
||||
}
|
||||
|
||||
func textDidChange(_ notification: Notification) {
|
||||
guard let textView = notification.object as? NSTextView else { return }
|
||||
parent.text = textView.string
|
||||
}
|
||||
|
||||
@objc func scrollViewDidScroll(_ notification: Notification) {
|
||||
guard !isUpdatingScroll,
|
||||
let scrollView = scrollView else { return }
|
||||
let offset = scrollView.contentView.bounds.origin.y
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
self?.parent.scrollOffset = offset
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
/// The main two-column editor layout: text editor on the left, results on the right.
|
||||
/// Scrolling is synchronized between both columns.
|
||||
struct TwoColumnEditorView: View {
|
||||
@State private var text: String = ""
|
||||
@State private var scrollOffset: CGFloat = 0
|
||||
@State private var results: [LineResult] = []
|
||||
@State private var evaluationTask: Task<Void, Never>?
|
||||
|
||||
/// Uses the Rust FFI engine. Falls back to StubCalculationEngine if the Rust
|
||||
/// library is not linked (e.g., during UI-only development).
|
||||
private let engine: CalculationEngine = RustCalculationEngine()
|
||||
|
||||
/// Debounce interval for re-evaluation after typing (seconds).
|
||||
private let evaluationDebounce: TimeInterval = 0.05
|
||||
|
||||
/// Font that respects the user's accessibility / Dynamic Type settings.
|
||||
private var editorFont: NSFont {
|
||||
// Use the system's preferred monospaced font size, which scales with
|
||||
// Accessibility > Display > Text Size in System Settings (macOS 14+).
|
||||
let baseSize = NSFont.systemFontSize
|
||||
// Scale with accessibility settings via the body text style size
|
||||
let preferredSize = NSFont.preferredFont(forTextStyle: .body, options: [:]).pointSize
|
||||
// Use the larger of system default or accessibility-preferred size
|
||||
let size = max(baseSize, preferredSize)
|
||||
return NSFont.monospacedSystemFont(ofSize: size, weight: .regular)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
HSplitView {
|
||||
// Left pane: Editor
|
||||
EditorTextView(
|
||||
text: $text,
|
||||
scrollOffset: $scrollOffset,
|
||||
font: editorFont
|
||||
)
|
||||
.frame(minWidth: 200)
|
||||
|
||||
// Divider is automatic with HSplitView
|
||||
|
||||
// Right pane: Answer column
|
||||
AnswerColumnView(
|
||||
results: results,
|
||||
scrollOffset: $scrollOffset,
|
||||
font: editorFont
|
||||
)
|
||||
.frame(minWidth: 120, idealWidth: 200)
|
||||
}
|
||||
.onChange(of: text) { _, newValue in
|
||||
scheduleEvaluation(newValue)
|
||||
}
|
||||
.onAppear {
|
||||
evaluateText(text)
|
||||
}
|
||||
}
|
||||
|
||||
/// Debounce evaluation so rapid typing doesn't cause excessive recalculation.
|
||||
private func scheduleEvaluation(_ newText: String) {
|
||||
evaluationTask?.cancel()
|
||||
evaluationTask = Task { @MainActor in
|
||||
try? await Task.sleep(for: .milliseconds(Int(evaluationDebounce * 1000)))
|
||||
guard !Task.isCancelled else { return }
|
||||
evaluateText(newText)
|
||||
}
|
||||
}
|
||||
|
||||
private func evaluateText(_ newText: String) {
|
||||
results = engine.evaluateSheet(newText)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user