| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from book_list.globals import get_session_data |
| 9 |
from book_list.theme import get_color |
| 10 |
from dom import add_extra_css, rule, unique_id |
| 11 |
from read_book.globals import ui_operations |
| 12 |
from session import session_defaults |
| 13 |
from widgets import create_button |
| 14 |
|
| 15 |
CONTAINER = unique_id('font-size-prefs') |
| 16 |
MIN_FONT_SIZE = 8 |
| 17 |
MAX_FONT_SIZE = 80 |
| 18 |
|
| 19 |
add_extra_css(def (): |
| 20 |
style = rule(CONTAINER, 'option.current', background_color='var(--calibre-color-primary-light)') |
| 21 |
style += rule(CONTAINER, 'option:hover', background_color='var(--calibre-color-primary-light)') |
| 22 |
return style |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
def change_font_size(sz): |
| 27 |
sd = get_session_data() |
| 28 |
if sd.get('base_font_size') is not sz: |
| 29 |
sd.set('base_font_size', sz) |
| 30 |
ui_operations.update_font_size() |
| 31 |
|
| 32 |
|
| 33 |
def apply_font_size(): |
| 34 |
fs = int(document.getElementById(CONTAINER).dataset.cfs) |
| 35 |
change_font_size(fs) |
| 36 |
|
| 37 |
|
| 38 |
def set_quick_size(ev): |
| 39 |
newval = ev.currentTarget.value |
| 40 |
try: |
| 41 |
int(newval) |
| 42 |
except: |
| 43 |
return |
| 44 |
if newval is not document.getElementById(CONTAINER).dataset.cfs: |
| 45 |
display_changed_font_size(newval) |
| 46 |
|
| 47 |
|
| 48 |
def change_font_size_by(frac): |
| 49 |
sd = get_session_data() |
| 50 |
sz = sd.get('base_font_size') |
| 51 |
amt = sz * frac |
| 52 |
if abs(amt) < 1: |
| 53 |
amt = -1 if amt < 0 else 1 |
| 54 |
nsz = Math.round(sz + amt) |
| 55 |
nsz = max(MIN_FONT_SIZE, min(nsz, MAX_FONT_SIZE)) |
| 56 |
change_font_size(nsz) |
| 57 |
|
| 58 |
|
| 59 |
def restore_default_font_size(): |
| 60 |
change_font_size(session_defaults().base_font_size) |
| 61 |
|
| 62 |
|
| 63 |
def display_changed_font_size(sz): |
| 64 |
sz = max(MIN_FONT_SIZE, min(int(sz), MAX_FONT_SIZE)) |
| 65 |
sz += '' |
| 66 |
c = document.getElementById(CONTAINER) |
| 67 |
c.dataset.cfs = sz |
| 68 |
for option in c.querySelectorAll('option'): |
| 69 |
if option.value is sz: |
| 70 |
option.classList.add('current') |
| 71 |
else: |
| 72 |
option.classList.remove('current') |
| 73 |
for input in c.querySelectorAll('input'): |
| 74 |
input.value = sz |
| 75 |
c.querySelector('.cfs_preview').style.fontSize = f'{sz}px' |
| 76 |
|
| 77 |
|
| 78 |
def create_font_size_panel(container, close): |
| 79 |
sd = get_session_data() |
| 80 |
cfs = sd.get('base_font_size') |
| 81 |
container.appendChild(E.div( |
| 82 |
style='width: 100%; height: 100%; display: flex; justify-content: center; align-items: center', |
| 83 |
E.div( |
| 84 |
id=CONTAINER, |
| 85 |
style='max-width: 500px; width: 80vw; border-radius: 8px; border: solid 1px currentColor; padding:1ex 1rem;', |
| 86 |
onclick=def (ev): ev.preventDefault(), ev.stopPropagation() |
| 87 |
))) |
| 88 |
container = container.lastChild.lastChild |
| 89 |
container.style.backgroundColor = get_color('window-background') |
| 90 |
container.dataset.cfs = cfs + '' |
| 91 |
quick = E.datalist(style='display:flex; justify-content:space-around; flex-wrap: wrap; align-items: baseline;') |
| 92 |
container.appendChild(quick) |
| 93 |
for sz in (10, 12, 14, 16, 18, 20, 22): |
| 94 |
quick.appendChild(E.option( |
| 95 |
'Aa', title='{} px'.format(sz), |
| 96 |
class_='current' if cfs is sz else '', |
| 97 |
value=sz + '', |
| 98 |
style=f'display: inline-block; font-size:{sz}px; padding: 5px; cursor: pointer; border-radius: 4px; margin: 0 0.5rem', |
| 99 |
onclick=def (ev): |
| 100 |
set_quick_size(ev) |
| 101 |
)) |
| 102 |
|
| 103 |
def set_size(ev): |
| 104 |
newval = ev.currentTarget.value |
| 105 |
try: |
| 106 |
q = int(newval) |
| 107 |
except: |
| 108 |
return |
| 109 |
if MIN_FONT_SIZE <= q <= MAX_FONT_SIZE: |
| 110 |
set_quick_size(ev) |
| 111 |
|
| 112 |
container.appendChild(E.div( |
| 113 |
style='display: flex; margin-top: 1rem', |
| 114 |
E.input( |
| 115 |
type='range', min=MIN_FONT_SIZE + '', max=MAX_FONT_SIZE + '', value=cfs + '', style='flex-grow: 4', |
| 116 |
oninput=set_quick_size, |
| 117 |
), |
| 118 |
E.span('\xa0', E.input( |
| 119 |
value=f'{cfs}', oninput=set_size, type='number', min=MIN_FONT_SIZE + '', max=MAX_FONT_SIZE + '', |
| 120 |
step='1', |
| 121 |
), '\xa0px') |
| 122 |
)) |
| 123 |
|
| 124 |
container.appendChild(E.div( |
| 125 |
style=f'font-size: {cfs}px; margin-top: 1rem; min-height: 60px; max-height: 60px; overflow: hidden; display: flex;', |
| 126 |
E.div(class_='cfs_preview', _('Sample to preview font size')) |
| 127 |
)) |
| 128 |
|
| 129 |
container.appendChild(E.div( |
| 130 |
style='margin-top: 1rem; text-align: right', |
| 131 |
create_button(_('OK'), highlight=True, action=def (): |
| 132 |
apply_font_size() |
| 133 |
close() |
| 134 |
), |
| 135 |
'\xa0\xa0', |
| 136 |
create_button(_('Cancel'), action=close), |
| 137 |
)) |
| 138 |
|
| 139 |
|
| 140 |
def develop(container): |
| 141 |
create_font_size_panel(container, def (): pass;) |