@@ -378,6 +378,9 @@ private fun NotesNavigation(
378378 var selectedHeading by rememberSaveable { mutableStateOf<String ?>(null ) }
379379 var editOnOpenNoteId by rememberSaveable { mutableStateOf<String ?>(null ) }
380380 var navigationRequest by rememberSaveable { mutableStateOf(0 ) }
381+ // The note-list search that was active when a note was opened from the list, so the opened
382+ // note starts by finding the same text. Every other way of opening a note clears it.
383+ var findOnOpen by rememberSaveable { mutableStateOf<String ?>(null ) }
381384 var noteHistory by rememberSaveable { mutableStateOf(emptyList<String >()) }
382385 var managingAccounts by rememberSaveable { mutableStateOf(false ) }
383386 var browsingBookmarks by rememberSaveable { mutableStateOf(false ) }
@@ -400,6 +403,7 @@ private fun NotesNavigation(
400403 noteHistory = noteHistory.dropLast(1 )
401404 selectedHeading = null
402405 editOnOpenNoteId = null
406+ findOnOpen = null
403407 navigationRequest++
404408 }
405409 BackHandler (enabled = managingAccounts && selectedNoteId == null ) {
@@ -431,6 +435,7 @@ private fun NotesNavigation(
431435 noteHistory = emptyList()
432436 selectedNoteId = note.localId
433437 selectedHeading = null
438+ findOnOpen = null
434439 navigationRequest++
435440 }
436441 }
@@ -451,6 +456,7 @@ private fun NotesNavigation(
451456 selectedNoteId = request.localId
452457 selectedHeading = null
453458 editOnOpenNoteId = null
459+ findOnOpen = null
454460 navigationRequest++
455461 }
456462 is WidgetRequest .CreateNote -> {
@@ -464,6 +470,7 @@ private fun NotesNavigation(
464470 selectedNoteId = note.localId
465471 selectedHeading = null
466472 editOnOpenNoteId = note.localId
473+ findOnOpen = null
467474 navigationRequest++
468475 }
469476 }
@@ -480,18 +487,21 @@ private fun NotesNavigation(
480487 heading = selectedHeading,
481488 startEditing = editOnOpenNoteId == noteId,
482489 navigationRequest = navigationRequest,
490+ initialFindQuery = findOnOpen,
483491 onInitialEditStarted = { editOnOpenNoteId = null },
484492 onBackToList = {
485493 selectedNoteId = null
486494 selectedHeading = null
487495 editOnOpenNoteId = null
496+ findOnOpen = null
488497 noteHistory = emptyList()
489498 },
490499 onOpen = { destination ->
491500 if (noteId != destination.localId) noteHistory = noteHistory + noteId
492501 selectedNoteId = destination.localId
493502 selectedHeading = destination.heading
494503 editOnOpenNoteId = null
504+ findOnOpen = null
495505 navigationRequest++
496506 }
497507 )
@@ -523,6 +533,7 @@ private fun NotesNavigation(
523533 selectedNoteId = localId
524534 selectedHeading = null
525535 editOnOpenNoteId = null
536+ findOnOpen = null
526537 navigationRequest++
527538 }
528539 )
@@ -551,14 +562,16 @@ private fun NotesNavigation(
551562 selectedNoteId = note.localId
552563 selectedHeading = null
553564 editOnOpenNoteId = note.localId
565+ findOnOpen = null
554566 navigationRequest++
555567 }
556568 },
557- onOpen = {
569+ onOpen = { localId, searchText ->
558570 noteHistory = emptyList()
559- selectedNoteId = it
571+ selectedNoteId = localId
560572 selectedHeading = null
561573 editOnOpenNoteId = null
574+ findOnOpen = searchText
562575 navigationRequest++
563576 }
564577 )
@@ -854,7 +867,8 @@ private fun NoteListScreen(
854867 onManageAccounts : () -> Unit ,
855868 onOpenBookmarks : () -> Unit ,
856869 onCreate : (String , String , String? ) -> Unit ,
857- onOpen : (String ) -> Unit
870+ /* * Opens a note with the active search text, if any, so the note can find it too. */
871+ onOpen : (String , String? ) -> Unit
858872) {
859873 var query by rememberSaveable { mutableStateOf(" " ) }
860874 var searchScope by rememberSaveable { mutableStateOf(NoteSearchScope .TITLE_AND_CONTENT ) }
@@ -1483,7 +1497,10 @@ private fun NoteListScreen(
14831497 selectedNoteIds + note.localId
14841498 }
14851499 } else {
1486- onOpen(note.localId)
1500+ onOpen(
1501+ note.localId,
1502+ query.trim().takeIf (String ::isNotEmpty)
1503+ )
14871504 }
14881505 },
14891506 onLongClick = {
@@ -2465,6 +2482,7 @@ private fun NoteDetailScreen(
24652482 heading : String? ,
24662483 startEditing : Boolean ,
24672484 navigationRequest : Int ,
2485+ initialFindQuery : String? ,
24682486 onInitialEditStarted : () -> Unit ,
24692487 onBackToList : () -> Unit ,
24702488 onOpen : (ResolvedNoteLink ) -> Unit
@@ -2569,14 +2587,21 @@ private fun NoteDetailScreen(
25692587 var canRedo by remember(localId) { mutableStateOf(false ) }
25702588 var pendingHeading by remember(localId, heading, navigationRequest) { mutableStateOf(heading) }
25712589 var loadImages by remember(localId) { mutableStateOf(true ) }
2572- var finding by rememberSaveable(localId) { mutableStateOf(false ) }
2590+ // A note opened from an active note-list search starts by finding that text, so the matches
2591+ // that made the note appear in the list are highlighted and the first one is scrolled to.
2592+ var finding by rememberSaveable(localId) { mutableStateOf(initialFindQuery != null ) }
25732593 var togglingTask by remember(localId) { mutableStateOf(false ) }
2574- var findQuery by rememberSaveable(localId) { mutableStateOf(" " ) }
2594+ var findQuery by rememberSaveable(localId) { mutableStateOf(initialFindQuery.orEmpty()) }
2595+ // Find opened for the list search was not a request to type, so it must not raise the keyboard.
2596+ var findFromListSearch by rememberSaveable(localId) {
2597+ mutableStateOf(initialFindQuery != null )
2598+ }
25752599 var currentMatch by rememberSaveable(localId) { mutableStateOf(0 ) }
25762600 var matches by remember(localId) { mutableStateOf(emptyList<IntRange >()) }
25772601 val closeFind = {
25782602 finding = false
25792603 findQuery = " "
2604+ findFromListSearch = false
25802605 currentMatch = 0
25812606 if (editing) editor?.focusForInput()
25822607 Unit
@@ -3136,7 +3161,8 @@ private fun NoteDetailScreen(
31363161 currentMatch = (currentMatch + 1 ) % matches.size
31373162 }
31383163 },
3139- onClose = closeFind
3164+ onClose = closeFind,
3165+ focusOnOpen = ! findFromListSearch
31403166 )
31413167 } else {
31423168 Row (
@@ -3365,7 +3391,8 @@ private fun NoteDetailScreen(
33653391 },
33663392 onClose = {
33673393 closeFind()
3368- }
3394+ },
3395+ focusOnOpen = ! findFromListSearch
33693396 )
33703397 }
33713398 if (noteTags.isNotEmpty()) {
@@ -4281,11 +4308,12 @@ private fun FindInNoteBar(
42814308 onQueryChange : (String ) -> Unit ,
42824309 onPrevious : () -> Unit ,
42834310 onNext : () -> Unit ,
4284- onClose : () -> Unit
4311+ onClose : () -> Unit ,
4312+ focusOnOpen : Boolean = true
42854313) {
42864314 val focusRequester = remember { FocusRequester () }
42874315 // Opening the bar is a request to type, so take the focus instead of asking for a second tap.
4288- LaunchedEffect (Unit ) { focusRequester.requestFocus() }
4316+ LaunchedEffect (Unit ) { if (focusOnOpen) focusRequester.requestFocus() }
42894317 Row (
42904318 modifier = Modifier .fillMaxWidth().padding(horizontal = 16 .dp),
42914319 verticalAlignment = Alignment .CenterVertically
0 commit comments