11import ExpoModulesCore
22import UIKit
33
4+ private final class WorkspaceTabBar : UITabBar {
5+ var onContentLayout : ( ( CGRect ) -> Void ) ?
6+
7+ override func layoutSubviews ( ) {
8+ super . layoutSubviews ( )
9+ let row = tabButtons ( in : self ) . reduce ( CGRect . null ) { $0 . union ( convert ( $1 . bounds , from : $1 ) ) }
10+ if ! row . isNull { onContentLayout ? ( row ) }
11+ }
12+
13+ override func safeAreaInsetsDidChange ( ) {
14+ super . safeAreaInsetsDidChange ( )
15+ invalidateIntrinsicContentSize ( )
16+ }
17+
18+ private func tabButtons ( in view : UIView ) -> [ UIView ] {
19+ view . subviews . filter { ! $0 . isHidden } . flatMap { subview in
20+ if subview is UIControl , subview . accessibilityIdentifier ? . hasPrefix ( " workspace-tab- " ) == true {
21+ return [ subview ]
22+ }
23+ return tabButtons ( in : subview )
24+ }
25+ }
26+ }
27+
428final class T3SidebarTabBarView : ExpoView , UITabBarDelegate {
529 let onSelectionChange = EventDispatcher ( )
6- private let tabBar = UITabBar ( )
7- var selectedIndex = 0 { didSet { updateSelection ( ) } }
30+ let onCompose = EventDispatcher ( )
31+ private let tabBar = WorkspaceTabBar ( )
32+ private let composeButton = UIButton ( type : . system )
33+ private lazy var tabBarTrailing = tabBar . trailingAnchor . constraint ( equalTo : trailingAnchor )
34+ private var itemsByTab : [ String : UITabBarItem ] = [ : ]
35+ var tabs = [ " threads " , " pullRequests " , " usage " , " settings " ] {
36+ didSet { if tabs != oldValue { updateItems ( ) } }
37+ }
38+ var selectedTab = " threads " { didSet { updateSelection ( ) } }
839 var selectedColor : UIColor = . systemBlue { didSet { tabBar . tintColor = selectedColor } }
40+ var compact = false {
41+ didSet {
42+ tabBarTrailing . constant = showsCompose ? ( compact ? - 64 : - 76 ) : 0
43+ tabBar . setNeedsLayout ( )
44+ }
45+ }
46+ var showsCompose = false {
47+ didSet {
48+ composeButton . isHidden = ! showsCompose
49+ tabBarTrailing . constant = showsCompose ? ( compact ? - 64 : - 76 ) : 0
50+ setNeedsLayout ( )
51+ }
52+ }
953 var appearance = " light " {
1054 didSet { overrideUserInterfaceStyle = appearance == " dark " ? . dark : . light }
1155 }
1256
1357 required init ( appContext : AppContext ? = nil ) {
1458 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 " ) )
2359 tabBar . itemPositioning = . fill
60+ // iPad's regular bar uses a compressed inline row even with fill positioning.
61+ // This floating sidebar control needs the same full-size items as a phone bar.
62+ if #available( iOS 17 . 0 , * ) {
63+ tabBar . traitOverrides . horizontalSizeClass = . compact
64+ }
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+ composeButton . isHidden = true
79+ addSubview ( composeButton )
80+ configureCompose ( )
81+ prepareItems ( )
82+ updateItems ( )
2783 }
2884
29- override func layoutSubviews ( ) {
30- super . layoutSubviews ( )
31- tabBar . frame = bounds
85+ private func configureCompose ( ) {
86+ var configuration : UIButton . Configuration
87+ if #available( iOS 26 . 0 , * ) {
88+ configuration = . glass ( )
89+ } else {
90+ configuration = . gray ( )
91+ }
92+ configuration . cornerStyle = . capsule
93+ configuration . baseForegroundColor = . label
94+ configuration . image = UIImage ( systemName : " square.and.pencil " )
95+ configuration . preferredSymbolConfigurationForImage = UIImage . SymbolConfiguration ( pointSize : 24 )
96+ composeButton . configuration = configuration
97+ }
98+
99+ private func prepareItems ( ) {
100+ let iconBundle = Bundle ( for : Self . self ) . url ( forResource : " T3NativeControlIcons " , withExtension : " bundle " ) . flatMap ( Bundle . init ( url : ) )
101+ let pullRequestIcon = UIImage ( named : " pull-request " , in : iconBundle , compatibleWith : nil ) ? . withRenderingMode ( . alwaysTemplate )
102+ let labels = [ " threads " : " Chats " , " pullRequests " : " PRs " , " usage " : " Usage " , " settings " : " Settings " ]
103+ let symbols = [ " threads " : " text.bubble " , " usage " : " chart.bar.xaxis " , " settings " : " gearshape " ]
104+ for ( tab , label ) in labels {
105+ let icon = tab == " pullRequests " ? pullRequestIcon : UIImage ( systemName : symbols [ tab ] ?? " circle " )
106+ let item = UITabBarItem ( title : nil , image : tabIcon ( icon ) , tag : 0 )
107+ item . accessibilityLabel = label
108+ item . accessibilityIdentifier = " workspace-tab- \( tab ) "
109+ if tab == " threads " { item . selectedImage = tabIcon ( UIImage ( systemName : " text.bubble.fill " ) ) }
110+ itemsByTab [ tab ] = item
111+ }
112+ }
113+
114+ private func updateItems ( ) {
115+ tabBar . items = tabs . enumerated ( ) . compactMap { index , tab in
116+ guard let item = itemsByTab [ tab ] else { return nil }
117+ item . tag = index
118+ return item
119+ }
32120 updateSelection ( )
33121 }
34122
123+ private func layoutCompose ( alongside frame : CGRect ) {
124+ guard showsCompose , frame . height > 0 else { return }
125+ let row = convert ( frame , from : tabBar )
126+ let size = row . height + 8
127+ composeButton . frame = CGRect ( x : row . maxX + 16 , y : row . midY - size / 2 , width : size , height : size )
128+ }
129+
130+ override func hitTest ( _ point : CGPoint , with event : UIEvent ? ) -> UIView ? {
131+ if showsCompose , let target = composeButton . hitTest ( convert ( point , to : composeButton ) , with : event ) {
132+ return target
133+ }
134+ return tabBar . hitTest ( convert ( point , to : tabBar ) , with : event )
135+ }
136+
35137 override func didMoveToWindow ( ) {
36138 super . didMoveToWindow ( )
37139 updateSelection ( )
@@ -49,12 +151,13 @@ final class T3SidebarTabBarView: ExpoView, UITabBarDelegate {
49151 }
50152
51153 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 ]
154+ guard let item = itemsByTab [ selectedTab ] , tabBar . items ? . contains ( item ) == true else { return }
155+ guard tabBar . selectedItem !== item else { return }
156+ tabBar . selectedItem = item
55157 }
56158
57159 func tabBar ( _ tabBar : UITabBar , didSelect item : UITabBarItem ) {
58- onSelectionChange ( [ " index " : item . tag ] )
160+ guard tabs . indices . contains ( item . tag ) else { return }
161+ onSelectionChange ( [ " tab " : tabs [ item . tag ] ] )
59162 }
60163}
0 commit comments