11import ExpoModulesCore
22import UIKit
33
4- final class T3SidebarTabBarView : ExpoView , UITabBarDelegate {
4+ private final class WorkspaceTabBar : UITabBar {
5+ var onContentLayout : ( ( CGRect ) -> Void ) ?
6+
7+ override func layoutSubviews ( ) {
8+ super . layoutSubviews ( )
9+ let row = tabButton ( in : self ) . map { convert ( $0 . bounds , from : $0 ) }
10+ onContentLayout ? ( row ?? safeAreaLayoutGuide . layoutFrame )
11+ }
12+
13+ override func safeAreaInsetsDidChange ( ) {
14+ super . safeAreaInsetsDidChange ( )
15+ invalidateIntrinsicContentSize ( )
16+ }
17+
18+ private func tabButton ( in view : UIView ) -> UIView ? {
19+ for subview in view . subviews where ! subview . isHidden {
20+ if subview is UIControl , subview . accessibilityIdentifier ? . hasPrefix ( " workspace-tab- " ) == true {
21+ return subview
22+ }
23+ if let button = tabButton ( in : subview ) { return button }
24+ }
25+ return nil
26+ }
27+ }
28+
29+ final class T3SidebarTabBarView : ExpoView , UITabBarDelegate , UIToolbarDelegate {
530 let onSelectionChange = EventDispatcher ( )
6- private let tabBar = UITabBar ( )
7- var selectedIndex = 0 { didSet { updateSelection ( ) } }
31+ let onCompose = EventDispatcher ( )
32+ private let tabBar = WorkspaceTabBar ( )
33+ private let composeButton = UIButton ( type : . system )
34+ private let composeToolbar = UIToolbar ( )
35+ private lazy var tabBarTrailing = tabBar . trailingAnchor . constraint ( equalTo : trailingAnchor )
36+ private lazy var composeWidth = composeButton . widthAnchor . constraint ( equalToConstant : 60 )
37+ private lazy var composeHeight = composeButton . heightAnchor . constraint ( equalToConstant : 60 )
38+ private var itemsByTab : [ String : UITabBarItem ] = [ : ]
39+ var tabs = [ " threads " , " pullRequests " , " usage " , " settings " ] {
40+ didSet { if tabs != oldValue { updateItems ( ) } }
41+ }
42+ var selectedTab = " threads " { didSet { updateSelection ( ) } }
843 var selectedColor : UIColor = . systemBlue { didSet { tabBar . tintColor = selectedColor } }
44+ var compact = false {
45+ didSet {
46+ configureCompose ( )
47+ tabBarTrailing . constant = showsCompose ? ( compact ? - 64 : - 76 ) : 0
48+ tabBar . setNeedsLayout ( )
49+ }
50+ }
51+ var showsCompose = false {
52+ didSet {
53+ composeToolbar . isHidden = ! showsCompose
54+ tabBarTrailing . constant = showsCompose ? ( compact ? - 64 : - 76 ) : 0
55+ setNeedsLayout ( )
56+ }
57+ }
958 var appearance = " light " {
1059 didSet { overrideUserInterfaceStyle = appearance == " dark " ? . dark : . light }
1160 }
1261
1362 required init ( appContext : AppContext ? = nil ) {
1463 super . init ( appContext : appContext )
15- let iconBundle = Bundle ( for : Self . self ) . url ( forResource : " T3NativeControlIcons " , withExtension : " bundle " ) . flatMap ( Bundle . init ( url : ) )
16- let pullRequestIcon = UIImage ( named : " pull-request " , in : iconBundle , compatibleWith : nil ) ? . withRenderingMode ( . alwaysTemplate )
17- tabBar . items = [
18- UITabBarItem ( title : " Threads " , image : tabIcon ( UIImage ( systemName : " text.bubble " ) ) , tag : 0 ) ,
19- UITabBarItem ( title : " Pulls " , image : tabIcon ( pullRequestIcon ) , tag : 1 ) ,
20- ]
21- tabBar . items ? . last ? . accessibilityLabel = " Pull requests "
22- tabBar . items ? . first ? . selectedImage = tabIcon ( UIImage ( systemName : " text.bubble.fill " ) )
2364 tabBar . itemPositioning = . fill
2465 tabBar . delegate = self
66+ tabBar . translatesAutoresizingMaskIntoConstraints = false
2567 addSubview ( tabBar )
26- updateSelection ( )
68+ NSLayoutConstraint . activate ( [
69+ tabBar . leadingAnchor . constraint ( equalTo : leadingAnchor ) ,
70+ tabBarTrailing ,
71+ tabBar . bottomAnchor . constraint ( equalTo : bottomAnchor ) ,
72+ ] )
73+ // UIKit's floating row changes with window geometry. Follow its laid-out items
74+ // instead of estimating their position from a second set of safe-area updates.
75+ tabBar . onContentLayout = { [ weak self ] frame in self ? . layoutCompose ( alongside : frame ) }
76+ composeButton . accessibilityLabel = " New chat "
77+ composeButton . addAction ( UIAction { [ weak self ] _ in self ? . onCompose ( ) } , for : . touchUpInside )
78+ NSLayoutConstraint . activate ( [ composeWidth , composeHeight ] )
79+ composeToolbar . items = [ UIBarButtonItem ( customView : composeButton ) ]
80+ composeToolbar . delegate = self
81+ composeToolbar . isHidden = true
82+ addSubview ( composeToolbar )
83+ configureCompose ( )
84+ prepareItems ( )
85+ updateItems ( )
2786 }
2887
29- override func layoutSubviews ( ) {
30- super . layoutSubviews ( )
31- tabBar . frame = bounds
88+ private func configureCompose ( ) {
89+ var configuration : UIButton . Configuration
90+ if #available( iOS 26 . 0 , * ) {
91+ configuration = compact ? . glass ( ) : . plain ( )
92+ composeToolbar . items ? . first ? . hidesSharedBackground = compact
93+ } else {
94+ configuration = . gray ( )
95+ let appearance = UIToolbarAppearance ( )
96+ appearance . configureWithTransparentBackground ( )
97+ composeToolbar . standardAppearance = appearance
98+ }
99+ configuration . cornerStyle = . capsule
100+ configuration . baseForegroundColor = . label
101+ configuration . image = UIImage ( systemName : " square.and.pencil " )
102+ configuration . preferredSymbolConfigurationForImage = UIImage . SymbolConfiguration ( pointSize : 24 )
103+ composeButton . configuration = configuration
104+ }
105+
106+ private func prepareItems ( ) {
107+ let iconBundle = Bundle ( for : Self . self ) . url ( forResource : " T3NativeControlIcons " , withExtension : " bundle " ) . flatMap ( Bundle . init ( url : ) )
108+ let pullRequestIcon = UIImage ( named : " pull-request " , in : iconBundle , compatibleWith : nil ) ? . withRenderingMode ( . alwaysTemplate )
109+ let labels = [ " threads " : " Chats " , " pullRequests " : " PRs " , " usage " : " Usage " , " settings " : " Settings " ]
110+ let symbols = [ " threads " : " text.bubble " , " usage " : " chart.bar.xaxis " , " settings " : " gearshape " ]
111+ for ( tab , label ) in labels {
112+ let icon = tab == " pullRequests " ? pullRequestIcon : UIImage ( systemName : symbols [ tab ] ?? " circle " )
113+ let item = UITabBarItem ( title : nil , image : tabIcon ( icon ) , tag : 0 )
114+ item . accessibilityLabel = label
115+ item . accessibilityIdentifier = " workspace-tab- \( tab ) "
116+ if tab == " threads " { item . selectedImage = tabIcon ( UIImage ( systemName : " text.bubble.fill " ) ) }
117+ itemsByTab [ tab ] = item
118+ }
119+ }
120+
121+ private func updateItems ( ) {
122+ tabBar . items = tabs . enumerated ( ) . compactMap { index , tab in
123+ guard let item = itemsByTab [ tab ] else { return nil }
124+ item . tag = index
125+ return item
126+ }
32127 updateSelection ( )
33128 }
34129
130+ private func layoutCompose ( alongside frame : CGRect ) {
131+ guard showsCompose , frame . height > 0 else { return }
132+ let row = convert ( frame , from : tabBar )
133+ let buttonHeight = row . height + ( compact ? 8 : 0 )
134+ composeWidth . constant = buttonHeight
135+ composeHeight . constant = buttonHeight
136+ let width = buttonHeight + 16
137+ let height = composeToolbar . sizeThatFits ( CGSize ( width : width , height : bounds . height ) ) . height
138+ composeToolbar . frame = CGRect ( x : bounds . width - 8 - width , y : row . midY - height / 2 , width : width , height : height )
139+ composeToolbar . layoutIfNeeded ( )
140+ }
141+
142+ override func hitTest ( _ point : CGPoint , with event : UIEvent ? ) -> UIView ? {
143+ if showsCompose , let target = composeButton . hitTest ( convert ( point , to : composeButton ) , with : event ) {
144+ return target
145+ }
146+ return tabBar . hitTest ( convert ( point , to : tabBar ) , with : event )
147+ }
148+
149+ func position ( for bar : UIBarPositioning ) -> UIBarPosition {
150+ . bottom
151+ }
152+
35153 override func didMoveToWindow ( ) {
36154 super . didMoveToWindow ( )
37155 updateSelection ( )
@@ -49,12 +167,13 @@ final class T3SidebarTabBarView: ExpoView, UITabBarDelegate {
49167 }
50168
51169 private func updateSelection ( ) {
52- guard let items = tabBar . items , items . indices . contains ( selectedIndex ) else { return }
53- guard tabBar . selectedItem !== items [ selectedIndex ] else { return }
54- tabBar . selectedItem = items [ selectedIndex ]
170+ guard let item = itemsByTab [ selectedTab ] , tabBar . items ? . contains ( item ) == true else { return }
171+ guard tabBar . selectedItem !== item else { return }
172+ tabBar . selectedItem = item
55173 }
56174
57175 func tabBar ( _ tabBar : UITabBar , didSelect item : UITabBarItem ) {
58- onSelectionChange ( [ " index " : item . tag ] )
176+ guard tabs . indices . contains ( item . tag ) else { return }
177+ onSelectionChange ( [ " tab " : tabs [ item . tag ] ] )
59178 }
60179}
0 commit comments