@@ -8,8 +8,12 @@ import ai.opencode.core.ui.OpenCodeIconSize |
8 | 8 | import ai.opencode.core.ui.OpenCodeTextStyles |
9 | 9 | import ai.opencode.core.ui.OpenCodeTheme |
10 | 10 | import ai.opencode.core.ui.SFSymbol |
| 11 | +import ai.opencode.feature.composer.plusmenu.findActivity |
11 | 12 | import ai.opencode.server.model.AgentDisplayName |
12 | 13 | import ai.opencode.server.model.OpenCodeAutocompleteAgent |
| 14 | +import android.os.SystemClock |
| 15 | +import android.window.OnBackInvokedCallback |
| 16 | +import android.window.OnBackInvokedDispatcher |
13 | 17 | import androidx.compose.foundation.clickable |
14 | 18 | import androidx.compose.foundation.layout.Arrangement |
15 | 19 | import androidx.compose.foundation.layout.Box |
@@ -24,21 +28,24 @@ import androidx.compose.material3.DropdownMenu |
24 | 28 | import androidx.compose.material3.DropdownMenuItem |
25 | 29 | import androidx.compose.material3.Text |
26 | 30 | import androidx.compose.runtime.Composable |
| 31 | +import androidx.compose.runtime.DisposableEffect |
27 | 32 | import androidx.compose.runtime.LaunchedEffect |
28 | 33 | import androidx.compose.runtime.collectAsState |
29 | 34 | import androidx.compose.runtime.getValue |
| 35 | +import androidx.compose.runtime.mutableLongStateOf |
30 | 36 | import androidx.compose.runtime.mutableStateOf |
31 | 37 | import androidx.compose.runtime.remember |
| 38 | +import androidx.compose.runtime.rememberUpdatedState |
32 | 39 | import androidx.compose.runtime.setValue |
33 | 40 | import androidx.compose.ui.Alignment |
34 | 41 | import androidx.compose.ui.Modifier |
35 | 42 | import androidx.compose.ui.draw.alpha |
| 43 | +import androidx.compose.ui.platform.LocalContext |
36 | 44 | import androidx.compose.ui.platform.testTag |
37 | 45 | import androidx.compose.ui.res.stringResource |
38 | 46 | import androidx.compose.ui.semantics.Role |
39 | 47 | import androidx.compose.ui.semantics.clearAndSetSemantics |
40 | 48 | import androidx.compose.ui.semantics.contentDescription |
41 | | -import androidx.compose.ui.semantics.heading |
42 | 49 | import androidx.compose.ui.semantics.role |
43 | 50 | import androidx.compose.ui.semantics.selected |
44 | 51 | import androidx.compose.ui.semantics.semantics |
@@ -47,6 +54,7 @@ import androidx.compose.ui.text.font.FontWeight |
47 | 54 | import androidx.compose.ui.text.style.TextOverflow |
48 | 55 | import androidx.compose.ui.unit.Dp |
49 | 56 | import androidx.compose.ui.unit.dp |
| 57 | +import androidx.compose.ui.window.PopupProperties |
50 | 58 |
|
51 | 59 | // Agent Selection (experimental `agentSelection`): choose the primary agent (Build, Plan, |
52 | 60 | // or a custom one) from the composer. |
@@ -65,16 +73,6 @@ object AgentPickerSharedKeys { |
65 | 73 | val knownPrimaryAgents = ai.opencode.server.ServerSharedKeys.knownPrimaryAgents |
66 | 74 | } |
67 | 75 |
|
68 | | -/** The description's first sentence, for a menu's subtitle. */ |
69 | | -val OpenCodeAutocompleteAgent.shortDescription: String? |
70 | | - get() { |
71 | | - val text = description?.trim()?.takeIf { it.isNotEmpty() } ?: return null |
72 | | - // The first non-empty run before a dot, or the whole text when none. |
73 | | - val rest = text.trimStart('.') |
74 | | - val sentence = if (rest.isEmpty()) text else rest.substringBefore('.') |
75 | | - return sentence.trim() |
76 | | - } |
77 | | - |
78 | 76 | /** |
79 | 77 | * The agent chip beside the model chip, only while Agent Selection is on ([isEnabled]). [isFolded] |
80 | 78 | * hides it as the composer folds. |
@@ -120,16 +118,21 @@ fun AgentChip( |
120 | 118 | targetDrop: Dp = 0.dp, |
121 | 119 | ) { |
122 | 120 | var open by remember { mutableStateOf(false) } |
| 121 | + var closedAt by remember { mutableLongStateOf(0L) } |
123 | 122 | // Mounted as the composer expands: the list is fresh before it opens. |
124 | 123 | LaunchedEffect(Unit) { onOpen() } |
125 | 124 | val chooseAgentDescription = stringResource(R.string.composer_agent_choose_description) |
126 | 125 | val secondary = OpenCodeTheme.colors.secondaryText |
| 126 | + AgentMenuBackHandler(enabled = open) { open = false } |
127 | 127 | // The whole menu moves its target down and its ink back up, so the menu drops from the lowered |
128 | 128 | // target too. |
129 | 129 | Box(modifier.offset(y = targetDrop)) { |
130 | 130 | Row( |
131 | 131 | Modifier |
132 | | - .clickable(enabled = enabled, role = Role.Button) { open = true } |
| 132 | + .clickable(enabled = enabled, role = Role.Button) { |
| 133 | + // A tap on the chip while the menu is open has already closed it as an outside tap. |
| 134 | + if (SystemClock.uptimeMillis() - closedAt > 500) open = !open |
| 135 | + } |
133 | 136 | .heightIn(min = 48.dp) |
134 | 137 | .clearAndSetSemantics { |
135 | 138 | contentDescription = chooseAgentDescription |
@@ -145,46 +148,58 @@ fun AgentChip( |
145 | 148 | Text(name, style = style, color = secondary, maxLines = 1, overflow = TextOverflow.Ellipsis) |
146 | 149 | OpenCodeIcon(SFSymbol.ChevronDown, null, size = 12.dp, tint = secondary) |
147 | 150 | } |
148 | | - DropdownMenu(expanded = open, onDismissRequest = { open = false }) { |
| 151 | + DropdownMenu( |
| 152 | + expanded = open, |
| 153 | + onDismissRequest = { |
| 154 | + open = false |
| 155 | + closedAt = SystemClock.uptimeMillis() |
| 156 | + }, |
| 157 | + properties = PopupProperties(focusable = false), |
| 158 | + ) { |
149 | 159 | if (agents.isEmpty()) { |
150 | 160 | DropdownMenuItem(text = { Text(stringResource(R.string.composer_agent_loading)) }, onClick = {}, enabled = false) |
151 | | - } else { |
152 | | - // One “Agent” section (menu order fixed, as `.menuOrder(.fixed)`). |
153 | | - Text( |
154 | | - stringResource(R.string.composer_agent_section_title), |
155 | | - style = OpenCodeTextStyles.footnote, |
156 | | - color = secondary, |
157 | | - modifier = Modifier.padding(horizontal = 16.dp, vertical = 6.dp).semantics { heading() }, |
| 161 | + } |
| 162 | + for (agent in agents) { |
| 163 | + val isOn = agent.id == currentID |
| 164 | + DropdownMenuItem( |
| 165 | + text = { |
| 166 | + Text( |
| 167 | + agent.displayName, |
| 168 | + style = OpenCodeTextStyles.body, |
| 169 | + fontWeight = if (isOn) FontWeight.Medium else null, |
| 170 | + maxLines = 1, |
| 171 | + overflow = TextOverflow.Ellipsis, |
| 172 | + ) |
| 173 | + }, |
| 174 | + trailingIcon = { |
| 175 | + Box(Modifier.width(24.dp)) { |
| 176 | + if (isOn) OpenCodeIcon(SFSymbol.Checkmark, null, size = OpenCodeIconSize.small) |
| 177 | + } |
| 178 | + }, |
| 179 | + modifier = Modifier.semantics { selected = isOn }, |
| 180 | + onClick = { |
| 181 | + open = false |
| 182 | + onPick(agent.id) |
| 183 | + }, |
158 | 184 | ) |
159 | | - for (agent in agents) { |
160 | | - val isOn = agent.id == currentID |
161 | | - DropdownMenuItem( |
162 | | - text = { |
163 | | - Column { |
164 | | - Text(agent.displayName, style = OpenCodeTextStyles.body) |
165 | | - agent.shortDescription?.let { |
166 | | - Text(it, style = OpenCodeTextStyles.footnote, color = secondary) |
167 | | - } |
168 | | - } |
169 | | - }, |
170 | | - // A menu's toggle draws the checkmark and a subtitle. |
171 | | - leadingIcon = { |
172 | | - Box(Modifier.width(24.dp)) { |
173 | | - if (isOn) OpenCodeIcon(SFSymbol.Checkmark, null, size = OpenCodeIconSize.small) |
174 | | - } |
175 | | - }, |
176 | | - modifier = Modifier.semantics { selected = isOn }, |
177 | | - onClick = { |
178 | | - open = false |
179 | | - onPick(agent.id) |
180 | | - }, |
181 | | - ) |
182 | | - } |
183 | 185 | } |
184 | 186 | } |
185 | 187 | } |
186 | 188 | } |
187 | 189 |
|
| 190 | +// The menu's popup doesn't take focus, so back goes to the activity; overlay priority runs it before the keyboard's. |
| 191 | +@Composable |
| 192 | +private fun AgentMenuBackHandler(enabled: Boolean, onBack: () -> Unit) { |
| 193 | + val activity = LocalContext.current.findActivity() ?: return |
| 194 | + val currentOnBack by rememberUpdatedState(onBack) |
| 195 | + DisposableEffect(activity, enabled) { |
| 196 | + if (!enabled) return@DisposableEffect onDispose { } |
| 197 | + val callback = OnBackInvokedCallback { currentOnBack() } |
| 198 | + activity.onBackInvokedDispatcher.registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_OVERLAY, callback) |
| 199 | + onDispose { activity.onBackInvokedDispatcher.unregisterOnBackInvokedCallback(callback) } |
| 200 | + } |
| 201 | +} |
| 202 | + |
188 | 203 | /** |
189 | 204 | * Settings › Composer › Default Agent, the agent new sessions start with, only while Agent |
190 | 205 | * Selection is on. Reads [AgentPickerSharedKeys.defaultAgentID] and |
|