| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2026, 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.library_data import all_virtual_libraries |
| 9 |
from book_list.saved_virtual_libraries import DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON, normalize_saved_virtual_library_icon, saved_virtual_library_item |
| 10 |
from book_list.saved_virtual_library_icon_picker import create_saved_virtual_library_icon_picker |
| 11 |
from book_list.theme import get_color |
| 12 |
from dom import add_extra_css, clear |
| 13 |
from modals import create_custom_dialog |
| 14 |
from widgets import create_button |
| 15 |
|
| 16 |
|
| 17 |
add_extra_css(def (): return ''' |
| 18 |
|
| 19 |
.cs-virtual-library-dialog { |
| 20 |
width: 100%; |
| 21 |
min-width: 0; |
| 22 |
max-width: min(42rem, calc(100vw - 2rem)); |
| 23 |
box-sizing: border-box; |
| 24 |
|
| 25 |
& .vl-row { |
| 26 |
margin-bottom: 0.75rem; |
| 27 |
} |
| 28 |
& .vl-checkbox-row { |
| 29 |
display: flex; |
| 30 |
align-items: center; |
| 31 |
margin-bottom: 0.35rem; |
| 32 |
} |
| 33 |
& .vl-label { |
| 34 |
display: block; |
| 35 |
margin: 0 0 0.45rem 0; |
| 36 |
font-weight: 700; |
| 37 |
opacity: 0.9; |
| 38 |
} |
| 39 |
& input[type="text"] { |
| 40 |
width: 100%; |
| 41 |
box-sizing: border-box; |
| 42 |
color: inherit; |
| 43 |
} |
| 44 |
& .vl-actions-save-cancel { |
| 45 |
display: flex; |
| 46 |
flex-wrap: wrap; |
| 47 |
align-items: stretch; |
| 48 |
justify-content: space-between; |
| 49 |
gap: 0.5rem; |
| 50 |
margin-top: 0.65rem; |
| 51 |
} |
| 52 |
& .vl-actions-save-cancel > a.calibre-push-button { |
| 53 |
flex: 1 1 0; |
| 54 |
min-width: 0; |
| 55 |
justify-content: center; |
| 56 |
} |
| 57 |
& .vl-actions-save-cancel--toprule { |
| 58 |
margin-top: 0.35rem; |
| 59 |
padding-top: 0.6rem; |
| 60 |
border-top: var(--calibre-border-style-subtle); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
.cs-vl-import-dialog { |
| 65 |
position: relative; |
| 66 |
width: 100%; |
| 67 |
min-width: 0; |
| 68 |
max-width: min(42rem, calc(100vw - 2rem)); |
| 69 |
max-height: calc(min(60vh, 100vh - 6rem) - 5rem); |
| 70 |
box-sizing: border-box; |
| 71 |
display: flex; |
| 72 |
flex-direction: column; |
| 73 |
overflow: hidden; |
| 74 |
|
| 75 |
& .vl-import-empty { |
| 76 |
opacity: 0.7; |
| 77 |
padding: 0.5rem 0; |
| 78 |
} |
| 79 |
& .vl-import-list { |
| 80 |
min-height: 8ex; |
| 81 |
overflow-y: auto; |
| 82 |
margin-bottom: 0.5rem; |
| 83 |
border: var(--calibre-border-style-subtle); |
| 84 |
border-radius: 6px; |
| 85 |
padding: 0.25rem 0; |
| 86 |
} |
| 87 |
& .vl-import-row { |
| 88 |
display: flex; |
| 89 |
align-items: center; |
| 90 |
gap: 0.55rem; |
| 91 |
padding: 0.45rem 0.65rem; |
| 92 |
} |
| 93 |
& .vl-import-row:not(:last-child) { |
| 94 |
border-bottom: var(--calibre-border-style-subtle); |
| 95 |
} |
| 96 |
& .vl-import-row input[type="checkbox"] { |
| 97 |
flex-shrink: 0; |
| 98 |
width: 1.1rem; |
| 99 |
height: 1.1rem; |
| 100 |
cursor: pointer; |
| 101 |
} |
| 102 |
& .vl-import-name { |
| 103 |
flex: 1 1 0; |
| 104 |
min-width: 0; |
| 105 |
overflow: hidden; |
| 106 |
text-overflow: ellipsis; |
| 107 |
white-space: nowrap; |
| 108 |
} |
| 109 |
& .vl-import-icon-wrap { |
| 110 |
display: flex; |
| 111 |
align-items: center; |
| 112 |
gap: 0.3rem; |
| 113 |
flex-shrink: 0; |
| 114 |
} |
| 115 |
& .vl-import-icon-label { |
| 116 |
font-size: 0.8rem; |
| 117 |
opacity: 0.75; |
| 118 |
white-space: nowrap; |
| 119 |
} |
| 120 |
& .vl-import-icon-btn { |
| 121 |
display: inline-flex; |
| 122 |
align-items: center; |
| 123 |
justify-content: center; |
| 124 |
width: 2.35rem; |
| 125 |
height: 2.35rem; |
| 126 |
padding: 0; |
| 127 |
margin: 0; |
| 128 |
font-size: 1.2rem; |
| 129 |
line-height: 1; |
| 130 |
border: var(--calibre-border-style-subtle); |
| 131 |
border-radius: 8px; |
| 132 |
background: rgba(0,0,0,0.02); |
| 133 |
color: inherit; |
| 134 |
cursor: pointer; |
| 135 |
transition: background 120ms ease, border-color 120ms ease; |
| 136 |
} |
| 137 |
& .vl-import-icon-btn:hover { |
| 138 |
background: var(--calibre-color-primary-light); |
| 139 |
border-color: var(--calibre-color-border); |
| 140 |
} |
| 141 |
& .vl-import-icon-btn:focus { |
| 142 |
outline: none; |
| 143 |
box-shadow: var(--calibre-focus-ring); |
| 144 |
} |
| 145 |
& .vl-import-icon-overlay { |
| 146 |
position: absolute; |
| 147 |
inset: 0; |
| 148 |
display: none; |
| 149 |
flex-direction: column; |
| 150 |
gap: 0.65rem; |
| 151 |
padding: 0.75rem; |
| 152 |
border-radius: 6px; |
| 153 |
overflow-y: auto; |
| 154 |
z-index: 10; |
| 155 |
} |
| 156 |
& .vl-import-overlay-title { |
| 157 |
font-weight: 700; |
| 158 |
font-size: 1rem; |
| 159 |
flex-shrink: 0; |
| 160 |
} |
| 161 |
& .vl-import-overlay-actions { |
| 162 |
display: flex; |
| 163 |
flex-wrap: wrap; |
| 164 |
gap: 0.5rem; |
| 165 |
justify-content: space-between; |
| 166 |
margin-top: 0.25rem; |
| 167 |
flex-shrink: 0; |
| 168 |
} |
| 169 |
& .vl-import-overlay-actions > a.calibre-push-button { |
| 170 |
flex: 1 1 0; |
| 171 |
min-width: 0; |
| 172 |
justify-content: center; |
| 173 |
} |
| 174 |
& .vl-actions-save-cancel { |
| 175 |
display: flex; |
| 176 |
flex-wrap: wrap; |
| 177 |
align-items: stretch; |
| 178 |
justify-content: space-between; |
| 179 |
gap: 0.5rem; |
| 180 |
margin-top: 0.65rem; |
| 181 |
flex-shrink: 0; |
| 182 |
} |
| 183 |
& .vl-actions-save-cancel > a.calibre-push-button { |
| 184 |
flex: 1 1 0; |
| 185 |
min-width: 0; |
| 186 |
justify-content: center; |
| 187 |
} |
| 188 |
& .vl-actions-save-cancel--toprule { |
| 189 |
margin-top: 0.35rem; |
| 190 |
padding-top: 0.6rem; |
| 191 |
border-top: var(--calibre-border-style-subtle); |
| 192 |
} |
| 193 |
} |
| 194 |
''' |
| 195 |
) |
| 196 |
|
| 197 |
|
| 198 |
def show_import_virtual_libraries_dialog(library_id, on_save): |
| 199 |
create_custom_dialog(_('Import Virtual Libraries'), def (parent, close_modal): |
| 200 |
vls = all_virtual_libraries() |
| 201 |
vl_names = sorted(Object.keys(vls)) |
| 202 |
if not vl_names.length: |
| 203 |
parent.appendChild(E.div(class_='cs-vl-import-dialog', |
| 204 |
E.div(_('No virtual libraries found in calibre settings.'), class_='vl-import-empty'), |
| 205 |
E.div(class_='vl-actions-save-cancel vl-actions-save-cancel--toprule', |
| 206 |
create_button(_('Close'), 'close', def (ev = None): close_modal();), |
| 207 |
), |
| 208 |
)) |
| 209 |
return |
| 210 |
# Shared overlay for icon picking – rendered over the import dialog |
| 211 |
overlay = E.div(class_='vl-import-icon-overlay') |
| 212 |
overlay.style.backgroundColor = get_color('dialog-background') |
| 213 |
def hide_overlay(ev=None): |
| 214 |
overlay.style.display = 'none' |
| 215 |
clear(overlay) |
| 216 |
def show_icon_overlay(row): |
| 217 |
picker = create_saved_virtual_library_icon_picker(row.icon) |
| 218 |
def confirm_icon(ev=None): |
| 219 |
row.icon = normalize_saved_virtual_library_icon(picker.get_value()) |
| 220 |
row.icon_btn.textContent = row.icon |
| 221 |
hide_overlay() |
| 222 |
clear(overlay) |
| 223 |
overlay.appendChild(E.div(class_='vl-import-overlay-title', _('Choose icon'))) |
| 224 |
overlay.appendChild(picker.root) |
| 225 |
overlay.appendChild(E.div(class_='vl-import-overlay-actions', |
| 226 |
create_button(_('Cancel'), 'close', hide_overlay), |
| 227 |
create_button(_('Select'), 'check', confirm_icon, highlight=True), |
| 228 |
)) |
| 229 |
overlay.style.display = 'flex' |
| 230 |
rows = [] |
| 231 |
list_div = E.div(class_='vl-import-list') |
| 232 |
for vl_name in vl_names: |
| 233 |
search = vls[vl_name] |
| 234 |
cb = E.input(type='checkbox') |
| 235 |
cb.checked = True |
| 236 |
icon_btn = E.button(type='button', class_='vl-import-icon-btn', |
| 237 |
title=_('Click to change icon')) |
| 238 |
icon_btn.textContent = DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON |
| 239 |
row_state = {'name': vl_name, 'search': search, 'checkbox': cb, |
| 240 |
'icon': DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON, 'icon_btn': icon_btn} |
| 241 |
def make_icon_handler(r): |
| 242 |
def handler(ev): |
| 243 |
ev.preventDefault() |
| 244 |
show_icon_overlay(r) |
| 245 |
return handler |
| 246 |
icon_btn.addEventListener('click', make_icon_handler(row_state)) |
| 247 |
rows.push(row_state) |
| 248 |
list_div.appendChild(E.div(class_='vl-import-row', |
| 249 |
cb, |
| 250 |
E.span(vl_name, class_='vl-import-name', title=vl_name), |
| 251 |
E.div(class_='vl-import-icon-wrap', |
| 252 |
E.span(_('Icon:'), class_='vl-import-icon-label'), |
| 253 |
icon_btn, |
| 254 |
), |
| 255 |
)) |
| 256 |
def do_import(ev=None): |
| 257 |
for row in rows: |
| 258 |
if row.checkbox.checked: |
| 259 |
item = saved_virtual_library_item(library_id, row.name, row.search, '', |
| 260 |
row.icon or DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON, True) |
| 261 |
if item: |
| 262 |
on_save(item) |
| 263 |
close_modal() |
| 264 |
parent.appendChild(E.div(class_='cs-vl-import-dialog', |
| 265 |
list_div, |
| 266 |
overlay, |
| 267 |
E.div(class_='vl-actions-save-cancel vl-actions-save-cancel--toprule', |
| 268 |
create_button(_('Cancel'), 'close', def (ev = None): close_modal();), |
| 269 |
create_button(_('Import'), 'plus', do_import, highlight=True), |
| 270 |
), |
| 271 |
)) |
| 272 |
) |
| 273 |
|
| 274 |
|
| 275 |
def show_saved_virtual_library_dialog( |
| 276 |
title, save_label, on_save, library_id, virtual_library='', initial_name='', initial_search='', |
| 277 |
initial_icon=DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON, initial_use_icon=True, on_delete=None, save_icon='check'): |
| 278 |
create_custom_dialog(title, def (parent, close_modal): |
| 279 |
name_input = E.input(type='text', value=((initial_name or '') + ''), placeholder=_('Virtual library name')) |
| 280 |
search_input = E.input(type='text', value=((initial_search or '') + ''), placeholder=_('Search expression')) |
| 281 |
icon_picker = create_saved_virtual_library_icon_picker((initial_icon or DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON) + '') |
| 282 |
use_icon_cb = E.input(type='checkbox') |
| 283 |
use_icon_cb.checked = initial_use_icon is not False |
| 284 |
icon_section = E.div(class_='vl-icon-section', |
| 285 |
E.div(class_='vl-row', |
| 286 |
E.label(_('Icon'), class_='vl-label'), |
| 287 |
icon_picker.root |
| 288 |
), |
| 289 |
) |
| 290 |
use_icon_help = E.div(_('When disabled, only the first character of the name is shown while the navigation is collapsed.'), class_='help') |
| 291 |
def sync_icon_section_visibility(ev=None): |
| 292 |
on = use_icon_cb.checked |
| 293 |
icon_section.style.display = 'block' if on else 'none' |
| 294 |
use_icon_help.style.display = 'none' if on else 'block' |
| 295 |
def save_item(ev=None): |
| 296 |
item = saved_virtual_library_item(library_id, name_input.value, search_input.value, virtual_library, icon_picker.get_value(), use_icon_cb.checked) |
| 297 |
if not item: |
| 298 |
return |
| 299 |
if on_save(item) is False: |
| 300 |
return |
| 301 |
close_modal() |
| 302 |
def delete_item(ev=None): |
| 303 |
if on_delete and on_delete() is False: |
| 304 |
return |
| 305 |
close_modal() |
| 306 |
def open_import(ev=None): |
| 307 |
close_modal() |
| 308 |
show_import_virtual_libraries_dialog(library_id, on_save) |
| 309 |
dialog = E.div(class_='cs-virtual-library-dialog', |
| 310 |
E.div(class_='vl-row', |
| 311 |
E.label(_('Virtual library name'), class_='vl-label'), |
| 312 |
name_input |
| 313 |
), |
| 314 |
E.div(class_='vl-row', |
| 315 |
E.label(_('Search expression'), class_='vl-label'), |
| 316 |
search_input |
| 317 |
), |
| 318 |
E.div(class_='vl-checkbox-row', |
| 319 |
use_icon_cb, |
| 320 |
E.span(_('Show icon in sidebar'), style='margin-left:0.45rem'), |
| 321 |
), |
| 322 |
use_icon_help, |
| 323 |
icon_section, |
| 324 |
) |
| 325 |
actions_class = 'vl-actions-save-cancel' |
| 326 |
if not on_delete: |
| 327 |
actions_class += ' vl-actions-save-cancel--toprule' |
| 328 |
dialog.appendChild(E.div(class_=actions_class, |
| 329 |
create_button(_('Cancel'), 'close', def (ev = None): close_modal();), |
| 330 |
create_button(save_label, save_icon, save_item, highlight=True), |
| 331 |
)) |
| 332 |
if on_delete: |
| 333 |
dialog.lastChild.insertBefore(create_button( |
| 334 |
_('Delete'), 'trash', delete_item, _('Delete this Virtual library')), dialog.lastChild.firstChild) |
| 335 |
else: |
| 336 |
dialog.lastChild.insertBefore( |
| 337 |
create_button(_('Import'), 'library', open_import, _('Import virtual libraries from calibre settings')), |
| 338 |
dialog.lastChild.firstChild) |
| 339 |
parent.appendChild(dialog) |
| 340 |
use_icon_cb.addEventListener('change', sync_icon_section_visibility) |
| 341 |
sync_icon_section_visibility() |
| 342 |
setTimeout(def (): |
| 343 |
name_input.focus() |
| 344 |
, 10) |
| 345 |
) |