| 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 cached_color_to_rgba |
| 10 |
from dom import ( |
| 11 |
add_extra_css, build_rule, clear, set_css, set_radio_group_value, svgicon, |
| 12 |
unique_id |
| 13 |
) |
| 14 |
from modals import error_dialog |
| 15 |
from read_book.globals import default_color_schemes, ui_operations |
| 16 |
from read_book.prefs.utils import create_button_box |
| 17 |
from session import session_defaults |
| 18 |
from widgets import create_button |
| 19 |
|
| 20 |
CONTAINER = unique_id('reader-color-scheme') |
| 21 |
COLOR_LIST = unique_id() |
| 22 |
ACTION_BUTTONS = unique_id() |
| 23 |
EDIT_SCHEME = unique_id() |
| 24 |
MARGINS = ('left', 'right', 'top', 'bottom') |
| 25 |
|
| 26 |
|
| 27 |
add_extra_css(def (): |
| 28 |
sel = '#' + COLOR_LIST |
| 29 |
style = build_rule(sel, list_style_type='none', display='flex', flex_wrap='wrap') |
| 30 |
sel += ' > li' |
| 31 |
style += build_rule(sel, padding='1ex 1rem', margin='1ex 0.5rem', border_radius='4px', cursor='pointer', border='solid 1px currentColor') |
| 32 |
style += build_rule(sel + ' svg', visibility='hidden') |
| 33 |
sel += '.current-color' |
| 34 |
style += build_rule(sel + ' svg', visibility='visible') |
| 35 |
style += build_rule('#{} #{} td'.format(CONTAINER, EDIT_SCHEME), padding='1ex 1em') |
| 36 |
sel = '#' + ACTION_BUTTONS |
| 37 |
style += sel + ( |
| 38 |
'{margin-top:2ex; padding-top:1ex; border-top: solid 1px currentColor;' |
| 39 |
+ ' margin-bottom: 2ex; padding-bottom: 1ex; border-bottom: solid 1px currentColor}' |
| 40 |
) |
| 41 |
style += build_rule(sel + ' > span ', margin='1ex 0.5rem', display='inline-block') |
| 42 |
return style |
| 43 |
) |
| 44 |
|
| 45 |
|
| 46 |
def get_container(): |
| 47 |
return document.getElementById(CONTAINER) |
| 48 |
|
| 49 |
|
| 50 |
def resolve_color_scheme(current_color_scheme): |
| 51 |
sd = get_session_data() |
| 52 |
cs = current_color_scheme or sd.get('current_color_scheme') or session_defaults().current_color_scheme |
| 53 |
ucs = sd.get('user_color_schemes') |
| 54 |
if default_color_schemes[cs]: |
| 55 |
ans = default_color_schemes[cs] |
| 56 |
elif ucs[cs] and ucs[cs].foreground and ucs[cs].background: |
| 57 |
ans = ucs[cs] |
| 58 |
else: |
| 59 |
for sn in default_color_schemes: |
| 60 |
ans = default_color_schemes[sn] |
| 61 |
break |
| 62 |
rgba = cached_color_to_rgba(ans.background) |
| 63 |
ans.is_dark_theme = max(rgba[0], rgba[1], rgba[2]) < 115 |
| 64 |
return ans |
| 65 |
|
| 66 |
|
| 67 |
def change_current_color(ev): |
| 68 |
ul = ev.currentTarget.parentNode |
| 69 |
for li in ul.childNodes: |
| 70 |
li.setAttribute('class', 'current-color' if li is ev.currentTarget else '') |
| 71 |
set_action_button_visibility(ul.parentNode) |
| 72 |
|
| 73 |
|
| 74 |
def new_color_scheme(ev): |
| 75 |
container = document.getElementById(EDIT_SCHEME) |
| 76 |
container.style.display = 'block' |
| 77 |
for inp in container.querySelectorAll('input'): |
| 78 |
if inp.name.endswith('_color_type'): |
| 79 |
inp.checked = inp.value is 'default' |
| 80 |
elif inp.name.startswith('margin_'): |
| 81 |
pass |
| 82 |
else: |
| 83 |
inp.value = {'name': '', 'bg': '#ffffff', 'fg': '#000000', 'link': '#0000ee'}[inp.name] |
| 84 |
container.querySelector('input').focus() |
| 85 |
return container |
| 86 |
|
| 87 |
|
| 88 |
def edit_color_scheme(ev): |
| 89 |
container = new_color_scheme(ev) |
| 90 |
ccs = current_color_scheme(container) |
| 91 |
all_schemes = all_color_schemes() |
| 92 |
if all_schemes[ccs]: |
| 93 |
scheme = all_schemes[ccs] |
| 94 |
container = document.getElementById(EDIT_SCHEME) |
| 95 |
container.querySelector('input').value = scheme.name |
| 96 |
container.querySelector('input[name=bg]').value = scheme.background |
| 97 |
container.querySelector('input[name=fg]').value = scheme.foreground |
| 98 |
set_radio_group_value(container, 'link_color_type', 'custom' if scheme.link else 'default') |
| 99 |
if scheme.link: |
| 100 |
container.querySelector('input[name=link]').value = scheme.link |
| 101 |
for which in MARGINS: |
| 102 |
attr = f'margin_{which}' |
| 103 |
val = scheme[attr] |
| 104 |
set_radio_group_value(container, f'{attr}_color_type', 'custom' if val else 'default') |
| 105 |
if val: |
| 106 |
bg, fg = val.split(':') |
| 107 |
container.querySelector(f'input[name={attr}_bg]').value = bg |
| 108 |
container.querySelector(f'input[name={attr}_fg]').value = fg |
| 109 |
|
| 110 |
|
| 111 |
def remove_color_scheme(ev): |
| 112 |
ccs = current_color_scheme() |
| 113 |
if default_color_schemes[ccs]: |
| 114 |
return error_dialog(_('Cannot remove'), _('Cannot remove a builtin color scheme')) |
| 115 |
sd = get_session_data() |
| 116 |
ucs = sd.get('user_color_schemes') |
| 117 |
v'delete ucs[ccs]' |
| 118 |
sd.set('user_color_schemes', ucs) |
| 119 |
create_color_buttons() |
| 120 |
set_current_color_scheme() |
| 121 |
|
| 122 |
|
| 123 |
def current_color_scheme(): |
| 124 |
try: |
| 125 |
return get_container().querySelector('li.current-color').getAttribute('data-name') |
| 126 |
except Exception: |
| 127 |
return session_defaults().current_color_scheme |
| 128 |
|
| 129 |
|
| 130 |
def set_current_color_scheme(value): |
| 131 |
ul = document.getElementById(COLOR_LIST) |
| 132 |
done = False |
| 133 |
for li in ul.childNodes: |
| 134 |
li.classList.remove('current-color') |
| 135 |
if li.getAttribute('data-name') is value: |
| 136 |
li.classList.add('current-color') |
| 137 |
done = True |
| 138 |
if not done: |
| 139 |
for li in ul.childNodes: |
| 140 |
li.classList.add('current-color') |
| 141 |
break |
| 142 |
set_action_button_visibility() |
| 143 |
|
| 144 |
|
| 145 |
def add_color_scheme(ev): |
| 146 |
colors = {} |
| 147 |
|
| 148 |
def check_color(col): |
| 149 |
colors[col] = div.querySelector(f'input[name={col}]').value |
| 150 |
if not /^#[0-9A-F]{6}$/i.test(colors[col]): |
| 151 |
error_dialog(_('Invalid color'), _( |
| 152 |
'The color {} is not a valid color').format(colors[col])) |
| 153 |
return False |
| 154 |
return True |
| 155 |
|
| 156 |
div = document.getElementById(EDIT_SCHEME) |
| 157 |
if this is not 'cancel': |
| 158 |
name = div.querySelector('input[name=name]').value |
| 159 |
if not name: |
| 160 |
error_dialog(_('Name not specified'), _( |
| 161 |
'You must specify a name for the color scheme')) |
| 162 |
return |
| 163 |
|
| 164 |
for col in ('bg', 'fg', 'link'): |
| 165 |
if not check_color(col): |
| 166 |
return |
| 167 |
for margin in MARGINS: |
| 168 |
for which in ('fg', 'bg'): |
| 169 |
if not check_color(f'margin_{margin}_{which}'): |
| 170 |
return |
| 171 |
|
| 172 |
key = '*' + name |
| 173 |
sd = get_session_data() |
| 174 |
ucs = Object.assign({}, sd.get('user_color_schemes')) |
| 175 |
ucs[key] = {'name': name, 'foreground': colors.fg, 'background': colors.bg} |
| 176 |
if div.querySelector('input[name=link_color_type]:checked').value is 'custom': |
| 177 |
ucs[key].link = colors.link |
| 178 |
for margin in MARGINS: |
| 179 |
if div.querySelector(f'input[name=margin_{margin}_color_type]:checked').value is 'custom': |
| 180 |
ucs[key][f'margin_{margin}'] = colors[f'margin_{margin}_bg'] + ':' + colors[f'margin_{margin}_fg'] |
| 181 |
sd.set('user_color_schemes', ucs) |
| 182 |
create_color_buttons() |
| 183 |
set_current_color_scheme(key) |
| 184 |
div.style.display = 'none' |
| 185 |
|
| 186 |
|
| 187 |
def all_color_schemes(): |
| 188 |
all_schemes = {} |
| 189 |
for k in default_color_schemes: |
| 190 |
all_schemes[k] = default_color_schemes[k] |
| 191 |
sd = get_session_data() |
| 192 |
ucs = sd.get('user_color_schemes') |
| 193 |
for k in ucs: |
| 194 |
all_schemes[k] = ucs[k] |
| 195 |
return all_schemes |
| 196 |
|
| 197 |
|
| 198 |
def create_color_buttons(): |
| 199 |
ul = document.getElementById(COLOR_LIST) |
| 200 |
sd = get_session_data() |
| 201 |
clear(ul) |
| 202 |
all_schemes = all_color_schemes() |
| 203 |
ccs = sd.get('current_color_scheme') |
| 204 |
if not all_schemes[ccs]: |
| 205 |
ccs = session_defaults().current_color_scheme |
| 206 |
for name in sorted(all_schemes, key=def (k): return all_schemes[k].name.toLowerCase();): |
| 207 |
scheme = all_schemes[name] |
| 208 |
is_current = name is ccs |
| 209 |
item = set_css(E.li(svgicon('check'), '\xa0' + scheme.name, data_name=name, onclick=change_current_color, |
| 210 |
class_='current-color' if is_current else ''), |
| 211 |
color=scheme.foreground, background_color=scheme.background) |
| 212 |
ul.appendChild(item) |
| 213 |
|
| 214 |
|
| 215 |
def set_action_button_visibility(): |
| 216 |
container = get_container() |
| 217 |
ccs = current_color_scheme(container) |
| 218 |
is_custom = ccs.startswith('*') |
| 219 |
is_first = True |
| 220 |
for button in container.querySelectorAll('#' + ACTION_BUTTONS + ' > span'): |
| 221 |
if is_first: |
| 222 |
is_first = False |
| 223 |
else: |
| 224 |
button.style.display = 'inline-block' if is_custom else 'none' |
| 225 |
|
| 226 |
|
| 227 |
def create_colors_panel(container, apply_func, cancel_func): |
| 228 |
container.appendChild(E.div(id=CONTAINER)) |
| 229 |
container = container.lastChild |
| 230 |
sd = get_session_data() |
| 231 |
cs = resolve_color_scheme() |
| 232 |
container.dataset.bg = cs.background |
| 233 |
container.dataset.fg = cs.foreground |
| 234 |
container.dataset.link = cs.link or '' |
| 235 |
container.appendChild(E.p(_('Choose a color scheme below'), style='margin:1ex 1em; padding: 1ex 0')) |
| 236 |
ul = E.ul(id=COLOR_LIST) |
| 237 |
container.appendChild(ul) |
| 238 |
container.appendChild(E.div( |
| 239 |
E.span(_('Override all book colors:') + '\xa0'), |
| 240 |
E.label(E.input(type='radio', name='override_book_colors', value='never'), _('Never')), |
| 241 |
' ', |
| 242 |
E.label(E.input(type='radio', name='override_book_colors', value='dark'), _('In dark mode')), |
| 243 |
' ', |
| 244 |
E.label(E.input(type='radio', name='override_book_colors', value='always'), _('Always')), |
| 245 |
style='margin:1ex 1em; padding: 1ex 0; white-space: pre-wrap' |
| 246 |
)) |
| 247 |
try: |
| 248 |
container.lastChild.querySelector(f'[name=override_book_colors][value={sd.get("override_book_colors")}]').checked = True |
| 249 |
except: |
| 250 |
container.lastChild.querySelector('[name=override_book_colors][value=never]').checked = True |
| 251 |
create_color_buttons() |
| 252 |
|
| 253 |
container.appendChild(E.div( |
| 254 |
E.span(create_button(_('New scheme'), 'plus', new_color_scheme)), |
| 255 |
E.span(create_button(_('Edit scheme'), 'pencil', edit_color_scheme)), |
| 256 |
E.span(create_button(_('Remove scheme'), 'trash', remove_color_scheme)), |
| 257 |
id=ACTION_BUTTONS, |
| 258 |
)) |
| 259 |
|
| 260 |
def margin_row(title, which): |
| 261 |
return E.tr( |
| 262 |
E.td(title), E.td( |
| 263 |
E.label(E.input(type='radio', name=f'margin_{which}_color_type', value='default'), _('Default')), |
| 264 |
'\xa0\xa0', |
| 265 |
E.label(E.input(type='radio', name=f'margin_{which}_color_type', value='custom'), _('Custom')), |
| 266 |
'\xa0\xa0', |
| 267 |
E.label(E.input(name=f'margin_{which}_bg', type='color', value='#ffffff', onclick=def (ev): |
| 268 |
set_radio_group_value(ev.currentTarget.closest('td'), f'margin_{which}_color_type', 'custom') |
| 269 |
), '\xa0' + _('Background')), |
| 270 |
'\xa0\xa0', |
| 271 |
E.label(E.input(name=f'margin_{which}_fg', type='color', value='#000000', onclick=def (ev): |
| 272 |
set_radio_group_value(ev.currentTarget.closest('td'), f'margin_{which}_color_type', 'custom') |
| 273 |
), '\xa0' + _('Foreground')), |
| 274 |
) |
| 275 |
) |
| 276 |
container.appendChild(E.div(id=EDIT_SCHEME, style='display:none', |
| 277 |
E.table( |
| 278 |
E.tr(E.td(_('Name:')), E.td(E.input(name='name'))), |
| 279 |
E.tr(E.td(_('Background:')), E.td(E.input(name='bg', type='color', value='#ffffff'))), |
| 280 |
E.tr(E.td(_('Foreground:')), E.td(E.input(name='fg', type='color', value='#000000'))), |
| 281 |
E.tr(E.td(_('Link:')), E.td( |
| 282 |
E.label(E.input(type='radio', name='link_color_type', value='default'), _('Default')), |
| 283 |
'\xa0\xa0', |
| 284 |
E.label(E.input(type='radio', name='link_color_type', value='custom'), _('Custom')), |
| 285 |
'\xa0', |
| 286 |
E.input(name='link', type='color', value='#000000', onclick=def (ev): |
| 287 |
set_radio_group_value(ev.currentTarget.closest('td'), 'link_color_type', 'custom') |
| 288 |
) |
| 289 |
)), |
| 290 |
margin_row(_('Top margin:'), 'top'), |
| 291 |
margin_row(_('Bottom margin:'), 'bottom'), |
| 292 |
margin_row(_('Left margin:'), 'left'), |
| 293 |
margin_row(_('Right margin:'), 'right'), |
| 294 |
), |
| 295 |
E.div(style='display:flex; justify-content: flex-end; margin: 1ex 1em', |
| 296 |
create_button(_('Apply'), 'check', add_color_scheme), E.span('\xa0'), create_button(_('Discard'), 'close', add_color_scheme.bind('cancel')) |
| 297 |
))) |
| 298 |
set_action_button_visibility() |
| 299 |
container.appendChild(E.div(style='margin: 1rem', create_button_box(None, apply_func, cancel_func))) |
| 300 |
|
| 301 |
develop = create_colors_panel |
| 302 |
|
| 303 |
|
| 304 |
def commit_colors(onchange): |
| 305 |
ccs = current_color_scheme() |
| 306 |
rcs = resolve_color_scheme(ccs) |
| 307 |
c = get_container() |
| 308 |
sd = get_session_data() |
| 309 |
prev_obc = sd.get('override_book_colors') |
| 310 |
cur_obc = c.querySelector('[name=override_book_colors]:checked').value |
| 311 |
sd.set('current_color_scheme', ccs) |
| 312 |
if cur_obc is not prev_obc: |
| 313 |
sd.set('override_book_colors', cur_obc) |
| 314 |
if rcs.foreground is not c.dataset.fg or rcs.background is not c.dataset.bg or c.dataset.link is not rcs.link or cur_obc is not prev_obc: |
| 315 |
ui_operations.update_color_scheme() |
| 316 |
onchange() |