Skip to content

Add implicit long → Color conversion and IsSpecified/IsUnspecified - #221

Merged
jonathanpeppers merged 1 commit into
mainfrom
jonathanpeppers/color-implicit-and-isspecified
Jun 10, 2026
Merged

jonathanpeppers merged 1 commit into
mainfrom
jonathanpeppers/color-implicit-and-isspecified

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Owner

Brings AndroidX.Compose.Color closer to Kotlin's androidx.compose.ui.graphics.Color surface by adding the missing inverse of the existing Color → long implicit, plus the two IsSpecified / IsUnspecified properties.

From the "API improvement ideas" brainstorm session (parent: jonathanpeppers/fictional-happiness).

Why

Theme color getters on the bound ColorScheme return long because Compose's androidx.compose.ui.graphics.Color is a @JvmInline value class over ULong. We already have implicit operator long(Color) going one way, so a Color flows into bindings without ceremony. The inverse was missing, so every call site reading a theme color into a facade slot wrapped it explicitly:

Color    = new Color(scheme.OnSurface),
Modifier = Modifier.Companion.Background(new Color(scheme.Primary)),

After this change:

Color    = scheme.OnSurface,
Modifier = Modifier.Companion.Background(scheme.Primary),

Kotlin's Color also exposes isSpecified / isUnspecified extension members on the inline class that peek at the packed sentinel. We didn't expose either — useful for "fall back to the parent / theme default" branches where Color.Unspecified is the marker:

if (scheme.OnSurface.IsSpecified)
{
    …
}

What changed

  • src/Microsoft.AndroidX.Compose/Color.cs — three new public members alongside the existing implicit operator:
    • public static implicit operator Color(long packedValue) → new(packedValue)
    • public bool IsSpecified → PackedValue != Unspecified.PackedValue
    • public bool IsUnspecified → inverse
  • samples/Jetchat/, samples/Reply/ — dropped the now-redundant new Color(...) wraps around binding-typed longs (theme colors + long locals derived from them). The implicit fires once at the property / parameter slot instead. In MessageFormatter.cs two var locals became explicit long since they're plumbed through to Color parameters elsewhere.
  • PublicAPI.Unshipped.txt — three new entries.

Hex-literal constructors (new Color(0xFFAARRGGBB)) and other Color(…) ctor shapes are unaffected — only new Color(<binding long>) wraps were dropped.

Ambiguity check

Confirmed via grep before adding the implicit:

  • No methods take both long and Color parameters where overload resolution would flip.
  • No (long) casts on Color exist in src/ or samples.
  • No callers compare to Color.Unspecified today (so the IsSpecified migration step is a no-op for now — the property is still added for parity with Kotlin).
  • Color == long becoming legal (via implicit lift) is benign — no Color == 0 or similar usages exist.

Verification

dotnet test  src/Microsoft.AndroidX.Compose.SourceGenerators.Tests    # 130/130 pass
dotnet build src/Microsoft.AndroidX.Compose                  # clean
dotnet build src/Microsoft.AndroidX.Compose.Gallery                  # clean
dotnet build samples/Jetchat                         # clean
dotnet build samples/Reply                           # clean
dotnet build samples/JetNews                         # clean

Not deployed — @jonathanpeppers is smoke-testing locally.

Co-authored-by: Copilot [email protected]

`Color` already has an implicit `Color → long` operator so wrappers flow
into bindings without ceremony, but the inverse was missing. Theme color
getters on the bound `ColorScheme` (and other binding-typed APIs) return
`long` because Kotlin's `androidx.compose.ui.graphics.Color` is a
`@JvmInline value class` over `ULong`, so every call site had to wrap:

    Color = new Color(scheme.OnSurface),

This adds the inverse implicit so the same call site reads:

    Color = scheme.OnSurface,

Also adds the missing `IsSpecified` / `IsUnspecified` properties that
Kotlin's `Color` exposes via extension members on the inline class.
Useful for "fall back to the parent / theme default" branches where
`Color.Unspecified` is the sentinel.

Drops the now-redundant `new Color(...)` wraps across the Jetchat and
Reply samples (the only places they appeared); the implicit fires once
at the property / parameter slot instead. Hex-literal constructors
(`new Color(0xFFAARRGGBB)`) and other ctor shapes are unaffected.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings June 10, 2026 00:41

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 adds the inverse of the existing Color → long implicit conversion (long → Color), plus two IsSpecified/IsUnspecified convenience properties that mirror Kotlin's Color.isSpecified/Color.isUnspecified. The new implicit eliminates boilerplate new Color(...) wraps when using long-typed theme colors from the bound ColorScheme, and the sentinel properties support "fall back to theme default" branching against Color.Unspecified.

Changes:

  • Three new public members on Color: implicit operator Color(long), IsSpecified, and IsUnspecified, with correct XML docs and public API tracking entries.
  • Sample apps (Jetchat, Reply) updated to drop all new Color(scheme.*) / new Color(localLong) wraps, exercising the new implicit conversion throughout.
  • MessageFormatter.cs locals changed from Color → long (matching the ColorScheme return type), relying on the implicit at the AppendSymbolAnnotation call boundary.

Reviewed changes

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

Show a summary per file
File Description
src/Microsoft.AndroidX.Compose/Color.cs Adds implicit operator Color(long), IsSpecified, and IsUnspecified
src/Microsoft.AndroidX.Compose/PublicAPI.Unshipped.txt Three new public API entries, correctly sorted
samples/Jetchat/Conversation.cs Drops new Color(...) wraps for theme colors and local long vars
samples/Jetchat/JetchatDrawer.cs Drops new Color(...) wraps for theme colors
samples/Jetchat/Profile.cs Drops new Color(...) wraps for theme colors
samples/Jetchat/EmojiSelector.cs Drops new Color(...) wraps for theme colors
samples/Jetchat/RecordButton.cs Drops new Color(...) wraps for theme colors
samples/Jetchat/MessageFormatter.cs Changes Color locals to long; implicit fires at callee boundary
samples/Reply/ReplySearchBar.cs Drops new Color(...) wraps for theme colors
samples/Reply/ReplyProfileImage.cs Drops new Color(...) wraps for theme colors
samples/Reply/ReplyEmailThreadItem.cs Drops new Color(...) wraps for theme colors
samples/Reply/ReplyEmailListItem.cs Drops new Color(...) wraps for theme colors
samples/Reply/EmptyComingSoon.cs Drops new Color(...) wraps for theme colors
samples/Reply/EmailDetailAppBar.cs Drops new Color(...) wraps for theme colors

@jonathanpeppers
jonathanpeppers enabled auto-merge (squash) June 10, 2026 00:45
@jonathanpeppers
jonathanpeppers merged commit a2ed1af into main Jun 10, 2026
2 checks passed
@jonathanpeppers
jonathanpeppers deleted the jonathanpeppers/color-implicit-and-isspecified branch June 10, 2026 00:46
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