Skip to content

Instantly share code, notes, and snippets.

View OskarGroth's full-sized avatar

Oskar Groth OskarGroth

View GitHub Profile
extension URLRequest {
public enum HTTPMethod: String {
case get = "GET"
case put = "PUT"
case post = "POST"
case delete = "DELETE"
case head = "HEAD"
case options = "OPTIONS"
case trace = "TRACE"
public class MouseTrackingView: NSView {
public var callback: ((Bool) -> Void)?
private var trackingArea: NSTrackingArea?
public override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
import SwiftUI
struct User: Identifiable {
var id: Int
var firstName: String
var lastName: String
}
struct UserRow: View {
var user: User
public class SheetAnimator: NSObject, NSViewControllerPresentationAnimator {
private var animationUUID: UUID?
public func animatePresentation(of viewController: NSViewController, from fromViewController: NSViewController) {
let containerView = fromViewController.view
let sheetView = viewController.view
let uuid = UUID()
animationUUID = uuid
@OskarGroth
OskarGroth / ContentView.swift
Created November 1, 2020 21:19
SwiftUI Vibrancy
import SwiftUI
@main
struct DemoVibrancyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class ObjA {
var isOn = false
}
struct ContentView: View {
// Because this is not an observed object, the UI in this view should theoretically never update
let obj = ObjA()
var body: some View {
import Cocoa
import SwiftUI
public extension View {
func bindWindow(_ window: Binding<NSWindow?>) -> some View {
return background(WindowAccessor(window))
}
}
public struct WindowAccessor: NSViewRepresentable {
import SwiftUI
class Coordinator: ObservableObject {
let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
init() {
let view = NSHostingView(rootView: Text("Hello from SwiftUI").fixedSize())
item.button?.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
struct ContentView: View {
var body: some View {
Image(nsImage: NSImage(named: .init(NSImage.applicationIconName))!)
.resizable()
.frame(width: 100, height: 100)
.tapWithHighlight(onTap: {
print("Tap")
}, onLongPress: {
print("Long Press")
})
import SwiftUI
@main
struct SwiftApp: App {
@State var someState = false
var body: some Scene {
WindowGroup {
Rectangle()