Skip to content

Promote built-in composition locals to top-level classes - #226

Merged
jonathanpeppers merged 2 commits into
mainfrom
jonathanpeppers/rename-locals-to-local
Jun 10, 2026
Merged

jonathanpeppers merged 2 commits into
mainfrom
jonathanpeppers/rename-locals-to-local

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Jun 10, 2026 •

Copy link
Copy Markdown
Owner

Promote the built-in composition locals (LocalContext, LocalConfiguration,
LocalLifecycleOwner, LocalResources, LocalView, LocalColorScheme) from
members on a Locals host class to top-level static classes in AndroidX.Compose.

Each one now wraps a private CompositionLocal<T> and exposes Current(IComposer)
plus Provides(T). Also renames CompositionLocal<T>.GetCurrent(IComposer) to
Current(IComposer) (which fixes a pre-existing broken <see cref="CompositionLocal{T}.Current"/>
XML doc reference the author had already half-written).

Motivation

This brings the C# read site as close to Kotlin's LocalContext.current shape
as the language allows. C# has no compiler plugin to thread $composer for us,
so the composer stays explicit — but everything else now matches:

// Kotlin
val ctx = LocalContext.current
// C# before
var ctx = Locals.LocalContext.GetCurrent(composer);   // "Locals.Local" stutters; "GetCurrent" is needlessly verbose

// C# after
var ctx = LocalContext.Current(composer);

The intermediate shape Local.LocalContext.Current(composer) (which an earlier
revision of this PR shipped) still stuttered. Dropping the host class entirely
removes the stutter without sacrificing any namespace hygiene — none of
LocalContext / LocalConfiguration / LocalLifecycleOwner / LocalResources
/ LocalView / LocalColorScheme collides with anything else in
AndroidX.Compose.

User-defined composition locals continue to use the
static readonly CompositionLocal<T> pattern. The call-site shape is identical
for built-in and user-defined locals — MyLocal.Current(composer) in both cases —
so there's no asymmetry to learn.

Considerations for review

A couple of tradeoffs worth a deliberate look — happy to revise either way:

  1. Discoverability. The previous Locals host class made all six locals
    findable through one symbol (and through using static AndroidX.Compose.Locals;).
    With them as top-level types, callers need to know the name
    (LocalContext, LocalLifecycleOwner, etc.) to find them. Probably fine —
    Kotlin developers already know these names, and IntelliSense surfaces them
    when typing Local. If we want a discoverability nudge, a "Built-in
    composition locals" section in docs/architecture.md would be the lowest-
    cost option (no code churn, no [Obsolete] aliases).

  2. First-party vs. third-party shape. Built-in locals are now top-level
    classes; user-defined locals are still static readonly CompositionLocal<T>
    fields on a host class. That's a shape asymmetry — first-party gets the
    cleaner read, third-party doesn't. The call site is identical
    (MyLocal.Current(composer)), but the declaration isn't. The asymmetry is
    intentional here: built-ins wrap a JNI peer field one-to-one and never
    re-bind, so the static-class wrapping is essentially free; user locals
    often live as fields on a theme/palette holder where a field is the
    natural form. Worth a sentence in docs if reviewers feel the asymmetry
    needs explaining.

No [Obsolete] alias

All affected entries lived in PublicAPI.Unshipped.txt, never Shipped.txt.
The repo is pre-1.0 and the README is explicit that the public API is not
stable. A deprecation cycle would just lock in dead code.

Files touched

Facade (src/Microsoft.AndroidX.Compose/):

  • Locals.cs — deleted (host class removed)
  • LocalContext.cs, LocalConfiguration.cs, LocalLifecycleOwner.cs,
    LocalResources.cs, LocalView.cs, LocalColorScheme.cs — added (one per built-in)
  • CompositionLocal.cs — GetCurrent → Current (method + 2 XML doc crefs, one previously broken)
  • CompositionLocalProvider.cs — XML doc example updated
  • ComposableNode.cs — XML doc cref updated
  • MaterialTheme.cs — internal callsite updated
  • PublicAPI.Unshipped.txt — old Locals entries out, 6 new class + 13 new method entries in (re-sorted; #nullable enable preserved)

Gallery (src/Microsoft.AndroidX.Compose.Gallery/Demos/):

  • LocalsMisc/BuiltInCompositionLocalsDemo.cs — added. Consolidated demo
    exercising LocalConfiguration (orientation + screen width — the canonical
    responsive-UI primitive in Compose), LocalView (haptic feedback on tap),
    and LocalResources (display density). Registered in Catalog.cs
    alongside the existing LocalsMisc demos.
  • LocalsMisc/LocalContextDemo.cs — 4 refs updated
  • LocalsMisc/ComposeViewInteropDemo.cs — 2 refs updated
  • LocalsMisc/CustomCompositionLocalDemo.cs — GetCurrent → Current on user-defined LocalPalette
  • StateEffectsAnimation/NullableMutableStateDemo.cs — GetCurrent → Current on user-defined LocalSelectedId

Demo coverage decisions

  • LocalContext — already demoed twice (LocalContextDemo,
    ComposeViewInteropDemo) and used internally by MaterialTheme. Ubiquitous
    in real Compose apps.
  • LocalConfiguration, LocalView, LocalResources — common in real
    Kotlin code (responsive layouts, haptics/IME/preview detection, resource
    lookups). Wired into the new consolidated demo.
  • LocalLifecycleOwner — intentionally not demoed. The binding wraps
    androidx.compose.ui.platform.LocalLifecycleOwner, which is deprecated
    upstream
    in favour of androidx.lifecycle.compose.LocalLifecycleOwner
    (note the #pragma warning disable CS0618 in LocalLifecycleOwner.cs).
    Follow-up: bind the non-deprecated peer, then add demo coverage.
  • LocalColorScheme — intentionally not demoed. App code reads the
    same value through MaterialTheme.ColorScheme / composer.ColorScheme(),
    both of which already have demo coverage. The direct accessor stays for
    parity with the Kotlin surface and for theme-aware library code.

Not touched (deliberate):

  • samples/ (Jetchat, JetNews, Reply), README.md, docs/architecture.md,
    docs/NOTES.md — no Locals or GetCurrent references in any of them;
    all three sample apps build clean against the renamed API without changes.
  • LocalsMisc/ folder — it's a demo category covering "CompositionLocal and
    miscellaneous" topics, not a reference to the renamed class.
  • Registry/Catalog.cs's "Locals, progress, image, icon" category description —
    freeform prose listing demo topics; "Local, progress, image, icon" reads oddly.
  • docs/api-coverage.md — auto-generated by scripts/api-comparison.cs;
    refreshes on next run.

Verification

  • dotnet test src/Microsoft.AndroidX.Compose.SourceGenerators.Tests — 130/130 pass.
  • dotnet build src/Microsoft.AndroidX.Compose — 0 warnings, 0 errors.
  • dotnet build src/Microsoft.AndroidX.Compose.Gallery — 0 warnings, 0 errors.
  • dotnet build samples/Jetchat/Jetchat.csproj — 0 warnings, 0 errors.
  • dotnet build samples/JetNews/JetNews.csproj — 0 warnings, 0 errors.
  • dotnet build samples/Reply/Reply.csproj — 0 warnings, 0 errors.

Co-authored-by: Copilot [email protected]

Copilot AI review requested due to automatic review settings June 10, 2026 00:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR renames two unshipped public API surfaces to better mirror Kotlin's composition-local idioms: the host class Locals → Local (singular prefix) and the read accessor CompositionLocal<T>.GetCurrent(IComposer) → .Current(IComposer). Combined, callsites now read Local.LocalContext.Current(composer) — closely matching Kotlin's LocalContext.current. Since all affected symbols are in PublicAPI.Unshipped.txt, no [Obsolete] alias is needed.

Changes:

  • Renamed the static class from Locals to Local (file renamed Locals.cs → Local.cs) with expanded XML documentation showing the idiomatic usage pattern.
  • Renamed CompositionLocal<T>.GetCurrent(IComposer) to .Current(IComposer) and updated all <see cref="…"/> references.
  • Updated all callsites across the facade, gallery demos, and PublicAPI.Unshipped.txt.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Microsoft.AndroidX.Compose/Local.cs Renamed class Locals → Local; expanded XML doc with code sample showing idiomatic callsite.
src/Microsoft.AndroidX.Compose/CompositionLocal.cs Renamed public method GetCurrent → Current; updated <see cref> in class-level docs.
src/Microsoft.AndroidX.Compose/ComposableNode.cs Updated <see cref> to Current(IComposer).
src/Microsoft.AndroidX.Compose/CompositionLocalProvider.cs Updated doc code sample Locals.LocalContext → Local.LocalContext.
src/Microsoft.AndroidX.Compose/MaterialTheme.cs Updated callsite to Local.LocalContext.Current(composer).
src/Microsoft.AndroidX.Compose/PublicAPI.Unshipped.txt Replaced all Locals entries with Local; replaced GetCurrent with Current.
src/Microsoft.AndroidX.Compose.Gallery/Demos/LocalsMisc/LocalContextDemo.cs Updated callsites and demo descriptions.
src/Microsoft.AndroidX.Compose.Gallery/Demos/LocalsMisc/ComposeViewInteropDemo.cs Updated callsite and doc comment.
src/Microsoft.AndroidX.Compose.Gallery/Demos/LocalsMisc/CustomCompositionLocalDemo.cs Updated callsite.
src/Microsoft.AndroidX.Compose.Gallery/Demos/StateEffectsAnimation/NullableMutableStateDemo.cs Updated callsite.

@jonathanpeppers
jonathanpeppers force-pushed the jonathanpeppers/rename-locals-to-local branch from 7e160d4 to 7ce7b87 Compare June 10, 2026 01:00
@jonathanpeppers jonathanpeppers changed the title Rename Local composition-local API Promote built-in composition locals to top-level classes Jun 10, 2026
Drop the `Locals` host class entirely; each built-in composition
local is now its own top-level static class wrapping a private
`CompositionLocal<T>` and exposing `Current(IComposer)` / `Provides(T)`.

This lets the C# read site match Kotlin's `LocalContext.current`
shape as closely as the language allows (modulo the explicit composer
that C# has no compiler plugin to auto-thread):

    // Kotlin
    val ctx = LocalContext.current

    // C# before
    var ctx = Locals.LocalContext.GetCurrent(composer);   // stutters + verbose

    // C# after
    var ctx = LocalContext.Current(composer);

Also renames `CompositionLocal<T>.GetCurrent(IComposer)` to
`Current(IComposer)`. This fixes a pre-existing broken
`<see cref="CompositionLocal{T}.Current"/>` XML doc reference and
benefits user-defined composition locals too, which keep the
`static readonly CompositionLocal<T>` field pattern -- the call-site
shape is identical across built-in and user-defined locals.

No `[Obsolete]` alias: all affected entries lived in
`PublicAPI.Unshipped.txt`, never `Shipped.txt`. The repo is pre-1.0
and the README does not promise API stability, so a deprecation
cycle would just lock in dead code.

Verified: 130/130 generator tests pass; facade and gallery build
with zero warnings/errors.

Co-authored-by: Copilot <[email protected]>
@jonathanpeppers
jonathanpeppers force-pushed the jonathanpeppers/rename-locals-to-local branch from 7ce7b87 to 8d42083 Compare June 10, 2026 01:11
The Locals promotion landed six new top-level composition-local
classes but only `LocalContext` had gallery coverage. Per the gallery
rule ("a feature without a demo is unverified"), wire up a
consolidated demo that:

  - reads `LocalConfiguration.Current(composer)` for orientation and
    screen width (the canonical responsive-UI primitive in Compose);
  - reads `LocalResources.Current(composer)` for display density; and
  - reads `LocalView.Current(composer)` and triggers haptic feedback
    on tap.

`LocalLifecycleOwner` is intentionally not exercised here -- the one
the binding exposes is `androidx.compose.ui.platform.LocalLifecycleOwner`,
deprecated upstream in favor of `androidx.lifecycle.compose`. A future
binding bump should expose the non-deprecated peer.

`LocalColorScheme` is also not exercised -- app code reads the same
value through the higher-level `MaterialTheme.ColorScheme` /
`composer.ColorScheme()` paths, which already have demo coverage.
The direct accessor stays for parity with the Kotlin surface and for
theme-aware library code.

Co-authored-by: Copilot <[email protected]>
@jonathanpeppers
jonathanpeppers merged commit aa497c2 into main Jun 10, 2026
1 check passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers/rename-locals-to-local branch June 10, 2026 01:26
jonathanpeppers added a commit that referenced this pull request Jun 10, 2026
After [#221](../pull/221) (implicit `long → Color` conversion) and [#222](../pull/222) (theme reads off composer), the `*Argb` suffix on color properties is vestigial — callers can pass `scheme.OnSurface` (a `long`) straight to a `Color?` property and the implicit conversion does the rest. The five hand-written facades that still exposed `long? *Argb` are renamed here to match the shape the source-generated `Text` facade already uses (`public Color? Color`).

### Renames

| File | Before | After |
|------|--------|-------|
| `Icon.cs` | `long? TintArgb` | `Color? Tint` |
| `HorizontalDivider.cs` | `long? ColorArgb` | `Color? Color` |
| `VerticalDivider.cs` | `long? ColorArgb` | `Color? Color` |
| `CircularProgressIndicator.cs` | `long? ColorArgb`, `long? TrackColorArgb` | `Color? Color`, `Color? TrackColor` |
| `LinearProgressIndicator.cs` | `long? ColorArgb`, `long? TrackColorArgb` | `Color? Color`, `Color? TrackColor` |

The render bodies still call the bound `DividerKt` / `ProgressIndicatorKt` / `IconKt` bindings (and the `IconPainter` bridge) with a `long` color slot; the new properties are converted via `Color is { } c ? c : 0L`.

### No `[Obsolete]` aliases

Same rationale as [#221](../pull/221), [#223](../pull/223), [#225](../pull/225), [#226](../pull/226), [#229](../pull/229): every removed entry was in `PublicAPI.Unshipped.txt`, never `Shipped.txt`, so a clean break is acceptable pre-1.0. Verified — only `Color.FromArgb(...)` factories remain (intentional, those aren't being renamed).

### Sweep

19 callsite renames across two samples; JetNews and the Gallery had no references.

| Sample | File | Hits |
|--------|------|------|
| Jetchat | `Conversation.cs` | 5 |
| Jetchat | `Profile.cs` | 4 |
| Jetchat | `RecordButton.cs` | 2 |
| Jetchat | `JetchatIcon.cs` | 2 |
| Jetchat | `JetchatDrawer.cs` | 1 |
| Reply | `EmailDetailAppBar.cs` | 1 |
| Reply | `ReplyEmailListItem.cs` | 1 |
| Reply | `ReplyEmailThreadItem.cs` | 1 |
| Reply | `ReplyProfileImage.cs` | 1 |
| Reply | `ReplySearchBar.cs` | 1 |

### Verified

- ✅ `dotnet test src/Microsoft.AndroidX.Compose.SourceGenerators.Tests` — 132/132
- ✅ `dotnet build src/Microsoft.AndroidX.Compose` — clean (no RS0016/RS0017)
- ✅ `dotnet build src/Microsoft.AndroidX.Compose.Gallery`
- ✅ `dotnet build samples/Jetchat`
- ✅ `dotnet build samples/JetNews`
- ✅ `dotnet build samples/Reply`

Not deployed to a device — leaving smoke-test for the user.

### Coordination

Round-2 #2. Disjoint file set from sibling round-2 PRs except for `PublicAPI.Unshipped.txt` — expect a mechanical sort conflict on merge with round-2 #1 (`Modifier.Padding` defaults) and round-2 #3 (`Func<IComposer, ComposableNode>` implicit).

Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants