Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion android/app/src/main/java/dev/opencode/mobile/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
package dev.opencode.mobile

import android.os.Bundle
import android.view.KeyEvent
import android.view.KeyboardShortcutGroup
import android.view.Menu
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.CompositionLocalProvider
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import dagger.hilt.android.AndroidEntryPoint
import dev.opencode.mobile.domain.event.WorldSignals
import dev.opencode.mobile.ui.components.LocalOpenCodeInputFocusRegistry
import dev.opencode.mobile.ui.keyboard.KeyboardShortcutHost
import dev.opencode.mobile.ui.keyboard.LocalKeyboardShortcutHost
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@Inject lateinit var worldSignals: WorldSignals

private val keyboardShortcuts = KeyboardShortcutHost()

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
worldSignals.register()

enableEdgeToEdge()
setContent { MainNavigation() }
setContent {
CompositionLocalProvider(
LocalKeyboardShortcutHost provides keyboardShortcuts,
LocalOpenCodeInputFocusRegistry provides keyboardShortcuts.inputFocus,
) {
MainNavigation()
}
}
}

// Runs before the view hierarchy, so Compose focus traversal cannot take Ctrl+Tab first.
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
if (keyboardShortcuts.handleKeyEvent(event, currentFocus)) return true
return super.dispatchKeyEvent(event)
}

override fun onProvideKeyboardShortcuts(
data: MutableList<KeyboardShortcutGroup>?,
menu: Menu?,
deviceId: Int,
) {
super.onProvideKeyboardShortcuts(data, menu, deviceId)
keyboardShortcuts.shortcutGroup()?.let { data?.add(it) }
}
}
11 changes: 11 additions & 0 deletions android/app/src/main/java/dev/opencode/mobile/Navigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ import dev.opencode.mobile.ui.connection.ConnectionSetupMode
import dev.opencode.mobile.ui.connection.ConnectionSetupScreen
import dev.opencode.mobile.ui.connection.ConnectionSetupViewModel
import dev.opencode.mobile.ui.icons.OpenCodeIcons
import dev.opencode.mobile.ui.keyboard.KeyboardShortcutAction
import dev.opencode.mobile.ui.keyboard.KeyboardShortcutHandler
import dev.opencode.mobile.ui.main.MainScreen
import dev.opencode.mobile.ui.main.SessionOpenTarget
import dev.opencode.mobile.ui.onboarding.AppOnboardingScreen
Expand Down Expand Up @@ -417,13 +419,18 @@ fun MainNavigation() {
},
onManageServersClick = { backStack.add(SettingsHome) },
selectedSessionId = if (useSplitLayout) paneSelection?.sessionId else null,
sessionKeyboardNavigationEnabled = useSplitLayout,
)
}
if (showsSplit) {
OpenCodeSplitPane(
screen = OpenCodeSplitPaneScreen.Home,
leading = home,
trailing = { control ->
KeyboardShortcutHandler(
KeyboardShortcutAction.ToggleSidebar,
onShortcut = control.toggle,
)
HomeSessionPane(
paneStack = paneStack,
detailControl = control,
Expand Down Expand Up @@ -538,6 +545,10 @@ fun MainNavigation() {
screen = OpenCodeSplitPaneScreen.Settings,
leading = settings,
trailing = { control ->
KeyboardShortcutHandler(
KeyboardShortcutAction.ToggleSidebar,
onShortcut = control.toggle,
)
settingsDetailContent(visibleSettingsDetail, control) {}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import dev.opencode.mobile.ui.components.OpenCodeAttachmentPresentation
import dev.opencode.mobile.ui.components.OpenCodeAttachmentVariant
import dev.opencode.mobile.ui.components.OpenCodeButton
import dev.opencode.mobile.ui.components.OpenCodeButtonContrast
import dev.opencode.mobile.ui.components.OpenCodeButtonVariant
import dev.opencode.mobile.ui.components.OpenCodeChoiceBadge
import dev.opencode.mobile.ui.components.OpenCodeSessionComposer
import dev.opencode.mobile.ui.components.OpenCodeSessionComposerAction
import dev.opencode.mobile.ui.components.OpenCodeSessionComposerState
import dev.opencode.mobile.ui.components.rememberOpenCodeSessionComposerFocus

private enum class SessionComposerGalleryScenario(
val label: String,
Expand Down Expand Up @@ -77,6 +82,7 @@ fun SessionComposerGalleryScreen(modifier: Modifier = Modifier) {
var mentionText by rememberSaveable { mutableStateOf(MentionGalleryText) }
var mentionsCleared by rememberSaveable { mutableStateOf(false) }
var lastAction by rememberSaveable { mutableStateOf("None") }
val composerFocus = rememberOpenCodeSessionComposerFocus()
var selectedLabels by rememberSaveable { mutableStateOf(listOf("Minimal patch")) }
var customSelected by rememberSaveable { mutableStateOf(false) }
var customText by rememberSaveable { mutableStateOf("") }
Expand Down Expand Up @@ -259,12 +265,29 @@ fun SessionComposerGalleryScreen(modifier: Modifier = Modifier) {
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Row(
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
OpenCodeButton(
variant = OpenCodeButtonVariant.CompactText("Focus input"),
contrast = OpenCodeButtonContrast.Low,
enabled = composerFocus.canRequestFocus,
onClick = { composerFocus.requestFocus() },
)
Text(
if (composerFocus.isFocused) "Input focused" else "Input not focused",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
}

Spacer(modifier = Modifier.weight(1f))

OpenCodeSessionComposer(
state = state,
focus = composerFocus,
onAction = { action ->
when (action) {
is OpenCodeSessionComposerAction.TextChanged ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ fun OpenCodeBottomSheet(
}
}

// The sheet is an overlay in the activity window, so window-level key handling must be told it is up.
OpenCodeModalSurfaceEffect(shown = visible || shouldRender)

if (!shouldRender) return

fun dismissWithAnimation() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package dev.opencode.mobile.ui.components

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf

/** Which kind of design-system text input holds focus. */
enum class OpenCodeFocusedTextInput {
/** The prompt text input of [OpenCodeSessionComposer]. */
SessionComposer,

/** Any other text input: [OpenCodeTextInput], [OpenCodeServerAddressInput], and so on. */
Field,
}

/**
* Window-wide view of design-system input focus: which text input is focused and whether a
* modal surface (bottom sheet, dialog) covers the screen. Text inputs and modal surfaces report
* into the registry provided by [LocalOpenCodeInputFocusRegistry]; app-level keyboard handling
* reads it. Main-thread only.
*/
class OpenCodeInputFocusRegistry {
private val focusedInputs = LinkedHashMap<Any, OpenCodeFocusedTextInput>()
private val modalSurfaces = LinkedHashSet<Any>()

/** [OpenCodeFocusedTextInput.Field] wins if both kinds report focus during a hand-off. */
val focusedTextInput: OpenCodeFocusedTextInput?
get() =
when {
OpenCodeFocusedTextInput.Field in focusedInputs.values -> OpenCodeFocusedTextInput.Field
OpenCodeFocusedTextInput.SessionComposer in focusedInputs.values ->
OpenCodeFocusedTextInput.SessionComposer
else -> null
}

val hasModalSurface: Boolean
get() = modalSurfaces.isNotEmpty()

fun updateTextInput(
owner: Any,
input: OpenCodeFocusedTextInput,
focused: Boolean,
) {
if (focused) {
focusedInputs[owner] = input
} else {
focusedInputs.remove(owner)
}
}

fun removeTextInput(owner: Any) {
focusedInputs.remove(owner)
}

fun updateModalSurface(
owner: Any,
shown: Boolean,
) {
if (shown) {
modalSurfaces.add(owner)
} else {
modalSurfaces.remove(owner)
}
}
}

/** Null outside the app root (previews, tests): inputs then report nowhere. */
val LocalOpenCodeInputFocusRegistry = staticCompositionLocalOf<OpenCodeInputFocusRegistry?> { null }

/**
* Focus callback for one text input. Wire it to `Modifier.onFocusChanged { report(it.isFocused) }`;
* the report is withdrawn when the input leaves composition, even if it never reported blur.
*/
@Composable
internal fun rememberOpenCodeTextInputFocusReport(input: OpenCodeFocusedTextInput): (Boolean) -> Unit {
val registry = LocalOpenCodeInputFocusRegistry.current
val owner = remember { Any() }
DisposableEffect(registry, owner) {
onDispose { registry?.removeTextInput(owner) }
}
return remember(registry, owner, input) {
{ focused -> registry?.updateTextInput(owner, input, focused) }
}
}

/** Marks a modal surface as covering the window while [shown]. */
@Composable
internal fun OpenCodeModalSurfaceEffect(shown: Boolean) {
val registry = LocalOpenCodeInputFocusRegistry.current
val owner = remember { Any() }
DisposableEffect(registry, owner, shown) {
registry?.updateModalSurface(owner, shown)
onDispose { registry?.updateModalSurface(owner, false) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.semantics.contentDescription
Expand Down Expand Up @@ -93,6 +94,7 @@ fun OpenCodeServerAddressInput(
val hostInteractionSource = remember { MutableInteractionSource() }
val schemeInteractionSource = remember { MutableInteractionSource() }
val hostFocused by hostInteractionSource.collectIsFocusedAsState()
val reportFocus = rememberOpenCodeTextInputFocusReport(OpenCodeFocusedTextInput.Field)

LaunchedEffect(value) {
val next = parseServerAddressValue(value, fallbackScheme = scheme)
Expand Down Expand Up @@ -235,7 +237,7 @@ fun OpenCodeServerAddressInput(
onDone = { focusManager.clearFocus() },
),
interactionSource = hostInteractionSource,
modifier = Modifier.fillMaxWidth(),
modifier = Modifier.fillMaxWidth().onFocusChanged { reportFocus(it.isFocused) },
)
}
}
Expand Down
Loading