Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ba1b4d2362 | |||
| 023c3bb2f1 | |||
| aec03c6c71 | |||
| c9c119689d | |||
| 38a327cdf6 | |||
| b25679c8de |
@@ -20,13 +20,20 @@ import android.view.WindowManager
|
||||
import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.viewModels
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.navigationBarsPadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
@@ -34,6 +41,7 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.getSystemService
|
||||
import androidx.core.graphics.Insets
|
||||
@@ -271,6 +279,7 @@ class ReaderActivity : BaseActivity() {
|
||||
private fun ReaderActivityBinding.setComposeOverlay(): Unit = composeOverlay.setComposeContent {
|
||||
val state by viewModel.state.collectAsState()
|
||||
val showPageNumber by readerPreferences.showPageNumber.collectAsState()
|
||||
val ttsState by ReaderTtsController.state.collectAsState()
|
||||
val settingsviewModel = remember {
|
||||
ReaderSettingsViewModel(
|
||||
readerState = viewModel.state,
|
||||
@@ -296,6 +305,54 @@ class ReaderActivity : BaseActivity() {
|
||||
ContentOverlay(state = state)
|
||||
|
||||
AppBars(state = state)
|
||||
|
||||
val showFloatingTts = ttsState.active &&
|
||||
ttsState.readerChapterId == state.currentChapter?.chapter?.id &&
|
||||
state.currentChapter?.pages?.any { it is TextReaderPage } == true
|
||||
if (showFloatingTts) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.navigationBarsPadding()
|
||||
.padding(end = 18.dp, bottom = 72.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(10.dp),
|
||||
) {
|
||||
IconButton(
|
||||
onClick = ::toggleTts,
|
||||
modifier = Modifier
|
||||
.size(52.dp)
|
||||
.background(
|
||||
color = MaterialTheme.colorScheme.surface.copy(alpha = 0.55f),
|
||||
shape = CircleShape,
|
||||
),
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(
|
||||
if (ttsState.playing) R.drawable.ic_pause_24dp else R.drawable.ic_play_arrow_24dp,
|
||||
),
|
||||
contentDescription = stringResource(
|
||||
if (ttsState.playing) MR.strings.reader_tts_pause else MR.strings.reader_tts_start,
|
||||
),
|
||||
tint = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.92f),
|
||||
)
|
||||
}
|
||||
IconButton(
|
||||
onClick = { ReaderTtsController.stop(this@ReaderActivity) },
|
||||
modifier = Modifier
|
||||
.size(52.dp)
|
||||
.background(
|
||||
color = MaterialTheme.colorScheme.surface.copy(alpha = 0.55f),
|
||||
shape = CircleShape,
|
||||
),
|
||||
) {
|
||||
Icon(
|
||||
painter = painterResource(R.drawable.ic_stop_24dp),
|
||||
contentDescription = stringResource(MR.strings.reader_tts_stop),
|
||||
tint = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.92f),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val onDismissRequest = viewModel::closeDialog
|
||||
@@ -701,7 +758,21 @@ class ReaderActivity : BaseActivity() {
|
||||
val ttsState = ReaderTtsController.state.value
|
||||
|
||||
if (ttsState.active && ttsState.readerChapterId == chapterId) {
|
||||
ReaderTtsController.toggle(this)
|
||||
if (ttsState.playing) {
|
||||
ReaderTtsController.toggle(this)
|
||||
} else {
|
||||
val currentPage = chapter.pages
|
||||
?.getOrNull((state.currentPage - 1).coerceAtLeast(0)) as? TextReaderPage
|
||||
if (currentPage != null) {
|
||||
ReaderTtsController.resumeFromSource(
|
||||
context = this,
|
||||
documentIndex = currentPage.anchor.documentIndex,
|
||||
sourceOffset = currentPage.anchor.startOffset,
|
||||
)
|
||||
} else {
|
||||
ReaderTtsController.toggle(this)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -799,10 +870,13 @@ class ReaderActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
ttsHighlightedPage = targetPage
|
||||
if (state.currentPage != targetPage.index + 1) {
|
||||
val targetPageIsVisible = state.currentPage == targetPage.index + 1
|
||||
if (ttsState.playing && !targetPageIsVisible) {
|
||||
moveToPageIndex(targetPage.index)
|
||||
}
|
||||
state.viewer?.refreshCurrentPage()
|
||||
if (ttsState.playing || targetPageIsVisible) {
|
||||
state.viewer?.refreshCurrentPage()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.text.SpannableString
|
||||
import android.text.StaticLayout
|
||||
import android.text.TextPaint
|
||||
import android.text.style.BackgroundColorSpan
|
||||
import android.text.style.ForegroundColorSpan
|
||||
import android.text.style.LineHeightSpan
|
||||
import eu.kanade.tachiyomi.ui.reader.setting.TextReaderStyle
|
||||
import java.io.ByteArrayInputStream
|
||||
@@ -274,13 +275,15 @@ internal object TextPageRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
val darkBackground = resolveColors(style).first == Color.BLACK
|
||||
val (_, baseTextColor) = resolveColors(style)
|
||||
val darkTheme = baseTextColor == Color.WHITE
|
||||
sentenceHighlightRange?.let { range ->
|
||||
applyHighlightSpan(
|
||||
spannable = spannable,
|
||||
textLength = text.length,
|
||||
range = range,
|
||||
color = if (darkBackground) Color.rgb(72, 68, 40) else Color.rgb(255, 249, 196),
|
||||
backgroundColor = if (darkTheme) Color.rgb(77, 66, 25) else Color.rgb(255, 243, 176),
|
||||
foregroundColor = if (darkTheme) Color.WHITE else Color.BLACK,
|
||||
)
|
||||
}
|
||||
activeHighlightRange?.let { range ->
|
||||
@@ -288,7 +291,8 @@ internal object TextPageRenderer {
|
||||
spannable = spannable,
|
||||
textLength = text.length,
|
||||
range = range,
|
||||
color = if (darkBackground) Color.rgb(150, 117, 0) else Color.rgb(255, 193, 7),
|
||||
backgroundColor = if (darkTheme) Color.rgb(255, 193, 7) else Color.rgb(255, 179, 0),
|
||||
foregroundColor = Color.BLACK,
|
||||
)
|
||||
}
|
||||
return spannable
|
||||
@@ -298,14 +302,21 @@ internal object TextPageRenderer {
|
||||
spannable: SpannableString,
|
||||
textLength: Int,
|
||||
range: IntRange,
|
||||
color: Int,
|
||||
backgroundColor: Int,
|
||||
foregroundColor: Int,
|
||||
) {
|
||||
if (range.isEmpty()) return
|
||||
val start = range.first.coerceIn(0, textLength)
|
||||
val endExclusive = (range.last + 1).coerceIn(start, textLength)
|
||||
if (start >= endExclusive) return
|
||||
spannable.setSpan(
|
||||
BackgroundColorSpan(color),
|
||||
BackgroundColorSpan(backgroundColor),
|
||||
start,
|
||||
endExclusive,
|
||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
|
||||
)
|
||||
spannable.setSpan(
|
||||
ForegroundColorSpan(foregroundColor),
|
||||
start,
|
||||
endExclusive,
|
||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
|
||||
|
||||
@@ -57,6 +57,20 @@ object ReaderTtsController {
|
||||
)
|
||||
}
|
||||
|
||||
fun resumeFromSource(
|
||||
context: Context,
|
||||
documentIndex: Int,
|
||||
sourceOffset: Long,
|
||||
) {
|
||||
if (!state.value.active) return
|
||||
context.startService(
|
||||
Intent(context, ReaderTtsService::class.java)
|
||||
.setAction(ReaderTtsService.ACTION_RESUME_FROM_SOURCE)
|
||||
.putExtra(ReaderTtsService.EXTRA_DOCUMENT_INDEX, documentIndex)
|
||||
.putExtra(ReaderTtsService.EXTRA_SOURCE_OFFSET, sourceOffset),
|
||||
)
|
||||
}
|
||||
|
||||
fun stop(context: Context) {
|
||||
if (!state.value.active) return
|
||||
context.startService(
|
||||
|
||||
@@ -46,6 +46,10 @@ class ReaderTtsService : Service() {
|
||||
when (intent?.action) {
|
||||
ACTION_START -> startSession(intent)
|
||||
ACTION_TOGGLE -> if (playing) pausePlayback() else resumePlayback()
|
||||
ACTION_RESUME_FROM_SOURCE -> resumeFromSource(
|
||||
documentIndex = intent.getIntExtra(EXTRA_DOCUMENT_INDEX, -1),
|
||||
sourceOffset = intent.getLongExtra(EXTRA_SOURCE_OFFSET, -1L),
|
||||
)
|
||||
ACTION_STOP -> stopPlayback()
|
||||
ACTION_SET_SPEED -> {
|
||||
playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, playbackSpeed).coerceIn(MIN_SPEED, MAX_SPEED)
|
||||
@@ -204,6 +208,70 @@ class ReaderTtsService : Service() {
|
||||
speakCurrentSegment()
|
||||
}
|
||||
|
||||
private fun resumeFromSource(
|
||||
documentIndex: Int,
|
||||
sourceOffset: Long,
|
||||
) {
|
||||
if (!initialized || documentIndex < 0 || sourceOffset < 0L) return
|
||||
val session = session ?: return
|
||||
|
||||
val exactPosition = session.pages.indexOfFirst { entry ->
|
||||
entry.documentIndex == documentIndex &&
|
||||
sourceOffset >= entry.sourceStartOffset &&
|
||||
sourceOffset < entry.sourceEndOffsetExclusive
|
||||
}
|
||||
val nextPosition = if (exactPosition < 0) {
|
||||
session.pages.indexOfFirst { entry ->
|
||||
entry.documentIndex == documentIndex && entry.sourceStartOffset >= sourceOffset
|
||||
}
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
val previousPosition = if (exactPosition < 0 && nextPosition < 0) {
|
||||
session.pages.indexOfLast { entry ->
|
||||
entry.documentIndex == documentIndex && entry.sourceStartOffset <= sourceOffset
|
||||
}
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
val newPagePosition = when {
|
||||
exactPosition >= 0 -> exactPosition
|
||||
nextPosition >= 0 -> nextPosition
|
||||
previousPosition >= 0 -> previousPosition
|
||||
else -> return
|
||||
}
|
||||
val entry = session.pages[newPagePosition]
|
||||
val relativeOffset = (sourceOffset - entry.sourceStartOffset)
|
||||
.coerceAtLeast(0L)
|
||||
.coerceAtMost(Int.MAX_VALUE.toLong())
|
||||
.toInt()
|
||||
|
||||
tts?.stop()
|
||||
pagePosition = newPagePosition
|
||||
pageSpeechStartOffset = relativeOffset
|
||||
currentWindow = null
|
||||
currentSegments = emptyList()
|
||||
lookaheadConsumedChars = emptyMap()
|
||||
segmentPosition = 0
|
||||
segmentSpeechOffset = 0
|
||||
currentUtteranceBaseOffset = 0
|
||||
currentUtteranceId = null
|
||||
playing = true
|
||||
ReaderTtsController.updateState(
|
||||
ReaderTtsController.State(
|
||||
active = true,
|
||||
playing = true,
|
||||
readerChapterId = readerChapterId,
|
||||
readerPageIndex = entry.readerPageIndex,
|
||||
documentIndex = documentIndex,
|
||||
sourceSentenceStart = sourceOffset,
|
||||
sourceSentenceEndExclusive = sourceOffset,
|
||||
),
|
||||
)
|
||||
refreshNotification()
|
||||
speakCurrentSegment()
|
||||
}
|
||||
|
||||
private fun speakCurrentSegment() {
|
||||
if (!playing || !initialized) return
|
||||
|
||||
@@ -419,8 +487,16 @@ class ReaderTtsService : Service() {
|
||||
active = true,
|
||||
playing = playing,
|
||||
readerChapterId = readerChapterId,
|
||||
readerPageIndex = page.readerPageIndex,
|
||||
documentIndex = page.documentIndex,
|
||||
readerPageIndex = if (!clearHighlight && previous.readerPageIndex >= 0) {
|
||||
previous.readerPageIndex
|
||||
} else {
|
||||
page.readerPageIndex
|
||||
},
|
||||
documentIndex = if (!clearHighlight && previous.documentIndex >= 0) {
|
||||
previous.documentIndex
|
||||
} else {
|
||||
page.documentIndex
|
||||
},
|
||||
sourceSentenceStart = if (clearHighlight) -1L else previous.sourceSentenceStart,
|
||||
sourceSentenceEndExclusive = if (clearHighlight) -1L else previous.sourceSentenceEndExclusive,
|
||||
sourceHighlightStart = if (clearHighlight) -1L else previous.sourceHighlightStart,
|
||||
@@ -688,6 +764,7 @@ class ReaderTtsService : Service() {
|
||||
companion object {
|
||||
const val ACTION_START = "eu.kanade.tachiyomi.reader.tts.START"
|
||||
const val ACTION_TOGGLE = "eu.kanade.tachiyomi.reader.tts.TOGGLE"
|
||||
const val ACTION_RESUME_FROM_SOURCE = "eu.kanade.tachiyomi.reader.tts.RESUME_FROM_SOURCE"
|
||||
const val ACTION_STOP = "eu.kanade.tachiyomi.reader.tts.STOP"
|
||||
const val ACTION_SET_SPEED = "eu.kanade.tachiyomi.reader.tts.SET_SPEED"
|
||||
const val ACTION_SET_ENGINE = "eu.kanade.tachiyomi.reader.tts.SET_ENGINE"
|
||||
@@ -695,6 +772,8 @@ class ReaderTtsService : Service() {
|
||||
|
||||
const val EXTRA_SESSION_ID = "session_id"
|
||||
const val EXTRA_START_PAGE_INDEX = "start_page_index"
|
||||
const val EXTRA_DOCUMENT_INDEX = "document_index"
|
||||
const val EXTRA_SOURCE_OFFSET = "source_offset"
|
||||
const val EXTRA_CHAPTER_ID = "chapter_id"
|
||||
const val EXTRA_TITLE = "title"
|
||||
const val EXTRA_SUBTITLE = "subtitle"
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M6,6h12v12H6z" />
|
||||
</vector>
|
||||
@@ -1110,6 +1110,7 @@
|
||||
<string name="pref_text_reader_tts_voice_settings">Voice data settings</string>
|
||||
<string name="reader_tts_start">Start TTS</string>
|
||||
<string name="reader_tts_pause">Pause TTS</string>
|
||||
<string name="reader_tts_stop">Stop TTS</string>
|
||||
<string name="pref_text_reader_line_spacing">Line spacing</string>
|
||||
<string name="pref_text_reader_paragraph_spacing">Paragraph spacing</string>
|
||||
<string name="pref_text_reader_font_family">Font</string>
|
||||
|
||||
@@ -918,6 +918,7 @@
|
||||
<string name="pref_text_reader_tts_voice_settings">음성 데이터 설정</string>
|
||||
<string name="reader_tts_start">TTS 시작</string>
|
||||
<string name="reader_tts_pause">TTS 일시정지</string>
|
||||
<string name="reader_tts_stop">TTS 정지</string>
|
||||
<string name="pref_text_reader_line_spacing">행간</string>
|
||||
<string name="pref_text_reader_paragraph_spacing">문단 간격</string>
|
||||
<string name="pref_text_reader_font_family">글꼴</string>
|
||||
|
||||
Reference in New Issue
Block a user