Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Editor Usability #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion app/src/main/java/com/farmerbb/notepad/model/NavState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package com.farmerbb.notepad.model
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.ui.geometry.Offset

private const val VIEW = "View"
private const val EDIT = "Edit"

sealed interface NavState {
object Empty: NavState
data class View(val id: Long): NavState
data class Edit(val id: Long? = null): NavState
data class Edit(val id: Long? = null, val offset: Offset?= null): NavState
}

val navStateSaver = Saver<MutableState<NavState>, Pair<String, Long?>>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.OutputTransformation
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
Expand All @@ -29,9 +31,11 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardCapitalization
Expand All @@ -44,9 +48,9 @@ import com.farmerbb.notepad.ui.components.RtlTextWrapper
import com.farmerbb.notepad.ui.previews.EditNotePreview
import kotlinx.coroutines.delay

private fun String.toTextFieldValue() = TextFieldValue(
text = this,
selection = TextRange(length)
private fun String.toTextFieldState() = TextFieldState(
initialText = this,
initialSelection = TextRange(length)
)

@Composable
Expand All @@ -57,18 +61,20 @@ fun EditNoteContent(
isPrinting: Boolean = false,
waitForAnimation: Boolean = false,
rtlLayout: Boolean = false,
onTextChanged: (String) -> Unit = {}
offset: Offset? = null,
onTextChanged: (String) -> Unit = {},
) {
val textStyle = if (isPrinting) {
baseTextStyle.copy(color = Color.Black)
} else baseTextStyle

val focusRequester = remember { FocusRequester() }
var value by remember { mutableStateOf(text.toTextFieldValue()) }
var value by remember { mutableStateOf(text.toTextFieldState()) }


LaunchedEffect(text) {
if (text != value.text) {
value = text.toTextFieldValue()
value = text.toTextFieldState()
}
}

Expand All @@ -80,18 +86,20 @@ fun EditNoteContent(
}
)

val layoutResult = remember { mutableStateOf<TextLayoutResult?>(null) }

RtlTextWrapper(text, rtlLayout) {
BasicTextField(
value = value,
onValueChange = {
value = it
onTextChanged(it.text)
},
state = value,
outputTransformation = OutputTransformation { onTextChanged(value.text.toString()) },
textStyle = textStyle,
cursorBrush = brush,
keyboardOptions = KeyboardOptions(
capitalization = KeyboardCapitalization.Sentences
),
onTextLayout = {
layoutResult.value = it()
},
modifier = Modifier
.padding(
horizontal = 16.dp,
Expand Down Expand Up @@ -122,6 +130,13 @@ fun EditNoteContent(
delay(200)
}

offset?.let { offset ->
layoutResult.value?.let { layoutResult ->
val position = layoutResult.getOffsetForPosition(offset)
value.edit { selection = TextRange(position) }
}
}

focusRequester.requestFocus()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fun ViewNoteContent(
isPrinting: Boolean = false,
showDoubleTapMessage: Boolean = false,
doubleTapMessageShown: () -> Unit = {},
onDoubleTap: () -> Unit = {}
onDoubleTap: (Offset?) -> Unit = {}
) {
val textStyle = if (isPrinting) {
baseTextStyle.copy(color = Color.Black)
Expand All @@ -76,23 +76,6 @@ fun ViewNoteContent(
Column(
modifier = Modifier
.fillMaxSize()
.pointerInteropFilter { motionEvent ->
if (motionEvent.action == MotionEvent.ACTION_DOWN) {
val now = System.currentTimeMillis()
val offset = Offset(motionEvent.x, motionEvent.y)
val rect = Rect(center = lastOffset, radius = radius)

when {
doubleTapTime > now && rect.contains(offset) -> onDoubleTap()
showDoubleTapMessage -> doubleTapMessageShown()
}

doubleTapTime = now + 200
lastOffset = offset
}

false
}
) {
Box(
modifier = if (isPrinting) Modifier else Modifier
Expand All @@ -104,6 +87,24 @@ fun ViewNoteContent(
vertical = 12.dp
)
.fillMaxWidth()
.pointerInteropFilter { motionEvent ->
if (motionEvent.action == MotionEvent.ACTION_DOWN) {
val now = System.currentTimeMillis()
val offset = Offset(motionEvent.x, motionEvent.y)
val rect = Rect(center = lastOffset, radius = radius)

val cursorAtOffset: Offset? = if (!markdown) offset else null
when {
doubleTapTime > now && rect.contains(offset) -> onDoubleTap(cursorAtOffset)
showDoubleTapMessage -> doubleTapMessageShown()
}

doubleTapTime = now + 200
lastOffset = offset
}

false
}

RtlTextWrapper(text, rtlLayout) {
SelectionContainer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ private fun NotepadComposeApp(
isPrinting = isPrinting,
showDoubleTapMessage = showDoubleTapMessage,
doubleTapMessageShown = vm::doubleTapMessageShown
) { navState = Edit(note.id) }
) { offset -> navState = Edit(note.id, offset) }
}
}
}
Expand Down Expand Up @@ -599,7 +599,8 @@ private fun NotepadComposeApp(
isPrinting = isPrinting,
waitForAnimation = note.id == -1L || directEdit,
rtlLayout = rtlLayout,
onTextChanged = vm::setText
onTextChanged = vm::setText,
offset = state.offset
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ private fun StandaloneEditor(
text = text,
baseTextStyle = textStyle,
isLightTheme = isLightTheme,
rtlLayout = rtlLayout
rtlLayout = rtlLayout,
offset = null
) { text = it }
}
)
Expand Down