| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from book_list.constants import book_list_container_id, cs_top_bar_host_id |
| 9 |
from book_list.router import back |
| 10 |
from book_list.router import home |
| 11 |
from book_list.theme import get_color, get_font_size |
| 12 |
from dom import set_css, clear, create_keyframes, build_rule, svgicon, add_extra_css |
| 13 |
from session import get_interface_data |
| 14 |
from utils import parse_url_params |
| 15 |
|
| 16 |
CLASS_NAME = 'main-top-bar' |
| 17 |
SPACING = '0.5em' |
| 18 |
VSPACING = '0.55ex' |
| 19 |
THROBBER_NAME = 'top-bar-throbber' |
| 20 |
|
| 21 |
add_extra_css(def (): |
| 22 |
sel = '.' + CLASS_NAME + ' ' |
| 23 |
style = '' |
| 24 |
style += create_keyframes(THROBBER_NAME, 'from { transform: scale(1); } 50% { transform: scale(0.5); } to { transform: scale(1); }') |
| 25 |
style += build_rule(sel + 'a', |
| 26 |
display='inline-flex', |
| 27 |
align_items='center', |
| 28 |
gap='0.35rem', |
| 29 |
line_height='1.0', |
| 30 |
overflow='hidden', |
| 31 |
flex_shrink='0', |
| 32 |
cursor='pointer', |
| 33 |
color=get_color('window-foreground'), |
| 34 |
background='none', |
| 35 |
padding_top=VSPACING, |
| 36 |
padding_bottom=VSPACING, |
| 37 |
border_radius='999px', |
| 38 |
padding_left='0.35em', |
| 39 |
padding_right='0.35em' |
| 40 |
) |
| 41 |
# ex alone shrinks too much on narrow layouts / nested em fonts; keep a readable minimum tap target |
| 42 |
style += build_rule(sel + 'a svg', |
| 43 |
display='block', height='max(1.125rem, 1.35ex)', width='max(1.125rem, 1.35ex)', |
| 44 |
vertical_align='middle', opacity='0.92', flex_shrink='0') |
| 45 |
style += build_rule(sel + 'a span', line_height='1.0') |
| 46 |
# Title shrinks on narrow widths; single line + ellipsis (main bar does not wrap). |
| 47 |
style += build_rule(sel + 'a.top-bar-title', |
| 48 |
flex_shrink='1', min_width='0', max_width='100%', |
| 49 |
overflow='hidden', text_overflow='ellipsis', white_space='nowrap') |
| 50 |
style += build_rule(sel + 'span.top-bar-title', |
| 51 |
flex_shrink='1', min_width='0', max_width='100%', |
| 52 |
overflow='hidden', text_overflow='ellipsis', white_space='nowrap', |
| 53 |
display='block') |
| 54 |
# No hover zoom; use highlight instead |
| 55 |
style += build_rule(sel + 'a:hover', background_color='var(--calibre-color-primary-light)') |
| 56 |
style += build_rule(sel + 'a:active', background_color='var(--calibre-color-primary-light-2)') |
| 57 |
style += build_rule(sel + 'a:focus', outline='none') |
| 58 |
style += build_rule(sel + 'a.top-bar-title:hover', transform='scale(1)', color=get_color('link-foreground'), font_style='normal') |
| 59 |
style += build_rule(sel + 'a.top-bar-title:active', transform='scale(1)', color=get_color('link-foreground'), font_style='normal') |
| 60 |
# Left/right slots: single row; title ellipsis handles narrow width. |
| 61 |
style += build_rule('.' + CLASS_NAME + ' > .main-top-bar-left', |
| 62 |
display='flex', flex_wrap='nowrap', align_items='center', |
| 63 |
gap='0.45em', |
| 64 |
min_width='0', max_width='100%', flex='1 1 0%', box_sizing='border-box', |
| 65 |
padding_left='0.45em', overflow='hidden') |
| 66 |
style += build_rule('.' + CLASS_NAME + ' > .main-top-bar-right', |
| 67 |
display='flex', flex_wrap='nowrap', align_items='center', |
| 68 |
justify_content='flex-end', gap='0.45em', |
| 69 |
flex='0 0 auto', margin_left='auto', box_sizing='border-box', |
| 70 |
padding_right='0.45em') |
| 71 |
# Do not reserve right-slot space when there are no controls (for very narrow layouts). |
| 72 |
style += build_rule('.' + CLASS_NAME + ' > .main-top-bar-right:empty', |
| 73 |
display='none', margin_left='0', padding_right='0') |
| 74 |
return style |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
def top_bar_target_container(container): |
| 79 |
# Book-list UI top bar mounts in the shell host above the sidebar + main column. |
| 80 |
if not container: |
| 81 |
return container |
| 82 |
bl = document.getElementById(book_list_container_id) |
| 83 |
host = document.getElementById(cs_top_bar_host_id) |
| 84 |
if bl and host and bl.contains(container): |
| 85 |
return host |
| 86 |
return container |
| 87 |
|
| 88 |
|
| 89 |
def create_markup(container): |
| 90 |
target = top_bar_target_container(container) |
| 91 |
if target and target.id is cs_top_bar_host_id: |
| 92 |
clear(target) |
| 93 |
if not target: |
| 94 |
return |
| 95 |
bar = E.div( |
| 96 |
class_=CLASS_NAME, |
| 97 |
E.div(class_='main-top-bar-left'), |
| 98 |
E.div(class_='main-top-bar-right') |
| 99 |
) |
| 100 |
set_css(bar, |
| 101 |
width='100%', display='flex', flex_direction='row', flex_wrap='nowrap', justify_content='flex-start', align_items='center', |
| 102 |
gap='0.45rem', |
| 103 |
overflow='hidden', |
| 104 |
font_size=get_font_size('title'), user_select='none', |
| 105 |
color=get_color('window-foreground'), background_color=get_color('window-background'), |
| 106 |
border_bottom='var(--calibre-border-style-subtle)', |
| 107 |
box_shadow='0 1px 0 rgba(0,0,0,0.03)' |
| 108 |
) |
| 109 |
target.appendChild(bar) |
| 110 |
|
| 111 |
|
| 112 |
def _has_visible_right_child(right): |
| 113 |
if not right: |
| 114 |
return False |
| 115 |
kids = right.children or [] |
| 116 |
for kid in kids: |
| 117 |
if not kid or kid.nodeType is not Node.ELEMENT_NODE: |
| 118 |
continue |
| 119 |
# Ignore explicitly hidden elements; they should not reserve slot width. |
| 120 |
if kid.hidden or kid.style?.display is 'none': |
| 121 |
continue |
| 122 |
try: |
| 123 |
cs = window.getComputedStyle(kid) |
| 124 |
if cs and cs.display is 'none': |
| 125 |
continue |
| 126 |
except Exception: |
| 127 |
pass |
| 128 |
return True |
| 129 |
return False |
| 130 |
|
| 131 |
|
| 132 |
def sync_right_slot_visibility(container): |
| 133 |
for bar in get_bars(container): |
| 134 |
right = bar.firstChild.nextSibling if bar?.firstChild else None |
| 135 |
if not right: |
| 136 |
continue |
| 137 |
has_visible = _has_visible_right_child(right) |
| 138 |
if has_visible: |
| 139 |
right.style.display = 'flex' |
| 140 |
right.style.marginLeft = 'auto' |
| 141 |
right.style.paddingRight = '0.45em' |
| 142 |
else: |
| 143 |
right.style.display = 'none' |
| 144 |
right.style.marginLeft = '0' |
| 145 |
right.style.paddingRight = '0' |
| 146 |
|
| 147 |
|
| 148 |
def get_bars(container): |
| 149 |
target = top_bar_target_container(container) |
| 150 |
if not target: |
| 151 |
if container: |
| 152 |
return container.getElementsByClassName(CLASS_NAME) |
| 153 |
return document.createElement('div').getElementsByClassName(CLASS_NAME) |
| 154 |
return target.getElementsByClassName(CLASS_NAME) |
| 155 |
|
| 156 |
|
| 157 |
def set_left_data( |
| 158 |
container, title='calibre', icon='heart', action=None, tooltip='', |
| 159 |
run_animation=False, title_action=None, title_tooltip=None, back_action=None |
| 160 |
): |
| 161 |
bars = get_bars(container) |
| 162 |
|
| 163 |
def on_back_click(event): |
| 164 |
event.preventDefault() |
| 165 |
if callable(back_action): |
| 166 |
back_action(event) |
| 167 |
else: |
| 168 |
back.bind(event)() |
| 169 |
|
| 170 |
if icon is 'heart': |
| 171 |
if not tooltip: |
| 172 |
tooltip = _('Donate to support calibre development') |
| 173 |
elif icon is 'back': |
| 174 |
if action is None: |
| 175 |
action = on_back_click |
| 176 |
interface_data = get_interface_data() |
| 177 |
# Determine if we are on the home page (no panel) |
| 178 |
try: |
| 179 |
is_home = not (parse_url_params().panel or '') |
| 180 |
except Exception: |
| 181 |
is_home = False |
| 182 |
|
| 183 |
for i, bar in enumerate(bars): |
| 184 |
left = bar.firstChild |
| 185 |
clear(left) |
| 186 |
title_elem = 'a' if callable(title_action) else 'span' |
| 187 |
# Show Back + Home buttons on all pages except the home page. |
| 188 |
# Keep the special donate heart behavior intact. |
| 189 |
# Use a plain close button when icon='close' (e.g. viewer overlay panels). |
| 190 |
if icon is 'heart': |
| 191 |
left.appendChild(E.a(title=tooltip, svgicon(icon))) |
| 192 |
elif icon is 'close': |
| 193 |
left.appendChild(E.a(title=tooltip or _('Close'), svgicon('close'))) |
| 194 |
else: |
| 195 |
if not is_home: |
| 196 |
left.appendChild(E.a(title=_('Back'), svgicon('arrow-left'))) |
| 197 |
left.appendChild(E.a(title=_('Go to the home page'), svgicon('home'))) |
| 198 |
left.appendChild(E(title_elem, title, title=title_tooltip, class_='top-bar-title', |
| 199 |
style='margin-left: {0}; font-weight: bold; padding-top: {1}; padding-bottom: {1}; vertical-align: middle'.format(SPACING, VSPACING))) |
| 200 |
if i is 0: |
| 201 |
a = left.firstChild |
| 202 |
if icon is 'heart': |
| 203 |
set_css(a, |
| 204 |
animation_name=THROBBER_NAME, animation_duration='1s', animation_timing_function='ease-in-out', |
| 205 |
animation_iteration_count='5', animation_play_state='running' if run_animation else 'paused' |
| 206 |
) |
| 207 |
set_css(a.firstChild, color=get_color('heart')) |
| 208 |
a.setAttribute('href', interface_data.donate_link) |
| 209 |
a.setAttribute('target', 'donate-to-calibre') |
| 210 |
if action is not None: |
| 211 |
a.addEventListener('click', def (event): |
| 212 |
event.preventDefault() |
| 213 |
action.bind(event)() |
| 214 |
) |
| 215 |
elif icon is 'close': |
| 216 |
if action is not None: |
| 217 |
a.addEventListener('click', def (event): |
| 218 |
event.preventDefault() |
| 219 |
action.bind(event)() |
| 220 |
) |
| 221 |
else: |
| 222 |
btns = left.getElementsByTagName('a') |
| 223 |
idx = 0 |
| 224 |
if not is_home: |
| 225 |
btns[idx].addEventListener('click', on_back_click) |
| 226 |
idx += 1 |
| 227 |
btns[idx].addEventListener('click', def (event): |
| 228 |
event.preventDefault() |
| 229 |
home.bind(event)() |
| 230 |
) |
| 231 |
|
| 232 |
if callable(title_action): |
| 233 |
# If we injected Back/Home buttons, title is further to the right |
| 234 |
a = left.querySelector('.top-bar-title') |
| 235 |
a.addEventListener('click', def (event): |
| 236 |
event.preventDefault() |
| 237 |
title_action.bind(event)() |
| 238 |
) |
| 239 |
|
| 240 |
|
| 241 |
def set_title(container, text): |
| 242 |
bars = get_bars(container) |
| 243 |
for bar in bars: |
| 244 |
left = bar.firstChild |
| 245 |
el = left.querySelector('.top-bar-title') if left else None |
| 246 |
if el: |
| 247 |
el.textContent = text |
| 248 |
|
| 249 |
|
| 250 |
def set_title_tooltip(container, text): |
| 251 |
bars = get_bars(container) |
| 252 |
for bar in bars: |
| 253 |
left = bar.firstChild |
| 254 |
el = left.querySelector('.top-bar-title') if left else None |
| 255 |
if el: |
| 256 |
el.title = text |
| 257 |
|
| 258 |
|
| 259 |
def create_top_bar(container, **kw): |
| 260 |
create_markup(container) |
| 261 |
set_left_data(container, **kw) |
| 262 |
|
| 263 |
|
| 264 |
def add_button(container, icon, action=None, tooltip=''): |
| 265 |
if not icon: |
| 266 |
raise ValueError('An icon must be specified') |
| 267 |
bars = get_bars(container) |
| 268 |
for i, bar in enumerate(bars): |
| 269 |
right = bar.firstChild.nextSibling |
| 270 |
right.appendChild(E.a( |
| 271 |
style='margin-left: ' + SPACING, |
| 272 |
title=tooltip, svgicon(icon), |
| 273 |
)) |
| 274 |
right.lastChild.dataset.buttonIcon = icon |
| 275 |
if i is 0: |
| 276 |
if action is not None: |
| 277 |
right.lastChild.addEventListener('click', def (event): |
| 278 |
event.preventDefault(), action() |
| 279 |
) |
| 280 |
sync_right_slot_visibility(container) |
| 281 |
|
| 282 |
|
| 283 |
def clear_buttons(container): |
| 284 |
bars = get_bars(container) |
| 285 |
for i, bar in enumerate(bars): |
| 286 |
right = bar.firstChild.nextSibling |
| 287 |
clear(right) |
| 288 |
sync_right_slot_visibility(container) |
| 289 |
|
| 290 |
|
| 291 |
def set_button_visibility(container, icon, visible): |
| 292 |
for bar in get_bars(container): |
| 293 |
right = bar.firstChild.nextSibling |
| 294 |
elem = right.querySelector(f'[data-button-icon="{icon}"]') |
| 295 |
if elem: |
| 296 |
elem.style.display = 'inline-block' if visible else 'none' |
| 297 |
sync_right_slot_visibility(container) |