SPB Git

Repository Public

TerminalSurface.swift 8.6 KB 237 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : Claude Remote / Codex Remote9// File             : Packages/ClaudeRemoteKit/Sources/ClaudeRemoteTerminal/TerminalSurface.swift10// Module           : ClaudeRemoteTerminal11// Repository       : https://git.spboucher.ai/claude-remote-apple12// Project Website  : https://claude.spboucher.ai13// Created          : 2026-11-2914// Last Modified    : 2026-11-2915//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: support@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import SwiftUI89import SwiftTerm9091#if os(macOS)92import AppKit9394/// Hosts a controller's terminal view in SwiftUI.95///96/// The surface never creates a terminal: it re-parents the controller's existing view, so the97/// same session can move between tabs, splits and windows without losing its state.98///99/// - Important: do not animate the size of this view or of its containers (each intermediate100///   size is a terminal resize). Wrap layout changes in `withTransaction(Transaction(animation: nil))`.101public struct TerminalSurface: NSViewRepresentable {102    private let controller: TerminalSessionController103    private let focusOnAppear: Bool104105    /// - Parameter focusOnAppear: make the terminal first responder when it enters a window.106    public init(controller: TerminalSessionController, focusOnAppear: Bool = true) {107        self.controller = controller108        self.focusOnAppear = focusOnAppear109    }110111    public func makeNSView(context: Context) -> TerminalHostView {112        let host = TerminalHostView()113        host.focusOnAppear = focusOnAppear114        host.adopt(controller.terminalView)115        return host116    }117118    public func updateNSView(_ host: TerminalHostView, context: Context) {119        host.focusOnAppear = focusOnAppear120        host.adopt(controller.terminalView)121    }122123    public static func dismantleNSView(_ host: TerminalHostView, coordinator: ()) {124        host.release()125    }126}127128/// Container whose only subview is the terminal view of the current controller.129public final class TerminalHostView: NSView {130    var focusOnAppear = true131    private weak var hosted: NSView?132133    func adopt(_ view: NSView) {134        guard hosted !== view || view.superview !== self else { return }135        hosted?.removeFromSuperview()136        view.removeFromSuperview()137        view.frame = bounds138        view.autoresizingMask = [.width, .height]139        addSubview(view)140        hosted = view141        focusIfNeeded()142    }143144    func release() {145        if hosted?.superview === self { hosted?.removeFromSuperview() }146        hosted = nil147    }148149    override public func viewDidMoveToWindow() {150        super.viewDidMoveToWindow()151        focusIfNeeded()152    }153154    private func focusIfNeeded() {155        guard focusOnAppear, let window, let hosted, hosted.superview === self else { return }156        DispatchQueue.main.async { [weak window, weak hosted] in157            guard let window, let hosted, hosted.window === window else { return }158            window.makeFirstResponder(hosted)159        }160    }161}162163#elseif os(iOS)164import UIKit165166/// Hosts a controller's terminal view in SwiftUI.167///168/// The surface never creates a terminal: it re-parents the controller's existing view. SwiftTerm's169/// default keyboard accessory is removed (`inputAccessoryView == nil`): build your own key row170/// from `TerminalKey` / `TerminalKeyDescriptor` and call `controller.send(key:)`. Hardware171/// keyboards work through the view's own key handling (arrows, Ctrl, Option as Meta).172///173/// - Important: do not animate the size of this view or of its containers (each intermediate174///   size is a terminal resize).175public struct TerminalSurface: UIViewRepresentable {176    private let controller: TerminalSessionController177    private let focusOnAppear: Bool178179    /// - Parameter focusOnAppear: become first responder (shows the keyboard) when entering a window.180    public init(controller: TerminalSessionController, focusOnAppear: Bool = false) {181        self.controller = controller182        self.focusOnAppear = focusOnAppear183    }184185    public func makeUIView(context: Context) -> TerminalHostView {186        let host = TerminalHostView()187        host.focusOnAppear = focusOnAppear188        host.adopt(controller.terminalView)189        return host190    }191192    public func updateUIView(_ host: TerminalHostView, context: Context) {193        host.focusOnAppear = focusOnAppear194        host.adopt(controller.terminalView)195    }196197    public static func dismantleUIView(_ host: TerminalHostView, coordinator: ()) {198        host.release()199    }200}201202/// Container whose only subview is the terminal view of the current controller.203public final class TerminalHostView: UIView {204    var focusOnAppear = false205    private weak var hosted: UIView?206207    func adopt(_ view: UIView) {208        guard hosted !== view || view.superview !== self else { return }209        hosted?.removeFromSuperview()210        view.removeFromSuperview()211        view.frame = bounds212        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]213        addSubview(view)214        hosted = view215        focusIfNeeded()216    }217218    func release() {219        if hosted?.superview === self { hosted?.removeFromSuperview() }220        hosted = nil221    }222223    override public func didMoveToWindow() {224        super.didMoveToWindow()225        focusIfNeeded()226    }227228    private func focusIfNeeded() {229        guard focusOnAppear, window != nil, let hosted, hosted.superview === self else { return }230        DispatchQueue.main.async { [weak hosted] in231            guard let hosted, hosted.window != nil else { return }232            _ = hosted.becomeFirstResponder()233        }234    }235}236#endif237
Files main
Menu

Theme