Debian Codegraph @

src/pyj/read_book/prefs/main.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.item_list import build_list, create_item
9 from book_list.top_bar import create_top_bar, set_title
10 from dom import clear, ensure_id
11 from read_book.globals import runtime
12 from read_book.prefs.colors import commit_colors, create_colors_panel
13 from read_book.prefs.fonts import commit_fonts, create_fonts_panel
14 from read_book.prefs.head_foot import commit_head_foot, create_head_foot_panel
15 from read_book.prefs.keyboard import commit_keyboard, create_keyboard_panel
16 from read_book.prefs.layout import commit_layout, create_layout_panel
17 from read_book.prefs.misc import commit_misc, create_misc_panel
18 from read_book.prefs.scrolling import commit_scrolling, create_scrolling_panel
19 from read_book.prefs.selection import commit_selection, create_selection_panel
20 from read_book.prefs.touch import commit_touch, create_touch_panel
21 from read_book.prefs.user_stylesheet import (
22 commit_user_stylesheet, create_user_stylesheet_panel
23 )
24
25
26 class Prefs:
27 def __init__(self, container, close_func, on_change):
28 self.close_func = close_func
29 self.on_change = on_change
30 self.main_title = _('Configure book reader')
31 container.appendChild(E.div(
32 style='contain: paint; width: 100%; height: 100%; display: flex; flex-direction: column',
33 tabindex='-1',
34 onkeydown=def (ev):
35 if ev.key is 'Escape':
36 ev.stopPropagation(), ev.preventDefault()
37 if not self.cancel():
38 self.close_func()
39 ,
40 E.div(), E.div(style='flex-grow: 100; overflow: auto')
41 ))
42 container = container.firstChild
43 create_top_bar(container.firstChild, title=self.main_title, icon='close', action=self.onclose)
44 self.container_id = ensure_id(container.lastChild)
45 self.stack = ['top']
46 self.display_top(container.lastChild)
47
48 def set_top_title(self, val):
49 set_title(self.container.parentNode, val)
50
51 def onchange(self):
52 self.on_change()
53
54 def cancel(self):
55 if self.stack.length > 1:
56 self.stack.pop()
57 self.display_panel(self.stack[-1])
58 return True
59
60 def onclose(self):
61 if self.stack.length > 1:
62 which = self.stack.pop()
63 close_func = getattr(self, 'close_' + which, None)
64 if close_func:
65 close_func.bind(self)()
66 self.display_panel(self.stack[-1])
67 else:
68 self.close_func()
69
70 def create_panel(self, container, which, create_func, needs_onchange):
71 self.set_top_title(self.title_map[which] or which)
72 if needs_onchange:
73 create_func(container, self.onclose, self.cancel, self.onchange)
74 else:
75 create_func(container, self.onclose, self.cancel)
76
77 @property
78 def container(self):
79 return document.getElementById(self.container_id)
80
81 def display_panel(self, which):
82 container = self.container
83 clear(container)
84 getattr(self, 'display_' + which)(container)
85 container.setAttribute('tabindex', '-1')
86 container.focus()
87
88 def show_panel(self, which):
89 self.stack.push(which)
90 self.display_panel(which)
91
92 def display_top(self, container):
93 self.set_top_title(self.main_title)
94 c = E.div()
95 container.appendChild(c)
96 self.title_map = {}
97 items = []
98
99 def ci(title, which, subtitle):
100 items.push(create_item(title, self.show_panel.bind(None, which), subtitle))
101 self.title_map[which] = title
102
103 ci(_('Colors'), 'colors', _('Colors of the page and text'))
104 ci(_('Page layout'), 'layout', _('Page margins and number of pages per screen'))
105 ci(_('Styles'), 'user_stylesheet', _('Style rules for text and background image'))
106 ci(_('Headers and footers'), 'head_foot', _('Customize the headers and footers'))
107 ci(_('Scrolling behavior'), 'scrolling', _('Control how the viewer scrolls'))
108 ci(_('Selection behavior'), 'selection', _('Control how the viewer selects text'))
109 ci(_('Touch behavior'), 'touch', _('Customize what various touchscreen gestures do'))
110 ci(_('Keyboard shortcuts'), 'keyboard', _('Customize the keyboard shortcuts'))
111 if runtime.is_standalone_viewer:
112 ci(_('Fonts'), 'fonts', _('Font choices'))
113 ci(_('Miscellaneous'), 'misc', _('Window size, last read position, toolbar, etc.'))
114 build_list(c, items)
115
116 def display_fonts(self, container):
117 self.create_panel(container, 'fonts', create_fonts_panel)
118
119 def close_fonts(self):
120 commit_fonts(self.onchange, self.container)
121
122 def display_misc(self, container):
123 self.create_panel(container, 'misc', create_misc_panel)
124
125 def close_misc(self):
126 commit_misc(self.onchange, self.container)
127
128 def display_colors(self, container):
129 self.create_panel(container, 'colors', create_colors_panel)
130
131 def close_colors(self):
132 commit_colors(self.onchange, self.container)
133
134 def display_layout(self, container):
135 self.create_panel(container, 'layout', create_layout_panel)
136
137 def close_layout(self):
138 commit_layout(self.onchange, self.container)
139
140 def display_user_stylesheet(self, container):
141 self.create_panel(container, 'user_stylesheet', create_user_stylesheet_panel)
142
143 def close_user_stylesheet(self):
144 commit_user_stylesheet(self.onchange, self.container)
145
146 def display_head_foot(self, container):
147 self.create_panel(container, 'head_foot', create_head_foot_panel)
148
149 def close_head_foot(self):
150 commit_head_foot(self.onchange, self.container)
151
152 def display_keyboard(self, container):
153 self.create_panel(container, 'keyboard', create_keyboard_panel, True)
154
155 def close_keyboard(self):
156 commit_keyboard(self.onchange, self.container)
157
158 def display_scrolling(self, container):
159 self.create_panel(container, 'scrolling', create_scrolling_panel)
160
161 def close_scrolling(self):
162 commit_scrolling(self.onchange, self.container)
163
164 def display_selection(self, container):
165 self.create_panel(container, 'selection', create_selection_panel)
166
167 def close_selection(self):
168 commit_selection(self.onchange, self.container)
169
170 def display_touch(self, container):
171 self.create_panel(container, 'touch', create_touch_panel)
172
173 def close_touch(self):
174 commit_touch(self.onchange, self.container)
175
176
177 def create_prefs_panel(container, close_func, on_change):
178 return Prefs(container, close_func, on_change)