Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : Groupe Ka — app iOS native7// File : GroupeKa/Design/Composants.swift8// Module : Groupe Ka9// Repository : https://git.spboucher.ai/groupe-ka-ios10// Project Website : Not specified11// Created : 2026-12-0312// Last Modified : 2026-12-0313//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI4243// Pièces d'interface communes. Le vocabulaire est celui d'une planche d'architecte : un cartouche en tête,44// des filets, des cotes ; jamais de carte flottante, de flou ni de dégradé.4546/// En-tête d'un écran : le symbole et le nom du groupe à gauche (ou le retour), un geste à droite.47struct EnTete<Droite: View>: View {48 var titre: String?49 var retour: (() -> Void)?50 var accueil: (() -> Void)?51 @ViewBuilder var droite: Droite5253 var body: some View {54 HStack(spacing: 10) {55 if let retour {56 Button(action: retour) {57 HStack(spacing: 6) {58 Fleche(sens: .gauche).frame(width: 16, height: 16)59 Text("Retour").ka(.sans, 15, graisse: 500, style: .subheadline, plafond: 1.4)60 }61 .foregroundStyle(Ka.encre2)62 .frame(minHeight: 44)63 .contentShape(Rectangle())64 }65 .buttonStyle(.plain)66 .accessibilityIdentifier("retour")67 } else {68 Button { accueil?() } label: {69 HStack(spacing: 9) {70 SymboleGroupe().frame(width: 26, height: 26)71 (Text("Groupe ").foregroundStyle(Ka.encre3) + Text("Ka").foregroundStyle(Ka.encre))72 .ka(.serif, 20, graisse: 500, style: .title3, plafond: 1.3)73 .tracking(-0.4)74 }75 .frame(minHeight: 44)76 .contentShape(Rectangle())77 }78 .buttonStyle(.plain)79 .accessibilityLabel("Groupe Ka")80 .accessibilityHint(accueil == nil ? "" : "Ramène la rotonde au repos")81 .accessibilityAddTraits(.isHeader)82 }83 Spacer(minLength: 8)84 if let titre {85 Text(titre).ka(.sans, 15, graisse: 500, style: .subheadline, plafond: 1.4).foregroundStyle(Ka.encre2).lineLimit(1)86 }87 droite88 }89 .padding(.horizontal, 16)90 .frame(minHeight: 52)91 .background(Ka.papier)92 }93}9495extension EnTete where Droite == EmptyView {96 init(titre: String? = nil, retour: (() -> Void)? = nil, accueil: (() -> Void)? = nil) {97 self.init(titre: titre, retour: retour, accueil: accueil) { EmptyView() }98 }99}100101/// Bouton de texte de l'en-tête (« Réglages »).102struct BoutonTexte: View {103 let titre: String104 var couleur: Color = Ka.encre2105 let action: () -> Void106 var body: some View {107 Button(action: action) {108 Text(titre).ka(.sans, 15, graisse: 500, style: .subheadline, plafond: 1.4)109 .foregroundStyle(couleur)110 .frame(minWidth: 44, minHeight: 44)111 .contentShape(Rectangle())112 }113 .buttonStyle(.plain)114 }115}116117/// Flèche tracée (les polices embarquées n'ont pas de flèches).118struct Fleche: View {119 enum Sens { case droite, gauche, haut, bas, sortie }120 var sens: Sens = .droite121 var body: some View {122 Canvas { ctx, t in123 var c = ctx124 c.scaleBy(x: t.width / 16, y: t.height / 16)125 switch sens {126 case .droite: break127 case .gauche: c.translateBy(x: 16, y: 0); c.scaleBy(x: -1, y: 1)128 case .haut: c.translateBy(x: 0, y: 16); c.rotate(by: .degrees(-90))129 case .bas: c.translateBy(x: 16, y: 0); c.rotate(by: .degrees(90))130 case .sortie: c.translateBy(x: 8, y: 8); c.rotate(by: .degrees(-45)); c.translateBy(x: -8, y: -8)131 }132 var p = Path()133 p.move(to: CGPoint(x: 3, y: 8)); p.addLine(to: CGPoint(x: 12.5, y: 8))134 p.move(to: CGPoint(x: 8.5, y: 3.5)); p.addLine(to: CGPoint(x: 13, y: 8)); p.addLine(to: CGPoint(x: 8.5, y: 12.5))135 c.stroke(p, with: .foreground, style: StrokeStyle(lineWidth: 1.6, lineCap: .round, lineJoin: .round))136 }137 .accessibilityHidden(true)138 }139}140141/// Titre de section : un filet d'encre, puis le titre en Newsreader (le haut d'un cartouche).142struct TitreSection: View {143 let titre: String144 var note: String?145 var body: some View {146 VStack(alignment: .leading, spacing: 0) {147 Filet(couleur: Ka.encre)148 Text(titre)149 .ka(.serif, 22, graisse: 500, style: .title2, plafond: 1.8)150 .tracking(-0.4)151 .foregroundStyle(Ka.encre)152 .padding(.top, 12)153 .fixedSize(horizontal: false, vertical: true)154 .accessibilityAddTraits(.isHeader)155 if let note, !note.isEmpty {156 Text(note).ka(.sans, 14, style: .footnote).foregroundStyle(Ka.encre2)157 .padding(.top, 4)158 .fixedSize(horizontal: false, vertical: true)159 }160 }161 .frame(maxWidth: .infinity, alignment: .leading)162 }163}164165/// Petit titre d'un bloc à l'intérieur d'une section (un graphique, un tableau).166struct TitreBloc: View {167 let titre: String168 var unite: String?169 var body: some View {170 VStack(alignment: .leading, spacing: 2) {171 Text(titre).ka(.sans, 15, graisse: 600, style: .subheadline).foregroundStyle(Ka.encre)172 .fixedSize(horizontal: false, vertical: true)173 .accessibilityAddTraits(.isHeader)174 if let unite, !unite.isEmpty {175 Text(unite).ka(.sans, 12, style: .caption1).foregroundStyle(Ka.encre3)176 }177 }178 .frame(maxWidth: .infinity, alignment: .leading)179 }180}181182/// Étiquette de cote : petites capitales espacées (légendes, colonnes).183struct Cote: View {184 let texte: String185 var couleur: Color = Ka.encre3186 var body: some View {187 Text(texte.uppercased(with: Fmt.locale))188 .ka(.sans, 11, graisse: 500, style: .caption2, plafond: 1.6)189 .tracking(0.6)190 .foregroundStyle(couleur)191 }192}193194/// Choix de la période : du texte, un filet sous le choix courant (comme les onglets du site).195struct ChoixPeriode: View {196 @Binding var periode: Periode197 var couleur: Color = Ka.encre198199 var body: some View {200 ScrollView(.horizontal, showsIndicators: false) {201 HStack(spacing: 2) {202 ForEach(Periode.allCases) { p in203 Button { periode = p } label: {204 VStack(spacing: 5) {205 Text(p.libelle)206 .ka(.sans, 15, graisse: p == periode ? 600 : 500, style: .subheadline, plafond: 1.5)207 .foregroundStyle(p == periode ? Ka.encre : Ka.encre2)208 Rectangle().fill(p == periode ? couleur : .clear).frame(height: 1.5)209 }210 .padding(.horizontal, 10)211 .frame(minHeight: 44)212 .fixedSize()213 .contentShape(Rectangle())214 }215 .buttonStyle(.plain)216 .accessibilityLabel(p.libelleLong)217 .accessibilityAddTraits(p == periode ? .isSelected : [])218 .accessibilityIdentifier("periode-\(p.rawValue)")219 }220 }221 .padding(.horizontal, 6)222 }223 .accessibilityElement(children: .contain)224 .accessibilityLabel("Période")225 }226}227228/// Dit d'où vient ce qui est affiché : lu à l'instant, gardé sur l'appareil (hors ligne), ou périmé côté serveur.229struct Fraicheur<T: Sendable>: View {230 let reponse: Reponse<T>?231 var erreur: APIErreur?232 var enCours = false233234 var body: some View {235 let lignes = textes236 if !lignes.isEmpty {237 VStack(alignment: .leading, spacing: 3) {238 ForEach(Array(lignes.enumerated()), id: \.offset) { _, l in239 HStack(alignment: .firstTextBaseline, spacing: 7) {240 Rectangle().fill(l.alerte ? Ka.encre : Ka.filet2).frame(width: 7, height: 7).offset(y: -1)241 Text(l.texte).ka(.sans, 13, graisse: l.alerte ? 500 : 400, style: .footnote)242 .foregroundStyle(l.alerte ? Ka.encre : Ka.encre3)243 .fixedSize(horizontal: false, vertical: true)244 }245 }246 }247 .frame(maxWidth: .infinity, alignment: .leading)248 .accessibilityElement(children: .combine)249 .accessibilityIdentifier("fraicheur")250 }251 }252253 private var textes: [(texte: String, alerte: Bool)] {254 var l: [(String, Bool)] = []255 if let r = reponse {256 switch r.provenance {257 case .horsLigne:258 l.append(("Hors ligne : dernière réponse gardée sur l'appareil, reçue \(Fmt.ilYA(r.recueA)).", true))259 case .reseau, .confirmee:260 l.append(("Lu \(Fmt.ilYA(r.recueA)).", false))261 }262 if r.perime {263 let quand = Dates.instant(r.meta?.lu_a).map { " (lecture du \(Fmt.jour($0)) à \(Fmt.heure($0)))" } ?? ""264 l.append(("Données périmées : le serveur n'a pas pu relire le site, c'est la dernière valeur connue\(quand).", true))265 }266 } else if let erreur {267 l.append((erreur.errorDescription ?? "La lecture a échoué.", true))268 } else if enCours {269 l.append(("Lecture en cours…", false))270 }271 return l272 }273}274275/// État vide ou panne, dit simplement, avec un geste pour réessayer.276struct Constat: View {277 let titre: String278 var texte: String?279 var reessayer: (() -> Void)?280 var body: some View {281 VStack(alignment: .leading, spacing: 8) {282 Text(titre).ka(.serif, 22, graisse: 500, style: .title2).foregroundStyle(Ka.encre)283 if let texte { Text(texte).ka(.sans, 15, style: .subheadline).foregroundStyle(Ka.encre2) }284 if let reessayer {285 Button("Réessayer", action: reessayer).buttonStyle(BoutonContour(couleur: Ka.encre)).padding(.top, 6)286 }287 }288 .frame(maxWidth: .infinity, alignment: .leading)289 .padding(.vertical, 24)290 }291}292293/// Bouton plein : un aplat de la couleur, le texte couleur papier.294struct BoutonPlein: ButtonStyle {295 var couleur: Color = Ka.encre296 func makeBody(configuration: Configuration) -> some View {297 configuration.label298 .ka(.sans, 15, graisse: 600, style: .subheadline, plafond: 1.5)299 .foregroundStyle(Ka.papier)300 .padding(.horizontal, 17)301 .frame(minHeight: 46)302 .background(couleur, in: RoundedRectangle(cornerRadius: 10))303 .opacity(configuration.isPressed ? 0.82 : 1)304 .contentShape(Rectangle())305 }306}307308/// Bouton au trait : un contour, le texte de la couleur.309struct BoutonContour: ButtonStyle {310 var couleur: Color = Ka.encre311 func makeBody(configuration: Configuration) -> some View {312 configuration.label313 .ka(.sans, 15, graisse: 600, style: .subheadline, plafond: 1.5)314 .foregroundStyle(couleur)315 .padding(.horizontal, 16)316 .frame(minHeight: 46)317 .background(RoundedRectangle(cornerRadius: 10).strokeBorder(couleur.opacity(0.55), lineWidth: 1.5))318 .opacity(configuration.isPressed ? 0.7 : 1)319 .contentShape(Rectangle())320 }321}322323/// Lien de texte souligné, à la couleur donnée.324struct LienTexte: View {325 let titre: String326 var couleur: Color = Ka.encre327 var fleche: Fleche.Sens? = .droite328 let action: () -> Void329 var body: some View {330 Button(action: action) {331 HStack(spacing: 6) {332 Text(titre).ka(.sans, 15, graisse: 600, style: .subheadline).underline(true, color: couleur.opacity(0.5))333 .multilineTextAlignment(.leading)334 if let fleche { Fleche(sens: fleche).frame(width: 15, height: 15) }335 }336 .foregroundStyle(couleur)337 .frame(minHeight: 44)338 .contentShape(Rectangle())339 }340 .buttonStyle(.plain)341 }342}343344/// Ligne « libellé … valeur » d'une fiche, avec son filet.345struct LigneFiche: View {346 let libelle: String347 let valeur: String348 var body: some View {349 VStack(spacing: 0) {350 ViewThatFits(in: .horizontal) {351 HStack(alignment: .firstTextBaseline, spacing: 16) {352 Text(libelle).ka(.sans, 14, style: .footnote).foregroundStyle(Ka.encre3)353 Spacer(minLength: 12)354 Text(valeur).ka(.sans, 15, graisse: 500, style: .subheadline).foregroundStyle(Ka.encre).multilineTextAlignment(.trailing)355 }356 VStack(alignment: .leading, spacing: 3) {357 Text(libelle).ka(.sans, 14, style: .footnote).foregroundStyle(Ka.encre3)358 Text(valeur).ka(.sans, 15, graisse: 500, style: .subheadline).foregroundStyle(Ka.encre)359 .fixedSize(horizontal: false, vertical: true)360 }361 .frame(maxWidth: .infinity, alignment: .leading)362 }363 .padding(.vertical, 10)364 Filet()365 }366 .accessibilityElement(children: .combine)367 }368}369370/// Largeur de lecture : sur iPad le contenu ne s'étire pas d'un bord à l'autre.371struct Colonne<Contenu: View>: View {372 var largeurMax: CGFloat = 1040373 @ViewBuilder var contenu: Contenu374 var body: some View {375 contenu376 .frame(maxWidth: largeurMax, alignment: .leading)377 .padding(.horizontal, 16)378 .frame(maxWidth: .infinity)379 }380}381382extension View {383 /// fond de papier d'un écran entier384 func fondPapier() -> some View { background(Ka.papier.ignoresSafeArea()) }385}386