Ui ux engineering

From BryantChi/Android-Skills · View interactive version

Ui ux engineering is an AI agent skill from the GitHub repository BryantChi/Android-Skills. Compose UI 工程化 — Material 3 Expressive、Edge-to-Edge (API 35)、Adaptive (List-Detail / Supporting Pane)、Spacing/Typography token、Predictive Back 動畫、Compose Stability/Compiler Metrics、Haptic Feedback、Espresso Accessibility 自動化,產出可長期維護的 Design System

How to use this skill

Skill file

UI/UX Engineering

Instructions

當問題涉及 Compose UI 架構、Design System、複雜互動模式、Edge-to-Edge、Adaptive、a11y 時載入。本 skill 不處理導航(→ @navigation_patterns)、不處理效能 profiling(→ @deep_performance_tuning)。Material 3 Expressive 的 dynamic color 與 motion token 為 2026 預設。

When to Use

When NOT to Use

Example Prompts

Workflow

  1. Token 層:Color / Typography / Spacing / Shape / Motion 五類 token 定義。
  2. Theme:MaterialTheme + Expressive 擴充 + CompositionLocalProvider 提供自家 token。
  3. Component 層:基於 token 封裝 Button/Card/TextField 等基礎元件。
  4. Pattern 層:Adaptive、Collapsing、Shared Element、List-Detail。
  5. a11y + Edge-to-Edge 收尾,Espresso Accessibility CI gate。

Practical Notes (2026-04)

| 元件 | 推薦 |

|------|------|

| Material 3 | 1.4.x(含 Expressive) |

| androidx.compose.material3.adaptive | 1.0+ |

| Dynamic Color | Android 12+ 啟用、低版本 fallback |

| Edge-to-Edge | targetSdk 35 強制 |

| Predictive Back | 視覺由本 skill;路由由 navigation skill |

| Haptic | HapticFeedback.performHapticFeedback(HapticFeedbackType.Confirm) |

| Accessibility 測試 | Espresso Accessibility + Compose semantics test |

Minimal Template

Token: colors / typography / spacing / shape / motion
Theme: AppTheme(dynamicColor = true) with fallback
Components: Button, Card, TextField, EmptyState, LoadingState, ErrorState
Patterns: AdaptivePane, CollapsingTopBar, SharedElement, ListDetailLayout
a11y: contentDescription audit, 48dp touch target, contrast >= 4.5:1
驗收: Quick Checklist

Design System:Token 與 Theme

Color(Material 3 Expressive + Dynamic Color)

@Composable
fun AppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    dynamicColor: Boolean = true,
    content: @Composable () -> Unit,
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val ctx = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(ctx) else dynamicLightColorScheme(ctx)
        }
        darkTheme -> ExpressiveDarkColorScheme  // brand fallback
        else -> ExpressiveLightColorScheme
    }

    CompositionLocalProvider(
        LocalSpacing provides Spacing(),
        LocalShape provides AppShapes(),
        LocalMotion provides MotionTokens(),
    ) {
        MaterialTheme(
            colorScheme = colorScheme,
            typography = ExpressiveTypography,
            shapes = MaterialTheme.shapes,
            content = content,
        )
    }
}

Expressive palette 含 surfaceContainerHighest、surfaceContainerLow、onSurfaceVariant,比舊 M3 顏色更層次。

Spacing / Shape / Motion Token

@Immutable data class Spacing(
    val xxs: Dp = 2.dp,
    val xs: Dp = 4.dp,
    val sm: Dp = 8.dp,
    val md: Dp = 16.dp,
    val lg: Dp = 24.dp,
    val xl: Dp = 32.dp,
    val xxl: Dp = 48.dp,
)
val LocalSpacing = staticCompositionLocalOf { Spacing() }

@Immutable data class MotionTokens(
    val short: AnimationSpec<Float> = tween(150, easing = FastOutSlowInEasing),
    val medium: AnimationSpec<Float> = tween(300, easing = FastOutSlowInEasing),
    val emphasized: AnimationSpec<Float> = tween(500, easing = EmphasizedEasing),
)
val LocalMotion = staticCompositionLocalOf { MotionTokens() }

Figma Token Sync(版本化)

config/design-tokens/
├── tokens.v3.json        # Figma 匯出(Style Dictionary 格式)
└── README.md             # 紀錄 Figma 檔案 URL + commit hash
// build-logic plugin: 把 tokens.json 編譯為 Kotlin object
// gradle task: ./gradlew generateDesignTokens

每次 Figma 改動 → 提 PR 改 tokens.v3.json + 更新 commit hash + 重新 codegen。

Edge-to-Edge(Android 15 強制)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    enableEdgeToEdge()    // androidx.activity:activity-compose
    setContent { AppTheme { App() } }
}
@Composable
fun App() {
    Scaffold(
        contentWindowInsets = WindowInsets.safeDrawing,
    ) { inner ->
        NavHost(
            navController = rememberNavController(),
            startDestination = HomeRoute,
            modifier = Modifier.padding(inner),
        ) { /* ... */ }
    }
}

底部 BottomBar / FAB 用 Modifier.windowInsetsPadding(WindowInsets.navigationBars):

BottomAppBar(
    modifier = Modifier.windowInsetsPadding(WindowInsets.navigationBars),
) { /* actions */ }

System bar 顏色:

WindowCompat.getInsetsController(window, window.decorView).apply {
    isAppearanceLightStatusBars = !darkTheme
    isAppearanceLightNavigationBars = !darkTheme
}

Adaptive(List-Detail / Supporting Pane)

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
fun ProductListDetail() {
    val navigator = rememberListDetailPaneScaffoldNavigator<String>()

    BackHandler(navigator.canNavigateBack()) { navigator.navigateBack() }

    ListDetailPaneScaffold(
        directive = navigator.scaffoldDirective,
        value = navigator.scaffoldValue,
        listPane = {
            AnimatedPane {
                ProductList(onClick = { id -> navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, id) })
            }
        },
        detailPane = {
            AnimatedPane {
                navigator.currentDestination?.content?.let { id -> ProductDetail(id) }
                    ?: EmptyDetailPane()
            }
        },
    )
}

寬度 < 600dp(手機)自動 single-pane;600-840dp 並排顯示;> 840dp 含 supporting pane。

Complex UI Patterns

Collapsing Top App Bar(Material 3 內建)

val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()

Scaffold(
    modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
    topBar = {
        LargeTopAppBar(
            title = { Text("Products") },
            scrollBehavior = scrollBehavior,
        )
    },
) { padding ->
    LazyColumn(contentPadding = padding) { /* items */ }
}

不需要再手寫 nested scroll 計算。

Shared Element Transition

SharedTransitionLayout {
    AnimatedContent(targetState = screen, label = "shared") { target ->
        when (target) {
            is Screen.List -> ProductList(
                onClick = { /* ... */ },
                imageModifier = { id ->
                    Modifier.sharedElement(
                        rememberSharedContentState("image-$id"),
                        animatedVisibilityScope = this@AnimatedContent,
                    )
                },
            )
            is Screen.Detail -> ProductDetail(
                imageModifier = Modifier.sharedElement(
                    rememberSharedContentState("image-${target.id}"),
                    animatedVisibilityScope = this@AnimatedContent,
                ),
            )
        }
    }
}

Predictive Back 視覺

導航與 cancellation 路徑見 @navigation_patterns;視覺端使用 PredictiveBackHandler 接 progress 0..1 動畫場景:

val offset = remember { Animatable(0f) }
PredictiveBackHandler { progress ->
    progress.collect { offset.snapTo(it.progress * 200f) }
}
Box(Modifier.offset { IntOffset(offset.value.toInt(), 0) }) { content() }

Compose Stability(避免不必要重組)

啟用 Compiler Metrics(在 @coding_style_conventions 已介紹),本 skill 教怎麼讀報告與調整。

# build/compose-reports/MyComposable-composables.txt
restartable scheme("[androidx.compose.ui.UiComposable]") fun MyCard(
  stable user: User
  unstable items: List<Item>     ← 問題
  stable onClick: Function0<Unit>
)

修法:

  1. 改用 immutable collection:androidx.compose.runtime:runtime 的 kotlinx.collections.immutable.ImmutableList。
  2. 資料類加 @Immutable 或 @Stable(保證契約)。
  3. stability config 標第三方型別為 stable。
@Immutable data class UiItem(val id: String, val title: String)

@Composable
fun MyCard(user: User, items: ImmutableList<UiItem>, onClick: () -> Unit) { /* ... */ }

Strong Skipping(Compose Compiler 1.5.4+ 預設啟用)會在參數 instance 不變時 skip 即使 unstable。

Haptic Feedback(Material 3 Expressive 整合)

val haptic = LocalHapticFeedback.current

Button(onClick = {
    haptic.performHapticFeedback(HapticFeedbackType.Confirm)
    onConfirm()
}) { Text("Submit") }

HapticFeedbackType 含 Confirm、Reject、GestureEnd、SegmentTick。系統會依裝置強度調整。

Accessibility 自動化

Compose Semantics 測試

composeTestRule.setContent { AppTheme { ProductCard(product = sample, onClick = {}) } }
composeTestRule.onNodeWithContentDescription("商品 ${sample.name}").assertHasClickAction()
composeTestRule.onAllNodes(isFocusable()).assertCountEquals(3)

Espresso Accessibility(整體掃描)

@Before fun enableA11y() {
    AccessibilityChecks.enable()
        .setRunChecksFromRootView(true)
        .setSuppressingResultMatcher(
            allOf(
                matchesViews(withId(R.id.decorative_image)),
                matchesCheckNames(`is`("DuplicateClickableBoundsCheck")),
            )
        )
}

@Test fun productScreen_passesAccessibility() {
    onView(withId(R.id.compose_view)).check(matches(isDisplayed()))
    // AccessibilityChecks 會在每次 ViewAction 時驗證
}

CI 跑 ./gradlew :app:connectedDebugAndroidTest -PaccessibilityChecks=true 為 a11y gate。

a11y Checklist

Cross-Skill References

Quick Checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.