mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-08-05 08:40:13 -04:00
DRYer
This commit is contained in:
parent
f7733bd2b4
commit
3ef6188559
@ -222,11 +222,19 @@ def layout(is_single_page):
|
|||||||
fit_images()
|
fit_images()
|
||||||
return gap
|
return gap
|
||||||
|
|
||||||
|
def current_scroll_offset():
|
||||||
|
return window.pageXOffset
|
||||||
|
|
||||||
|
|
||||||
|
def scroll_to_offset(x):
|
||||||
|
window.scrollTo(x, 0)
|
||||||
|
|
||||||
|
|
||||||
def scroll_to_column(number, notify=False, duration=1000):
|
def scroll_to_column(number, notify=False, duration=1000):
|
||||||
pos = number * col_and_gap
|
pos = number * col_and_gap
|
||||||
limit = document.body.scrollWidth - screen_width
|
limit = document.body.scrollWidth - screen_width
|
||||||
pos = min(pos, limit)
|
pos = min(pos, limit)
|
||||||
window.scrollTo(pos, 0)
|
scroll_to_offset(pos)
|
||||||
|
|
||||||
def scroll_to_xpos(xpos, notify=False, duration=1000):
|
def scroll_to_xpos(xpos, notify=False, duration=1000):
|
||||||
# Scroll so that the column containing xpos is the left most column in
|
# Scroll so that the column containing xpos is the left most column in
|
||||||
@ -235,7 +243,7 @@ def scroll_to_xpos(xpos, notify=False, duration=1000):
|
|||||||
print(xpos, 'is not a number, cannot scroll to it!')
|
print(xpos, 'is not a number, cannot scroll to it!')
|
||||||
return
|
return
|
||||||
if is_full_screen_layout:
|
if is_full_screen_layout:
|
||||||
window.scrollTo(0, 0)
|
scroll_to_offset(0)
|
||||||
return
|
return
|
||||||
scroll_to_column(column_at(xpos), notify=notify, duration=duration)
|
scroll_to_column(column_at(xpos), notify=notify, duration=duration)
|
||||||
|
|
||||||
@ -272,7 +280,7 @@ def column_location(elem):
|
|||||||
def column_boundaries():
|
def column_boundaries():
|
||||||
# Return the column numbers at the left edge and after the right edge
|
# Return the column numbers at the left edge and after the right edge
|
||||||
# of the viewport
|
# of the viewport
|
||||||
l = column_at(window.pageXOffset + 10)
|
l = column_at(current_scroll_offset() + 10)
|
||||||
return l, l + cols_per_screen
|
return l, l + cols_per_screen
|
||||||
|
|
||||||
def current_column_location():
|
def current_column_location():
|
||||||
@ -280,7 +288,7 @@ def current_column_location():
|
|||||||
# visible in the viewport
|
# visible in the viewport
|
||||||
if is_full_screen_layout:
|
if is_full_screen_layout:
|
||||||
return 0
|
return 0
|
||||||
c = column_at(window.pageXOffset + 10)
|
c = column_at(current_scroll_offset() + 10)
|
||||||
return c * col_and_gap
|
return c * col_and_gap
|
||||||
|
|
||||||
def next_screen_location():
|
def next_screen_location():
|
||||||
@ -291,7 +299,7 @@ def next_screen_location():
|
|||||||
cc = current_column_location()
|
cc = current_column_location()
|
||||||
ans = cc + screen_width
|
ans = cc + screen_width
|
||||||
if cols_per_screen > 1:
|
if cols_per_screen > 1:
|
||||||
current_col = column_at(window.pageXOffset + 10)
|
current_col = column_at(current_scroll_offset() + 10)
|
||||||
ncols = (document.body.scrollWidth + gap) // col_and_gap
|
ncols = (document.body.scrollWidth + gap) // col_and_gap
|
||||||
cols_left = ncols - (current_col + cols_per_screen)
|
cols_left = ncols - (current_col + cols_per_screen)
|
||||||
if cols_left < cols_per_screen:
|
if cols_left < cols_per_screen:
|
||||||
@ -300,7 +308,7 @@ def next_screen_location():
|
|||||||
if limit < col_and_gap:
|
if limit < col_and_gap:
|
||||||
return -1
|
return -1
|
||||||
if ans > limit:
|
if ans > limit:
|
||||||
ans = limit if window.pageXOffset < limit else -1
|
ans = limit if current_scroll_offset() < limit else -1
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def previous_screen_location():
|
def previous_screen_location():
|
||||||
@ -313,7 +321,7 @@ def previous_screen_location():
|
|||||||
if ans < 0:
|
if ans < 0:
|
||||||
# We ignore small scrolls (less than 15px) when going to previous
|
# We ignore small scrolls (less than 15px) when going to previous
|
||||||
# screen
|
# screen
|
||||||
ans = 0 if window.pageXOffset > 15 else -1
|
ans = 0 if current_scroll_offset() > 15 else -1
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def next_col_location():
|
def next_col_location():
|
||||||
@ -326,7 +334,7 @@ def next_col_location():
|
|||||||
ans = cc + col_and_gap
|
ans = cc + col_and_gap
|
||||||
limit = document.body.scrollWidth - window_width()
|
limit = document.body.scrollWidth - window_width()
|
||||||
if ans > limit:
|
if ans > limit:
|
||||||
ans = limit if window.pageXOffset < limit else -1
|
ans = limit if current_scroll_offset() < limit else -1
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def previous_col_location():
|
def previous_col_location():
|
||||||
@ -338,7 +346,7 @@ def previous_col_location():
|
|||||||
cc = current_column_location()
|
cc = current_column_location()
|
||||||
ans = cc - col_and_gap
|
ans = cc - col_and_gap
|
||||||
if ans < 0:
|
if ans < 0:
|
||||||
ans = 0 if window.pageXOffset > 0 else -1
|
ans = 0 if current_scroll_offset() > 0 else -1
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def jump_to_anchor(name):
|
def jump_to_anchor(name):
|
||||||
@ -418,7 +426,7 @@ def current_cfi():
|
|||||||
while cury < window_height():
|
while cury < window_height():
|
||||||
curx = left
|
curx = left
|
||||||
while curx < right - gap:
|
while curx < right - gap:
|
||||||
cfi = cfi_at_point(curx-window.pageXOffset, cury-window.pageYOffset)
|
cfi = cfi_at_point(curx-current_scroll_offset(), cury-window.pageYOffset)
|
||||||
if cfi:
|
if cfi:
|
||||||
# print('Viewport cfi:', cfi)
|
# print('Viewport cfi:', cfi)
|
||||||
return cfi
|
return cfi
|
||||||
@ -440,7 +448,7 @@ def progress_frac(frac):
|
|||||||
limit = document.body.scrollWidth - window_width()
|
limit = document.body.scrollWidth - window_width()
|
||||||
if limit <= 0:
|
if limit <= 0:
|
||||||
return 0.0
|
return 0.0
|
||||||
return window.pageXOffset / limit
|
return current_scroll_offset() / limit
|
||||||
limit = document.body.scrollHeight - window_height()
|
limit = document.body.scrollHeight - window_height()
|
||||||
if limit <= 0:
|
if limit <= 0:
|
||||||
return 0.0
|
return 0.0
|
||||||
@ -472,13 +480,13 @@ def onkeydown(evt):
|
|||||||
if key is 'up' or key is 'down':
|
if key is 'up' or key is 'down':
|
||||||
handled = True
|
handled = True
|
||||||
if evt.ctrlKey:
|
if evt.ctrlKey:
|
||||||
window.scrollTo(0 if key is 'left' else document_width(), 0)
|
scroll_to_offset(0 if key is 'left' else document_width())
|
||||||
else:
|
else:
|
||||||
scroll_by_page(key is 'up', True)
|
scroll_by_page(key is 'up', True)
|
||||||
elif (key is 'left' or key is 'right') and not evt.altKey:
|
elif (key is 'left' or key is 'right') and not evt.altKey:
|
||||||
handled = True
|
handled = True
|
||||||
if evt.ctrlKey:
|
if evt.ctrlKey:
|
||||||
window.scrollTo(0 if key is 'left' else document_width(), 0)
|
scroll_to_offset(0 if key is 'left' else document_width())
|
||||||
else:
|
else:
|
||||||
scroll_by_page(key is 'left', False)
|
scroll_by_page(key is 'left', False)
|
||||||
elif key is 'home' or key is 'end':
|
elif key is 'home' or key is 'end':
|
||||||
@ -487,9 +495,9 @@ def onkeydown(evt):
|
|||||||
get_boss().send_message('goto_doc_boundary', start=key is 'home')
|
get_boss().send_message('goto_doc_boundary', start=key is 'home')
|
||||||
else:
|
else:
|
||||||
if key is 'home':
|
if key is 'home':
|
||||||
window.scrollTo(0, 0)
|
scroll_to_offset(0)
|
||||||
else:
|
else:
|
||||||
window.scrollTo(document_width(), 0)
|
scroll_to_offset(document_width())
|
||||||
elif key is 'pageup' or key is 'pagedown' or key is 'space':
|
elif key is 'pageup' or key is 'pagedown' or key is 'space':
|
||||||
handled = True
|
handled = True
|
||||||
scroll_by_page(key is 'pageup', True)
|
scroll_by_page(key is 'pageup', True)
|
||||||
@ -519,7 +527,7 @@ anchor_funcs = {
|
|||||||
return column_at(x)
|
return column_at(x)
|
||||||
,
|
,
|
||||||
'visibility': def visibility(pos):
|
'visibility': def visibility(pos):
|
||||||
first = column_at(window.pageXOffset + 10)
|
first = column_at(current_scroll_offset() + 10)
|
||||||
if pos < first:
|
if pos < first:
|
||||||
return -1
|
return -1
|
||||||
if pos < first + cols_per_screen:
|
if pos < first + cols_per_screen:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user