Skip to content

Instantly share code, notes, and snippets.

View mntone's full-sized avatar
🏠

monotone mntone

🏠
View GitHub Profile
import SwiftUI
struct ContentView: View {
@State var path: [String] = []
func navigationButton(value: String) -> some View {
NavigationButton {
path.append(value)
} label: {
Text("NavigationButton")
@yakushevichsv
yakushevichsv / UnfairLock.swift
Created September 5, 2021 08:53
os_unfair_lock_t instead of NS{Recursive}Lock
// MARK: - UnfairLock
final class UnfairLock {
private let unfairLock: os_unfair_lock_t //UnsafeMutablePointer<os_unfair_lock>
private let unfairValue: os_unfair_lock_s
init() {
unfairLock = .allocate(capacity: 1)
unfairValue = .init()
unfairLock.initialize(to: unfairValue)
}
class FloatingPanel: NSPanel {
init(contentRect: NSRect, backing: NSWindow.BackingStoreType, defer flag: Bool) {
// Not sure if .titled does affect anything here. Kept it because I think it might help with accessibility but I did not test that.
super.init(contentRect: contentRect, styleMask: [.nonactivatingPanel, .resizable, .closable, .fullSizeContentView], backing: backing, defer: flag)
// Set this if you want the panel to remember its size/position
// self.setFrameAutosaveName("a unique name")
// Allow the pannel to be on top of almost all other windows
@YusukeHosonuma
YusukeHosonuma / Example.swift
Created August 15, 2020 02:07
NWPathMonitor を Combine の Publisher として扱えるようにする拡張
import SwiftUI
import Network
final class ViewModel: ObservableObject {
@Published var status: NWPath.Status = .satisfied
init() {
NWPathMonitor()
.publisher()
.map { $0.status }
@Dev1an
Dev1an / CGPoint+SIMD.swift
Created August 8, 2020 10:32
SIMD extensions for CGPoint and CGSize
import struct CoreGraphics.CGSize
import struct CoreGraphics.CGPoint
import struct CoreGraphics.CGFloat
extension CGSize: SIMD {
public typealias MaskStorage = SIMD2<CGFloat.NativeType.SIMDMaskScalar>
public subscript(index: Int) -> CGFloat {
get {
index == 0 ? width : height
@creaaa
creaaa / Controlling Publishing with Connectable Publishers
Last active October 20, 2021 02:21
share、makeConnectable、connect
share() を消すとどうなるか?
=> 購読が1本ではなく、2本発生するので、1も2も両方 valueとfinished を無事受け取れる。
Received data 1: 1256 bytes.
Received completion 1: finished.
Received data 2: 1256 bytes.
Received completion 2: finished.
これだとPublisherの生成コストがエコではないから、share()を使ってPublisherを struct => class化し、
@takoikatakotako
takoikatakotako / ContentView.swift
Created April 30, 2020 10:33
SwiftUI で文字列中にタップ可能なリンクを追加する
import SwiftUI
struct ContentView: View {
var body: some View {
AttributedText(getAttributeString())
}
func getAttributeString() -> NSAttributedString {
let baseString = """
@cenkbilgen
cenkbilgen / URLSessionDownloadPublisher.swift
Created February 26, 2020 20:52
An extension to URLSession to create a Combine Publisher for URLDownloadTask
import Foundation
import Combine
fileprivate class CancellableStore {
static let shared = CancellableStore()
var cancellables = Set<AnyCancellable>()
}
public enum DownloadOutput {
case complete(Data)
@yimajo
yimajo / ContentView.swift
Last active September 6, 2021 09:21
SwiftUIでBindingは参照元の値を変更できるが、Stateは参照元の値を変更できないサンプル。Playgroundで動作する。
import PlaygroundSupport
import SwiftUI
import Combine
class ObservableObject1: ObservableObject {
@Published var name: String = "src"
}
struct ContentView1: View {
@ObservedObject private var object = ObservableObject1()
@Harry-Harrison
Harry-Harrison / ContentView.swift
Last active July 11, 2023 10:42
Haptic Feedback Vibrations in SwiftUI
// This prints a list of buttons that on tap will fire a different type of haptic vibration
import SwiftUI
struct ContentView: View {
let generator = UINotificationFeedbackGenerator()
var body: some View {
VStack(alignment: .center, spacing: 30.0) {
Button(action: {