E-book viewer: Prevent the display from sleeping when using auto-scroll or read aloud modes (Implemented only on Windows and macOS)

This commit is contained in:
Kovid Goyal 2021-10-24 12:20:12 +05:30
parent a44cb71cbb
commit 0078c8426f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 21 additions and 2 deletions

View File

@ -13,12 +13,10 @@ if iswindows:
def prevent_sleep(reason=''): def prevent_sleep(reason=''):
set_thread_execution_state(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED) set_thread_execution_state(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)
print(11111111111)
return 1 return 1
def allow_sleep(cookie): def allow_sleep(cookie):
set_thread_execution_state(ES_CONTINUOUS) set_thread_execution_state(ES_CONTINUOUS)
print(2222222222)
elif ismacos: elif ismacos:
from cocoa import ( from cocoa import (
create_io_pm_assertion, kIOPMAssertionTypeNoDisplaySleep, create_io_pm_assertion, kIOPMAssertionTypeNoDisplaySleep,

View File

@ -116,6 +116,7 @@ class ActionsToolBar(ToolBar):
def __init__(self, parent=None): def __init__(self, parent=None):
ToolBar.__init__(self, parent) ToolBar.__init__(self, parent)
self.setObjectName('actions_toolbar') self.setObjectName('actions_toolbar')
self.prevent_sleep_cookie = None
self.customContextMenuRequested.connect(self.show_context_menu) self.customContextMenuRequested.connect(self.show_context_menu)
def update_action_state(self, book_open): def update_action_state(self, book_open):
@ -235,15 +236,35 @@ class ActionsToolBar(ToolBar):
a.setChecked(True) a.setChecked(True)
a.setToolTip(_('Switch to paged mode -- where the text is broken into pages')) a.setToolTip(_('Switch to paged mode -- where the text is broken into pages'))
def change_sleep_permission(self, disallow_sleep=True):
from .control_sleep import prevent_sleep, allow_sleep
if disallow_sleep:
if self.prevent_sleep_cookie is None:
try:
self.prevent_sleep_cookie = prevent_sleep()
except Exception:
import traceback
traceback.print_exc()
else:
if self.prevent_sleep_cookie is not None:
try:
allow_sleep(self.prevent_sleep_cookie)
except Exception:
import traceback
traceback.print_exc()
self.prevent_sleep_cookie = None
def update_autoscroll_action(self, active): def update_autoscroll_action(self, active):
self.autoscroll_action.setChecked(active) self.autoscroll_action.setChecked(active)
self.autoscroll_action.setToolTip( self.autoscroll_action.setToolTip(
_('Turn off auto-scrolling') if active else _('Turn on auto-scrolling')) _('Turn off auto-scrolling') if active else _('Turn on auto-scrolling'))
self.change_sleep_permission(active)
def update_read_aloud_action(self, active): def update_read_aloud_action(self, active):
self.toggle_read_aloud_action.setChecked(active) self.toggle_read_aloud_action.setChecked(active)
self.toggle_read_aloud_action.setToolTip( self.toggle_read_aloud_action.setToolTip(
_('Stop reading') if active else _('Read the text of the book aloud')) _('Stop reading') if active else _('Read the text of the book aloud'))
self.change_sleep_permission(active)
def update_reference_mode_action(self, enabled): def update_reference_mode_action(self, enabled):
self.reference_action.setChecked(enabled) self.reference_action.setChecked(enabled)