| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, 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.globals import get_session_data |
| 9 |
from book_list.router import back |
| 10 |
from book_list.top_bar import create_top_bar |
| 11 |
from dom import clear, ensure_id, add_extra_css, build_rule |
| 12 |
from widgets import create_button, create_select |
| 13 |
|
| 14 |
pp_counter = 0 |
| 15 |
widget_counter = 0 |
| 16 |
|
| 17 |
CLASS_NAME = 'cs-prefs' |
| 18 |
|
| 19 |
add_extra_css(def (): |
| 20 |
sel = '.' + CLASS_NAME + ' ' |
| 21 |
style = '' |
| 22 |
# Match page gutter + readable width |
| 23 |
style += build_rule(sel, |
| 24 |
padding='0.75rem 1rem 1rem 1rem', |
| 25 |
max_width='980px', |
| 26 |
width='100%', |
| 27 |
box_sizing='border-box', |
| 28 |
min_width='0', |
| 29 |
margin_left='auto', |
| 30 |
margin_right='auto' |
| 31 |
) |
| 32 |
style += r''' |
| 33 |
@media (max-width: 720px) { |
| 34 |
.cs-prefs { padding-left: 0.75rem; padding-right: 0.75rem; } |
| 35 |
} |
| 36 |
@media (max-width: 520px) { |
| 37 |
.cs-prefs .item-main { |
| 38 |
flex-direction: column; |
| 39 |
align-items: stretch; |
| 40 |
} |
| 41 |
.cs-prefs .control { |
| 42 |
flex-direction: column; |
| 43 |
align-items: stretch; |
| 44 |
justify-content: flex-start; |
| 45 |
} |
| 46 |
.cs-prefs .control .label { |
| 47 |
flex: 0 0 auto; |
| 48 |
} |
| 49 |
} |
| 50 |
''' |
| 51 |
style += build_rule(sel + '.list', |
| 52 |
display='flex', flex_direction='column', gap='0.65rem', |
| 53 |
min_width='0', |
| 54 |
width='100%', |
| 55 |
box_sizing='border-box', |
| 56 |
) |
| 57 |
style += build_rule(sel + '.item', |
| 58 |
padding='0.85rem 1rem', |
| 59 |
border_radius='16px', |
| 60 |
border='var(--calibre-border-style-subtle)', |
| 61 |
background='linear-gradient(135deg, var(--calibre-color-surface-1) 0%, var(--calibre-color-surface-2) 100%)', |
| 62 |
box_shadow='0 12px 32px rgba(0,0,0,0.08)', |
| 63 |
min_width='0', |
| 64 |
max_width='100%', |
| 65 |
box_sizing='border-box', |
| 66 |
# No frosted glass effects (restore original, flat styling) |
| 67 |
) |
| 68 |
style += build_rule(sel + '.item:hover', |
| 69 |
border='var(--calibre-border-style)', |
| 70 |
box_shadow='0 16px 44px rgba(0,0,0,0.10)' |
| 71 |
) |
| 72 |
style += build_rule(sel + '.item-main', |
| 73 |
display='flex', |
| 74 |
align_items='center', |
| 75 |
justify_content='space-between', |
| 76 |
gap='1rem', |
| 77 |
flex_wrap='wrap', |
| 78 |
min_width='0', |
| 79 |
width='100%', |
| 80 |
box_sizing='border-box', |
| 81 |
) |
| 82 |
style += build_rule(sel + '.control', |
| 83 |
display='flex', |
| 84 |
align_items='center', |
| 85 |
justify_content='space-between', |
| 86 |
gap='0.75rem', |
| 87 |
flex='1 1 auto', |
| 88 |
min_width='0', |
| 89 |
max_width='100%', |
| 90 |
box_sizing='border-box', |
| 91 |
) |
| 92 |
style += build_rule(sel + '.label', |
| 93 |
font_weight='800', |
| 94 |
white_space='normal', |
| 95 |
opacity='0.92', |
| 96 |
flex='1 1 auto', |
| 97 |
min_width='0', |
| 98 |
max_width='100%', |
| 99 |
overflow_wrap='anywhere', |
| 100 |
word_break='break-word', |
| 101 |
) |
| 102 |
style += build_rule(sel + '.control input[type="text"],' + sel + '.control input[type="number"]', |
| 103 |
min_width='0', |
| 104 |
max_width='100%', |
| 105 |
width='100%', |
| 106 |
flex='1 1 auto', |
| 107 |
) |
| 108 |
style += build_rule(sel + '.help', |
| 109 |
margin_top='0.65rem', |
| 110 |
padding_top='0.65rem', |
| 111 |
border_top='var(--calibre-border-style-subtle)', |
| 112 |
font_size='0.85rem', |
| 113 |
font_weight='700', |
| 114 |
opacity='0.75', |
| 115 |
display='none', |
| 116 |
max_width='100%', |
| 117 |
overflow_wrap='anywhere', |
| 118 |
word_break='break-word', |
| 119 |
) |
| 120 |
style += build_rule(sel + '.footer', |
| 121 |
margin_top='1rem', |
| 122 |
display='flex', |
| 123 |
justify_content='flex-end' |
| 124 |
) |
| 125 |
return style |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
class ConfigItem: |
| 130 |
def __init__(self, item_data): |
| 131 |
nonlocal widget_counter |
| 132 |
widget_counter += 1 |
| 133 |
self.widget_id = 'pref-widget-' + widget_counter |
| 134 |
self.item_data = item_data |
| 135 |
self.ignore_ui_value_changed = False |
| 136 |
|
| 137 |
def initialize(self): |
| 138 |
self.ignore_ui_value_changed = True |
| 139 |
try: |
| 140 |
self.to_ui(self.from_storage()) |
| 141 |
finally: |
| 142 |
self.ignore_ui_value_changed = False |
| 143 |
return self |
| 144 |
|
| 145 |
@property |
| 146 |
def container(self): |
| 147 |
return document.getElementById(self.widget_id) |
| 148 |
|
| 149 |
@property |
| 150 |
def control(self): |
| 151 |
return self.container.lastChild |
| 152 |
|
| 153 |
def from_storage(self): |
| 154 |
val = get_session_data().get(self.item_data.name) |
| 155 |
if self.item_data.from_storage: |
| 156 |
val = self.item_data.from_storage(val) |
| 157 |
return val |
| 158 |
|
| 159 |
def to_storage(self, val): |
| 160 |
if self.item_data.to_storage: |
| 161 |
val = self.item_data.to_storage(val) |
| 162 |
get_session_data().set(self.item_data.name, val) |
| 163 |
|
| 164 |
def defval(self): |
| 165 |
val = get_session_data().defval(self.item_data.name) |
| 166 |
if self.item_data.from_storage: |
| 167 |
val = self.item_data.from_storage(val) |
| 168 |
return val |
| 169 |
|
| 170 |
def ui_value_changed(self): |
| 171 |
if self.ignore_ui_value_changed: |
| 172 |
return |
| 173 |
val = self.from_ui() |
| 174 |
self.to_storage(val) |
| 175 |
if self.item_data.onchange: |
| 176 |
self.item_data.onchange(val) |
| 177 |
|
| 178 |
def reset_to_default(self): |
| 179 |
self.to_ui(self.defval()) |
| 180 |
self.ui_value_changed() |
| 181 |
|
| 182 |
def to_ui(self, val): |
| 183 |
pass |
| 184 |
|
| 185 |
def from_ui(self): |
| 186 |
pass |
| 187 |
|
| 188 |
|
| 189 |
class Choices(ConfigItem): |
| 190 |
def __init__(self, item_data, container, onfocus): |
| 191 |
ConfigItem.__init__(self, item_data) |
| 192 |
options = {} |
| 193 |
for choice, text in item_data.choices: |
| 194 |
options[choice] = text |
| 195 |
c, select = create_select(item_data.text, '', **options) |
| 196 |
div = E.div( |
| 197 |
id=self.widget_id, |
| 198 |
E.span(item_data.text, class_='label'), |
| 199 |
c |
| 200 |
) |
| 201 |
div.classList.add('control') |
| 202 |
container.appendChild(div) |
| 203 |
select.addEventListener('change', self.ui_value_changed.bind(self)) |
| 204 |
select.addEventListener('focus', onfocus) |
| 205 |
div.addEventListener('click', onfocus) |
| 206 |
|
| 207 |
@property |
| 208 |
def control(self): |
| 209 |
return self.container.getElementsByTagName('select')[0] |
| 210 |
|
| 211 |
def to_ui(self, val): |
| 212 |
self.control.value = val |
| 213 |
|
| 214 |
def from_ui(self): |
| 215 |
return self.control.value |
| 216 |
|
| 217 |
|
| 218 |
class CheckBox(ConfigItem): |
| 219 |
def __init__(self, item_data, container, onfocus): |
| 220 |
ConfigItem.__init__(self, item_data) |
| 221 |
div = E.div( |
| 222 |
id=self.widget_id, |
| 223 |
style='cursor: pointer', |
| 224 |
E.input(type='checkbox'), |
| 225 |
E.span(item_data.text, class_='label') |
| 226 |
) |
| 227 |
div.classList.add('control') |
| 228 |
container.appendChild(div) |
| 229 |
control = div.firstChild |
| 230 |
control.addEventListener('change', self.ui_value_changed.bind(self)) |
| 231 |
control.addEventListener('focus', onfocus) |
| 232 |
div.addEventListener('click', onfocus) |
| 233 |
div.lastChild.addEventListener('click', self.toggle.bind(self)) |
| 234 |
|
| 235 |
@property |
| 236 |
def control(self): |
| 237 |
return self.container.firstChild |
| 238 |
|
| 239 |
def to_ui(self, val): |
| 240 |
self.control.checked = bool(val) |
| 241 |
|
| 242 |
def from_ui(self): |
| 243 |
return bool(self.control.checked) |
| 244 |
|
| 245 |
def toggle(self): |
| 246 |
self.to_ui(not self.from_ui()) |
| 247 |
self.ui_value_changed() |
| 248 |
|
| 249 |
|
| 250 |
class SpinBox(ConfigItem): |
| 251 |
def __init__(self, item_data, container, onfocus): |
| 252 |
ConfigItem.__init__(self, item_data) |
| 253 |
div = E.div( |
| 254 |
id=self.widget_id, |
| 255 |
E.span(item_data.text, class_='label'), |
| 256 |
E.input(type='number', step='any', min='1', max='100') |
| 257 |
) |
| 258 |
div.classList.add('control') |
| 259 |
container.appendChild(div) |
| 260 |
control = div.lastChild |
| 261 |
for attr in 'min max step'.split(' '): |
| 262 |
val = item_data[attr] |
| 263 |
if val is not undefined and val is not None: |
| 264 |
control.setAttribute(attr, '' + val) |
| 265 |
control.addEventListener('change', self.ui_value_changed.bind(self)) |
| 266 |
control.addEventListener('focus', onfocus) |
| 267 |
div.addEventListener('click', onfocus) |
| 268 |
|
| 269 |
def to_ui(self, val): |
| 270 |
self.control.value = val |
| 271 |
|
| 272 |
def from_ui(self): |
| 273 |
return self.control.value |
| 274 |
|
| 275 |
|
| 276 |
class LineEdit(ConfigItem): |
| 277 |
def __init__(self, item_data, container, onfocus): |
| 278 |
ConfigItem.__init__(self, item_data) |
| 279 |
div = E.div( |
| 280 |
id=self.widget_id, |
| 281 |
E.span(item_data.text, class_='label'), |
| 282 |
E.input(type='text') |
| 283 |
) |
| 284 |
div.classList.add('control') |
| 285 |
container.appendChild(div) |
| 286 |
control = div.lastChild |
| 287 |
control.addEventListener('change', self.ui_value_changed.bind(self)) |
| 288 |
control.addEventListener('focus', onfocus) |
| 289 |
div.addEventListener('click', onfocus) |
| 290 |
|
| 291 |
def to_ui(self, val): |
| 292 |
self.control.value = val or '' |
| 293 |
|
| 294 |
def from_ui(self): |
| 295 |
return self.control.value or '' |
| 296 |
|
| 297 |
|
| 298 |
class ColorEdit(ConfigItem): |
| 299 |
def __init__(self, item_data, container, onfocus): |
| 300 |
ConfigItem.__init__(self, item_data) |
| 301 |
div = E.div( |
| 302 |
id=self.widget_id, |
| 303 |
E.span(item_data.text, class_='label'), |
| 304 |
E.input(type='color') |
| 305 |
) |
| 306 |
div.classList.add('control') |
| 307 |
container.appendChild(div) |
| 308 |
control = div.lastChild |
| 309 |
control.addEventListener('change', self.ui_value_changed.bind(self)) |
| 310 |
control.addEventListener('input', self.ui_value_changed.bind(self)) |
| 311 |
control.addEventListener('focus', onfocus) |
| 312 |
div.addEventListener('click', onfocus) |
| 313 |
|
| 314 |
def to_ui(self, val): |
| 315 |
self.control.value = val or '' |
| 316 |
|
| 317 |
def from_ui(self): |
| 318 |
return self.control.value or '' |
| 319 |
|
| 320 |
|
| 321 |
def onfocus(state, name): |
| 322 |
return def (ev): |
| 323 |
c = document.getElementById(state.container_id) |
| 324 |
div = c.querySelector('div[data-name="{}"]'.format(name)) |
| 325 |
if div?.lastChild: |
| 326 |
div.lastChild.style.display = 'block' |
| 327 |
|
| 328 |
|
| 329 |
def create_prefs_widget(container, prefs_data): |
| 330 |
state = {} |
| 331 |
state.container_id = ensure_id(container) |
| 332 |
state.widgets = [] |
| 333 |
clear(container) |
| 334 |
try: |
| 335 |
container.classList.add(CLASS_NAME) |
| 336 |
except Exception: |
| 337 |
pass |
| 338 |
|
| 339 |
outer = E.div(class_='list') |
| 340 |
container.appendChild(outer) |
| 341 |
|
| 342 |
for item in prefs_data: |
| 343 |
div = E.div( |
| 344 |
title=item.tooltip, |
| 345 |
data_name=item.name, |
| 346 |
class_='item', |
| 347 |
E.div(class_='item-main'), |
| 348 |
E.div(item.tooltip or '', class_='help') |
| 349 |
) |
| 350 |
outer.appendChild(div) |
| 351 |
val = get_session_data().get(item.name) |
| 352 |
if item.from_storage: |
| 353 |
val = item.from_storage(val) |
| 354 |
if item.choices: |
| 355 |
cls = Choices |
| 356 |
elif item.type is 'color': |
| 357 |
cls = ColorEdit |
| 358 |
elif val is True or val is False: |
| 359 |
cls = CheckBox |
| 360 |
elif jstype(val) is 'number': |
| 361 |
cls = SpinBox |
| 362 |
else: |
| 363 |
cls = LineEdit |
| 364 |
state.widgets.push((new cls(item, div.firstChild, onfocus(state, item.name))).initialize()) |
| 365 |
if state.widgets.length: |
| 366 |
container.appendChild(E.div(class_='footer', |
| 367 |
create_button(_('Restore default settings'), 'refresh', reset_to_defaults.bind(None, state)) |
| 368 |
)) |
| 369 |
|
| 370 |
|
| 371 |
def reset_to_defaults(state): |
| 372 |
for w in state.widgets: |
| 373 |
w.reset_to_default() |
| 374 |
|
| 375 |
|
| 376 |
def prefs_panel_handler(title_func, get_prefs_data, on_close=None, icon='close'): |
| 377 |
def onkeydown(container_id, close_action, ev): |
| 378 |
if ev.key is 'Escape': |
| 379 |
ev.preventDefault(), ev.stopPropagation() |
| 380 |
close_action() |
| 381 |
|
| 382 |
def close_action(): |
| 383 |
if on_close is not None: |
| 384 |
on_close() |
| 385 |
back() |
| 386 |
|
| 387 |
def init_prefs_panel(container_id): |
| 388 |
container = document.getElementById(container_id) |
| 389 |
container.addEventListener('keydown', onkeydown.bind(None, container_id, close_action), {'passive': False, 'capture': True}) |
| 390 |
container.setAttribute('tabindex', '0') |
| 391 |
create_top_bar(container, title=title_func(), action=close_action, icon=icon) |
| 392 |
container.appendChild(E.div()) |
| 393 |
create_prefs_widget(container.lastChild, get_prefs_data()) |
| 394 |
container.focus() |
| 395 |
|
| 396 |
return init_prefs_panel |