SPB Git

Repository Public

Layout.swift 7.2 KB 173 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Search iOS9// File             : Components/Layout.swift10// Description      : Wrapping chip layout, chip style, staggered entrance and11//                    loading placeholder.12// Module           : Components13// Repository       : https://git.spboucher.ai/sp-search-ios14// Project Website  : https://search.spboucher.ai15// Created          : 2026-12-0116// Last Modified    : 2026-12-0117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author           : Simon-Pierre Boucher23// Position         : Professor of Finance24// Institution      : Université du Québec en Outaouais (UQO)25// Department       : Département des sciences administratives26// Organization     : SP Boucher27//28// Website          : https://spboucher.ai29// Git              : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General          : info@spboucher.ai36// Professional     : contact@spboucher.ai37// Direct           : simon@spboucher.ai38// Research         : research@spboucher.ai39// Development      : dev@spboucher.ai40// Support          : support@spboucher.ai41// Security/Admin   : admin@spboucher.ai42// Notifications    : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification   : Public69// Distribution     : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License          : None specified (no license file in this repository)83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import SwiftUI9192/// Lays children out in rows, wrapping to the next line when the width runs out.93struct FlowLayout: Layout {94    var spacing: CGFloat = 89596    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {97        arrange(width: proposal.width ?? .infinity, subviews: subviews).size98    }99100    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {101        let placed = arrange(width: bounds.width, subviews: subviews)102        for (i, p) in placed.points.enumerated() {103            subviews[i].place(at: CGPoint(x: bounds.minX + p.x, y: bounds.minY + p.y), proposal: ProposedViewSize(width: min(placed.sizes[i].width, bounds.width), height: nil))104        }105    }106107    private func arrange(width: CGFloat, subviews: Subviews) -> (size: CGSize, points: [CGPoint], sizes: [CGSize]) {108        var points: [CGPoint] = [], sizes: [CGSize] = []109        var x: CGFloat = 0, y: CGFloat = 0, rowHeight: CGFloat = 0, maxX: CGFloat = 0110        for v in subviews {111            var s = v.sizeThatFits(.unspecified)112            if s.width > width { s = v.sizeThatFits(ProposedViewSize(width: width, height: nil)) }113            if x > 0, x + s.width > width { x = 0; y += rowHeight + spacing; rowHeight = 0 }114            points.append(CGPoint(x: x, y: y)); sizes.append(s)115            x += s.width + spacing116            rowHeight = max(rowHeight, s.height)117            maxX = max(maxX, x - spacing)118        }119        return (CGSize(width: maxX, height: y + rowHeight), points, sizes)120    }121}122123/// Square-cornered chip with a hairline: an example query, an operator, an active filter.124struct ChipStyle: ButtonStyle {125    var mono = false126    func makeBody(configuration: Configuration) -> some View {127        configuration.label128            .font(mono ? .data(.subheadline, weight: .medium) : .subheadline.weight(.medium))129            .foregroundStyle(Theme.ink)130            .padding(.horizontal, 12).padding(.vertical, 8)131            .frame(minHeight: 36)132            .background(configuration.isPressed ? Theme.sunk : Theme.raised, in: RoundedRectangle(cornerRadius: 4))133            .overlay(RoundedRectangle(cornerRadius: 4).stroke(Theme.rule, lineWidth: 1))134            .contentShape(Rectangle())135    }136}137138/// Entrance of a result: a short rise and fade, later rows slightly after earlier ones. Played once per result,139/// not at all under Reduce Motion.140struct Entrance: ViewModifier {141    let index: Int142    let alreadyShown: Bool143    let onShown: () -> Void144    @Environment(\.accessibilityReduceMotion) private var reduceMotion145    @State private var visible = false146147    func body(content: Content) -> some View {148        let shown = visible || alreadyShown || reduceMotion149        content150            .opacity(shown ? 1 : 0)151            .offset(y: shown ? 0 : 14)152            .onAppear {153                guard !shown else { onShown(); return }154                withAnimation(.easeOut(duration: 0.32).delay(Double(min(index, 9)) * 0.04)) { visible = true }155                onShown()156            }157    }158}159160/// Slow breathing of a placeholder; still under Reduce Motion.161struct Breathing: ViewModifier {162    @Environment(\.accessibilityReduceMotion) private var reduceMotion163    @State private var dim = false164    func body(content: Content) -> some View {165        content166            .opacity(dim ? 0.45 : 1)167            .onAppear {168                guard !reduceMotion else { return }169                withAnimation(.easeInOut(duration: 0.9).repeatForever(autoreverses: true)) { dim = true }170            }171    }172}173
Files main
Menu

Theme