mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
feat(agent): the conversation menu moves to the chat's top right corner (#2583)
This commit is contained in:
1 parent
b3ebd54d87
commit
9a964ba221
2 files changed
+71
-34
No files matched your search
@@ -9,6 +9,8 @@ import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.animation.expandVertically
|
||||
import androidx.compose.animation.fadeIn
|
||||
import androidx.compose.animation.shrinkHorizontally
|
||||
import androidx.compose.animation.expandHorizontally
|
||||
import androidx.compose.animation.fadeOut
|
||||
import androidx.compose.animation.shrinkVertically
|
||||
import androidx.compose.foundation.Canvas
|
||||
@@ -129,7 +131,8 @@ private const val COMPOSER_SLIDE_MS = 350L
|
||||
@Composable
|
||||
internal fun AgentPage(
|
||||
modifier: Modifier, inputModifier: Modifier = Modifier, visible: Boolean = true, startTyping: Boolean = true, hidesPet: Boolean = true,
|
||||
) = AgentPageContent(modifier, inputModifier, visible, startTyping, hidesPet, showControlsWithoutKeyboard = false)
|
||||
menuSlot: AgentMenuSlot? = null,
|
||||
) = AgentPageContent(modifier, inputModifier, visible, startTyping, hidesPet, showControlsWithoutKeyboard = false, menuSlot)
|
||||
|
||||
@Composable
|
||||
internal fun AgentPageWithControls(
|
||||
@@ -140,7 +143,7 @@ internal fun AgentPageWithControls(
|
||||
@Composable
|
||||
private fun AgentPageContent(
|
||||
modifier: Modifier, inputModifier: Modifier, visible: Boolean, startTyping: Boolean, hidesPet: Boolean,
|
||||
showControlsWithoutKeyboard: Boolean,
|
||||
showControlsWithoutKeyboard: Boolean, menuSlot: AgentMenuSlot? = null,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val view = LocalView.current
|
||||
@@ -172,6 +175,16 @@ private fun AgentPageContent(
|
||||
val composer = remember { ComposerState(startTyping && AgentHost.entries.isEmpty()) }
|
||||
val focusManager = LocalFocusManager.current
|
||||
val togglePanel: (AgentPanel) -> Unit = { panel = if (panel == it) AgentPanel.NONE else it }
|
||||
var menuOpen by rememberSaveable { mutableStateOf(false) }
|
||||
val menu: @Composable () -> Unit = {
|
||||
AgentMenu(menuOpen, { menuOpen = it }, onPanel = { panel = it }, onSetup = { setup = true })
|
||||
}
|
||||
// A host with a title bar shows the menu there; otherwise it floats over the conversation's top corner.
|
||||
val inSlot = menuSlot != null && panel != AgentPanel.HISTORY && panel != AgentPanel.PROCESSES
|
||||
if (menuSlot != null) DisposableEffect(menuSlot, inSlot) {
|
||||
menuSlot.content = if (inSlot) menu else null
|
||||
onDispose { menuSlot.content = null }
|
||||
}
|
||||
// The keyboard leaves too little height for a panel and the editor together, so typing closes the panel.
|
||||
val keyboardOpen = WindowInsets.ime.getBottom(LocalDensity.current) > 0
|
||||
LaunchedEffect(keyboardOpen) { if (keyboardOpen && (panel == AgentPanel.MODEL || panel == AgentPanel.USAGE)) panel = AgentPanel.NONE }
|
||||
@@ -237,9 +250,10 @@ private fun AgentPageContent(
|
||||
) {
|
||||
AgentJumpPill(toLatest)
|
||||
}
|
||||
if (menuSlot == null) Box(Modifier.align(Alignment.TopEnd).padding(top = 4.dp)) { menu() }
|
||||
}
|
||||
androidx.compose.animation.AnimatedVisibility(!hidden, enter = expandVertically() + fadeIn(), exit = shrinkVertically() + fadeOut()) {
|
||||
AgentComposer(composer, draft, inputModifier, panel, togglePanel, showControlsWithoutKeyboard) { setup = true }
|
||||
AgentComposer(composer, draft, inputModifier, panel, togglePanel, showControlsWithoutKeyboard)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -269,6 +283,46 @@ private class ComposerState(typing: Boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
/** Where a host with its own title bar shows the conversation's menu. */
|
||||
@Stable
|
||||
internal class AgentMenuSlot {
|
||||
var content by mutableStateOf<(@Composable () -> Unit)?>(null)
|
||||
}
|
||||
|
||||
/** History, processes, a new conversation, the pet and setup, folded behind one button. */
|
||||
@Composable
|
||||
private fun AgentMenu(open: Boolean, onOpen: (Boolean) -> Unit, onPanel: (AgentPanel) -> Unit, onSetup: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
val working = AgentHost.state == AgentHost.State.WORKING
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.clip(Shape16).background(colors.surface.copy(alpha = if (open) 0.97f else 0.88f))
|
||||
.border(1.dp, pal.line, Shape16).padding(end = 2.dp),
|
||||
) {
|
||||
androidx.compose.animation.AnimatedVisibility(open, enter = expandHorizontally(expandFrom = Alignment.End) + fadeIn(),
|
||||
exit = shrinkHorizontally(shrinkTowards = Alignment.End) + fadeOut()) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
HeaderAction(Icons.Outlined.History, stringResource(R.string.agent_history), size = 36.dp, tag = "agent-history") { onOpen(false); onPanel(AgentPanel.HISTORY) }
|
||||
HeaderAction(Icons.Outlined.Memory, stringResource(R.string.agent_processes), size = 36.dp, tag = "agent-processes") { onOpen(false); onPanel(AgentPanel.PROCESSES) }
|
||||
HeaderAction(Icons.Outlined.Add, stringResource(R.string.agent_new), enabled = !working && AgentHost.entries.isNotEmpty(), size = 36.dp, tag = "agent-new") {
|
||||
onOpen(false)
|
||||
AgentHost.forget(context)
|
||||
}
|
||||
HeaderAction(Icons.Outlined.Pets, stringResource(R.string.setup_agent_pet), selected = AgentPet.enabled, size = 36.dp, tag = "agent-pet-toggle") {
|
||||
AgentPet.set(context, !AgentPet.enabled)
|
||||
}
|
||||
HeaderAction(Icons.Outlined.Settings, stringResource(R.string.agent_setup_title), enabled = !working, size = 36.dp, tag = "agent-settings") {
|
||||
onOpen(false)
|
||||
onSetup()
|
||||
}
|
||||
}
|
||||
}
|
||||
HeaderAction(Icons.Outlined.MoreHoriz, stringResource(R.string.agent_more), selected = open, size = 36.dp, tag = "agent-menu") { onOpen(!open) }
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AgentPanelBar(title: String, onClose: () -> Unit) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth().padding(bottom = 6.dp)) {
|
||||
@@ -512,13 +566,12 @@ internal fun liveUsed(limit: AgentHost.Limit) = if (limit.resetsAt > 0 && limit.
|
||||
@Composable
|
||||
private fun AgentComposer(
|
||||
composer: ComposerState, draftState: MutableState<String>, inputModifier: Modifier, panel: AgentPanel, onPanel: (AgentPanel) -> Unit,
|
||||
showControlsWithoutKeyboard: Boolean, onSetup: () -> Unit,
|
||||
showControlsWithoutKeyboard: Boolean,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
var draft by draftState
|
||||
var labOpen by rememberSaveable { mutableStateOf(false) }
|
||||
if (AgentHost.lab == null) labOpen = false
|
||||
var controlsOpen by rememberSaveable { mutableStateOf(false) }
|
||||
var hadFocus by remember { mutableStateOf(false) }
|
||||
val send = send@{
|
||||
val text = draft.trim()
|
||||
@@ -559,17 +612,15 @@ private fun AgentComposer(
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.padding(top = 8.dp).fillMaxWidth().horizontalScroll(rememberScrollState()),
|
||||
) { AgentActivity(labOpen) { labOpen = !labOpen } }
|
||||
AgentEditor(composer, draftState, inputModifier, panel, onPanel, onSetup, controlsOpen, { controlsOpen = it },
|
||||
highlightControls = showControlsWithoutKeyboard, send = send)
|
||||
} else AgentReadingBar(composer, draft, inputModifier, labOpen, onLab = { labOpen = !labOpen }) {
|
||||
AgentEditor(composer, draftState, inputModifier, panel, onPanel, highlightControls = showControlsWithoutKeyboard, send = send)
|
||||
} else AgentReadingBar(
|
||||
composer, draft, inputModifier, labOpen, onLab = { labOpen = !labOpen }, onUsage = { onPanel(AgentPanel.USAGE) },
|
||||
) {
|
||||
// The session drawer is driven by a controller, so its editor opens without raising the keyboard.
|
||||
if (showControlsWithoutKeyboard) {
|
||||
controlsOpen = false
|
||||
composer.typing = true
|
||||
composer.opening = true
|
||||
} else {
|
||||
controlsOpen = true
|
||||
composer.open()
|
||||
}
|
||||
} else composer.open()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -577,7 +628,7 @@ private fun AgentComposer(
|
||||
/** The composer while reading: one line holding what is going on, opened by a press or by Y. */
|
||||
@Composable
|
||||
private fun AgentReadingBar(
|
||||
composer: ComposerState, draft: String, inputModifier: Modifier, labOpen: Boolean, onLab: () -> Unit, onControls: () -> Unit,
|
||||
composer: ComposerState, draft: String, inputModifier: Modifier, labOpen: Boolean, onLab: () -> Unit, onUsage: () -> Unit, onOpen: () -> Unit,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val colors = MaterialTheme.colorScheme
|
||||
@@ -588,7 +639,7 @@ private fun AgentReadingBar(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.fillMaxWidth().padding(top = 8.dp).height(44.dp).clip(Shape16).background(colors.surface)
|
||||
.border(if (focused) 2.dp else 1.dp, if (focused) pal.primary else pal.line, Shape16)
|
||||
.then(inputModifier).onFocusChanged { focused = it.isFocused }.clickable { composer.open() }.testTag("agent-composer-bar")
|
||||
.then(inputModifier).onFocusChanged { focused = it.isFocused }.clickable(onClick = onOpen).testTag("agent-composer-bar")
|
||||
.padding(start = 14.dp, end = 4.dp),
|
||||
) {
|
||||
Text(
|
||||
@@ -600,7 +651,7 @@ private fun AgentReadingBar(
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp), verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.weight(1f).padding(start = 8.dp).horizontalScroll(rememberScrollState()),
|
||||
) { AgentActivity(labOpen, onLab) }
|
||||
HeaderAction(Icons.Outlined.MoreHoriz, stringResource(R.string.agent_more), size = 36.dp, tag = "agent-controls", onClick = onControls)
|
||||
ContextRing("agent-usage-bar", onUsage)
|
||||
if (working) Box(
|
||||
contentAlignment = Alignment.Center,
|
||||
modifier = Modifier.padding(start = 4.dp).size(36.dp).clip(CircleShape).background(pal.error.copy(alpha = 0.22f))
|
||||
@@ -613,7 +664,7 @@ private fun AgentReadingBar(
|
||||
@Composable
|
||||
private fun AgentEditor(
|
||||
composer: ComposerState, draftState: MutableState<String>, inputModifier: Modifier, panel: AgentPanel, onPanel: (AgentPanel) -> Unit,
|
||||
onSetup: () -> Unit, menu: Boolean, onMenu: (Boolean) -> Unit, highlightControls: Boolean = false, send: () -> Unit,
|
||||
highlightControls: Boolean = false, send: () -> Unit,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val colors = MaterialTheme.colorScheme
|
||||
@@ -660,22 +711,6 @@ private fun AgentEditor(
|
||||
}
|
||||
ContextRing("agent-usage", onClick = { onPanel(AgentPanel.USAGE) })
|
||||
}
|
||||
if (menu) {
|
||||
HeaderAction(Icons.Outlined.History, stringResource(R.string.agent_history), size = 32.dp, tag = "agent-history") { onMenu(false); onPanel(AgentPanel.HISTORY) }
|
||||
HeaderAction(Icons.Outlined.Memory, stringResource(R.string.agent_processes), size = 32.dp, tag = "agent-processes") { onMenu(false); onPanel(AgentPanel.PROCESSES) }
|
||||
HeaderAction(Icons.Outlined.Add, stringResource(R.string.agent_new), enabled = !working && AgentHost.entries.isNotEmpty(), size = 32.dp, tag = "agent-new") {
|
||||
onMenu(false)
|
||||
AgentHost.forget(context)
|
||||
}
|
||||
HeaderAction(Icons.Outlined.Pets, stringResource(R.string.setup_agent_pet), selected = AgentPet.enabled, size = 32.dp, tag = "agent-pet-toggle") {
|
||||
AgentPet.set(context, !AgentPet.enabled)
|
||||
}
|
||||
HeaderAction(Icons.Outlined.Settings, stringResource(R.string.agent_setup_title), enabled = !working, size = 32.dp, tag = "agent-settings") {
|
||||
onMenu(false)
|
||||
onSetup()
|
||||
}
|
||||
}
|
||||
HeaderAction(if (menu) Icons.Outlined.Close else Icons.Outlined.MoreHoriz, stringResource(R.string.agent_more), size = 32.dp, tag = "agent-menu") { onMenu(!menu) }
|
||||
}
|
||||
Row(verticalAlignment = Alignment.Bottom) {
|
||||
if (LocalActivityResultRegistryOwner.current != null) AttachButton()
|
||||
|
||||
@@ -519,6 +519,7 @@ internal fun PetChat(open: Boolean, motion: PetMotion, onClose: () -> Unit, onCl
|
||||
} else {
|
||||
Rect(margin, motion.dockY + motion.size + gap, w - margin, h - margin)
|
||||
}
|
||||
val menuSlot = remember { AgentMenuSlot() }
|
||||
val grow = remember { Animatable(0f) }
|
||||
val fade = remember { Animatable(0f) }
|
||||
LaunchedEffect(open) {
|
||||
@@ -559,9 +560,10 @@ internal fun PetChat(open: Boolean, motion: PetMotion, onClose: () -> Unit, onCl
|
||||
Text(stringResource(R.string.agent_pet_name), fontSize = 16.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground,
|
||||
modifier = Modifier.padding(start = 4.dp))
|
||||
Spacer(Modifier.weight(1f))
|
||||
menuSlot.content?.invoke()
|
||||
HeaderAction(Icons.Outlined.Close, stringResource(R.string.agent_close), size = 34.dp, tag = "agent-pet-close", onClick = onClose)
|
||||
}
|
||||
AgentPage(Modifier.weight(1f).fillMaxWidth(), visible = open, startTyping = AgentHost.entries.isEmpty(), hidesPet = false)
|
||||
AgentPage(Modifier.weight(1f).fillMaxWidth(), visible = open, startTyping = AgentHost.entries.isEmpty(), hidesPet = false, menuSlot = menuSlot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user