| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2026 |
| 3 |
from __python__ import hash_literals, bound_methods |
| 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.library_data import browse_field_settings, set_browse_fields |
| 10 |
from book_list.prefs import create_prefs_widget |
| 11 |
from book_list.search import get_prefs as get_tb_prefs |
| 12 |
from book_list.theme import apply_book_list_theme |
| 13 |
from book_list.top_bar import create_top_bar |
| 14 |
from book_list.ui import set_panel_handler |
| 15 |
from dom import add_extra_css, build_rule, clear |
| 16 |
from session import all_settings |
| 17 |
|
| 18 |
|
| 19 |
CLASS_NAME = 'cs-ui-settings' |
| 20 |
DEFAULT_PRIMARY_HEX = all_settings.ui_highlight_colors.default.light |
| 21 |
DEFAULT_PRIMARY_DARK_HEX = all_settings.ui_highlight_colors.default.dark |
| 22 |
|
| 23 |
|
| 24 |
add_extra_css(def (): |
| 25 |
sel = '.' + CLASS_NAME + ' ' |
| 26 |
style = '' |
| 27 |
# Reuse cs-prefs layout/classes; keep UI-settings-specific overrides only. |
| 28 |
style += build_rule(sel + 'input[type="color"]', |
| 29 |
flex='0 0 auto', |
| 30 |
width='72px', |
| 31 |
height='44px', |
| 32 |
padding='0', |
| 33 |
box_sizing='border-box', |
| 34 |
border='none', |
| 35 |
border_radius='10px', |
| 36 |
background_color='transparent', |
| 37 |
cursor='pointer' |
| 38 |
) |
| 39 |
# Make the internal color swatch fill the rounded input |
| 40 |
style += r''' |
| 41 |
.cs-ui-settings input[type="color"]{ |
| 42 |
appearance: none; |
| 43 |
overflow: hidden; |
| 44 |
padding: 0; |
| 45 |
background: transparent; |
| 46 |
border: 0 !important; |
| 47 |
outline: none !important; |
| 48 |
box-shadow: none !important; |
| 49 |
} |
| 50 |
.cs-ui-settings input[type="color"]::-webkit-color-swatch-wrapper{ |
| 51 |
padding: 0; |
| 52 |
width: 100%; |
| 53 |
height: 100%; |
| 54 |
} |
| 55 |
.cs-ui-settings input[type="color"]::-webkit-color-swatch{ |
| 56 |
border: 0; |
| 57 |
border-radius: 10px; |
| 58 |
width: 100%; |
| 59 |
height: 100%; |
| 60 |
display: block; |
| 61 |
} |
| 62 |
.cs-ui-settings input[type="color"]::-moz-color-swatch{ |
| 63 |
border: 0; |
| 64 |
border-radius: 10px; |
| 65 |
width: 100%; |
| 66 |
height: 100%; |
| 67 |
} |
| 68 |
''' |
| 69 |
# Prefer shared cs-prefs spacing; keep help a little softer. |
| 70 |
style += build_rule(sel + '.help', opacity='0.75') |
| 71 |
style += build_rule('.cs-ui-settings.cs-prefs .help', display='block') |
| 72 |
style += build_rule(sel + '.browse-intro', |
| 73 |
margin='0 0 0.85rem 0', |
| 74 |
opacity='0.8', |
| 75 |
overflow_wrap='anywhere', |
| 76 |
) |
| 77 |
# Tabs widget |
| 78 |
style += build_rule(sel + '.cs-tabs', |
| 79 |
width='100%', |
| 80 |
min_width='0', |
| 81 |
box_sizing='border-box', |
| 82 |
) |
| 83 |
style += build_rule(sel + '[role="tablist"]', |
| 84 |
display='flex', |
| 85 |
flex_direction='row', |
| 86 |
gap='0', |
| 87 |
border_bottom='var(--calibre-border-style)', |
| 88 |
margin_bottom='1rem', |
| 89 |
max_width='100%', |
| 90 |
overflow_x='auto', overflow_y='hidden', |
| 91 |
) |
| 92 |
style += build_rule(sel + '[role="tab"]', |
| 93 |
background='none', |
| 94 |
border='none', |
| 95 |
border_bottom='2px solid transparent', |
| 96 |
padding='0.6rem 1.1rem', |
| 97 |
font_size='0.97rem', |
| 98 |
font_weight='600', |
| 99 |
color='var(--calibre-color-window-foreground)', |
| 100 |
opacity='0.65', |
| 101 |
cursor='pointer', |
| 102 |
border_radius='0', |
| 103 |
margin_bottom='-1px', |
| 104 |
transition='color var(--cs-dur-2) var(--cs-ease), border-color var(--cs-dur-2) var(--cs-ease), opacity var(--cs-dur-2) var(--cs-ease)', |
| 105 |
outline='none', |
| 106 |
user_select='none', |
| 107 |
white_space='nowrap', |
| 108 |
) |
| 109 |
style += r''' |
| 110 |
@media (max-width: 520px) { |
| 111 |
.cs-ui-settings [role="tab"] { |
| 112 |
padding-left: 0.75rem; |
| 113 |
padding-right: 0.75rem; |
| 114 |
} |
| 115 |
} |
| 116 |
''' |
| 117 |
style += build_rule(sel + '[role="tabpanel"]', |
| 118 |
min_width='0', |
| 119 |
width='100%', |
| 120 |
box_sizing='border-box', |
| 121 |
) |
| 122 |
style += build_rule(sel + '[role="tabpanel"][hidden]', |
| 123 |
display='none', |
| 124 |
) |
| 125 |
return style |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
def create_tabs_widget(root): |
| 130 |
# --- Tabs widget --- |
| 131 |
tab_interface_id = 'cs-ui-tab-panel-interface' |
| 132 |
tab_browse_id = 'cs-ui-tab-panel-browse' |
| 133 |
tab_tagbrowser_id = 'cs-ui-tab-panel-tagbrowser' |
| 134 |
|
| 135 |
tab_interface_btn = E.button( |
| 136 |
_('Interface'), |
| 137 |
role='tab', |
| 138 |
aria_selected='true', |
| 139 |
aria_controls=tab_interface_id, |
| 140 |
tabindex='0', |
| 141 |
title=_('Customise the look & feel of the interface'), |
| 142 |
) |
| 143 |
tab_tagbrowser_btn = E.button( |
| 144 |
_('Tag browser'), |
| 145 |
role='tab', |
| 146 |
aria_selected='false', |
| 147 |
aria_controls=tab_tagbrowser_id, |
| 148 |
tabindex='-1', |
| 149 |
title=_('Customise how the Tag browser (accessible via the search button) behaves'), |
| 150 |
) |
| 151 |
tab_browse_btn = E.button( |
| 152 |
_('Browse'), |
| 153 |
role='tab', |
| 154 |
aria_selected='false', |
| 155 |
aria_controls=tab_browse_id, |
| 156 |
tabindex='-1', |
| 157 |
title=_('Choose fields to show in the Browse section'), |
| 158 |
) |
| 159 |
|
| 160 |
tablist = E.div(role='tablist', aria_label=_('Settings sections')) |
| 161 |
tablist.appendChild(tab_interface_btn) |
| 162 |
tablist.appendChild(tab_browse_btn) |
| 163 |
tablist.appendChild(tab_tagbrowser_btn) |
| 164 |
|
| 165 |
panel_interface = E.div( |
| 166 |
id=tab_interface_id, |
| 167 |
role='tabpanel', |
| 168 |
) |
| 169 |
panel_browse = E.div( |
| 170 |
id=tab_browse_id, |
| 171 |
role='tabpanel', |
| 172 |
) |
| 173 |
panel_tagbrowser = E.div( |
| 174 |
id=tab_tagbrowser_id, |
| 175 |
role='tabpanel', |
| 176 |
) |
| 177 |
panel_browse.setAttribute('hidden', '') |
| 178 |
panel_tagbrowser.setAttribute('hidden', '') |
| 179 |
|
| 180 |
tabs_widget = E.div(class_='cs-tabs') |
| 181 |
tabs_widget.appendChild(tablist) |
| 182 |
tabs_widget.appendChild(panel_interface) |
| 183 |
tabs_widget.appendChild(panel_browse) |
| 184 |
tabs_widget.appendChild(panel_tagbrowser) |
| 185 |
root.appendChild(tabs_widget) |
| 186 |
|
| 187 |
def select_tab(btn, panel): |
| 188 |
for tb in tablist.querySelectorAll('[role="tab"]'): |
| 189 |
tb.setAttribute('aria-selected', 'false') |
| 190 |
tb.setAttribute('tabindex', '-1') |
| 191 |
btn.setAttribute('aria-selected', 'true') |
| 192 |
btn.setAttribute('tabindex', '0') |
| 193 |
for p in tabs_widget.querySelectorAll('[role="tabpanel"]'): |
| 194 |
p.setAttribute('hidden', '') |
| 195 |
panel.removeAttribute('hidden') |
| 196 |
|
| 197 |
tab_interface_btn.addEventListener('click', def (ev): select_tab(tab_interface_btn, panel_interface);) |
| 198 |
tab_browse_btn.addEventListener('click', def (ev): select_tab(tab_browse_btn, panel_browse);) |
| 199 |
tab_tagbrowser_btn.addEventListener('click', def (ev): select_tab(tab_tagbrowser_btn, panel_tagbrowser);) |
| 200 |
|
| 201 |
def on_tablist_keydown(ev): |
| 202 |
tabs = tablist.querySelectorAll('[role="tab"]') |
| 203 |
idx = -1 |
| 204 |
for i in range(tabs.length): |
| 205 |
if tabs[i] is ev.target: |
| 206 |
idx = i |
| 207 |
break |
| 208 |
if idx < 0: |
| 209 |
return |
| 210 |
if ev.key is 'ArrowRight': |
| 211 |
ev.preventDefault() |
| 212 |
next_idx = (idx + 1) % tabs.length |
| 213 |
tabs[next_idx].focus() |
| 214 |
tabs[next_idx].click() |
| 215 |
elif ev.key is 'ArrowLeft': |
| 216 |
ev.preventDefault() |
| 217 |
next_idx = (idx - 1 + tabs.length) % tabs.length |
| 218 |
tabs[next_idx].focus() |
| 219 |
tabs[next_idx].click() |
| 220 |
|
| 221 |
tablist.addEventListener('keydown', on_tablist_keydown) |
| 222 |
return panel_interface, panel_browse, panel_tagbrowser |
| 223 |
|
| 224 |
|
| 225 |
def selected_browse_fields(container): |
| 226 |
ans = [] |
| 227 |
for checkbox in container.querySelectorAll('input[data-field-key]'): |
| 228 |
if checkbox.checked: |
| 229 |
ans.push(checkbox.getAttribute('data-field-key')) |
| 230 |
return ans |
| 231 |
|
| 232 |
|
| 233 |
def save_browse_fields(container, ev): |
| 234 |
set_browse_fields(selected_browse_fields(container)) |
| 235 |
|
| 236 |
|
| 237 |
def _highlight_light_from_storage(val): |
| 238 |
return (val.light if val and jstype(val) is 'object' else None) or DEFAULT_PRIMARY_HEX |
| 239 |
|
| 240 |
|
| 241 |
def _highlight_dark_from_storage(val): |
| 242 |
return (val.dark if val and jstype(val) is 'object' else None) or DEFAULT_PRIMARY_DARK_HEX |
| 243 |
|
| 244 |
|
| 245 |
def _highlight_light_to_storage(color): |
| 246 |
existing = get_session_data().get('ui_highlight_colors') or {} |
| 247 |
return {'light': color, 'dark': existing.dark or DEFAULT_PRIMARY_DARK_HEX} |
| 248 |
|
| 249 |
|
| 250 |
def _highlight_dark_to_storage(color): |
| 251 |
existing = get_session_data().get('ui_highlight_colors') or {} |
| 252 |
return {'light': existing.light or DEFAULT_PRIMARY_HEX, 'dark': color} |
| 253 |
|
| 254 |
|
| 255 |
def create_browse_fields_widget(container): |
| 256 |
def refresh(): |
| 257 |
if container.isConnected: |
| 258 |
create_browse_fields_widget(container) |
| 259 |
else: |
| 260 |
window.cs_refresh_browse_field_settings = None |
| 261 |
|
| 262 |
window.cs_refresh_browse_field_settings = refresh |
| 263 |
clear(container) |
| 264 |
try: |
| 265 |
container.classList.add('cs-prefs') |
| 266 |
except Exception: |
| 267 |
pass |
| 268 |
container.appendChild(E.div(_('Control which categories are visible in the sidebar.'), class_='browse-intro')) |
| 269 |
outer = E.div(class_='list') |
| 270 |
container.appendChild(outer) |
| 271 |
|
| 272 |
found = False |
| 273 |
for field in browse_field_settings(): |
| 274 |
found = True |
| 275 |
div = E.div( |
| 276 |
class_='item', |
| 277 |
E.div(class_='item-main'), |
| 278 |
) |
| 279 |
control = E.label( |
| 280 |
style='cursor: pointer', |
| 281 |
E.input(type='checkbox', data_field_key=field.key), |
| 282 |
E.span(field.name or field.key, class_='label'), |
| 283 |
) |
| 284 |
control.classList.add('control') |
| 285 |
div.firstChild.appendChild(control) |
| 286 |
checkbox = control.firstChild |
| 287 |
checkbox.checked = bool(field.enabled) |
| 288 |
checkbox.addEventListener('change', save_browse_fields.bind(None, container)) |
| 289 |
outer.appendChild(div) |
| 290 |
|
| 291 |
if not found: |
| 292 |
outer.appendChild(E.div( |
| 293 |
class_='item', |
| 294 |
E.div( |
| 295 |
class_='item-main', |
| 296 |
E.div(class_='control', E.span(_('No browse fields available'), class_='label')) |
| 297 |
) |
| 298 |
)) |
| 299 |
|
| 300 |
|
| 301 |
def get_ui_prefs(): |
| 302 |
return [ |
| 303 |
{ |
| 304 |
'name': 'book_list_color_scheme', |
| 305 |
'text': _('Color scheme'), |
| 306 |
'tooltip': _('Light or dark styling for the interface. Automatic follows your device or browser'), |
| 307 |
'choices': [('auto', _('Automatic')), ('dark', _('Dark')), ('light', _('Light'))], |
| 308 |
'onchange': apply_book_list_theme, |
| 309 |
}, |
| 310 |
{ |
| 311 |
'name': 'ui_highlight_colors', |
| 312 |
'text': _('Highlight color (light theme)'), |
| 313 |
'tooltip': _('Used for primary buttons, links, focus rings, et cetera in the light theme'), |
| 314 |
'type': 'color', |
| 315 |
'from_storage': _highlight_light_from_storage, |
| 316 |
'to_storage': _highlight_light_to_storage, |
| 317 |
'onchange': apply_book_list_theme, |
| 318 |
}, |
| 319 |
{ |
| 320 |
'name': 'ui_highlight_colors', |
| 321 |
'text': _('Highlight color (dark theme)'), |
| 322 |
'tooltip': _('Used for primary buttons, links, focus rings, et cetera in the dark theme'), |
| 323 |
'type': 'color', |
| 324 |
'from_storage': _highlight_dark_from_storage, |
| 325 |
'to_storage': _highlight_dark_to_storage, |
| 326 |
'onchange': apply_book_list_theme, |
| 327 |
}, |
| 328 |
{ |
| 329 |
'name': 'show_sidebar_in_book_viewer', |
| 330 |
'text': _('Show sidebar when reading'), |
| 331 |
'tooltip': _('Show the left navigation sidebar when reading books'), |
| 332 |
}, |
| 333 |
{ |
| 334 |
'name': 'series_nav_display_mode', |
| 335 |
'text': _('Series display mode'), |
| 336 |
'tooltip': _('Choose how series are displayed in the Series navigation panel'), |
| 337 |
'choices': [('stack', _('Stacked covers')), ('list', _('List')),], |
| 338 |
}, |
| 339 |
] |
| 340 |
|
| 341 |
|
| 342 |
def init(container_id): |
| 343 |
container = document.getElementById(container_id) |
| 344 |
container.appendChild(E.div(class_='cs-prefs ' + CLASS_NAME)) |
| 345 |
root = container.lastChild |
| 346 |
create_top_bar(container, title=_('Settings'), icon='back') |
| 347 |
panel_interface, panel_browse, panel_tagbrowser = create_tabs_widget(root) |
| 348 |
# --- Interface tab content --- |
| 349 |
create_prefs_widget(panel_interface, get_ui_prefs()) |
| 350 |
# --- Browse tab content --- |
| 351 |
create_browse_fields_widget(panel_browse) |
| 352 |
# --- Tag browser tab content --- |
| 353 |
create_prefs_widget(panel_tagbrowser, get_tb_prefs()) |
| 354 |
|
| 355 |
|
| 356 |
def set_panel_handlers(): |
| 357 |
set_panel_handler('book_list^prefs', init) |