Skip to content

Instantly share code, notes, and snippets.

View Dev1an's full-sized avatar

Damiaan Dufaux Dev1an

View GitHub Profile
@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
@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 }

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 / 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 }
//
// main.swift
// ImageExtractor
//
// Created by Damiaan on 13/08/2019.
// Copyright © 2019 dPro. All rights reserved.
//
import Quartz
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
@Dev1an
Dev1an / SplitCell.js
Last active February 3, 2020 12:19
Prosemirror-Table splitting a cell
function splitCellVertically(state, dispatch) {
if (!isInTable(state)) { return false }
const rect = selectedRect(state);
const selectionWidth = rect.right - rect.left
const selectionHeight = rect.bottom - rect.top
if (selectionWidth > 1 || selectionHeight > 1) {return false}
if (dispatch) {
const tr = state.tr
const ref = rect
const row = rect.bottom
@Dev1an
Dev1an / Above.swift
Created March 7, 2020 00:35
Show a SwiftUI View floating above another View without altering its layout.
import SwiftUI
extension View {
func float<Content: View>(above: Content) -> ModifiedContent<Self, Above<Content>> {
self.modifier(Above(aboveContent: above))
}
}
struct Above<AboveContent: View>: ViewModifier {
let aboveContent: AboveContent
import Foundation
import SwiftUI
struct ContentView: View {
@State private var model = Model()
var body: some View {
ZStack {
CustomCollectionView(lastSelectedIndex: $model.index)
IndexPreviewer(model: model)