| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
|
| 7 |
from book_list.globals import get_session_data |
| 8 |
from book_list.theme import cached_color_to_rgba |
| 9 |
from dom import unique_id |
| 10 |
from read_book.globals import ui_operations |
| 11 |
|
| 12 |
SIZE = 10 |
| 13 |
|
| 14 |
|
| 15 |
class BookScrollbar: |
| 16 |
def __init__(self, view): |
| 17 |
self.view = view |
| 18 |
self.container_id = unique_id('book-scrollbar') |
| 19 |
self.sync_to_contents_timer = 0 |
| 20 |
self.sync_contents_timer = 0 |
| 21 |
|
| 22 |
@property |
| 23 |
def container(self): |
| 24 |
return document.getElementById(self.container_id) |
| 25 |
|
| 26 |
def create(self): |
| 27 |
self.on_bob_mousedown = self.on_bob_mouse_event.bind(None, 'down') |
| 28 |
self.on_bob_mousemove = self.on_bob_mouse_event.bind(None, 'move') |
| 29 |
self.on_bob_mouseup = self.on_bob_mouse_event.bind(None, 'up') |
| 30 |
return E.div( |
| 31 |
id=self.container_id, |
| 32 |
style=f'height: 100vh; background-color: #aaa; width: {SIZE}px; border-radius: 5px', |
| 33 |
onclick=self.bar_clicked, |
| 34 |
oncontextmenu=self.context_menu, |
| 35 |
E.div( |
| 36 |
style=f'position: relative; width: 100%; height: {int(2.2*SIZE)}px;' + |
| 37 |
' background-color: #444; border-radius: 5px', onmousedown=self.on_bob_mousedown), |
| 38 |
E.div(style='position: absolute; z-index: 2147483647; width: 100vw; height: 100vh; left: 0; top: 0; display: none;') |
| 39 |
) |
| 40 |
|
| 41 |
def context_menu(self, ev): |
| 42 |
if ui_operations.scrollbar_context_menu: |
| 43 |
ev.preventDefault(), ev.stopPropagation() |
| 44 |
c = self.container |
| 45 |
bob = c.firstChild |
| 46 |
height = c.clientHeight - bob.clientHeight |
| 47 |
top = max(0, min(ev.clientY - bob.clientHeight, height)) |
| 48 |
frac = max(0, min(top / height, 1)) |
| 49 |
ui_operations.scrollbar_context_menu(ev.screenX, ev.screenY, frac) |
| 50 |
|
| 51 |
def bar_clicked(self, evt): |
| 52 |
if evt.button is 0: |
| 53 |
c = self.container |
| 54 |
b = c.firstChild |
| 55 |
bob_top = b.offsetTop |
| 56 |
bob_bottom = bob_top + b.offsetHeight |
| 57 |
if evt.clientY < bob_top: |
| 58 |
self.view.side_margin_clicked('left', evt) |
| 59 |
elif evt.clientY > bob_bottom: |
| 60 |
self.view.side_margin_clicked('right', evt) |
| 61 |
|
| 62 |
def on_bob_mouse_event(self, which, evt): |
| 63 |
c = self.container |
| 64 |
bob = c.firstChild |
| 65 |
mouse_grab = bob.nextSibling |
| 66 |
if which is 'move': |
| 67 |
top = evt.pageY - self.down_y |
| 68 |
height = c.clientHeight - bob.clientHeight |
| 69 |
top = max(0, min(top, height)) |
| 70 |
bob.style.top = f'{top}px' |
| 71 |
evt.preventDefault(), evt.stopPropagation() |
| 72 |
frac = bob.offsetTop / height |
| 73 |
if self.sync_contents_timer: |
| 74 |
window.clearTimeout(self.sync_contents_timer) |
| 75 |
self.sync_contents_timer = window.setTimeout( |
| 76 |
self.view.goto_frac.bind(None, frac), |
| 77 |
2 |
| 78 |
) |
| 79 |
elif which is 'down': |
| 80 |
if evt.button is not 0: |
| 81 |
return |
| 82 |
evt.preventDefault(), evt.stopPropagation() |
| 83 |
self.down_y = evt.clientY - bob.getBoundingClientRect().top |
| 84 |
mouse_grab.style.display = 'block' |
| 85 |
window.addEventListener( |
| 86 |
'mousemove', |
| 87 |
self.on_bob_mousemove, |
| 88 |
{'capture': True, 'passive': False} |
| 89 |
) |
| 90 |
window.addEventListener( |
| 91 |
'mouseup', |
| 92 |
self.on_bob_mouseup, |
| 93 |
{'capture': True, 'passive': False} |
| 94 |
) |
| 95 |
elif which is 'up': |
| 96 |
self.down_y = 0 |
| 97 |
window.removeEventListener( |
| 98 |
'mousemove', |
| 99 |
self.on_bob_mousemove, |
| 100 |
{'capture': True, 'passive': False} |
| 101 |
) |
| 102 |
window.removeEventListener( |
| 103 |
'mouseup', |
| 104 |
self.on_bob_mouseup, |
| 105 |
{'capture': True, 'passive': False} |
| 106 |
) |
| 107 |
window.setTimeout(def (): self.container.firstChild.nextSibling.style.display='none';, 10) |
| 108 |
evt.preventDefault(), evt.stopPropagation() |
| 109 |
|
| 110 |
def apply_visibility(self): |
| 111 |
sd = get_session_data() |
| 112 |
self.container.style.display = 'block' if sd.get('book_scrollbar') else 'none' |
| 113 |
|
| 114 |
@property |
| 115 |
def effective_width(self): |
| 116 |
return SIZE if self.container.style.display is 'block' else 0 |
| 117 |
|
| 118 |
def set_position(self, frac): |
| 119 |
c = self.container |
| 120 |
frac = max(0, min(frac, 1)) |
| 121 |
c.firstChild.style.top = f'{frac * (c.clientHeight - c.firstChild.clientHeight)}px' |
| 122 |
|
| 123 |
def _sync_to_contents(self): |
| 124 |
self.sync_to_contents_timer = 0 |
| 125 |
self.set_position(self.sync_to_contents_frac) |
| 126 |
|
| 127 |
def sync_to_contents(self, frac): |
| 128 |
self.sync_to_contents_frac = frac |
| 129 |
if not self.sync_to_contents_timer: |
| 130 |
self.sync_to_contents_timer = window.setTimeout( |
| 131 |
self._sync_to_contents, |
| 132 |
50 |
| 133 |
) |
| 134 |
|
| 135 |
def apply_color_scheme(self, colors): |
| 136 |
fg = cached_color_to_rgba(colors.foreground) |
| 137 |
bg = cached_color_to_rgba(colors.background) |
| 138 |
|
| 139 |
def mix(fg, bg, frac): |
| 140 |
def m(x): |
| 141 |
return frac * fg[x] + (1 - frac) * bg[x] |
| 142 |
return [m[0], m[1], m[2]] |
| 143 |
|
| 144 |
rbg = mix(fg, bg, 0.3) |
| 145 |
rfg = mix(fg, bg, 0.7) |
| 146 |
|
| 147 |
c = self.container |
| 148 |
c.style.backgroundColor = f'rgb({rbg[0]}, {rbg[1]}, {rbg[2]})' |
| 149 |
c.firstChild.style.backgroundColor = f'rgb({rfg[0]}, {rfg[1]}, {rfg[2]})' |