| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2026 |
| 3 |
from __python__ import hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from book_list.saved_virtual_libraries import ( |
| 9 |
DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON, normalize_saved_virtual_library_icon |
| 10 |
) |
| 11 |
from book_list.theme import get_color |
| 12 |
from dom import add_extra_css, build_rule, unique_id |
| 13 |
|
| 14 |
|
| 15 |
# (emoji stored in session, short description for picker tooltip / a11y) |
| 16 |
SAVED_VIRTUAL_LIBRARY_ICON_CHOICES = [ |
| 17 |
('📁', _('Folder')), |
| 18 |
('📂', _('Open folder')), |
| 19 |
('🔖', _('Bookmark')), |
| 20 |
('📖', _('Book')), |
| 21 |
('📚', _('Library')), |
| 22 |
('🏠', _('Home')), |
| 23 |
('⭐', _('Star')), |
| 24 |
('🌟', _('Sparkle')), |
| 25 |
('❤️', _('Heart')), |
| 26 |
('🔍', _('Search')), |
| 27 |
('➕', _('Plus')), |
| 28 |
('✏️', _('Pencil')), |
| 29 |
('🖼️', _('Image')), |
| 30 |
('🔗', _('Link')), |
| 31 |
('🤖', _('AI')), |
| 32 |
('🎲', _('Random')), |
| 33 |
('✅', _('Checkmark')), |
| 34 |
('✨', _('Highlight')), |
| 35 |
('⚠️', _('Warning')), |
| 36 |
('❓', _('Help')), |
| 37 |
('🗑️', _('Trash')), |
| 38 |
('⚙️', _('Settings')), |
| 39 |
('👤', _('User')), |
| 40 |
('📣', _('Announcement')), |
| 41 |
('🔄', _('Refresh')), |
| 42 |
('⬆️', _('Sort up')), |
| 43 |
('⬇️', _('Sort down')), |
| 44 |
('📋', _('Clipboard')), |
| 45 |
('📤', _('Send')), |
| 46 |
('🎯', _('Target')), |
| 47 |
('💡', _('Idea')), |
| 48 |
('🏷️', _('Tag')), |
| 49 |
('🌈', _('Rainbow')), |
| 50 |
('🎵', _('Music')), |
| 51 |
('🎬', _('Film')), |
| 52 |
('📌', _('Pin')), |
| 53 |
('📝', _('Memo')), |
| 54 |
] |
| 55 |
|
| 56 |
|
| 57 |
add_extra_css(def (): |
| 58 |
sel = '.cs-vl-icon-picker ' |
| 59 |
ans = build_rule('.cs-vl-icon-picker', margin_top='0.35rem') |
| 60 |
ans += build_rule(sel + '.cs-vl-icon-grid', |
| 61 |
display='flex', |
| 62 |
flex_wrap='wrap', |
| 63 |
gap='0.45rem', |
| 64 |
align_items='stretch', |
| 65 |
justify_content='flex-start', |
| 66 |
) |
| 67 |
ans += build_rule(sel + 'button.cs-vl-icon-btn', |
| 68 |
display='inline-flex', |
| 69 |
align_items='center', |
| 70 |
justify_content='center', |
| 71 |
width='2.35rem', |
| 72 |
height='2.35rem', |
| 73 |
padding='0', |
| 74 |
margin='0', |
| 75 |
box_sizing='border-box', |
| 76 |
border='var(--calibre-border-style-subtle)', |
| 77 |
border_radius='10px', |
| 78 |
background_color='rgba(0,0,0,0.02)', |
| 79 |
color=get_color('window-foreground'), |
| 80 |
cursor='pointer', |
| 81 |
line_height='1', |
| 82 |
transition='background 120ms ease, border-color 120ms ease, box-shadow 120ms ease', |
| 83 |
) |
| 84 |
ans += build_rule(sel + 'button.cs-vl-icon-btn .cs-vl-emoji', |
| 85 |
font_size='1.3rem', |
| 86 |
line_height='1', |
| 87 |
user_select='none', |
| 88 |
pointer_events='none', |
| 89 |
) |
| 90 |
ans += build_rule(sel + 'button.cs-vl-icon-btn:hover', |
| 91 |
background_color='var(--calibre-color-primary-light)', |
| 92 |
border_color='var(--calibre-color-border)', |
| 93 |
) |
| 94 |
ans += build_rule(sel + 'button.cs-vl-icon-btn:focus', |
| 95 |
outline='none', |
| 96 |
box_shadow='var(--calibre-focus-ring)', |
| 97 |
) |
| 98 |
ans += build_rule(sel + 'button.cs-vl-icon-btn-selected', |
| 99 |
background_color='var(--calibre-color-primary-light-2)', |
| 100 |
border_color='var(--calibre-color-border)', |
| 101 |
font_weight='600', |
| 102 |
) |
| 103 |
ans += build_rule(sel + '.cs-vl-icon-custom-wrap', |
| 104 |
margin_top='0.55rem', |
| 105 |
display='flex', |
| 106 |
flex_direction='column', |
| 107 |
gap='0.35rem', |
| 108 |
width='100%', |
| 109 |
max_width='100%', |
| 110 |
box_sizing='border-box', |
| 111 |
) |
| 112 |
ans += build_rule(sel + '.cs-vl-icon-custom-wrap label', |
| 113 |
font_size='0.88rem', |
| 114 |
font_weight='600', |
| 115 |
opacity='0.88', |
| 116 |
) |
| 117 |
ans += build_rule(sel + 'input.cs-vl-icon-custom-input', |
| 118 |
width='100%', |
| 119 |
max_width='100%', |
| 120 |
box_sizing='border-box', |
| 121 |
padding='0.5rem 0.65rem', |
| 122 |
border_radius='10px', |
| 123 |
border='var(--calibre-border-style-subtle)', |
| 124 |
background_color='rgba(0,0,0,0.02)', |
| 125 |
color=get_color('window-foreground'), |
| 126 |
font_size='1.1rem', |
| 127 |
line_height='1.3', |
| 128 |
) |
| 129 |
ans += build_rule(sel + '.cs-vl-icon-custom-hint', |
| 130 |
font_size='0.82rem', |
| 131 |
font_weight='600', |
| 132 |
opacity='0.72', |
| 133 |
line_height='1.35', |
| 134 |
) |
| 135 |
return ans |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
def create_saved_virtual_library_icon_picker(initial_icon=DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON): |
| 140 |
state = {'icon': normalize_saved_virtual_library_icon(initial_icon) + ''} |
| 141 |
grid = E.div(class_='cs-vl-icon-grid', role='radiogroup', aria_label=_('Icon presets')) |
| 142 |
|
| 143 |
inp_id = unique_id('cs-vl-icon-custom') |
| 144 |
|
| 145 |
def sync_grid_only(): |
| 146 |
found = False |
| 147 |
for btn in grid.querySelectorAll('button.cs-vl-icon-btn'): |
| 148 |
nm = btn.dataset.vlIcon |
| 149 |
on = nm is state.icon |
| 150 |
if on: |
| 151 |
found = True |
| 152 |
btn.classList.add('cs-vl-icon-btn-selected') |
| 153 |
else: |
| 154 |
btn.classList.remove('cs-vl-icon-btn-selected') |
| 155 |
btn.setAttribute('aria-checked', 'true' if on else 'false') |
| 156 |
return found |
| 157 |
|
| 158 |
def sync_from_input(): |
| 159 |
raw = (custom_inp.value or '') + '' |
| 160 |
state.icon = normalize_saved_virtual_library_icon(raw) |
| 161 |
sync_grid_only() |
| 162 |
|
| 163 |
def make_handler(stored_emoji): |
| 164 |
def on_click(evt): |
| 165 |
evt.preventDefault() |
| 166 |
state.icon = stored_emoji |
| 167 |
custom_inp.value = stored_emoji |
| 168 |
sync_grid_only() |
| 169 |
return on_click |
| 170 |
|
| 171 |
for emoji_char, label in SAVED_VIRTUAL_LIBRARY_ICON_CHOICES: |
| 172 |
btn = E.button( |
| 173 |
type='button', |
| 174 |
class_='cs-vl-icon-btn', |
| 175 |
role='radio', |
| 176 |
title=label, |
| 177 |
) |
| 178 |
btn.dataset.vlIcon = emoji_char |
| 179 |
btn.setAttribute('aria-label', label) |
| 180 |
btn.appendChild(E.span(emoji_char, class_='cs-vl-emoji')) |
| 181 |
btn.addEventListener('click', make_handler(emoji_char)) |
| 182 |
grid.appendChild(btn) |
| 183 |
|
| 184 |
custom_inp = E.input( |
| 185 |
type='text', |
| 186 |
id=inp_id, |
| 187 |
class_='cs-vl-icon-custom-input', |
| 188 |
placeholder=_('e.g. emoji or a few characters'), |
| 189 |
aria_label=_('Custom icon'), |
| 190 |
) |
| 191 |
custom_inp.addEventListener('input', def (ev): |
| 192 |
sync_from_input() |
| 193 |
) |
| 194 |
custom_inp.addEventListener('change', def (ev): |
| 195 |
sync_from_input() |
| 196 |
) |
| 197 |
|
| 198 |
custom_wrap = E.div( |
| 199 |
class_='cs-vl-icon-custom-wrap', |
| 200 |
E.label(_('Custom'), class_='cs-vl-custom-label', for_=inp_id), |
| 201 |
custom_inp, |
| 202 |
) |
| 203 |
help_span = E.span( |
| 204 |
_('You can pick a preset above or type your own (emoji, letter, etc.).'), |
| 205 |
class_='cs-vl-icon-custom-hint', |
| 206 |
) |
| 207 |
custom_wrap.appendChild(help_span) |
| 208 |
|
| 209 |
if not sync_grid_only(): |
| 210 |
custom_inp.value = state.icon + '' |
| 211 |
root = E.div(class_='cs-vl-icon-picker', grid, custom_wrap) |
| 212 |
|
| 213 |
def get_value(): |
| 214 |
raw = (custom_inp.value or '') + '' |
| 215 |
trimmed = raw.strip() |
| 216 |
if trimmed: |
| 217 |
return normalize_saved_virtual_library_icon(trimmed) |
| 218 |
return state.icon or DEFAULT_SAVED_VIRTUAL_LIBRARY_ICON |
| 219 |
|
| 220 |
return {'root': root, 'get_value': get_value} |