Skip to content

Commit 9d5ad43

Browse files
committed
focus keyboard on text field when compose view is opened and update changelog
1 parent e0a5c14 commit 9d5ad43

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
- Haptics for copying links, bookmarking, and selecting timelines
88
- CW state shown with filled/unfilled icon in composer
99
- Option to swap order of notifications and explore tabs
10+
- Compose view now focuses on the text field when opened
1011

1112
### Fixed
1213
- Crash when app is put into the background
14+
- Don't allow biting your own posts
1315

1416
## [0.0.2-alpha] - 2026/07/04
1517

shared/src/commonMain/kotlin/site/remlit/snowdrop/view/ComposeView.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,27 @@ import androidx.compose.material3.TextField
2626
import androidx.compose.material3.TextFieldDefaults
2727
import androidx.compose.material3.TopAppBar
2828
import androidx.compose.runtime.Composable
29+
import androidx.compose.runtime.LaunchedEffect
2930
import androidx.compose.runtime.getValue
3031
import androidx.compose.runtime.mutableStateOf
3132
import androidx.compose.runtime.remember
3233
import androidx.compose.runtime.setValue
3334
import androidx.compose.ui.Alignment
3435
import androidx.compose.ui.Modifier
3536
import androidx.compose.ui.draw.clip
37+
import androidx.compose.ui.focus.FocusRequester
38+
import androidx.compose.ui.focus.focusRequester
39+
import androidx.compose.ui.focus.onFocusChanged
3640
import androidx.compose.ui.graphics.Color
3741
import androidx.compose.ui.platform.LocalFocusManager
42+
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
3843
import androidx.compose.ui.text.font.FontWeight
3944
import androidx.compose.ui.text.style.TextOverflow
4045
import androidx.compose.ui.unit.dp
4146
import androidx.compose.ui.unit.sp
4247
import androidx.lifecycle.compose.collectAsStateWithLifecycle
4348
import com.russhwolf.settings.ExperimentalSettingsApi
49+
import kotlinx.coroutines.delay
4450
import org.jetbrains.compose.resources.painterResource
4551
import org.jetbrains.compose.resources.stringResource
4652
import site.remlit.snowdrop.api.statuses.createStatus
@@ -80,6 +86,7 @@ import snowdrop.shared.generated.resources.visibility_public_description
8086
import snowdrop.shared.generated.resources.visibility_unlisted
8187
import snowdrop.shared.generated.resources.visibility_unlisted_description
8288
import snowdrop.shared.generated.resources.write_your_post_here
89+
import kotlin.time.Duration.Companion.milliseconds
8390

8491
@Composable
8592
@OptIn(ExperimentalSettingsApi::class)
@@ -92,6 +99,8 @@ fun ComposeView(
9299
val navHandler = LocalNavController.current
93100
val snackbarHandler = SnackbarController.current
94101
val focusManager = LocalFocusManager.current
102+
val focusRequester = remember { FocusRequester() }
103+
val keyboardController = LocalSoftwareKeyboardController.current
95104

96105
val currentAccount by getCurrentAccountObjectFlow()
97106
.collectAsStateWithLifecycle(null)
@@ -135,6 +144,12 @@ fun ComposeView(
135144
}
136145
}
137146

147+
LaunchedEffect(Unit) {
148+
// kinda jank but there's no good way around this
149+
delay(50.milliseconds)
150+
focusRequester.requestFocus()
151+
}
152+
138153
TopAppBar(
139154
navigationIcon = {
140155
IconButton(onClick = { navHandler.popBackStack() }) {
@@ -320,7 +335,15 @@ fun ComposeView(
320335
value = content,
321336
placeholder = { Text(stringResource(Res.string.write_your_post_here)) },
322337
onValueChange = { content = it },
323-
modifier = Modifier.fillMaxWidth().fillMaxHeight(),
338+
modifier = Modifier
339+
.focusRequester(focusRequester)
340+
.onFocusChanged {
341+
if (it.hasFocus) {
342+
keyboardController?.show()
343+
}
344+
}
345+
.fillMaxWidth()
346+
.fillMaxHeight(),
324347
colors = TextFieldDefaults.colors(
325348
unfocusedContainerColor = Color(0x00000000),
326349
unfocusedIndicatorColor = Color(0x00000000),

0 commit comments

Comments
 (0)