@@ -51,6 +51,8 @@ import androidx.compose.foundation.selection.toggleable
5151import androidx.compose.foundation.shape.CircleShape
5252import androidx.compose.foundation.shape.RoundedCornerShape
5353import androidx.compose.foundation.text.BasicTextField
54+ import androidx.compose.foundation.text.KeyboardActions
55+ import androidx.compose.foundation.text.KeyboardOptions
5456import androidx.compose.foundation.text.selection.SelectionContainer
5557import androidx.compose.foundation.verticalScroll
5658import androidx.compose.material.icons.Icons
@@ -155,6 +157,7 @@ import androidx.compose.ui.semantics.selected
155157import androidx.compose.ui.semantics.semantics
156158import androidx.compose.ui.text.TextRange
157159import androidx.compose.ui.text.font.FontFamily
160+ import androidx.compose.ui.text.input.ImeAction
158161import androidx.compose.ui.text.input.TextFieldValue
159162import androidx.compose.ui.text.style.TextOverflow
160163import androidx.compose.ui.unit.IntOffset
@@ -551,12 +554,12 @@ private fun NotesNavigation(
551554 onReconnectAccount = onReconnectAccount,
552555 onManageAccounts = { managingAccounts = true },
553556 onOpenBookmarks = { browsingBookmarks = true },
554- onCreate = { accountId, category, searchText ->
557+ onCreate = { accountId, category, name ->
555558 scope.launch {
556- val note = if (searchText == null ) {
559+ val note = if (name == null ) {
557560 component.createNote(accountId, category)
558561 } else {
559- component.createNoteFromSearch (accountId, searchText , category)
562+ component.createNamedNote (accountId, name , category)
560563 }
561564 noteHistory = emptyList()
562565 selectedNoteId = note.localId
@@ -866,6 +869,7 @@ private fun NoteListScreen(
866869 onReconnectAccount : (String ) -> Unit ,
867870 onManageAccounts : () -> Unit ,
868871 onOpenBookmarks : () -> Unit ,
872+ /* * Creates a note in an account and category, with the given name or an automatic one. */
869873 onCreate : (String , String , String? ) -> Unit ,
870874 /* * Opens a note with the active search text, if any, so the note can find it too. */
871875 onOpen : (String , String? ) -> Unit
@@ -939,6 +943,12 @@ private fun NoteListScreen(
939943 .collectAsStateWithLifecycle(context = UiDispatcher )
940944 val compactNoteList by component.settings.compactNoteList
941945 .collectAsStateWithLifecycle(context = UiDispatcher )
946+ val askForNewNoteName by component.settings.askForNewNoteName
947+ .collectAsStateWithLifecycle(context = UiDispatcher )
948+ // Visibility is kept apart from the name because the field may report a last value change
949+ // while the dialog closes, which must not reopen it.
950+ var namingNewNote by rememberSaveable(accountId) { mutableStateOf(false ) }
951+ var newNoteName by rememberSaveable(accountId) { mutableStateOf(" " ) }
942952 val appearance by component.settings.appearance
943953 .collectAsStateWithLifecycle(context = UiDispatcher )
944954 val headerContainer =
@@ -1005,9 +1015,19 @@ private fun NoteListScreen(
10051015 previousOffset = offset
10061016 }
10071017 }
1008- val createNote = {
1018+ val createNamedNote = { name : String? ->
10091019 val category = (categoryScope as ? NoteCategoryScope .Category )?.value.orEmpty()
1010- onCreate(accountId, category, query.takeIf (NoteNames ::isValid))
1020+ onCreate(accountId, category, name)
1021+ }
1022+ val createNote = {
1023+ val searchName = query.takeIf (NoteNames ::isValid)
1024+ if (askForNewNoteName) {
1025+ // Offer the name the note would have been given without asking.
1026+ newNoteName = component.defaultNoteName(searchName)
1027+ namingNewNote = true
1028+ } else {
1029+ createNamedNote(searchName)
1030+ }
10111031 }
10121032
10131033 LaunchedEffect (accountId) { withContext(UiDispatcher ) { component.refresh(accountId) } }
@@ -1534,6 +1554,17 @@ private fun NoteListScreen(
15341554 onDismiss = { tagFilterOpen = false }
15351555 )
15361556 }
1557+ if (namingNewNote) {
1558+ NewNoteNameDialog (
1559+ name = newNoteName,
1560+ onNameChange = { newNoteName = it },
1561+ onDismiss = { namingNewNote = false },
1562+ onConfirm = {
1563+ namingNewNote = false
1564+ createNamedNote(newNoteName)
1565+ }
1566+ )
1567+ }
15371568 if (showSettings) {
15381569 AlertDialog (
15391570 onDismissRequest = { showSettings = false },
@@ -1576,6 +1607,13 @@ private fun NoteListScreen(
15761607 onCheckedChange = component.settings::setHideCreateButtonOnScroll,
15771608 testTag = " toggle-hide-create-button-on-scroll"
15781609 )
1610+ SettingsCheckbox (
1611+ label = " Ask for name of new notes" ,
1612+ description = " Suggest the automatic name and let you change it." ,
1613+ checked = askForNewNoteName,
1614+ onCheckedChange = component.settings::setAskForNewNoteName,
1615+ testTag = " toggle-ask-for-new-note-name"
1616+ )
15791617 OutlinedTextField (
15801618 value = bookmarksPath,
15811619 onValueChange = { component.settings.setBookmarksPath(accountId, it) },
@@ -4085,6 +4123,64 @@ private fun RenameNoteDialog(
40854123 )
40864124}
40874125
4126+ /* * Asks for a new note's name, starting from the name it would otherwise have been given. */
4127+ @Composable
4128+ private fun NewNoteNameDialog (
4129+ name : String ,
4130+ onNameChange : (String ) -> Unit ,
4131+ onDismiss : () -> Unit ,
4132+ onConfirm : () -> Unit
4133+ ) {
4134+ val focusRequester = remember { FocusRequester () }
4135+ // The suggested name is selected, so typing replaces it and accepting keeps it.
4136+ var fieldValue by remember {
4137+ mutableStateOf(TextFieldValue (name, selection = TextRange (0 , name.length)))
4138+ }
4139+ // Focus is taken once the field is placed in the dialog's window, as in RenameNoteDialog.
4140+ var fieldPlaced by remember { mutableStateOf(false ) }
4141+ LaunchedEffect (fieldPlaced) {
4142+ if (fieldPlaced) focusRequester.requestFocus()
4143+ }
4144+ val valid = NoteNames .isValid(name)
4145+ AlertDialog (
4146+ onDismissRequest = onDismiss,
4147+ title = { Text (" New note" ) },
4148+ text = {
4149+ OutlinedTextField (
4150+ value = fieldValue,
4151+ onValueChange = {
4152+ fieldValue = it
4153+ onNameChange(it.text)
4154+ },
4155+ label = { Text (" Note name" ) },
4156+ singleLine = true ,
4157+ supportingText = {
4158+ Text (" Characters a file name cannot hold are replaced by spaces." )
4159+ },
4160+ keyboardOptions = KeyboardOptions (imeAction = ImeAction .Done ),
4161+ keyboardActions = KeyboardActions (onDone = { if (valid) onConfirm() }),
4162+ modifier = Modifier .fillMaxWidth().focusRequester(focusRequester)
4163+ .onPlaced { fieldPlaced = true }
4164+ .testTag(" new-note-name-field" )
4165+ )
4166+ },
4167+ confirmButton = {
4168+ TextButton (
4169+ onClick = onConfirm,
4170+ enabled = valid,
4171+ modifier = Modifier .testTag(" confirm-new-note" )
4172+ ) { Text (" Create" ) }
4173+ },
4174+ dismissButton = {
4175+ TextButton (
4176+ onClick = onDismiss,
4177+ modifier = Modifier .testTag(" cancel-new-note" )
4178+ ) { Text (" Cancel" ) }
4179+ },
4180+ modifier = Modifier .testTag(" new-note-name-dialog" )
4181+ )
4182+ }
4183+
40884184@Composable
40894185private fun ChangeNoteCategoryDialog (
40904186 currentCategory : String ,
0 commit comments