Last active
June 12, 2022 06:33
Star
You must be signed in to star a gist
An 82-liner SwiftUI script similar to CodeCows 🐮
This file contains 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 | |
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()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog articles for the Gist:
It is about writing a SwiftUI app using just Swift Package Manager, no Xcode.