Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Keyboard9// File : KeyboardUI/KeyboardRootView.swift10// Description : Container of the whole keyboard: backdrop, suggestion bar11// or inline field strip, then the typing surface or one12// panel. Handles the one-handed layout (narrowed surface13// with a side gutter).14// Module : KeyboardUI15// Repository : https://git.spboucher.ai/sp-keyboard16// Created : 2026-12-0117// Last Modified : 2026-12-0118//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author : Simon-Pierre Boucher24// Position : Professor of Finance25// Institution : Université du Québec en Outaouais (UQO)26// Department : Département des sciences administratives27// Organization : SP Boucher28//29// Website : https://spboucher.ai30// Git : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General : info@spboucher.ai37// Professional : contact@spboucher.ai38// Direct : simon@spboucher.ai39// Research : research@spboucher.ai40// Development : dev@spboucher.ai41// Support : support@spboucher.ai42// Security/Admin : admin@spboucher.ai43// Notifications : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification : Public70// Distribution : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License : None specified (no license file in this repository)84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import UIKit92import KeyboardCore93import ThemeEngine9495final class KeyboardRootView: UIView {96 enum OneHandedAction { case flip, expand }9798 let backdrop = BackdropView()99 let bar = SuggestionBarView()100 let strip = FieldStripView()101 let surface = KeyboardSurfaceView()102 let emojiPanel = EmojiPanelView()103 let snippetPanel = SnippetPanelView()104 let aiPanel = AIPanelView()105 private let gutter = UIStackView()106 private let flipButton = UIButton(type: .system)107 private let expandButton = UIButton(type: .system)108 private let topRule = UIView()109110 var onOneHandedAction: ((OneHandedAction) -> Void)?111 /// The system switched between light and dark.112 var onAppearanceChange: (() -> Void)?113 var topHeight: CGFloat = SuggestionBarView.height114 var bodyHeight: CGFloat = 216115 var mode: KeyboardController.Mode = .typing { didSet { if oldValue != mode { updateVisibility() } } }116 var isCapturing = false { didSet { if oldValue != isCapturing { updateVisibility() } } }117 var oneHanded: OneHandedMode = .off { didSet { if oldValue != oneHanded { updateVisibility() } } }118119 static let oneHandedFraction: CGFloat = 0.82120121 override init(frame: CGRect) {122 super.init(frame: frame)123 clipsToBounds = true124 addSubview(backdrop)125 addSubview(topRule)126 [bar, strip, surface, emojiPanel, snippetPanel, aiPanel].forEach(addSubview)127 gutter.axis = .vertical128 gutter.distribution = .fillEqually129 addSubview(gutter)130 flipButton.accessibilityLabel = "Déplacer le clavier de l'autre côté"131 flipButton.addAction(UIAction { [weak self] _ in self?.onOneHandedAction?(.flip) }, for: .touchUpInside)132 expandButton.setImage(UIImage(systemName: "arrow.up.left.and.arrow.down.right"), for: .normal)133 expandButton.accessibilityLabel = "Quitter le mode une main"134 expandButton.addAction(UIAction { [weak self] _ in self?.onOneHandedAction?(.expand) }, for: .touchUpInside)135 gutter.addArrangedSubview(expandButton)136 gutter.addArrangedSubview(flipButton)137 registerForTraitChanges([UITraitUserInterfaceStyle.self]) { (view: KeyboardRootView, _: UITraitCollection) in138 view.onAppearanceChange?()139 }140 updateVisibility()141 }142143 @available(*, unavailable)144 required init?(coder: NSCoder) { fatalError("init(coder:) is not used") }145146 func apply(theme: Theme) {147 backgroundColor = theme.background.average.opacity(1).uiColor148 backdrop.apply(theme)149 bar.apply(theme: theme)150 strip.apply(theme: theme)151 surface.apply(theme: theme)152 emojiPanel.apply(theme: theme)153 snippetPanel.apply(theme: theme)154 aiPanel.apply(theme: theme)155 topRule.backgroundColor = theme.suggestionText.opacity(0.12).uiColor156 flipButton.tintColor = theme.suggestionText.uiColor157 expandButton.tintColor = theme.suggestionText.uiColor158 }159160 private func updateVisibility() {161 let typing = mode == .typing162 bar.isHidden = isCapturing || mode == .ai163 strip.isHidden = !isCapturing164 surface.isHidden = !typing165 emojiPanel.isHidden = mode != .emoji166 snippetPanel.isHidden = mode != .snippets167 aiPanel.isHidden = mode != .ai168 topRule.isHidden = mode == .ai169 gutter.isHidden = oneHanded == .off || !typing170 flipButton.setImage(UIImage(systemName: oneHanded == .left ? "chevron.right.2" : "chevron.left.2"), for: .normal)171 setNeedsLayout()172 }173174 override func layoutSubviews() {175 super.layoutSubviews()176 backdrop.frame = bounds177 let safe = bounds.inset(by: UIEdgeInsets(top: 0, left: safeAreaInsets.left, bottom: 0, right: safeAreaInsets.right))178 let top = CGRect(x: safe.minX, y: 0, width: safe.width, height: topHeight)179 bar.frame = top180 strip.frame = top181 topRule.frame = CGRect(x: safe.minX + 8, y: topHeight - 0.5, width: safe.width - 16, height: 1 / max(traitCollection.displayScale, 1))182 let body = CGRect(x: safe.minX, y: topHeight, width: safe.width, height: max(0, bounds.height - topHeight - 3))183 emojiPanel.frame = body184 snippetPanel.frame = body185 aiPanel.frame = CGRect(x: safe.minX, y: 0, width: safe.width, height: bounds.height - 3)186 if oneHanded == .off || mode != .typing {187 surface.frame = body188 } else {189 let width = body.width * Self.oneHandedFraction190 let gutterWidth = body.width - width191 if oneHanded == .left {192 surface.frame = CGRect(x: body.minX, y: body.minY, width: width, height: body.height)193 gutter.frame = CGRect(x: body.minX + width, y: body.minY, width: gutterWidth, height: body.height)194 } else {195 surface.frame = CGRect(x: body.minX + gutterWidth, y: body.minY, width: width, height: body.height)196 gutter.frame = CGRect(x: body.minX, y: body.minY, width: gutterWidth, height: body.height)197 }198 }199 }200}201