Skip to content

JetBrains/intellij-community / .claude/skills/compose-ui

compose-ui

Verified

Write or audit Compose Desktop UI state, effects, and boundaries.

FileKindSize
OWNERSHIPother20 B
SKILL.mdskill md637 B
architecture/GUIDE.mdmarkdown5.7 KB
architecture/evals/evals.jsonconfig6.5 KB
architecture/references/animation.mdmarkdown11.3 KB
architecture/references/component-api-lists-performance.mdmarkdown4.1 KB
architecture/references/desktop-seams-review-output.mdmarkdown1.5 KB
architecture/references/effects.mdmarkdown1.7 KB
architecture/references/state-boundaries.mdmarkdown5.4 KB
architecture/skill-source.jsonconfig2.0 KB
audit/GUIDE.mdmarkdown14.1 KB
audit/evals/evals.jsonconfig18.1 KB
audit/references/api-hygiene.mdmarkdown8.8 KB
audit/references/audit-search-patterns.mdmarkdown3.9 KB
audit/references/boundary-violations.mdmarkdown3.3 KB
audit/references/effects/derived-state-of-guide.mdmarkdown2.2 KB
audit/references/effects/effect-selection-guide.mdmarkdown2.7 KB
audit/references/effects/infinite-loop-patterns.mdmarkdown2.6 KB
audit/references/effects/overview.mdmarkdown7.7 KB
audit/references/lazy-layouts/animate-item.mdmarkdown2.0 KB
audit/references/lazy-layouts/cache-window-and-prefetch.mdmarkdown3.2 KB
audit/references/lazy-layouts/desktop-scroll-measurement.mdmarkdown2.0 KB
audit/references/lazy-layouts/keys-and-content-type.mdmarkdown2.2 KB
audit/references/lazy-layouts/nested-lazy-layouts.mdmarkdown1.6 KB
audit/references/lazy-layouts/overview.mdmarkdown7.5 KB
audit/references/measurement/benchmarking-methodology.mdmarkdown1.9 KB
audit/references/measurement/overview.mdmarkdown5.5 KB
audit/references/measurement/profiler-setup.mdmarkdown2.5 KB
audit/references/modifiers/custom-modifier-node.mdmarkdown3.4 KB
audit/references/modifiers/modifier-order.mdmarkdown2.1 KB
audit/references/modifiers/overview.mdmarkdown7.4 KB
audit/references/modifiers/phase-deferral.mdmarkdown2.6 KB
audit/references/reporting-guidance.mdmarkdown2.3 KB
audit/references/stability/ci-stability-gate.mdmarkdown2.0 KB
audit/references/stability/desktop-measurement.mdmarkdown2.2 KB
audit/references/stability/overview.mdmarkdown7.6 KB
audit/references/stability/reading-classes-txt.mdmarkdown2.1 KB
audit/references/stability/reading-composables-txt.mdmarkdown1.9 KB
audit/references/stability/stabilising-types.mdmarkdown3.1 KB
audit/references/stability/stability-config-syntax.mdmarkdown1.6 KB
audit/references/stability/stability-inference-algorithm.mdmarkdown3.4 KB
audit/references/stability/strong-skipping-escape-hatches.mdmarkdown2.6 KB
audit/references/subcomposition/desktop-subcomposition-measurement.mdmarkdown1.7 KB
audit/references/subcomposition/overview.mdmarkdown7.2 KB
audit/references/subcomposition/subcomposition-decision-tree.mdmarkdown1.8 KB
audit/references/subcomposition/swing-panel-subcomposition.mdmarkdown1.8 KB
audit/references/tracing-setup.mdmarkdown5.3 KB
audit/references/tracing-usage.mdmarkdown6.3 KB
audit/skill-source.jsonconfig2.5 KB

audit/references/lazy-layouts/overview.md

View on GitHub
# Compose Lazy Layouts — Scroll Performance and Correctness

Measure before and after every lazy-layout optimisation. If scrolling is smooth, the list is fast enough; focus only on
visible jank, state loss, or flicker.

> **Routing:** Use this workflow when an audit finding points to lazy-list scroll jank, items
> losing state on scroll, flicker on insert/remove, or unexpected recompositions during scroll.

## Workflow

### Phase 1 — Measure

- [ ] **1. Reproduce the user journey.** Scroll at normal speed, then fast-fling. Watch for jank,
  flicker, or state loss.

- [ ] **2. Add temporary logging to item composables.**

```kotlin
LazyColumn {
    items(data, key = { it.id }) { item ->
        SideEffect { println("[Lazy] item ${item.id} recomposed") }
        ItemRow(item)
    }
}
```

If every item logs on every scroll tick, you have a recomposition problem. If only newly-visible
items log, behaviour is correct.

- [ ] **3. Profile scroll CPU.** Attach YourKit or run with JFR to check whether `ComposerKt` or `SnapshotStateKt`
  consumes significant CPU time during scroll.

For Desktop-specific measurement, read
[desktop-scroll-measurement.md](desktop-scroll-measurement.md).

### Phase 2 — Diagnose

- [ ] **4. Check for missing `key`.** Without `key`, Compose uses positional identity. On
  insert/remove/reorder, every item after the change point is treated as new.

- [ ] **5. Check for missing `contentType`.** Without `contentType`, items with different layouts
  cannot reuse each other's slots, raising bind/unbind overhead.

- [ ] **6. Check for capturing lambdas inside `items { }`.** `items { }` is a `LazyListScope`
  extension, not a `@Composable` function. Strong Skipping does **not** auto-remember lambdas here.

- [ ] **7. Check for nested lazy layouts.** Each nested list needs its own keys and should not be
  more than 2 levels deep.

- [ ] **8. Check scroll state retention.** If the list resets to top on tab switches or
  navigation, `LazyListState` is not being kept at the right scope.

For key/contentType deep-dive, read [keys-and-content-type.md](keys-and-content-type.md).

### Phase 3 — Fix

- [ ] **9. Add stable, unique `key`.** The key must be stable across recompositions and unique
  within the list. `id` fields are ideal; `hashCode()` or `index` are not.

```kotlin
LazyColumn {
    items(data, key = { it.id }) { item -> ItemRow(item) }
}
```

- [ ] **10. Add `contentType` when items have different layouts.**

```kotlin
LazyColumn {
    items(items = data, key = { it.id }, contentType = { it::class }) { item ->
        when (item) {
            is Header -> HeaderItem(item)
            is Body -> BodyItem(item)
        }
    }
}
```

- [ ] **11. Stabilise lambda parameters.** Use method references or hoist lambdas outside
  `items`.

```kotlin
// WRONG — new lambda every recompose
items(data, key = { it.id }) { item -> ItemRow(item, onClick = { vm.select(item.id) }) }

// RIGHT — method reference is stable
items(data, key = { it.id }) { item -> ItemRow(item, onClick = vm::select) }
```

- [ ] **12. Use `LazyLayoutCacheWindow` for expensive items.** Pass it through
  `rememberLazyListState` — there is no modifier API for the cache window.

```kotlin
@OptIn(ExperimentalFoundationApi::class)
val listState = rememberLazyListState(
    cacheWindow = LazyLayoutCacheWindow(ahead = 200.dp, behind = 100.dp)
)
LazyColumn(state = listState) { /* ... */ }
```

`LazyLayoutCacheWindow` is an interface; factory takes `Dp` distances. A fraction-based variant
also exists: `LazyLayoutCacheWindow(aheadFraction = 0.5f, behindFraction = 0.25f)`. The API is
experimental and may change; verify against the current KDoc.

For cache window and prefetch details, read [cache-window-and-prefetch.md](cache-window-and-prefetch.md).

- [ ] **13. Use `Modifier.animateItem()` for insert/remove animations.**

```kotlin
@OptIn(ExperimentalFoundationApi::class)
LazyColumn {
    items(data, key = { it.id }) { item ->
        ItemRow(modifier = Modifier.animateItem(), item = item)
    }
}
```

For animation patterns and pitfalls, read [animate-item.md](animate-item.md).

- [ ] **14. Handle nested lazy layouts carefully.**

For nested layout rules, read [nested-lazy-layouts.md](nested-lazy-layouts.md).

### Phase 4 — Verify

- [ ] **15. Re-run the logging test.** Only newly-visible items should log during scroll.
- [ ] **16. Re-run the profiler.** CPU during scroll should drop.
- [ ] **17. Test state retention.** Scroll to item 50, scroll away, back — item 50 retains state.
- [ ] **18. Test insert/remove/reorder.** Items should not flicker or jump.

## Patterns

### Keys prevent state loss

```kotlin
// WRONG — checkboxes reset when scrolled out and back
LazyColumn { items(todos) { todo -> Checkbox(checked = todo.done, ...) } }

// RIGHT — state survives scroll
LazyColumn { items(todos, key = { it.id }) { todo -> Checkbox(checked = todo.done, ...) } }
```

### contentType improves recycling

```kotlin
// WRONG — Header and Body share slots, causing measure thrash
LazyColumn { items(mixed) { item -> when (item) { is Header -> ...; is Body -> ... } } }

// RIGHT — separate pools, no thrash
LazyColumn {
    items(mixed, key = { it.id }, contentType = { it::class }) { item ->
        when (item) { is Header -> HeaderUi(item); is Body -> BodyUi(item) }
    }
}
```

### Hoist LazyListState to survive navigation

```kotlin
// WRONG — resets on every navigation
@Composable fun ChatScreen() {
    val listState = rememberLazyListState()
    LazyColumn(state = listState) { /* ... */ }
}

// RIGHT — hoist into the presenter or a long-lived state holder and pass it down
@Composable fun ChatScreen(listState: LazyListState) {
    LazyColumn(state = listState) { /* ... */ }
}
// In the presenter or coordinator: val listState = LazyListState()
```

## Mandatory rules

- **MUST** provide `key` for every `items`/`itemsIndexed` call where the data set can change.
  Exception: truly static lists that never mutate.
- **MUST** provide `contentType` when a list contains items with different layout structures.
- **MUST NOT** use `index` as a `key` — on insert/remove every index shifts and every item is new.
- **MUST NOT** use `hashCode()` as a key unless guaranteed unique and stable.
- **MUST** stabilise lambdas passed into `items { }` blocks.
- **MUST NOT** deeply nest lazy layouts (>2 levels).
- **MUST** measure before adding `LazyLayoutCacheWindow`. It increases memory.
- **MUST** use `Modifier.animateItem()` on the **item root**, not on an inner child.

## Verification checklist

- [ ] Every `items`/`itemsIndexed` call has a `key` (or is explicitly static).
- [ ] Lists with mixed layouts have `contentType`.
- [ ] No capturing lambdas inside `items { }` blocks.
- [ ] Logging confirms only newly-visible items recompose during scroll.
- [ ] State (focus, selection, scroll position) survives scroll-out-and-back.
- [ ] Insert/remove/reorder does not cause flicker or jump.
- [ ] If `LazyLayoutCacheWindow` is used, memory was measured before and after.
- [ ] If `animateItem()` is used, animations run smoothly.

## References

- [keys-and-content-type.md](keys-and-content-type.md) — key selection, contentType pools
- [cache-window-and-prefetch.md](cache-window-and-prefetch.md) — `LazyLayoutCacheWindow`, memory trade-offs
- [animate-item.md](animate-item.md) — `Modifier.animateItem()` placement, glitch avoidance
- [nested-lazy-layouts.md](nested-lazy-layouts.md) — nesting limits, carousel patterns
- [desktop-scroll-measurement.md](desktop-scroll-measurement.md) — logging, CPU profiling on Desktop
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall