Debian Codegraph @

src/pyj/read_book/prefs/layout.pyj

raw
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 dom import add_extra_css, build_rule, element, unique_id
10 from read_book.globals import runtime
11 from read_book.prefs.utils import create_button_box
12 from session import session_defaults
13 from utils import safe_set_inner_html
14
15 CONTAINER = unique_id('reader-page-layout')
16 MARGINS = unique_id('reader-page-margins')
17 READ_MODE = unique_id('read-mode')
18 FS_MODE = unique_id('fs-mode')
19 COLS = unique_id('cols-per-screen')
20 TEXT_AREA = unique_id('text-area')
21
22 add_extra_css(def ():
23 sel = '#' + MARGINS
24 style = build_rule(sel, margin_left='1rem', margin_top='-1ex')
25 style += build_rule('#{} td'.format(CONTAINER), padding='1ex')
26 return style
27 )
28
29
30 def restore_defaults():
31 defaults = session_defaults()
32 container = document.getElementById(CONTAINER)
33 for which in 'top bottom left right'.split(' '):
34 container.querySelector('input[name={}]'.format(which)).value = str(defaults['margin_' + which])
35 for name in 'paged flow'.split(' '):
36 container.querySelector(f'#{READ_MODE} input[data-name={name}]').checked = defaults.read_mode is name
37 if not runtime.is_standalone_viewer:
38 container.querySelector(f'#{FS_MODE} input[value={defaults.fullscreen_when_opening}]').checked = True
39 for name in 'portrait landscape'.split(' '):
40 container.querySelector('input[name={}]'.format(name)).value = str(defaults.columns_per_screen[name])
41 for which in 'width height'.split(' '):
42 container.querySelector('input[name={}]'.format(which)).value = str(defaults['max_text_' + which])
43 container.querySelector('input[name=cover_preserve_aspect_ratio]').checked = defaults.cover_preserve_aspect_ratio
44
45
46 def create_layout_panel(container, apply_func, cancel_func):
47 container.appendChild(E.div(id=CONTAINER))
48 container = container.lastChild
49 sd = get_session_data()
50 container.appendChild(E.p(style='margin:1ex 1rem; padding: 1ex 0'))
51 safe_set_inner_html(container.lastChild, _('Current window size is: <b>{0}x{1}</b> pixels').format(window.innerWidth, window.innerHeight))
52 container.appendChild(E.p(_('Change the page margins (in pixels) below'), style='margin:1ex 1rem; padding: 1ex 0'))
53 container.appendChild(E.table(id=MARGINS))
54 labels = {'top': _('Top:'), 'bottom': _('Bottom:'), 'left': _('Left:'), 'right': _('Right:')}
55
56 def item(which, tr):
57 tr.appendChild(E.td(labels[which]))
58 tr.appendChild(E.td(E.input(type='number', max='9999', min='0', step='1', name=which, value=str(sd.get('margin_' + which)))))
59
60 container.lastChild.appendChild(E.tr())
61 item('left', container.lastChild.lastChild)
62 item('right', container.lastChild.lastChild)
63 container.lastChild.appendChild(E.tr())
64 item('top', container.lastChild.lastChild)
65 item('bottom', container.lastChild.lastChild)
66
67 def sec(text):
68 container.appendChild(E.div(text, style='margin: 2ex 1rem; padding-top:2ex; border-top: solid 1px; max-width: 50em'))
69
70 sec(_('Choose the page layout mode. In paged mode, the text is split up into individual pages, as in a paper book. In flow mode'
71 ' text is presented as one long scrolling page, as in web browsers.'))
72 container.appendChild(E.div(id=READ_MODE, style='margin: 1ex 2rem; display: flex;'))
73 rm = sd.get('read_mode')
74 rm = 'flow' if rm is 'flow' else 'paged'
75
76 def rb(name, text):
77 d = container.lastChild
78 d.appendChild(E.label(E.input(type='radio', name='page-layout-mode', data_name=name, checked=rm is name), text))
79 rb('paged', _('Paged mode'))
80 container.lastChild.appendChild(E.span('\xa0', style='width:3em'))
81 rb('flow', _('Flow mode'))
82
83 sec(_('In paged mode, control the number of pages per screen. A setting of zero means the number of pages is'
84 ' set based on the screen size.'))
85 cps = sd.get('columns_per_screen')
86 container.appendChild(E.table(style='margin: 1ex 1rem', id=COLS,
87 E.tr(
88 E.td(_('Portrait:')), E.td(E.input(type='number', name='portrait', min='0', step='1', max='20', value=str(cps.portrait))),
89 E.td(_('Landscape:')), E.td(E.input(type='number', name='landscape', min='0', step='1', max='20', value=str(cps.landscape))),
90 )))
91
92 sec(_('Change the maximum screen area (in pixels) used to display text.'
93 ' A value of zero means that all available screen area is used.'))
94 container.appendChild(E.table(style='margin: 1ex 1rem', id=TEXT_AREA,
95 E.tr(
96 E.td(_('Width:')), E.td(E.input(type='number', name='width', min='0', step='10', max='99999', value=str(sd.get('max_text_width')))),
97 E.td(_('Height:')), E.td(E.input(type='number', name='height', min='0', step='10', max='99999', value=str(sd.get('max_text_height')))),
98 )))
99
100 sec(_('Miscellaneous'))
101 container.appendChild(E.div(style='margin: 1ex 2rem; display: flex;',
102 E.label(E.input(type='checkbox', name='cover_preserve_aspect_ratio', checked=sd.get('cover_preserve_aspect_ratio')), _('Preserve cover aspect ratio'))))
103 if not runtime.is_standalone_viewer:
104 name = 'fullscreen_when_opening'
105 val = sd.get(name)
106 if 'auto always never'.split(' ').indexOf(val or '') < 0:
107 val = session_defaults().fullscreen_when_opening
108 container.appendChild(E.div(
109 E.div(style='margin: 1ex 2rem', id=FS_MODE,
110 _('When opening a book enter fullscreen:'), ' ',
111 E.label(E.input(type='radio', name=name, value='auto', checked=val is 'auto'), _('Auto')),
112 '\xa0',
113 E.label(E.input(type='radio', name=name, value='always', checked=val is 'always'), _('Always')),
114 '\xa0',
115 E.label(E.input(type='radio', name=name, value='never', checked=val is 'never'), _('Never')),
116 )
117 ))
118
119 container.appendChild(E.div(style='margin: 1rem', create_button_box(restore_defaults, apply_func, cancel_func)))
120
121
122 develop = create_layout_panel
123
124
125 def commit_layout(onchange, container):
126 was_changed = False
127 sd = get_session_data()
128 for which in 'top bottom left right'.split(' '):
129 i = element(MARGINS, '[name={}]'.format(which))
130 try:
131 val = int(i.value)
132 except:
133 continue
134 if val is not sd.get('margin_' + which):
135 was_changed = True
136 sd.set('margin_' + which, val)
137 rm = sd.get('read_mode')
138 rm = 'flow' if rm is 'flow' else 'paged'
139 crm = 'paged' if element(READ_MODE, 'input').checked else 'flow'
140 if rm is not crm:
141 was_changed = True
142 sd.set('read_mode', crm)
143 if not runtime.is_standalone_viewer:
144 fs = sd.get('fullscreen_when_opening')
145 cfs = document.querySelector(f'#{FS_MODE} input[name="fullscreen_when_opening"]:checked').value
146 if cfs is not fs:
147 was_changed = True
148 sd.set('fullscreen_when_opening', cfs)
149 cps = sd.get('columns_per_screen')
150 cps = {'portrait': cps.portrait, 'landscape': cps.landscape}
151 for which in ('portrait', 'landscape'):
152 inp = element(COLS, 'input[name={}]'.format(which))
153 try:
154 val = int(inp.value)
155 except:
156 continue
157 if cps[which] is not val:
158 cps[which] = val
159 sd.set('columns_per_screen', cps)
160 was_changed = True
161 for which in ('width', 'height'):
162 try:
163 val = int(element(TEXT_AREA, 'input[name={}]'.format(which)).value)
164 except:
165 continue
166 if val is not sd.get('max_text_' + which):
167 was_changed = True
168 sd.set('max_text_' + which, val)
169
170 cover_preserve_aspect_ratio = element(CONTAINER, 'input[name=cover_preserve_aspect_ratio]').checked
171 if cover_preserve_aspect_ratio is not sd.get('cover_preserve_aspect_ratio'):
172 was_changed = True
173 sd.set('cover_preserve_aspect_ratio', cover_preserve_aspect_ratio)
174
175 if was_changed:
176 onchange()