Skip to content

Instantly share code, notes, and snippets.

View Dev1an's full-sized avatar

Damiaan Dufaux Dev1an

View GitHub Profile
@Dev1an
Dev1an / VerticalAlignments.swift
Created August 11, 2019 15:22
Dynamic creation of new VerticalAlignments
import SwiftUI
enum Recurse<X>: AlignmentID {
static func defaultValue(in context: ViewDimensions) -> CGFloat {
return 0
}
}
extension VerticalAlignment {
private static func create<A>(depth: UInt, continuation: A) -> AlignmentID.Type {
if depth == 0 { return Recurse<A>.self }

Proper alignment

When using a different alignment guide for each division I get the desired result. However I cannot dynamically create new alignment guides during runtime (for example when I change the expression) because a custom alignment guide should be a Swift Type and as far as I know you cannot create new types during runtime.

centerAlignment

Faulty center alignment

When I reuse the same custom alignment guide multiple times, SwiftUI ignores the guides and defaults to center alignment.

@Dev1an
Dev1an / clampingBug.swift
Created August 4, 2019 16:17
SwiftUI clamping bug
ZStack(alignment: .trailing) {
if model.cursor == model.number.count {
HStack { Cursor() }
}
HStack(spacing: 0) {
ForEach(digits, id: \.0) { (offset, character) in
ZStack(alignment: .leading) {
Text(String(character))
HStack(spacing: 0) {
self.overlay.onTapGesture { self.model.cursor = offset }
@Dev1an
Dev1an / Throttler.swift
Last active May 6, 2019 11:28
Throttle new tasks down when older tasks are still executing.
//
// Throttler.swift
// 3D Viewer
//
// Created by Damiaan on 20/02/2019.
// Copyright © 2019 Devian. All rights reserved.
//
import Dispatch
//
// Created by Damiaan on 25/01/2019.
// Copyright © 2019 Devian. All rights reserved.
//
import AppKit
class PopUpButton<Element>: NSPopUpButton {
var items: SmartCollection<Element> { didSet {bindSmartCollection()} }
@Dev1an
Dev1an / SmartCollection.swift
Last active January 24, 2019 18:35
An observable CustomStringConvertible, RandomAccess-, RangeReplaceableCollection in 50 lines of swift
// Created by Damiaan on 23/01/2019.
// Copyright © 2019 Devian. All rights reserved.
class SmartCollection<Element> {
class Reference {
private (set) var pointee: Element
init(to element: Element) {
pointee = element
}
@Dev1an
Dev1an / NSTableViewDataSource.swift
Last active March 6, 2023 11:38
Simple table data source for NSTableView
// Created by Damiaan on 17/01/2019.
// Copyright © 2019 Devian. All rights reserved.
import AppKit
class TableDataSource<C: RandomAccessCollection>: NSObject, NSTableViewDataSource {
public struct ColumnSpecifier {
let keyPath: PartialKeyPath<C.Element>
fileprivate let transformer: Any?
@Dev1an
Dev1an / AStepIndicator.swift
Last active January 8, 2024 04:51
Simple step indicator view for iOS using UIStackView, IBDesignable, IBInspectable, Auto Layout
//
// Stepper.swift
// Stepperindicator
//
// Created by Damiaan on 13/01/2019.
// Copyright © 2019 Devian. All rights reserved.
//
import UIKit
@Dev1an
Dev1an / simpleStructSyntax.swift
Created September 3, 2018 11:21
Create a simple struct node using SwiftSyntax
import SwiftSyntax
import Foundation
/// creates `struct Magic { let hat: Int }`
let magicStruct = SyntaxFactory.makeStructDecl(
attributes: nil,
modifiers: nil,
structKeyword: SyntaxFactory.makeStructKeyword(),
identifier: SyntaxFactory.makeIdentifier("Magic"),
genericParameterClause: nil,
@Dev1an
Dev1an / Evangelizo-Youversion.md
Last active August 19, 2018 08:05
Evangelizo cross referenced with Youversion bible book reference abbreviations.
Evangelizo Youversion Dutch description
1_Ch 1CH Uit het 1e boek der Kronieken
2_Ch 2CH Uit het 2e boek der Kronieken
1_Co 1CO Uit de 1e brief van de heilige apostel Paulus aan de christenen van Korinte
2_Co 2CO Uit de 2e brief van de heilige apostel Paulus aan de christenen van Korinte
1_Jn 1JN Uit de 1e brief van de apostel Johannes
2_Jn 2JN Uit de 2e brief van de heilige apostel Johannes
3_Jn 3JN Uit de 3e brief van de heilige apostel Johannes
1_M 1MA Uit het 1e boek der Makkabeeën