~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
80 lines · 3.5 KB Raw History
import SwiftUI

struct PadColumnFlick {
    let releaseOffset: CGFloat
    let projectedOffset: CGFloat
    let direction: CGFloat
    var recentFlick = false
}

enum PadColumnLayout {
    static let actionSpacing: CGFloat = 12
    static let edgePadding: CGFloat = 12

    static func opensDetailsInColumns(windowSize: CGSize) -> Bool {
        windowSize.width > windowSize.height
    }

    static func usesColumns(windowSize: CGSize, screenSize: CGSize) -> Bool {
        let isMiniPortrait = isMini(screenSize: screenSize) && windowSize.height >= windowSize.width
        return !isMiniPortrait && windowSize.width >= 24 + 320 * 2 + 12
    }

    static func usesFloatingComposeButton(windowSize: CGSize, screenSize: CGSize) -> Bool {
        let screenWidth = min(screenSize.width, screenSize.height)
        return screenWidth > 0 && windowSize.width > 0
            && !usesColumns(windowSize: windowSize, screenSize: screenSize)
            && windowSize.height >= windowSize.width
            && windowSize.width >= screenWidth - 1
    }

    static func isMini(screenSize: CGSize) -> Bool {
        let shortEdge = min(screenSize.width, screenSize.height)
        return shortEdge > 0 && shortEdge < 768
    }

    static func loadingColumnCount(visibleColumnCount: Int, existingColumnCount: Int) -> Int {
        let count = (2...4).contains(visibleColumnCount) ? visibleColumnCount : 2
        return max(0, count - existingColumnCount)
    }

    static func columnWidth(windowWidth: CGFloat, visibleColumnCount: Int) -> CGFloat {
        let count = CGFloat((2...4).contains(visibleColumnCount) ? visibleColumnCount : 2)
        return max(320, (windowWidth - 24 - 12 * (count - 1)) / count)
    }

    static func columnsFit(columnCount: Int, columnWidth: CGFloat, windowWidth: CGFloat) -> Bool {
        let contentWidth = 24 + CGFloat(columnCount) * columnWidth + CGFloat(max(0, columnCount - 1)) * 12
        return contentWidth <= windowWidth + 1
    }

    static func flickTarget(
        release: PadColumnFlick,
        columnWidth: CGFloat, columnCount: Int, contentWidth: CGFloat, windowWidth: CGFloat
    ) -> CGFloat? {
        let releaseOffset = release.releaseOffset
        let projectedOffset = release.projectedOffset
        let direction = release.direction
        let recentFlick = release.recentFlick
        let projectedMovement = projectedOffset - releaseOffset
        let movement = abs(projectedMovement) >= 12 ? projectedMovement : (recentFlick ? direction : 0)
        guard movement * direction > 0,
            columnWidth > 0, windowWidth > 0, columnCount > 0
        else { return nil }
        let maximum = max(0, contentWidth - windowWidth)
        guard releaseOffset >= 0, releaseOffset <= maximum else { return nil }
        let left = releaseOffset
        let right = left + windowWidth
        let stride = columnWidth + 12
        let indices = 0..<columnCount
        let first = indices.first(where: { 12 + CGFloat($0) * stride >= left - 1 }) ?? columnCount
        let last = indices.last(where: { 12 + CGFloat($0) * stride + columnWidth <= right + 1 }) ?? -1
        let targetIndex = movement > 0 ? last + 1 : first - 1
        guard indices.contains(targetIndex) else { return nil }
        let targetLeft = 12 + CGFloat(targetIndex) * stride
        let target = movement > 0 ? targetLeft + columnWidth - windowWidth + edgePadding : targetLeft - edgePadding
        let clamped = min(maximum, max(0, target))
        guard (movement > 0 && clamped > left) || (movement < 0 && clamped < left) else { return nil }
        return clamped
    }
}