Skip to content

Expose Slider and RangeSlider OnValueChangeFinished - #429

Open
davidortinau wants to merge 1 commit into
jonathanpeppers:mainfrom
davidortinau:slider-value-change-finished
Open

davidortinau wants to merge 1 commit into
jonathanpeppers:mainfrom
davidortinau:slider-value-change-finished

Conversation

@davidortinau

Copy link
Copy Markdown

Slider and RangeSlider always pass onValueChangeFinished: null to Material 3, so C# apps can't run work once a drag, tap, key press or accessibility set-progress ends. This adds an optional OnValueChangeFinished to both, following the existing optional Slider properties (ValueRange, Colors, Thumb).

API

public sealed partial class Slider      { public Action? OnValueChangeFinished { get; set; } }
public sealed class RangeSlider         { public Action? OnValueChangeFinished { get; set; } }

public static partial class Composables
{
    // New; the previous 8-parameter overload stays for compiled callers.
    public static void Slider(float value, Action<float> onValueChange, bool enabled = true, int steps = 0,
        Modifier? modifier = null, Action? thumb = null, FloatRange? valueRange = null,
        SliderColors? colors = null, Action? onValueChangeFinished = null);
}
// Apply an app text-size change once per gesture, not on every step.
new Slider(value: scale.Value, onValueChange: v => scale.Value = v)
{
    OnValueChangeFinished = () => ApplyTextScale(scale.Value),
}

Leaving it unset keeps today's behavior: the Kotlin default bit stays set and Kotlin uses its null default.

Approach

  • Generator: ComposeFacadeGenerator classified every IFunction0 as a required constructor Action. A nullable IFunction0? now becomes an optional Action? on every surface: a tree property, and an optional parameter on the explicit and implicit direct methods. It goes through the existing optional-callback lowering ([Callback] IFunction1? slots such as onTextLayout): RememberAction only when supplied, so the peer is identity-stable and the latest delegate runs, and the $default bit is cleared only when supplied. Non-nullable IFunction0 is unchanged, and no existing generated facade had a nullable IFunction0? parameter. The per-slot [Callback] unboxing moved into a shared CallbackAdapterBody helper, and the seven CallbackType is not null sites now use HasCallbackWrapper.
  • Slider: [FacadeAdded] IFunction0? onValueChangeFinished on the existing wrapper passthrough, forwarded to the bound SliderKt.Slider. [FacadeAdded] keeps the old Composables.Slider CLR signature and its direct helper; they leave the bit set and pass default.
  • RangeSlider (hand-written): an Action? property lowered with composer.RememberAction, clearing RangeSliderDefault.OnValueChangeFinished only when set.
  • No new JNI or bound members: both bound overloads already take onValueChangeFinished, and the peer is the existing MutableComposableLambda0.
  • Public API entries, Slider/RangeSlider XML docs, the classification and changed-bit rows in .github/copilot-instructions.md, and a Gallery demo (selection-slider-change-finished). docs/api-coverage.md is per symbol and Slider/RangeSlider were already covered, so it doesn't change.

Validation

  • dotnet build Microsoft.AndroidX.Compose.slnx: 0 warnings, 0 errors (.NET SDK 11.0.100-rc.2.26504.105).
  • dotnet test src/Microsoft.AndroidX.Compose.SourceGenerators.Tests: 559 passed. Two new tests fail with the generator change reverted:
    • NullableFunction0_IsOptionalActionOnEverySurface pins the tree property, its constructor (unchanged), the conditional RememberAction, the default-bit clearing and the changed bits on the tree and direct surfaces.
    • AddedOptionalActionKeepsOldContractsAndKotlinDefaultUntilSupplied loads compiled consumers: a binary built against the old contract and the same source rebuilt both still call the legacy overload, which keeps the Kotlin default bit set. The tree property and the new direct overload clear the bit only when a callback is supplied.
  • The generated Slider facade (EmitCompilerGeneratedFiles) matches: if (OnValueChangeFinished is not null) __defaults &= ~...OnValueChangeFinished, RememberAction only when set, and the legacy Composables.Slider passes default with the bit left set.
  • New SliderValueChangeFinishedTests (4 tests) inject real MotionEvent drags and taps into a Slider, a RangeSlider and a slider without the callback. They check that the callback waits for the release, runs exactly once after the last onValueChange, and calls the latest delegate after a recomposition. They also check that a slider without the callback still drags. They compile in the solution build. I did not run them; no device or emulator was used for this PR.
  • Gallery selection-slider-change-finished builds; not run on a device.

Not verified: any device or emulator run (device tests, Gallery), Release/R8 or NativeAOT, physical devices, TalkBack, or keyboard input.

Fixes #428

🤖 Generated with Claude Code

https://claude.ai/code/session_01WnVZbQvGRyjADVNbVVTgwD

Add an optional OnValueChangeFinished (Action?) to Slider and RangeSlider,
forwarded to the bound Material 3 onValueChangeFinished that both facades
passed as null. Apps can now run work once a drag, tap, key press or
accessibility set-progress ends instead of on every onValueChange step.
Leaving it unset keeps the Kotlin default.

ComposeFacadeGenerator now surfaces a nullable IFunction0? bridge parameter
as an optional Action? on the tree and direct surfaces, through the existing
optional-callback lowering: RememberAction and the cleared $default bit only
when supplied. Slider marks the slot [FacadeAdded], so the previous
Composables.Slider signature and its direct helper remain.

Add generator and binary-compatibility tests, device tests driven by real
MotionEvents, a Gallery demo, public API entries and docs.

Fixes jonathanpeppers#428

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WnVZbQvGRyjADVNbVVTgwD
Copilot AI balanced review requested due to automatic review settings November 5, 2026 05:27

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.

🔵 Needs a closer look

The Android touch tests and Gallery demo were compiled but not run, leaving callback ordering and identity stability during drag and recomposition unverified on a device or emulator.

No open issues 🎉

What changed in this PR

Adds optional OnValueChangeFinished callbacks to Slider and RangeSlider, addressing #428 so apps can defer work until a value change ends.

Changes:

  • Extends facade generation to expose nullable IFunction0 callbacks as optional Action properties and parameters.
  • Wires both sliders to the existing Material 3 callbacks while preserving omitted-callback behavior and the previous Composables.Slider binary signature.
  • Adds API documentation, generator and compatibility tests, touch-input device tests, and a Gallery demo.

The PR reports a successful solution build and 559 passing generator tests. Device tests and the Gallery demo were not run on a device or emulator.

File Description
src/​Microsoft.AndroidX.Compose/​Slider.cs Documents completion-callback usage.
src/​Microsoft.AndroidX.Compose/​RangeSlider.cs Adds and forwards the optional completion callback.
src/​Microsoft.AndroidX.Compose/​PublicAPI.Unshipped.txt Records new properties and compatible Slider overloads.
src/​Microsoft.AndroidX.Compose/​ComposeBridges.cs Forwards Slider’s callback to the bound API.
src/​Microsoft.AndroidX.Compose.SourceGenerators/​ComposeFacadeGenerator.cs Generates optional zero-argument callback surfaces and adapters.
src/​Microsoft.AndroidX.Compose.SourceGenerators.Tests/​FacadeGeneratorTests.cs Tests nullable callback generation and masks.
src/​Microsoft.AndroidX.Compose.SourceGenerators.Tests/​FacadeAddedCompatibilityTests.cs Tests legacy compatibility and callback default handling.
src/​Microsoft.AndroidX.Compose.Gallery/​Registry/​Catalog.cs Registers the completion-callback demo.
src/​Microsoft.AndroidX.Compose.Gallery/​Demos/​Selection/​SliderChangeFinishedDemo.cs Demonstrates live versus committed slider values.
src/​Microsoft.AndroidX.Compose.DeviceTests/​SliderValueChangeFinishedTests.cs Adds touch-input timing and callback-refresh tests.
src/​Microsoft.AndroidX.Compose.DeviceTests/​SliderValueChangeFinishedTestActivity.cs Hosts sliders for native input tests.
src/​Microsoft.AndroidX.Compose.DeviceTests/​SliderTouchBounds.cs Supplies screen coordinates for injected touch events.
.github/​copilot-instructions.md Documents optional callback classification and changed-mask behavior.

Review effort: Balanced

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.

Expose onValueChangeFinished on Slider and RangeSlider so apps can act when a change ends

2 participants