|
| 1 | +import AppKit |
| 2 | +import CodeEditTextView |
| 3 | + |
| 4 | +class ReformattingGuideView: NSView { |
| 5 | + private let column: Int = 80 |
| 6 | + |
| 7 | + override init(frame frameRect: NSRect) { |
| 8 | + super.init(frame: frameRect) |
| 9 | + wantsLayer = true |
| 10 | + print("ReformattingGuideView initialized with frame: \(frameRect)") |
| 11 | + } |
| 12 | + |
| 13 | + required init?(coder: NSCoder) { |
| 14 | + fatalError("init(coder:) has not been implemented") |
| 15 | + } |
| 16 | + |
| 17 | + override func draw(_ dirtyRect: NSRect) { |
| 18 | + super.draw(dirtyRect) |
| 19 | + print("Drawing guide view with frame: \(frame)") |
| 20 | + |
| 21 | + // For debugging, make the guide more visible |
| 22 | + NSColor.red.withAlphaComponent(0.3).setFill() |
| 23 | + frame.fill() |
| 24 | + |
| 25 | + // Draw a border around the guide for better visibility |
| 26 | + NSColor.blue.setStroke() |
| 27 | + let borderPath = NSBezierPath(rect: frame) |
| 28 | + borderPath.lineWidth = 2.0 |
| 29 | + borderPath.stroke() |
| 30 | + |
| 31 | + // Get the current theme's background color to determine if we're in light or dark mode |
| 32 | + let isLightMode = NSApp.effectiveAppearance.name == .aqua |
| 33 | + |
| 34 | + // Set the line color based on the theme |
| 35 | + let lineColor = isLightMode ? |
| 36 | + NSColor.black.withAlphaComponent(0.1) : |
| 37 | + NSColor.white.withAlphaComponent(0.1) |
| 38 | + |
| 39 | + // Set the shaded area color (slightly more transparent) |
| 40 | + let shadedColor = isLightMode ? |
| 41 | + NSColor.black.withAlphaComponent(0.05) : |
| 42 | + NSColor.white.withAlphaComponent(0.05) |
| 43 | + |
| 44 | + // Draw the vertical line (accounting for inverted Y coordinate system) |
| 45 | + lineColor.setStroke() |
| 46 | + let linePath = NSBezierPath() |
| 47 | + linePath.move(to: NSPoint(x: frame.minX, y: frame.maxY)) // Start at top |
| 48 | + linePath.line(to: NSPoint(x: frame.minX, y: frame.minY)) // Draw down to bottom |
| 49 | + linePath.lineWidth = 1.0 |
| 50 | + linePath.stroke() |
| 51 | + |
| 52 | + // Draw the shaded area to the right of the line |
| 53 | + shadedColor.setFill() |
| 54 | + let shadedRect = NSRect( |
| 55 | + x: frame.minX, |
| 56 | + y: frame.minY, |
| 57 | + width: frame.width, |
| 58 | + height: frame.height |
| 59 | + ) |
| 60 | + shadedRect.fill() |
| 61 | + } |
| 62 | + |
| 63 | + func updatePosition(in textView: TextView) { |
| 64 | + // Wait for the text view to have a valid width |
| 65 | + guard textView.frame.width > 0 else { |
| 66 | + print("Text view width is 0, skipping position update") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + // Calculate the x position based on the font's character width and column number |
| 71 | + let charWidth = textView.font.boundingRectForFont.width |
| 72 | + let xPosition = CGFloat(column) * charWidth |
| 73 | + |
| 74 | + print("Updating guide position:") |
| 75 | + print("- Character width: \(charWidth)") |
| 76 | + print("- Target column: \(column)") |
| 77 | + print("- Calculated x position: \(xPosition)") |
| 78 | + print("- Text view width: \(textView.frame.width)") |
| 79 | + |
| 80 | + // Ensure we don't create an invalid frame |
| 81 | + let maxWidth = max(0, textView.frame.width - xPosition/2) |
| 82 | + |
| 83 | + // Update the frame to be a vertical line at column 80 with a shaded area to the right |
| 84 | + let newFrame = NSRect( |
| 85 | + x: 200, |
| 86 | + y: 0, |
| 87 | + width: maxWidth, |
| 88 | + height: textView.frame.height |
| 89 | + ) |
| 90 | + print("Setting new frame: \(newFrame)") |
| 91 | + |
| 92 | + frame = newFrame |
| 93 | + needsDisplay = true |
| 94 | + } |
| 95 | +} |
0 commit comments