Debian Codegraph @

src/pyj/read_book/globals.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import hash_literals
4
5 from aes import random_bytes
6 from encodings import hexlify
7 from gettext import gettext as _, register_callback, gettext as gt
8
9 _boss = None
10 dark_link_color = '__DARK_LINK_COLOR__'
11
12
13 def set_boss(b):
14 nonlocal _boss
15 _boss = b
16
17
18 def get_boss():
19 return _boss
20
21
22 def current_book():
23 return current_book.book
24 current_book.book = None
25
26
27 def rtl_page_progression():
28 # Other options are "ltr" and "default." For Calibre, "default" is LTR.
29 return current_book().manifest.page_progression_direction == 'rtl'
30
31
32 def ltr_page_progression():
33 # Only RTL and LTR are supported, so it must be LTR if not RTL.
34 return not rtl_page_progression()
35
36 uid = 'calibre-' + hexlify(random_bytes(12))
37
38
39 def viewport_mode_changer(val):
40 if val:
41 viewport_mode_changer.val = val
42 return viewport_mode_changer.val
43
44
45 def current_layout_mode():
46 return current_layout_mode.value
47 current_layout_mode.value = 'flow'
48
49
50 def set_layout_mode(val):
51 current_layout_mode.value = val
52 viewport_mode_changer()(val)
53
54
55 def current_spine_item():
56 return current_spine_item.value
57 current_spine_item.value = None
58
59
60 def set_current_spine_item(val):
61 current_spine_item.value = val
62
63
64 def toc_anchor_map():
65 return toc_anchor_map.value
66
67
68 def set_toc_anchor_map(val):
69 toc_anchor_map.value = val
70
71 default_color_schemes = {
72 'system': {'foreground': '#000000', 'background': '#ffffff', 'name': _('System')},
73 'white': {'foreground': '#000000', 'background': '#ffffff', 'name': _('White')},
74 'black': {'foreground': '#ffffff', 'background': '#000000', 'link': '#4f81bd', 'name': _('Black')},
75 'sepia-light': {'foreground': '#39322B', 'background': '#F6F3E9', 'name': _('Sepia light')},
76 'sepia-dark': {'background': '#39322B', 'foreground': '#F6F3E9', 'link': '#4f81bd', 'name': _('Sepia dark')},
77 }
78
79
80 def set_system_colors(spec):
81 c = default_color_schemes.system
82 c.foreground = spec.foreground
83 c.background = spec.background
84 v'delete c.link'
85 if spec.link:
86 c.link = spec.link
87
88 register_callback(def ():
89 # Ensure the color scheme names are translated
90 for key in default_color_schemes:
91 scheme = default_color_schemes[key]
92 scheme.name = gt(scheme.name)
93 # set the system colors if in dark mode
94 if window.matchMedia and window.matchMedia('(prefers-color-scheme: dark)').matches:
95 set_system_colors({'background': '#111', 'foreground': '#ddd', 'link': dark_link_color})
96 )
97
98 runtime = {
99 'is_standalone_viewer': False,
100 'viewer_in_full_screen': False,
101 'in_develop_mode': v'(typeof window !== "undefined") && !!window.calibre_in_develop_mode',
102 'QT_VERSION': 0,
103 }
104
105
106 ui_operations = {
107 'get_file': None,
108 'get_mathjax_files': None,
109 'update_url_state': None,
110 'update_last_read_time': None,
111 'show_error': None,
112 'redisplay_book': None,
113 'reload_book': None,
114 'forward_gesture': None,
115 'update_color_scheme': None,
116 'update_font_size': None,
117 'goto_cfi': None,
118 'delete_book': None,
119 'focus_iframe': None,
120 'toggle_toc': None,
121 'toggle_full_screen': None,
122 'reset_reading_rates': None,
123 'has_unsynced_changes': None,
124 'upload_pending_annotations': None,
125 }
126
127
128 annot_id_uuid_map = {}
129
130
131 def clear_annot_id_uuid_map():
132 for key in Object.keys(annot_id_uuid_map):
133 v'delete annot_id_uuid_map[key]'
134
135
136 def is_dark_theme(set_val):
137 if set_val?:
138 is_dark_theme.ans = set_val
139 return v'!!is_dark_theme.ans'