Skip to content

Commit

Permalink
E-book viewer: Prevent the display from sleeping when using auto-scro…
Browse files Browse the repository at this point in the history
…ll or read aloud modes (Implemented only on Windows and macOS)
  • Loading branch information
kovidgoyal committed Oct 24, 2021
1 parent a44cb71 commit 0078c84
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/calibre/gui2/viewer/control_sleep.py
Expand Up @@ -13,12 +13,10 @@

def prevent_sleep(reason=''):
set_thread_execution_state(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)
print(11111111111)
return 1

def allow_sleep(cookie):
set_thread_execution_state(ES_CONTINUOUS)
print(2222222222)
elif ismacos:
from cocoa import (
create_io_pm_assertion, kIOPMAssertionTypeNoDisplaySleep,
Expand Down
21 changes: 21 additions & 0 deletions src/calibre/gui2/viewer/toolbars.py
Expand Up @@ -116,6 +116,7 @@ class ActionsToolBar(ToolBar):
def __init__(self, parent=None):
ToolBar.__init__(self, parent)
self.setObjectName('actions_toolbar')
self.prevent_sleep_cookie = None
self.customContextMenuRequested.connect(self.show_context_menu)

def update_action_state(self, book_open):
Expand Down Expand Up @@ -235,15 +236,35 @@ def update_mode_action(self):
a.setChecked(True)
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):
self.autoscroll_action.setChecked(active)
self.autoscroll_action.setToolTip(
_('Turn off auto-scrolling') if active else _('Turn on auto-scrolling'))
self.change_sleep_permission(active)

def update_read_aloud_action(self, active):
self.toggle_read_aloud_action.setChecked(active)
self.toggle_read_aloud_action.setToolTip(
_('Stop reading') if active else _('Read the text of the book aloud'))
self.change_sleep_permission(active)

def update_reference_mode_action(self, enabled):
self.reference_action.setChecked(enabled)
Expand Down

0 comments on commit 0078c84

Please sign in to comment.