Debian Codegraph @

src/fenrirscreenreader/core/speechHistoryManager.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Fenrir TTY screen reader
5 # By Chrys, Storm Dragon, and contributors.
6
7 from fenrirscreenreader.core.i18n import _
8
9
10 class SpeechHistoryManager:
11 def __init__(self):
12 self.env = None
13 self.history = []
14 self.curr_index = -1
15 self.active = False
16 self.bindings_backup = None
17 self.raw_bindings_backup = None
18
19 def initialize(self, environment):
20 self.env = environment
21
22 def shutdown(self):
23 self.set_active(False)
24
25 def is_active(self):
26 return self.active
27
28 def add_text(self, text):
29 if self.active:
30 return False
31 if not isinstance(text, str):
32 return False
33 if text == "":
34 return False
35 text_key = self._get_history_key(text)
36 if text_key == "":
37 return False
38 if text_key in [self._get_history_key(item) for item in self.history]:
39 return False
40 history_size = self._get_history_size()
41 if history_size <= 0:
42 return False
43 self.history.insert(0, text)
44 del self.history[history_size:]
45 if self.curr_index >= len(self.history):
46 self.curr_index = len(self.history) - 1
47 return True
48
49 def open_history(self):
50 if not self.history:
51 self.env["runtime"]["OutputManager"].present_text(
52 _("speech history empty"), interrupt=True
53 )
54 return False
55 self.curr_index = -1
56 self.set_active(True)
57 self.env["runtime"]["OutputManager"].present_text(
58 _("Speech history"), interrupt=True
59 )
60 return True
61
62 def close_history(self, announce=True):
63 if announce:
64 self.env["runtime"]["OutputManager"].present_text(
65 _("speech history closed"), interrupt=True
66 )
67 self.set_active(False)
68
69 def next_entry(self):
70 if not self._has_history():
71 return
72 if self.curr_index == -1:
73 self.curr_index = 0
74 self.present_current()
75 return
76 if self.curr_index <= 0:
77 self.curr_index = 0
78 self.env["runtime"]["OutputManager"].present_text(
79 _("First speech history item"), interrupt=True
80 )
81 self.present_current(interrupt=False)
82 return
83 self.curr_index -= 1
84 self.present_current()
85
86 def prev_entry(self):
87 if not self._has_history():
88 return
89 if self.curr_index == -1:
90 self.curr_index = 0
91 self.present_current()
92 return
93 if self.curr_index >= len(self.history) - 1:
94 self.curr_index = len(self.history) - 1
95 self.env["runtime"]["OutputManager"].present_text(
96 _("Last speech history item"), interrupt=True
97 )
98 self.present_current(interrupt=False)
99 return
100 self.curr_index += 1
101 self.present_current()
102
103 def present_current(self, interrupt=True):
104 if not self._has_history():
105 self.env["runtime"]["OutputManager"].present_text(
106 _("speech history empty"), interrupt=True
107 )
108 return
109 self.env["runtime"]["OutputManager"].present_text(
110 self.history[self.curr_index], interrupt=interrupt
111 )
112
113 def copy_current_to_clipboard(self):
114 if not self._has_history():
115 self.close_history()
116 return
117 text = self.history[self.curr_index]
118 self.env["runtime"]["MemoryManager"].add_value_to_first_index(
119 "clipboardHistory", text
120 )
121 self.env["runtime"]["OutputManager"].present_text(
122 _("copied to clipboard"),
123 sound_icon="CopyToClipboard",
124 interrupt=True,
125 )
126 self.set_active(False)
127
128 def set_active(self, active):
129 if active == self.active:
130 return
131 self.active = active
132 if self.active:
133 self._install_bindings()
134 else:
135 self._restore_bindings()
136
137 def _has_history(self):
138 if not self.history:
139 self.curr_index = -1
140 return False
141 if self.curr_index >= len(self.history):
142 self.curr_index = len(self.history) - 1
143 return True
144
145 def _get_history_size(self):
146 try:
147 return self.env["runtime"]["SettingsManager"].get_setting_as_int(
148 "speech", "history_size"
149 )
150 except Exception:
151 return 50
152
153 def _get_history_key(self, text):
154 return " ".join(text.split())
155
156 def _install_bindings(self):
157 self.bindings_backup = self.env["bindings"].copy()
158 self.raw_bindings_backup = self.env["rawBindings"].copy()
159 self.env["bindings"] = {
160 str([1, ["KEY_UP"]]): "SPEECH_HISTORY_PREV",
161 str([1, ["KEY_DOWN"]]): "SPEECH_HISTORY_NEXT",
162 str([1, ["KEY_SPACE"]]): "SPEECH_HISTORY_CURRENT",
163 str([1, ["KEY_ENTER"]]): "SPEECH_HISTORY_COPY",
164 str([1, ["KEY_KPENTER"]]): "SPEECH_HISTORY_COPY",
165 str([1, ["KEY_ESC"]]): "SPEECH_HISTORY_CLOSE",
166 }
167 modal_raw_bindings = {
168 str([1, ["KEY_UP"]]): [1, ["KEY_UP"]],
169 str([1, ["KEY_DOWN"]]): [1, ["KEY_DOWN"]],
170 str([1, ["KEY_SPACE"]]): [1, ["KEY_SPACE"]],
171 str([1, ["KEY_ENTER"]]): [1, ["KEY_ENTER"]],
172 str([1, ["KEY_KPENTER"]]): [1, ["KEY_KPENTER"]],
173 str([1, ["KEY_ESC"]]): [1, ["KEY_ESC"]],
174 }
175 self.env["rawBindings"] = self.raw_bindings_backup.copy()
176 self.env["rawBindings"].update(modal_raw_bindings)
177 self._refresh_input_bindings()
178
179 def _restore_bindings(self):
180 if self.bindings_backup is not None:
181 self.env["bindings"] = self.bindings_backup
182 if self.raw_bindings_backup is not None:
183 self.env["rawBindings"] = self.raw_bindings_backup
184 self.bindings_backup = None
185 self.raw_bindings_backup = None
186 self._reset_input_state()
187 self._refresh_input_bindings()
188
189 def _reset_input_state(self):
190 try:
191 self.env["runtime"]["InputManager"].reset_input_state()
192 except Exception:
193 pass
194
195 def _refresh_input_bindings(self):
196 try:
197 refresh_grabs = getattr(
198 self.env["runtime"]["InputDriver"], "refresh_grabs", None
199 )
200 if refresh_grabs:
201 refresh_grabs(force=True)
202 except Exception:
203 pass