Skip to content

Instantly share code, notes, and snippets.

@helje5
Last active Aug 30, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@helje5 helje5 commented Aug 25, 2021

Blog article for the Gist: 🐮 SwiftUI Tools w/ just SwiftPM

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