Skip to content

Add Spacer.Width/.Height/.Size/.Weight static factories#225

Merged
jonathanpeppers merged 2 commits into
mainfrom
jonathanpeppers/spacer-static-factories
Jun 10, 2026
Merged

Add Spacer.Width/.Height/.Size/.Weight static factories#225
jonathanpeppers merged 2 commits into
mainfrom
jonathanpeppers/spacer-static-factories

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Owner

Brainstorm idea #7 — make the "insert a small gap" idiom one expression instead of three.

Motivation

Inserting an 8-dp gap reads:

// before
new Spacer(Modifier.Companion.Width(8))
new Spacer(Modifier.Companion.Height(8))
new Spacer(Modifier.Companion.Weight(1f, fill: true))

// after
Spacer.Width(8)
Spacer.Height(8)
Spacer.Weight(1f)

Mirrors the shape of Modifier.Width/Height/Size. Same lowering — each factory is new Spacer(Modifier.Companion.X(...)) under the hood, no new properties on Spacer, no behavioural change.

API

Five new statics on AndroidX.Compose.Spacer:

Factory Equivalent
Spacer.Width(Dp) new Spacer(Modifier.Width(dp))
Spacer.Height(Dp) new Spacer(Modifier.Height(dp))
Spacer.Size(Dp) new Spacer(Modifier.Size(dp))
Spacer.Size(Dp, Dp) new Spacer(Modifier.Size(w, h))
Spacer.Weight(float, bool fill = true) new Spacer(Modifier.Weight(w, fill))

All take Dp; the existing implicit operator Dp(int) lets callers write Spacer.Width(8) literally.

The two existing ctors (Spacer(), Spacer(Modifier)) are kept — one site (JetchatDrawer.cs:32 StatusBarsPadding) still legitimately uses the modifier ctor and four sentinel new Spacer() calls live in pattern-match defaults. Not removing them; this PR is non-breaking despite the original suggestion's openness to breaking changes.

Migrated call sites (24)

File W/H/S Weight
samples/Jetchat/Conversation.cs 7
samples/Jetchat/JetchatDrawer.cs 1
samples/Jetchat/Profile.cs 3
samples/Jetchat/RecordButton.cs 2
samples/JetNews/HomeCards.cs 1 1
samples/JetNews/JetnewsDrawer.cs 1
samples/JetNews/PostBody.cs 1
samples/JetNews/PostScreen.cs 2
samples/Reply/EmptyComingSoon.cs 1
src/Microsoft.AndroidX.Compose.Gallery/Demos/Navigation/BackHandlerDemo.cs 4
Total 23 1

Out-of-scope (untouched): 4 sentinel new Spacer() arms in HomeScreen.cs / PostScreen.cs, plus 1 new Spacer(Modifier.Companion.StatusBarsPadding()) in JetchatDrawer.cs.

Coordination — Modifier-statics race

The Modifier-statics PR (jonathanpeppers/modifier-static-factories) hasn't merged. Factory bodies use Modifier.Companion.Width(dp) (existing instance method on Modifier.Companion), not Modifier.Width(dp) (the not-yet-merged static). Once that PR lands a trivial mechanical follow-up can strip .Companion from these five lines in Spacer.cs.

Verified

  • dotnet test src/Microsoft.AndroidX.Compose.SourceGenerators.Tests — 130/130 passing
  • dotnet build src/Microsoft.AndroidX.Compose — 0 errors
  • dotnet build src/Microsoft.AndroidX.Compose.Gallery — 0 errors, 0 warnings
  • dotnet build samples/{Jetchat,JetNews,Reply} — all green

No device deploy.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

Replaces the verbose `new Spacer(Modifier.Companion.Width(8))` shape
with `Spacer.Width(8)`. Five static factories on `Spacer`:

* `Spacer.Width(Dp)`  / `Spacer.Height(Dp)`  / `Spacer.Size(Dp)`
* `Spacer.Size(Dp width, Dp height)` (mirrors `Modifier.Size(Dp, Dp)`)
* `Spacer.Weight(float weight, bool fill = true)` for the
  push-siblings-apart idiom inside `Row` / `Column`.

Migrates 24 call sites across samples and the gallery demo. Keeps the
existing `Spacer()` and `Spacer(Modifier)` ctors -- the latter still
has one legitimate caller (`StatusBarsPadding`) so removing it would
be a breaking change with no real ergonomics win.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 10, 2026 00:47

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 five static factory methods to Spacer (Width, Height, Size (two overloads), and Weight) to shorten the common "insert a gap" idiom from new Spacer(Modifier.Companion.Width(8)) to Spacer.Width(8). Each factory is a trivial delegation to the existing Modifier.Companion instance method. The two existing constructors are preserved, keeping this a non-breaking change. All 24 eligible call sites across the sample apps and gallery are mechanically migrated.

Changes:

  • Added 5 new static factory methods on Spacer with comprehensive XML docs, each delegating to Modifier.Companion.Width/Height/Size/Weight.
  • Migrated 24 call sites in 10 files (samples/Jetchat, samples/JetNews, samples/Reply, Gallery) from new Spacer(Modifier.Companion.X(...)) to the new short form.
  • Updated PublicAPI.Unshipped.txt with the 5 new public API entries.

Reviewed changes

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

Show a summary per file
File Description
src/Microsoft.AndroidX.Compose/Spacer.cs Adds Width(Dp), Height(Dp), Size(Dp), Size(Dp, Dp), Weight(float, bool) static factories
src/Microsoft.AndroidX.Compose/PublicAPI.Unshipped.txt Registers 5 new public API entries (sorted, correct signatures)
samples/Jetchat/Conversation.cs Migrates 7 spacer call sites
samples/Jetchat/JetchatDrawer.cs Migrates 1 spacer call site
samples/Jetchat/Profile.cs Migrates 3 spacer call sites
samples/Jetchat/RecordButton.cs Migrates 2 spacer call sites
samples/JetNews/HomeCards.cs Migrates 2 spacer call sites (including 1 Weight)
samples/JetNews/JetnewsDrawer.cs Migrates 1 spacer call site
samples/JetNews/PostBody.cs Migrates 1 spacer call site
samples/JetNews/PostScreen.cs Migrates 2 spacer call sites
samples/Reply/EmptyComingSoon.cs Migrates 1 spacer call site
src/Microsoft.AndroidX.Compose.Gallery/Demos/Navigation/BackHandlerDemo.cs Migrates 4 spacer call sites

…r-static-factories

# Conflicts:
#	samples/Jetchat/Conversation.cs
@jonathanpeppers jonathanpeppers merged commit 5dab30d into main Jun 10, 2026
1 check passed
@jonathanpeppers jonathanpeppers deleted the jonathanpeppers/spacer-static-factories branch June 10, 2026 01:06
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 <223556219+Copilot@users.noreply.github.com>
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