Skip to content

Commit 0ee02f1

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
Scrolling to active search result (#514)
* Scrolling to active search result * increasing bottom margin --------- Co-authored-by: Leandro Ferreira <[email protected]>
1 parent e7c45e4 commit 0ee02f1

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

application/features/editor/src/commonMain/kotlin/io/writeopia/editor/features/editor/ui/TextEditor.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ internal fun TextEditor(
3838

3939
if (position != null) {
4040
LaunchedEffect(position, block = {
41-
noteEditorViewModel.scrollToPosition.collectLatest {
42-
listState.animateScrollBy(70F)
41+
noteEditorViewModel.scrollToPosition.collectLatest { position ->
42+
if (position == -1) {
43+
listState.animateScrollBy(70F)
44+
} else if (position != null) {
45+
listState.scrollToItem(position, scrollOffset = -100)
46+
}
4347
}
4448
})
4549
}

application/features/editor/src/commonMain/kotlin/io/writeopia/editor/features/editor/ui/desktop/DesktopNoteEditorScreen.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ import androidx.compose.ui.draw.clip
3636
import androidx.compose.ui.focus.FocusRequester
3737
import androidx.compose.ui.focus.focusRequester
3838
import androidx.compose.ui.graphics.SolidColor
39+
import androidx.compose.ui.input.key.Key
40+
import androidx.compose.ui.input.key.KeyEventType
41+
import androidx.compose.ui.input.key.key
42+
import androidx.compose.ui.input.key.onPreviewKeyEvent
43+
import androidx.compose.ui.input.key.type
3944
import androidx.compose.ui.text.style.TextAlign
4045
import androidx.compose.ui.unit.dp
4146
import io.writeopia.common.utils.icons.WrIcons
@@ -128,7 +133,17 @@ fun DesktopNoteEditorScreen(
128133
onValueChange = noteEditorViewModel::searchInDocument,
129134
modifier = Modifier.defaultMinSize(minWidth = 160.dp)
130135
.focusRequester(focusRequester)
131-
.padding(8.dp),
136+
.padding(8.dp)
137+
.onPreviewKeyEvent { keyEvent ->
138+
if (keyEvent.type == KeyEventType.KeyUp &&
139+
keyEvent.key.keyCode == Key.Enter.keyCode
140+
) {
141+
noteEditorViewModel.nextSearchResult()
142+
true
143+
}
144+
145+
false
146+
},
132147
singleLine = true,
133148
textStyle = MaterialTheme.typography.bodySmall.copy(
134149
color = MaterialTheme.colorScheme.onBackground

application/features/editor/src/commonMain/kotlin/io/writeopia/editor/features/editor/viewmodel/NoteEditorKmpViewModel.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,14 @@ class NoteEditorKmpViewModel(
284284
if (finds.isEmpty()) return@combine drawState
285285

286286
val mutableStories = drawState.stories.toMutableList()
287-
val activeFindPosition = if (finds.size > currentSearchIndex) finds.elementAt(currentSearchIndex) else null
287+
val activeFindPosition =
288+
if (finds.size > currentSearchIndex) finds.elementAt(currentSearchIndex) else null
288289
_totalSearchResults.value = finds.size
289290

291+
if (activeFindPosition != null) {
292+
writeopiaManager.scrollToPosition(activeFindPosition)
293+
}
294+
290295
finds.forEach { position ->
291296
val realPosition = minOf(position * 2, mutableStories.lastIndex)
292297
val toDraw = mutableStories[realPosition]
@@ -745,7 +750,8 @@ class NoteEditorKmpViewModel(
745750
return@launch
746751
}
747752

748-
val currentPosition = writeopiaManager.currentStory.value.focus ?: writeopiaManager.currentStory.value.selection.position
753+
val currentPosition = writeopiaManager.currentStory.value.focus
754+
?: writeopiaManager.currentStory.value.selection.position
749755
val newIndex = finds.indexOfFirst { it >= currentPosition }
750756

751757
_currentSearchIndex.value = if (newIndex != -1) newIndex else 0

writeopia_ui/src/commonMain/kotlin/io/writeopia/ui/manager/WriteopiaStateManager.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ class WriteopiaStateManager(
648648
// Todo: Fix this when the inner position are completed
649649
// backStackManager.addAction(BackstackAction.Add(newStory, newPosition))
650650
_currentStory.value = newState.copy(selection = Selection.start())
651-
_scrollToPosition.value = newPosition
651+
_scrollToPosition.value = -1
652652
}
653653
}
654654
}
@@ -660,6 +660,10 @@ class WriteopiaStateManager(
660660
_currentStory.value = story.copy(focus = position)
661661
}
662662

663+
fun scrollToPosition(position: Int) {
664+
_scrollToPosition.value = position
665+
}
666+
663667
private fun selected(isSelected: Boolean, position: Int) {
664668
if (!isEditable) return
665669
if (_currentStory.value.stories[position] != null) {

writeopia_ui/src/jvmMain/kotlin/io/writeopia/ui/drawer/factory/DefaultDrawersDesktop.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object DefaultDrawersDesktop : DrawersFactory {
5757

5858
return CommonDrawers.create(
5959
manager,
60-
30.dp,
60+
300.dp,
6161
aiExplanation,
6262
editable,
6363
onHeaderClick,

0 commit comments

Comments
 (0)