|
| 1 | +package expo.modules.t3nativecontrols |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.view.ViewGroup |
| 5 | +import androidx.compose.foundation.layout.WindowInsets |
| 6 | +import androidx.compose.foundation.layout.Spacer |
| 7 | +import androidx.compose.foundation.layout.fillMaxSize |
| 8 | +import androidx.compose.material3.Icon |
| 9 | +import androidx.compose.material3.MaterialTheme |
| 10 | +import androidx.compose.material3.NavigationBar |
| 11 | +import androidx.compose.material3.NavigationBarItem |
| 12 | +import androidx.compose.material3.NavigationBarItemDefaults |
| 13 | +import androidx.compose.material3.NavigationRail |
| 14 | +import androidx.compose.material3.NavigationRailItem |
| 15 | +import androidx.compose.material3.NavigationRailItemDefaults |
| 16 | +import androidx.compose.material3.Text |
| 17 | +import androidx.compose.material3.darkColorScheme |
| 18 | +import androidx.compose.material3.lightColorScheme |
| 19 | +import androidx.compose.runtime.getValue |
| 20 | +import androidx.compose.runtime.mutableStateOf |
| 21 | +import androidx.compose.runtime.setValue |
| 22 | +import androidx.compose.ui.Modifier |
| 23 | +import androidx.compose.ui.graphics.Color |
| 24 | +import androidx.compose.ui.platform.ComposeView |
| 25 | +import androidx.compose.ui.platform.ViewCompositionStrategy |
| 26 | +import androidx.compose.ui.res.painterResource |
| 27 | +import androidx.compose.ui.unit.sp |
| 28 | +import expo.modules.kotlin.AppContext |
| 29 | +import expo.modules.kotlin.modules.Module |
| 30 | +import expo.modules.kotlin.modules.ModuleDefinition |
| 31 | +import expo.modules.kotlin.viewevent.EventDispatcher |
| 32 | +import expo.modules.kotlin.views.ExpoView |
| 33 | + |
| 34 | +class T3WorkspaceNavigationModule : Module() { |
| 35 | + override fun definition() = ModuleDefinition { |
| 36 | + Name("T3WorkspaceNavigation") |
| 37 | + View(T3WorkspaceNavigationView::class) { |
| 38 | + Prop("selectedTab") { view: T3WorkspaceNavigationView, value: String -> view.selectedTab = value } |
| 39 | + Prop("rail") { view: T3WorkspaceNavigationView, value: Boolean -> view.rail = value } |
| 40 | + Prop("dark") { view: T3WorkspaceNavigationView, value: Boolean -> view.dark = value } |
| 41 | + Prop("textScale") { view: T3WorkspaceNavigationView, value: Float -> view.textScale = value } |
| 42 | + Prop("containerColor") { view: T3WorkspaceNavigationView, value: Int -> view.containerColor = value } |
| 43 | + Prop("selectedColor") { view: T3WorkspaceNavigationView, value: Int -> view.selectedColor = value } |
| 44 | + Prop("indicatorColor") { view: T3WorkspaceNavigationView, value: Int -> view.indicatorColor = value } |
| 45 | + Prop("unselectedColor") { view: T3WorkspaceNavigationView, value: Int -> view.unselectedColor = value } |
| 46 | + Events("onTabSelected") |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** One Material3 selection surface moves from below the list to its leading edge. */ |
| 52 | +class T3WorkspaceNavigationView(context: Context, appContext: AppContext) : ExpoView(context, appContext) { |
| 53 | + // Compose must remeasure native children when Yoga resizes the folded window. |
| 54 | + override val shouldUseAndroidLayout = true |
| 55 | + |
| 56 | + private val onTabSelected by EventDispatcher() |
| 57 | + var selectedTab by mutableStateOf("threads") |
| 58 | + var rail by mutableStateOf(false) |
| 59 | + var dark by mutableStateOf(false) |
| 60 | + var textScale by mutableStateOf(1f) |
| 61 | + var containerColor by mutableStateOf(0) |
| 62 | + var selectedColor by mutableStateOf(0) |
| 63 | + var indicatorColor by mutableStateOf(0) |
| 64 | + var unselectedColor by mutableStateOf(0) |
| 65 | + |
| 66 | + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { |
| 67 | + // Fabric measures the replacement bar before attaching it on a fold. |
| 68 | + // Compose needs an attached window to obtain its lifecycle recomposer. |
| 69 | + if (!isAttachedToWindow) { |
| 70 | + setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)) |
| 71 | + return |
| 72 | + } |
| 73 | + super.onMeasure(widthMeasureSpec, heightMeasureSpec) |
| 74 | + } |
| 75 | + |
| 76 | + override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { |
| 77 | + if (isAttachedToWindow) super.onLayout(changed, left, top, right, bottom) |
| 78 | + } |
| 79 | + |
| 80 | + override fun onAttachedToWindow() { |
| 81 | + super.onAttachedToWindow() |
| 82 | + requestLayout() |
| 83 | + } |
| 84 | + |
| 85 | + init { |
| 86 | + addView(ComposeView(context).apply { |
| 87 | + setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnDetachedFromWindowOrReleasedFromPool) |
| 88 | + setContent { |
| 89 | + MaterialTheme(colorScheme = if (dark) darkColorScheme() else lightColorScheme()) { |
| 90 | + val tabs = listOf("threads", "pullRequests") |
| 91 | + if (rail) { |
| 92 | + NavigationRail( |
| 93 | + modifier = Modifier.fillMaxSize(), |
| 94 | + containerColor = Color(containerColor), |
| 95 | + windowInsets = WindowInsets(0), |
| 96 | + ) { |
| 97 | + Spacer(Modifier.weight(1f)) |
| 98 | + tabs.forEach { tab -> |
| 99 | + NavigationRailItem( |
| 100 | + selected = selectedTab == tab, |
| 101 | + onClick = { onTabSelected(mapOf("tab" to tab)) }, |
| 102 | + icon = { TabIcon(tab) }, |
| 103 | + label = { Text(if (tab == "threads") "Chats" else "PRs", fontSize = (12 * textScale).sp) }, |
| 104 | + colors = NavigationRailItemDefaults.colors( |
| 105 | + selectedIconColor = Color(selectedColor), |
| 106 | + selectedTextColor = Color(selectedColor), |
| 107 | + indicatorColor = Color(indicatorColor), |
| 108 | + unselectedIconColor = Color(unselectedColor), |
| 109 | + unselectedTextColor = Color(unselectedColor), |
| 110 | + ), |
| 111 | + ) |
| 112 | + } |
| 113 | + Spacer(Modifier.weight(1f)) |
| 114 | + } |
| 115 | + } else { |
| 116 | + NavigationBar( |
| 117 | + modifier = Modifier.fillMaxSize(), |
| 118 | + containerColor = Color(containerColor), |
| 119 | + tonalElevation = androidx.compose.ui.unit.Dp(0f), |
| 120 | + windowInsets = WindowInsets(0), |
| 121 | + ) { |
| 122 | + tabs.forEach { tab -> |
| 123 | + NavigationBarItem( |
| 124 | + selected = selectedTab == tab, |
| 125 | + onClick = { onTabSelected(mapOf("tab" to tab)) }, |
| 126 | + icon = { TabIcon(tab) }, |
| 127 | + label = { Text(if (tab == "threads") "Chats" else "Pull requests", fontSize = (12 * textScale).sp) }, |
| 128 | + colors = NavigationBarItemDefaults.colors( |
| 129 | + selectedIconColor = Color(selectedColor), |
| 130 | + selectedTextColor = Color(selectedColor), |
| 131 | + indicatorColor = Color(indicatorColor), |
| 132 | + unselectedIconColor = Color(unselectedColor), |
| 133 | + unselectedTextColor = Color(unselectedColor), |
| 134 | + ), |
| 135 | + ) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + }, LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) |
| 142 | + } |
| 143 | + |
| 144 | + @androidx.compose.runtime.Composable |
| 145 | + private fun TabIcon(tab: String) { |
| 146 | + Icon( |
| 147 | + painter = painterResource(if (tab == "threads") R.drawable.t3_chats else R.drawable.t3_pull_requests), |
| 148 | + contentDescription = if (tab == "threads") "Chats" else "Pull requests", |
| 149 | + ) |
| 150 | + } |
| 151 | +} |
0 commit comments