Skip to content

Touch and input snippet migration - #1138

Draft
barbaralaw wants to merge 29 commits into
android:mainfrom
StellarElements:touch-and-input-snippet-migration
Draft

barbaralaw wants to merge 29 commits into
android:mainfrom
StellarElements:touch-and-input-snippet-migration

Conversation

@barbaralaw

Copy link
Copy Markdown
Contributor

Summary

Migrates hardcoded Kotlin samples from three Compose touch-input guides into :compose:snippets as region-tagged source, so the guides can import them instead of hardcoding them.

Two of those pages already had the samples in this repo (commands.kt, KeyboardShortcutsHelper.kt). This PR only adds the page that did not: copy-and-paste.

On that new page: 8 snippets. 5 publish exactly what the page shows. 3 differ, every one
listed below with a reason.
Line wrapping is not counted as a difference: spotlessApply
runs before submission and rewraps long calls without changing what a reader reads.

Region tags for the new file begin android_compose_touchinput_copyandpaste_; the tables drop that prefix.

How to read the "why" column

Why What a reader of the page would see
hardcoded snippet defect fixed The published Kotlin does not compile, and this is not a sample meant to demonstrate an error. The region publishes the corrected line.

Per page

1. Copy and paste

https://developer.android.com/develop/ui/compose/touch-input/copy-and-paste

Not previously in the repo. 8 snippets, 5 match, 3 differ.

Snippet Page section Difference Why
set_text Copy with ClipboardManager setText("Hello, clipboard") → setText(AnnotatedString("Hello, clipboard")) hardcoded snippet defect fixed
paste_text Paste with ClipboardManager append(text.toString()) → append(annotatedString.text); var textFieldState → val textFieldState hardcoded snippet defect fixed
paste_rich_content Paste a rich content extra ) dropped on hasMediaType(MediaType.Image); by remember { mutableListOf<Uri>() } → remember { mutableStateListOf<Uri>() } hardcoded snippet defect fixed

Matching as published: selection_container, clip_entry, copy_rich_content, has_media_type, sensitive_content.

2. Handle keyboard actions

https://developer.android.com/develop/ui/compose/touch-input/keyboard-input/commands

Already region-tagged in compose/snippets/.../touchinput/keyboardinput/commands.kt. This PR does not change that file.

3. Keyboard Shortcuts Helper

https://developer.android.com/develop/ui/compose/touch-input/keyboard-input/keyboard-shortcuts-helper

Already region-tagged in compose/snippets/.../touchinput/keyboardinput/KeyboardShortcutsHelper.kt. This PR does not change that file.

Not extracted

Kotlin only. No Java twins on the copy-and-paste page.

Live snippet fixes this surfaced

  1. Copy with ClipboardManager: clipboardManager.setText("Hello, clipboard") — ClipboardManager.setText takes AnnotatedString, not String.
  2. Paste with ClipboardManager: append(text.toString()) — text is not in scope in the edit lambda; the pasted value is annotatedString.
  3. Paste a rich content: transferableContent.hasMediaType(MediaType.Image)) — extra ). val imageList by remember { mutableListOf<Uri>() } is not snapshot state, so later imageList.add is not a correct Compose pattern.

Verification

  • ./gradlew :compose:snippets:compileDebugKotlin — passes
  • ./gradlew :compose:snippets:lintDebug — passes
  • ./gradlew spotlessApply — clean

kkuan2011 and others added 29 commits August 4, 2026 20:18
Replaced Any? with a more reasonable List<String>.

Added an example Log.e to avoid a comment.

Signed-off-by: morgillo <morgillo@google.com>
…EXCLUDE section to keep up the unnecessary (on DAC) method implementation.

Signed-off-by: morgillo <morgillo@google.com>
Signed-off-by: morgillo <morgillo@google.com>
Signed-off-by: morgillo <morgillo@google.com>
…iew-based snippets.

Signed-off-by: morgillo <morgillo@google.com>
#1 (comment)
Signed-off-by: morgillo <morgillo@google.com>
#1 (comment)
Signed-off-by: morgillo <morgillo@google.com>
#1 (comment)
Signed-off-by: morgillo <morgillo@google.com>
#1 (comment)
Signed-off-by: morgillo <morgillo@google.com>
#1 (comment)

Signed-off-by: morgillo <morgillo@google.com>
Signed-off-by: morgillo <morgillo@google.com>
#1 (comment)
Signed-off-by: morgillo <morgillo@google.com>
…snippets

katherinekuan/migrate-dac-snippets
android#1031 (comment)

Signed-off-by: Ivan Morgillo <imorgillo@gmail.com>
Aaron asked for one rule rather than a mix, and a period is the one he wants.
Eight comments across the predictive back and edge-to-edge snippets.

Two of them, the status bar icon lines, are copied from the setup-e2e page,
which publishes them without a period, so the page picks up the change when it
renders the include.

The TODO in GlanceSnippets.kt keeps no period. "TODO: handle error." reads
wrong.
* Add code snippets for parcelize

* Use a declaration-level opt-in for the DataClass snippet

The page's data class serializer block opens with a file-level
@file:OptIn. A file annotation must precede the package declaration, so
it cannot sit inside a region tag, and the published snippet lost the
opt-in entirely -- a reader copying it would hit a compile error.

Moving the annotation onto the class puts it inside the region, makes
both @DataClass snippets use the same form, and lets the file-level
annotation be dropped: nothing else in the file required it.

This deviates from the page as published. The page's block needs the
matching change.

* Include the import line in the basic and parcelable_creator snippets

* Code review updates

---------

Co-authored-by: Katherine Kuan <843428+kkuan2011@users.noreply.github.com>
`./gradlew spotlessCheck` has been failing on main since these two files landed
in #3, on nothing more than a missing newline at end of file. Two bytes, one per
file, and `spotlessApply` writes both.

It is not cosmetic while it sits on main. Every branch cut from main inherits a
red format gate, so the check that is supposed to say "your change is clean"
says "something is broken" to everyone regardless of what they changed. Three
snippet pull requests this week each carried it as an unrelated failure, and
`upstream-preflight check` runs spotlessApply and asserts a clean tree before
anything goes to android/snippets, so it refuses every branch on principle.

Upstream does not have these files. They are ours, from #3, so the sync with
android:main was never going to fix them.

Only the two files the formatter names, and nothing else in the tree.
@google-cla

google-cla Bot commented Sep 24, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

5 participants