Created
May 24, 2025 04:04
-
-
Save hanrw/4fbb048a758b82bdf92288dfdc87dee9 to your computer and use it in GitHub Desktop.
CodeWindowView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct CodeWindowView: View { | |
@State var visibleLines: [(text: String, lineNumber: Int)] = [] | |
@State private var scrollProxy: ScrollViewProxy? | |
var body: some View { | |
ZStack { | |
// Window chrome | |
VStack(spacing: 0) { | |
// Title bar | |
HStack(spacing: 6) { | |
Circle() | |
.fill(Color.red.opacity(0.8)) | |
.frame(width: 12, height: 12) | |
Circle() | |
.fill(Color.yellow.opacity(0.8)) | |
.frame(width: 12, height: 12) | |
Circle() | |
.fill(Color.green.opacity(0.8)) | |
.frame(width: 12, height: 12) | |
Spacer() | |
Text("code.tsx") | |
.font(.system(size: 11)) | |
.foregroundColor(.secondary) | |
Spacer() | |
} | |
.padding(.horizontal, 12) | |
.padding(.vertical, 8) | |
.background(Color(NSColor.windowBackgroundColor)) | |
Divider() | |
// Code area | |
ScrollViewReader { proxy in | |
ScrollView(.vertical, showsIndicators: false) { | |
VStack(alignment: .leading, spacing: 0) { | |
ForEach(Array(visibleLines.enumerated()), id: \.offset) { index, line in | |
HStack(spacing: 16) { | |
// Line number with gutter | |
Text("\(line.lineNumber)") | |
.font(.system(size: 11, weight: .regular, design: .monospaced)) | |
.foregroundColor(Color(NSColor.tertiaryLabelColor)) | |
.frame(width: 30, alignment: .trailing) | |
.padding(.trailing, 8) | |
.background( | |
Rectangle() | |
.fill(Color(NSColor.separatorColor).opacity(0.1)) | |
) | |
// Code line | |
Text(line.text.isEmpty ? " " : line.text) | |
.font(.system(size: 12, weight: .regular, design: .monospaced)) | |
.foregroundColor(Color(NSColor.labelColor)) | |
.frame(maxWidth: .infinity, alignment: .leading) | |
.textSelection(.enabled) | |
} | |
.frame(height: 18) | |
.id(index) | |
} | |
} | |
.padding(.vertical, 8) | |
} | |
.frame(width: 600, height: 80) | |
.background(Color(NSColor.textBackgroundColor)) | |
.onAppear { | |
scrollProxy = proxy | |
} | |
} | |
} | |
.cornerRadius(8) | |
.overlay( | |
RoundedRectangle(cornerRadius: 8) | |
.stroke(Color(NSColor.separatorColor), lineWidth: 0.5) | |
) | |
.shadow(color: Color.black.opacity(0.1), radius: 4, x: 0, y: 2) | |
// Gradient overlay | |
VStack(spacing: 0) { | |
Spacer() | |
.frame(height: 36) // Account for title bar | |
LinearGradient( | |
gradient: Gradient(stops: [ | |
.init(color: Color(NSColor.textBackgroundColor), location: 0), | |
.init(color: Color(NSColor.textBackgroundColor).opacity(0.7), location: 0.4), | |
.init(color: Color(NSColor.textBackgroundColor).opacity(0), location: 1) | |
]), | |
startPoint: .top, | |
endPoint: .bottom | |
) | |
.frame(height: 25) | |
.allowsHitTesting(false) | |
Spacer() | |
// Bottom gradient | |
LinearGradient( | |
gradient: Gradient(stops: [ | |
.init(color: Color(NSColor.textBackgroundColor).opacity(0), location: 0), | |
.init(color: Color(NSColor.textBackgroundColor).opacity(0.7), location: 0.6), | |
.init(color: Color(NSColor.textBackgroundColor), location: 1) | |
]), | |
startPoint: .top, | |
endPoint: .bottom | |
) | |
.frame(height: 25) | |
.allowsHitTesting(false) | |
} | |
} | |
.frame(width: 600, height: 124) | |
} | |
} | |
#Preview { | |
let codeLines = """ | |
import React from 'react'; | |
import { View, Text } from 'react-native'; | |
import { styles } from './styles'; | |
const MyComponent: React.FC = () => { | |
return ( | |
<View style={styles.container}> | |
<Text style={styles.text}>Hello, World!</Text> | |
</View> | |
); | |
}; | |
""".trimmingCharacters(in: .whitespaces) | |
CodeWindowView(visibleLines: codeLines | |
.split(separator: "\n") | |
.enumerated() | |
.map { (index, line) in | |
(text: String(line), lineNumber: index + 1) | |
} | |
) | |
} |
Author
hanrw
commented
May 24, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment