Skip to content

Instantly share code, notes, and snippets.

View Dev1an's full-sized avatar

Damiaan Dufaux Dev1an

View GitHub Profile
import SwiftUI
struct FixedHexagon: View {
var body: some view { Hexagon().aspectRatio(2/sqrt(3), contentMode: .fit) }
}
struct Hexagon: Shape {
func path(in rect: CGRect) -> Path {
Path { path in
let width = rect.size.width
//
// main.swift
// ImageExtractor
//
// Created by Damiaan on 13/08/2019.
// Copyright © 2019 dPro. All rights reserved.
//
import Quartz
@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