Skip to content

Commit

Permalink
Add [All] selection to WordSelectionText
Browse files Browse the repository at this point in the history
  • Loading branch information
firemaples committed Jan 21, 2024
1 parent 99a5b4a commit 2ff47c2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import tw.firemaples.onscreenocr.R
import tw.firemaples.onscreenocr.utils.WordBoundary
import java.util.Locale

Expand All @@ -27,7 +29,8 @@ fun WordSelectionText(
) {
var selectedStart by remember { mutableStateOf(-1) }
val annotatedString = buildText(
text = text,
fullText = text,
textAll = stringResource(id = R.string.text_all_text),
locale = locale,
selectedStart = selectedStart,
selectedSpanStyle = selectedSpanStyle,
Expand All @@ -40,24 +43,23 @@ fun WordSelectionText(
val clicked = annotatedString.getStringAnnotations(offset, offset)
.firstOrNull()
if (clicked != null) {
if (selectedStart == clicked.start) {
selectedStart = -1
} else {
selectedStart = clicked.start
onTextSelected.invoke(clicked.item)
}
selectedStart = clicked.start
onTextSelected.invoke(clicked.tag)
}
},
)
}

private fun buildText(
text: String,
fullText: String,
textAll: String,
locale: Locale,
selectedStart: Int,
selectedSpanStyle: SpanStyle
) = buildAnnotatedString {
if (text.isEmpty()) return@buildAnnotatedString
if (fullText.isEmpty()) return@buildAnnotatedString

val text = "$textAll $fullText"

val boundaries = WordBoundary.breakWords(text = text, locale = locale)
if (boundaries.isEmpty()) {
Expand All @@ -83,13 +85,31 @@ private fun buildText(
append(text.substring(textStart until nextBoundary.start))
textStart = nextBoundary.start
} else if (textStart == nextBoundary.start) {
val style = if (textStart == selectedStart) selectedStyle else unselectedStyle
val word = text.substring(nextBoundary.start until nextBoundary.end)
withStyle(style = style) {
pushStringAnnotation(tag = word, annotation = word)
append(word)
val style = if (textStart == selectedStart)
selectedStyle else unselectedStyle

if (nextBoundary.start < textAll.length) {
while (true) {
val next = boundaries.getOrNull(index + 1)
if (next == null || next.start >= textAll.length) {
break
}
index++
}

withStyle(style = style) {
pushStringAnnotation(tag = fullText, annotation = textAll)
append(textAll)
}
textStart = textAll.length
} else {
val word = text.substring(nextBoundary.start until nextBoundary.end)
withStyle(style = style) {
pushStringAnnotation(tag = word, annotation = word)
append(word)
}
textStart = nextBoundary.end
}
textStart = nextBoundary.end
index++
}
}
Expand Down
3 changes: 2 additions & 1 deletion main/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<string name="msg_ocr_only_mode_hint">Translation language selection is not available for OCR only mode</string>
<string name="text_request_permission">Request permission</string>
<string name="text_open_in_browser">Open in Browser</string>
<string name="text_all_text" translatable="false">[All]</string>

<string name="msg_evertranslator_needs_display_over_other_apps_permission_to_show_a_floating_window_for_easily_using">EverTranslator needs the [Display over other apps] permission to show a floating window on the screen.</string>
<string name="msg_evertranslator_needs_capture_screen_to_find_the_text_from_the_screen_for_you">EverTranslator needs the [Capture Screen] permission to find the text from the screen for you.</string>
Expand Down Expand Up @@ -117,4 +118,4 @@
<item>None</item>
</string-array>
<string name="pref_title_keep_media_projection_resources">Keep MediaProjection resources</string>
</resources>
</resources>

0 comments on commit 2ff47c2

Please sign in to comment.