Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 38a327cdf6 | |||
| b25679c8de |
@@ -20,13 +20,20 @@ import android.view.WindowManager
|
|||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.navigationBarsPadding
|
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.AlertDialog
|
||||||
import androidx.compose.material3.CircularProgressIndicator
|
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.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.collectAsState
|
import androidx.compose.runtime.collectAsState
|
||||||
@@ -34,6 +41,7 @@ import androidx.compose.runtime.getValue
|
|||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.res.painterResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.core.content.getSystemService
|
import androidx.core.content.getSystemService
|
||||||
import androidx.core.graphics.Insets
|
import androidx.core.graphics.Insets
|
||||||
@@ -271,6 +279,7 @@ class ReaderActivity : BaseActivity() {
|
|||||||
private fun ReaderActivityBinding.setComposeOverlay(): Unit = composeOverlay.setComposeContent {
|
private fun ReaderActivityBinding.setComposeOverlay(): Unit = composeOverlay.setComposeContent {
|
||||||
val state by viewModel.state.collectAsState()
|
val state by viewModel.state.collectAsState()
|
||||||
val showPageNumber by readerPreferences.showPageNumber.collectAsState()
|
val showPageNumber by readerPreferences.showPageNumber.collectAsState()
|
||||||
|
val ttsState by ReaderTtsController.state.collectAsState()
|
||||||
val settingsviewModel = remember {
|
val settingsviewModel = remember {
|
||||||
ReaderSettingsViewModel(
|
ReaderSettingsViewModel(
|
||||||
readerState = viewModel.state,
|
readerState = viewModel.state,
|
||||||
@@ -296,6 +305,31 @@ class ReaderActivity : BaseActivity() {
|
|||||||
ContentOverlay(state = state)
|
ContentOverlay(state = state)
|
||||||
|
|
||||||
AppBars(state = state)
|
AppBars(state = state)
|
||||||
|
|
||||||
|
if (ttsState.active) {
|
||||||
|
IconButton(
|
||||||
|
onClick = ::toggleTts,
|
||||||
|
modifier = Modifier
|
||||||
|
.align(Alignment.BottomEnd)
|
||||||
|
.navigationBarsPadding()
|
||||||
|
.padding(end = 18.dp, bottom = 72.dp)
|
||||||
|
.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),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val onDismissRequest = viewModel::closeDialog
|
val onDismissRequest = viewModel::closeDialog
|
||||||
@@ -701,7 +735,16 @@ class ReaderActivity : BaseActivity() {
|
|||||||
val ttsState = ReaderTtsController.state.value
|
val ttsState = ReaderTtsController.state.value
|
||||||
|
|
||||||
if (ttsState.active && ttsState.readerChapterId == chapterId) {
|
if (ttsState.active && ttsState.readerChapterId == chapterId) {
|
||||||
|
if (ttsState.playing) {
|
||||||
ReaderTtsController.toggle(this)
|
ReaderTtsController.toggle(this)
|
||||||
|
} else {
|
||||||
|
val currentPageIndex = (state.currentPage - 1).coerceAtLeast(0)
|
||||||
|
if (currentPageIndex != ttsState.readerPageIndex) {
|
||||||
|
ReaderTtsController.resumeFromPage(this, currentPageIndex)
|
||||||
|
} else {
|
||||||
|
ReaderTtsController.toggle(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import android.text.SpannableString
|
|||||||
import android.text.StaticLayout
|
import android.text.StaticLayout
|
||||||
import android.text.TextPaint
|
import android.text.TextPaint
|
||||||
import android.text.style.BackgroundColorSpan
|
import android.text.style.BackgroundColorSpan
|
||||||
|
import android.text.style.ForegroundColorSpan
|
||||||
import android.text.style.LineHeightSpan
|
import android.text.style.LineHeightSpan
|
||||||
import eu.kanade.tachiyomi.ui.reader.setting.TextReaderStyle
|
import eu.kanade.tachiyomi.ui.reader.setting.TextReaderStyle
|
||||||
import java.io.ByteArrayInputStream
|
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 ->
|
sentenceHighlightRange?.let { range ->
|
||||||
applyHighlightSpan(
|
applyHighlightSpan(
|
||||||
spannable = spannable,
|
spannable = spannable,
|
||||||
textLength = text.length,
|
textLength = text.length,
|
||||||
range = range,
|
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 ->
|
activeHighlightRange?.let { range ->
|
||||||
@@ -288,7 +291,8 @@ internal object TextPageRenderer {
|
|||||||
spannable = spannable,
|
spannable = spannable,
|
||||||
textLength = text.length,
|
textLength = text.length,
|
||||||
range = range,
|
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
|
return spannable
|
||||||
@@ -298,14 +302,21 @@ internal object TextPageRenderer {
|
|||||||
spannable: SpannableString,
|
spannable: SpannableString,
|
||||||
textLength: Int,
|
textLength: Int,
|
||||||
range: IntRange,
|
range: IntRange,
|
||||||
color: Int,
|
backgroundColor: Int,
|
||||||
|
foregroundColor: Int,
|
||||||
) {
|
) {
|
||||||
if (range.isEmpty()) return
|
if (range.isEmpty()) return
|
||||||
val start = range.first.coerceIn(0, textLength)
|
val start = range.first.coerceIn(0, textLength)
|
||||||
val endExclusive = (range.last + 1).coerceIn(start, textLength)
|
val endExclusive = (range.last + 1).coerceIn(start, textLength)
|
||||||
if (start >= endExclusive) return
|
if (start >= endExclusive) return
|
||||||
spannable.setSpan(
|
spannable.setSpan(
|
||||||
BackgroundColorSpan(color),
|
BackgroundColorSpan(backgroundColor),
|
||||||
|
start,
|
||||||
|
endExclusive,
|
||||||
|
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
|
||||||
|
)
|
||||||
|
spannable.setSpan(
|
||||||
|
ForegroundColorSpan(foregroundColor),
|
||||||
start,
|
start,
|
||||||
endExclusive,
|
endExclusive,
|
||||||
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
|
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
|
||||||
|
|||||||
@@ -57,6 +57,15 @@ object ReaderTtsController {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun resumeFromPage(context: Context, readerPageIndex: Int) {
|
||||||
|
if (!state.value.active) return
|
||||||
|
context.startService(
|
||||||
|
Intent(context, ReaderTtsService::class.java)
|
||||||
|
.setAction(ReaderTtsService.ACTION_RESUME_FROM_PAGE)
|
||||||
|
.putExtra(ReaderTtsService.EXTRA_START_PAGE_INDEX, readerPageIndex),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fun stop(context: Context) {
|
fun stop(context: Context) {
|
||||||
if (!state.value.active) return
|
if (!state.value.active) return
|
||||||
context.startService(
|
context.startService(
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ class ReaderTtsService : Service() {
|
|||||||
when (intent?.action) {
|
when (intent?.action) {
|
||||||
ACTION_START -> startSession(intent)
|
ACTION_START -> startSession(intent)
|
||||||
ACTION_TOGGLE -> if (playing) pausePlayback() else resumePlayback()
|
ACTION_TOGGLE -> if (playing) pausePlayback() else resumePlayback()
|
||||||
|
ACTION_RESUME_FROM_PAGE -> resumeFromPage(
|
||||||
|
intent.getIntExtra(EXTRA_START_PAGE_INDEX, -1),
|
||||||
|
)
|
||||||
ACTION_STOP -> stopPlayback()
|
ACTION_STOP -> stopPlayback()
|
||||||
ACTION_SET_SPEED -> {
|
ACTION_SET_SPEED -> {
|
||||||
playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, playbackSpeed).coerceIn(MIN_SPEED, MAX_SPEED)
|
playbackSpeed = intent.getFloatExtra(EXTRA_SPEED, playbackSpeed).coerceIn(MIN_SPEED, MAX_SPEED)
|
||||||
@@ -204,6 +207,28 @@ class ReaderTtsService : Service() {
|
|||||||
speakCurrentSegment()
|
speakCurrentSegment()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun resumeFromPage(readerPageIndex: Int) {
|
||||||
|
if (!initialized || readerPageIndex < 0) return
|
||||||
|
val session = session ?: return
|
||||||
|
val newPagePosition = session.pages.indexOfFirst { it.readerPageIndex >= readerPageIndex }
|
||||||
|
.takeIf { it >= 0 }
|
||||||
|
?: return
|
||||||
|
|
||||||
|
tts?.stop()
|
||||||
|
pagePosition = newPagePosition
|
||||||
|
pageSpeechStartOffset = 0
|
||||||
|
currentWindow = null
|
||||||
|
currentSegments = emptyList()
|
||||||
|
lookaheadConsumedChars = emptyMap()
|
||||||
|
segmentPosition = 0
|
||||||
|
segmentSpeechOffset = 0
|
||||||
|
currentUtteranceBaseOffset = 0
|
||||||
|
currentUtteranceId = null
|
||||||
|
playing = true
|
||||||
|
updatePlaybackState(clearHighlight = true)
|
||||||
|
speakCurrentSegment()
|
||||||
|
}
|
||||||
|
|
||||||
private fun speakCurrentSegment() {
|
private fun speakCurrentSegment() {
|
||||||
if (!playing || !initialized) return
|
if (!playing || !initialized) return
|
||||||
|
|
||||||
@@ -419,8 +444,16 @@ class ReaderTtsService : Service() {
|
|||||||
active = true,
|
active = true,
|
||||||
playing = playing,
|
playing = playing,
|
||||||
readerChapterId = readerChapterId,
|
readerChapterId = readerChapterId,
|
||||||
readerPageIndex = page.readerPageIndex,
|
readerPageIndex = if (!clearHighlight && previous.readerPageIndex >= 0) {
|
||||||
documentIndex = page.documentIndex,
|
previous.readerPageIndex
|
||||||
|
} else {
|
||||||
|
page.readerPageIndex
|
||||||
|
},
|
||||||
|
documentIndex = if (!clearHighlight && previous.documentIndex >= 0) {
|
||||||
|
previous.documentIndex
|
||||||
|
} else {
|
||||||
|
page.documentIndex
|
||||||
|
},
|
||||||
sourceSentenceStart = if (clearHighlight) -1L else previous.sourceSentenceStart,
|
sourceSentenceStart = if (clearHighlight) -1L else previous.sourceSentenceStart,
|
||||||
sourceSentenceEndExclusive = if (clearHighlight) -1L else previous.sourceSentenceEndExclusive,
|
sourceSentenceEndExclusive = if (clearHighlight) -1L else previous.sourceSentenceEndExclusive,
|
||||||
sourceHighlightStart = if (clearHighlight) -1L else previous.sourceHighlightStart,
|
sourceHighlightStart = if (clearHighlight) -1L else previous.sourceHighlightStart,
|
||||||
@@ -688,6 +721,7 @@ class ReaderTtsService : Service() {
|
|||||||
companion object {
|
companion object {
|
||||||
const val ACTION_START = "eu.kanade.tachiyomi.reader.tts.START"
|
const val ACTION_START = "eu.kanade.tachiyomi.reader.tts.START"
|
||||||
const val ACTION_TOGGLE = "eu.kanade.tachiyomi.reader.tts.TOGGLE"
|
const val ACTION_TOGGLE = "eu.kanade.tachiyomi.reader.tts.TOGGLE"
|
||||||
|
const val ACTION_RESUME_FROM_PAGE = "eu.kanade.tachiyomi.reader.tts.RESUME_FROM_PAGE"
|
||||||
const val ACTION_STOP = "eu.kanade.tachiyomi.reader.tts.STOP"
|
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_SPEED = "eu.kanade.tachiyomi.reader.tts.SET_SPEED"
|
||||||
const val ACTION_SET_ENGINE = "eu.kanade.tachiyomi.reader.tts.SET_ENGINE"
|
const val ACTION_SET_ENGINE = "eu.kanade.tachiyomi.reader.tts.SET_ENGINE"
|
||||||
|
|||||||
Reference in New Issue
Block a user