calibre/src/pyj/widgets.pyj
2016-02-05 17:41:30 +05:30

94 lines
3.4 KiB
Plaintext

# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
from dom import build_rule, clear
from elementmaker import E
from book_list.theme import get_color
BUTTON_VPADDING = '0.5ex'
def create_button(text, icon=None, action=None, tooltip=None):
cls = ''
if icon:
cls = str.format('fa fa-{} fa-lg', icon)
text = '\xa0' + text
ans = E.button(E.i(class_=cls), text, class_='calibre-push-button', type='button', title=tooltip or '')
if action is not None:
ans.addEventListener('click', def(event): event.preventDefault(), action(event);)
return ans
create_button.style = build_rule('button.calibre-push-button',
border_radius='1em', background_clip='padding-box', background_color=get_color('button-start'),
background_image=str.format('linear-gradient(to bottom, {}, {})', get_color('button-start'), get_color('button-end')),
padding=BUTTON_VPADDING + ' 1em', color=get_color('button-text'), cursor='pointer', font_size='inherit'
)
create_button.style += build_rule('button.calibre-push-button:hover', transform='scale(1.2)')
create_button.style += build_rule('button.calibre-push-button:active', transform='scale(2)')
def create_spinner():
return E.i(class_='fa fa-spin fa-spinner')
id_counter = 0
class Breadcrumbs:
STYLE_RULES = build_rule(
'.calibre-breadcrumbs',
user_select='none', white_space='nowrap', background_color=get_color('window-background2'),
z_index='-1', border_radius='10px', margin='1ex 1em', margin_bottom='0'
)
STYLE_RULES += build_rule(
'.calibre-breadcrumbs > li',
cursor='pointer', display='inline-block', line_height='26px', margin='0 9px 0 -10px',
padding='0.5ex 1rem', position='relative'
)
STYLE_RULES += build_rule('.calibre-breadcrumbs > li:hover', color='red')
STYLE_RULES += build_rule('.calibre-breadcrumbs > li:active', color='red', transform='scale(1.5)')
STYLE_RULES += build_rule(
'.calibre-breadcrumbs > li:before, .calibre-breadcrumbs > li:after',
border_right='2px solid currentColor', content='""', display='block', height='50%',
position='absolute', left='0', top='0', right='0', transform='skewX(45deg)'
)
STYLE_RULES += build_rule(
'.calibre-breadcrumbs > li:after',
bottom='0', top='auto', transform='skewX(-45deg)'
)
STYLE_RULES += build_rule(
'.calibre-breadcrumbs > li:last-of-type:before, .calibre-breadcrumbs > li:last-of-type:after',
display='none')
def __init__(self, container):
nonlocal id_counter id_counter += 1
container.setAttribute('id', container.getAttribute('id') or ('calibre-breadcrumbs-' + id_counter))
container.classList.add('calibre-breadcrumbs')
clear(container)
self.container_id = container.getAttribute('id')
@property
def container(self):
return document.getElementById(self.container_id)
def reset(self):
clear(self.container)
def add_crumb(self, callback):
li = E.li()
if callback:
li.addEventListener('click', callback)
self.container.appendChild(li)
return li
def get_widget_css():
ans = 'a, button:focus { outline: none }; a, button::-moz-focus-inner { border: 0 }\n'
ans += create_button.style
ans += Breadcrumbs.prototype.STYLE_RULES
return ans