E-book viewer: Read aloud: Fix a regression in the previous release that caused an error when using Read aloud on a chapter with no text, such as the cover page. Fixes #2006062 [Read Aloud not working on Windows](https://bugs.launchpad.net/calibre/+bug/2006062)

This commit is contained in:
Kovid Goyal 2023-02-07 11:56:44 +05:30
parent d0cbe42b30
commit 2053856864
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -32,6 +32,13 @@ def split_into_chunks(marked_text, chunk_size):
yield chunk
def chunk_has_text(chunk):
for x in chunk:
if isinstance(x, str) and x:
return True
return False
class Client:
mark_template = ''
@ -73,7 +80,19 @@ class Client:
self.dispatch_on_main_thread(partial(self.handle_event, msg))
def speak_current_chunk(self):
self.backend.speak(self.current_chunks[self.current_chunk_idx], is_cued=True)
chunk = self.current_chunks[self.current_chunk_idx]
if chunk_has_text(chunk):
self.backend.speak(chunk, is_cued=True)
else:
self.handle_end_event()
def handle_end_event(self):
if self.current_chunk_idx >= len(self.current_chunks) - 1:
self.clear_chunks()
self.callback_ignoring_errors(Event(EventType.end))
else:
self.current_chunk_idx += 1
self.speak_current_chunk()
def handle_event(self, x):
if isinstance(x, MarkReached):
@ -83,12 +102,7 @@ class Client:
elif isinstance(x, MediaStateChanged):
if self.current_chunks:
if x.state is MediaState.ended:
if self.current_chunk_idx >= len(self.current_chunks) - 1:
self.clear_chunks()
self.callback_ignoring_errors(Event(EventType.end))
else:
self.current_chunk_idx += 1
self.speak_current_chunk()
self.handle_end_event()
elif x.state is MediaState.failed:
self.clear_chunks()
self.callback_ignoring_errors(Event(EventType.cancel))