1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! Rotary input events (Wear OS crown / rotating bezel).
//!
//! This mirrors Jetpack Compose for Wear OS's
//! `androidx.compose.ui.input.rotary` package: a [`RotaryScrollEvent`] carries
//! a scroll amount **already converted to pixels**, and handlers return `true`
//! to consume the event and stop it propagating.
//!
//! # Sign convention
//!
//! Android reports the crown/bezel delta on `MotionEvent.AXIS_SCROLL` in
//! *detents*, where a **positive** value means the user scrolled *up / away
//! from themselves*. Compose negates that value before scaling it to pixels,
//! so a positive `AXIS_SCROLL` becomes a **negative**
//! [`RotaryScrollEvent::vertical_scroll_pixels`].
//!
//! Evidence — `AndroidComposeView.android.kt`, `handleRotaryEvent`
//! (androidx-main):
//!
//! ```text
//! private fun handleRotaryEvent(event: MotionEvent): Boolean {
//! val config = android.view.ViewConfiguration.get(context)
//! val axisValue = -event.getAxisValue(AXIS_SCROLL)
//! val rotaryEvent =
//! RotaryScrollEvent(
//! verticalScrollPixels = axisValue * getScaledVerticalScrollFactor(config, context),
//! horizontalScrollPixels =
//! axisValue * getScaledHorizontalScrollFactor(config, context),
//! uptimeMillis = event.eventTime,
//! inputDeviceId = event.deviceId,
//! )
//! ...
//! }
//! ```
//!
//! Note that Compose feeds the *same* (negated) `AXIS_SCROLL` value into both
//! the vertical and the horizontal field, scaled by the respective scroll
//! factor — it does **not** read `AXIS_HSCROLL`. A rotary encoder has one
//! degree of freedom; the two fields exist so a horizontally scrolling
//! container can consume the same gesture. [`RotaryScrollEvent::from_detents`]
//! reproduces this exactly.
//!
//! The resulting pixel value is a *scroll amount in content space*: it can be
//! handed straight to a scroll container's "scroll by N pixels" API with the
//! same meaning a wheel/touch scroll delta would have.
/// Android's default vertical scroll factor expressed in density-independent
/// pixels.
///
/// Android derives the real factor from the current theme's
/// `listPreferredItemHeight` via
/// `ViewConfiguration.getScaledVerticalScrollFactor()`, which needs a JVM
/// `Context`. Cranpose's Android backend has no JNI dependency in its input
/// path, so it defaults to this value scaled by the display density and lets
/// the host override it with the exact platform number (see
/// `AppShell::set_rotary_scroll_factor`).
pub const DEFAULT_ROTARY_SCROLL_FACTOR_DP: f32 = 64.0;
/// A rotary scroll event produced by a Wear OS crown or rotating bezel.
///
/// Field-for-field equivalent to Compose's
/// `androidx.compose.ui.input.rotary.RotaryScrollEvent`. The scroll amounts are
/// in **pixels**, not detents: the platform layer has already multiplied the
/// raw axis value by the system scroll factor.
///
/// This type is `Copy` and contains no heap data, so dispatching one allocates
/// nothing.
/// Turns continuous rotary travel into discrete steps while retaining sub-step
/// travel between events. The input and step size use the same caller-chosen
/// unit, typically platform-resolved pixels.
/// Converts a raw Android `AXIS_SCROLL` detent value into pixels using
/// Compose's sign convention: positive detents (crown turned up/away) produce a
/// **negative** pixel amount.