The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import ExpoModulesCore |
| 2 | import UIKit |
| 3 | |
| 4 | private final class WorkspaceTabBar: UITabBar { |
| 5 | var onContentLayout: ((CGRect) -> Void)? |
| 6 | |
| 7 | override func layoutSubviews() { |
| 8 | super.layoutSubviews() |
| 9 | // iOS 26.6 doesn't copy item identifiers onto its tab buttons; the bar holds no other controls. |
| 10 | let identified = tabButtons(in: self) { $0.accessibilityIdentifier?.hasPrefix("workspace-tab-") == true } |
| 11 | let candidates = identified.isEmpty ? tabButtons(in: self) { _ in true } : identified |
| 12 | // Pressing a tab adds a magnified copy of the row for the glass lens, without identifiers. |
| 13 | // Sizing compose from it leaves compose enlarged until the bar lays out again. |
| 14 | // The copy is many points larger; the tolerance only absorbs conversion rounding. |
| 15 | let buttons = candidates.filter { |
| 16 | let size = convert($0.bounds, from: $0).size |
| 17 | return abs(size.width - $0.bounds.width) < 1 && abs(size.height - $0.bounds.height) < 1 |
| 18 | } |
| 19 | let row = buttons.reduce(CGRect.null) { $0.union(convert($1.bounds, from: $1)) } |
| 20 | if !row.isNull { onContentLayout?(row) } |
| 21 | } |
| 22 | |
| 23 | override func safeAreaInsetsDidChange() { |
| 24 | super.safeAreaInsetsDidChange() |
| 25 | invalidateIntrinsicContentSize() |
| 26 | } |
| 27 | |
| 28 | private func tabButtons(in view: UIView, matching match: (UIView) -> Bool) -> [UIView] { |
| 29 | view.subviews.filter { !$0.isHidden }.flatMap { subview in |
| 30 | if subview is UIControl, match(subview) { return [subview] } |
| 31 | return tabButtons(in: subview, matching: match) |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | final class T3SidebarTabBarView: ExpoView, UITabBarDelegate { |
| 37 | let onSelectionChange = EventDispatcher() |
| 38 | let onCompose = EventDispatcher() |
| 39 | private let tabBar = WorkspaceTabBar() |
| 40 | private let composeButton = UIButton(type: .system) |
| 41 | private lazy var tabBarTrailing = tabBar.trailingAnchor.constraint(equalTo: trailingAnchor) |
| 42 | private var itemsByTab: [String: UITabBarItem] = [:] |
| 43 | var tabs = ["threads", "pullRequests", "issues", "usage", "settings"] { |
| 44 | didSet { if tabs != oldValue { updateItems() } } |
| 45 | } |
| 46 | var selectedTab = "threads" { didSet { updateSelection() } } |
| 47 | var selectedColor: UIColor = .systemBlue { didSet { tabBar.tintColor = selectedColor } } |
| 48 | var compact = false { |
| 49 | didSet { |
| 50 | tabBarTrailing.constant = showsCompose ? (compact ? -64 : -76) : 0 |
| 51 | tabBar.setNeedsLayout() |
| 52 | } |
| 53 | } |
| 54 | var showsCompose = false { |
| 55 | didSet { |
| 56 | composeButton.isHidden = !showsCompose |
| 57 | tabBarTrailing.constant = showsCompose ? (compact ? -64 : -76) : 0 |
| 58 | setNeedsLayout() |
| 59 | // JS moves this view's edge by the same amount, so the bar keeps its frame and won't lay |
| 60 | // itself out; compose is placed only from that layout. |
| 61 | tabBar.setNeedsLayout() |
| 62 | } |
| 63 | } |
| 64 | var appearance = "light" { |
| 65 | didSet { overrideUserInterfaceStyle = appearance == "dark" ? .dark : .light } |
| 66 | } |
| 67 | |
| 68 | required init(appContext: AppContext? = nil) { |
| 69 | super.init(appContext: appContext) |
| 70 | tabBar.itemPositioning = .fill |
| 71 | // iPad's regular bar uses a compressed inline row even with fill positioning. |
| 72 | // This floating sidebar control needs the same full-size items as a phone bar. |
| 73 | if #available(iOS 17.0, *) { |
| 74 | tabBar.traitOverrides.horizontalSizeClass = .compact |
| 75 | } |
| 76 | tabBar.delegate = self |
| 77 | tabBar.translatesAutoresizingMaskIntoConstraints = false |
| 78 | addSubview(tabBar) |
| 79 | NSLayoutConstraint.activate([ |
| 80 | tabBar.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 81 | tabBarTrailing, |
| 82 | tabBar.bottomAnchor.constraint(equalTo: bottomAnchor), |
| 83 | ]) |
| 84 | // UIKit's floating row changes with window geometry. Follow its laid-out items |
| 85 | // instead of estimating their position from a second set of safe-area updates. |
| 86 | tabBar.onContentLayout = { [weak self] frame in self?.layoutCompose(alongside: frame) } |
| 87 | composeButton.accessibilityLabel = "New chat" |
| 88 | composeButton.addAction(UIAction { [weak self] _ in self?.onCompose() }, for: .touchUpInside) |
| 89 | composeButton.isHidden = true |
| 90 | addSubview(composeButton) |
| 91 | configureCompose() |
| 92 | prepareItems() |
| 93 | updateItems() |
| 94 | } |
| 95 | |
| 96 | private func configureCompose() { |
| 97 | var configuration: UIButton.Configuration |
| 98 | if #available(iOS 26.0, *) { |
| 99 | configuration = .glass() |
| 100 | } else { |
| 101 | configuration = .gray() |
| 102 | } |
| 103 | configuration.cornerStyle = .capsule |
| 104 | configuration.baseForegroundColor = .label |
| 105 | configuration.image = tabIcon(UIImage(systemName: "square.and.pencil")) |
| 106 | composeButton.configuration = configuration |
| 107 | } |
| 108 | |
| 109 | private func prepareItems() { |
| 110 | let iconBundle = Bundle(for: Self.self).url(forResource: "T3NativeControlIcons", withExtension: "bundle").flatMap(Bundle.init(url:)) |
| 111 | let pullRequestIcon = UIImage(named: "pull-request", in: iconBundle, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate) |
| 112 | let labels = ["threads": "Chats", "pullRequests": "PRs", "issues": "Issues", "usage": "Usage", "settings": "Settings"] |
| 113 | let issueIcon = UIImage(named: "issue", in: iconBundle, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate) |
| 114 | let symbols = ["threads": "text.bubble", "usage": "chart.bar.xaxis", "settings": "gearshape"] |
| 115 | for (tab, label) in labels { |
| 116 | let icon = tab == "pullRequests" ? pullRequestIcon : tab == "issues" ? issueIcon : UIImage(systemName: symbols[tab] ?? "circle") |
| 117 | let item = UITabBarItem(title: nil, image: tabIcon(icon), tag: 0) |
| 118 | item.accessibilityLabel = label |
| 119 | item.accessibilityIdentifier = "workspace-tab-\(tab)" |
| 120 | if tab == "threads" { item.selectedImage = tabIcon(UIImage(systemName: "text.bubble.fill")) } |
| 121 | itemsByTab[tab] = item |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private func updateItems() { |
| 126 | tabBar.items = tabs.enumerated().compactMap { index, tab in |
| 127 | guard let item = itemsByTab[tab] else { return nil } |
| 128 | item.tag = index |
| 129 | return item |
| 130 | } |
| 131 | updateSelection() |
| 132 | } |
| 133 | |
| 134 | private func layoutCompose(alongside frame: CGRect) { |
| 135 | guard showsCompose, frame.height > 0 else { return } |
| 136 | let row = convert(frame, from: tabBar) |
| 137 | let size = row.height + 8 |
| 138 | composeButton.frame = CGRect(x: row.maxX + 16, y: row.midY - size / 2, width: size, height: size) |
| 139 | } |
| 140 | |
| 141 | override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { |
| 142 | if showsCompose, let target = composeButton.hitTest(convert(point, to: composeButton), with: event) { |
| 143 | return target |
| 144 | } |
| 145 | return tabBar.hitTest(convert(point, to: tabBar), with: event) |
| 146 | } |
| 147 | |
| 148 | override func didMoveToWindow() { |
| 149 | super.didMoveToWindow() |
| 150 | updateSelection() |
| 151 | } |
| 152 | |
| 153 | // Give symbols and bitmap assets the same image box; UIKit otherwise scales symbols independently. |
| 154 | private func tabIcon(_ image: UIImage?) -> UIImage? { |
| 155 | guard let image else { return nil } |
| 156 | let canvas = CGSize(width: 28, height: 28) |
| 157 | let scale = min(24 / image.size.width, 24 / image.size.height) |
| 158 | let size = CGSize(width: image.size.width * scale, height: image.size.height * scale) |
| 159 | return UIGraphicsImageRenderer(size: canvas).image { _ in |
| 160 | image.draw(in: CGRect(x: (canvas.width - size.width) / 2, y: (canvas.height - size.height) / 2, width: size.width, height: size.height)) |
| 161 | }.withRenderingMode(.alwaysTemplate) |
| 162 | } |
| 163 | |
| 164 | private func updateSelection() { |
| 165 | guard let item = itemsByTab[selectedTab], tabBar.items?.contains(item) == true else { return } |
| 166 | guard tabBar.selectedItem !== item else { return } |
| 167 | tabBar.selectedItem = item |
| 168 | } |
| 169 | |
| 170 | func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { |
| 171 | guard tabs.indices.contains(item.tag) else { return } |
| 172 | onSelectionChange(["tab": tabs[item.tag]]) |
| 173 | } |
| 174 | } |