Skip to content

Instantly share code, notes, and snippets.

@helje5
Last active June 12, 2022 06:33
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save helje5/df0e7306229d5b6439972d9a43b70321 to your computer and use it in GitHub Desktop.
An 82-liner SwiftUI script similar to CodeCows 🐮
import SwiftUI
import cows // @AlwaysRightInstitute
struct ContentView: View {
@State var searchString = ""
@State var matches = allCows
@State var selectedCow : String?
let font = Font(NSFont
.monospacedSystemFont(ofSize: NSFont.systemFontSize, weight: .regular))
var body: some View {
NavigationView {
ScrollView {
TextField("Search", text: $searchString)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(8)
.onChange(of: searchString) { nv in
matches = nv.isEmpty
? cows.allCows
: cows.allCows.filter { $0.contains(searchString) }
}
Spacer()
}
ScrollView {
VStack(spacing: 0) {
if matches.isEmpty {
Text("Didn't find cows matching '\(searchString)' 🐮")
.padding()
.font(.title)
Divider()
}
ForEach(matches.isEmpty ? allCows : matches, id: \.self) { cow in
Text(verbatim: cow)
.font(font)
.onDrag { NSItemProvider(object: cow as NSString ) }
.padding()
.background(
RoundedRectangle(cornerRadius: 16)
.strokeBorder()
.foregroundColor(.accentColor)
.padding(4)
.opacity(selectedCow == cow ? 1 : 0)
)
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
.onTapGesture {
selectedCow = selectedCow == cow ? nil : cow
}
}
}
}
.ignoresSafeArea()
}
}
}
@main
struct TestAppApp: App {
init() {
DispatchQueue.main.async {
NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: true)
NSApp.windows.first?.makeKeyAndOrderFront(nil)
}
}
var body: some Scene {
WindowGroup {
ContentView()
.frame(minWidth: 640, minHeight: 320)
}
.windowStyle(HiddenTitleBarWindowStyle())
}
}
@helje5
Copy link
Author

helje5 commented Aug 25, 2021

Blog articles for the Gist:

It is about writing a SwiftUI app using just Swift Package Manager, no Xcode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment