Debian Codegraph @

src/fenrirscreenreader/core/cursorManager.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 import debug
8
9
10 class CursorManager:
11 def __init__(self):
12 pass
13
14 def initialize(self, environment):
15 self.env = environment
16
17 def should_process_numpad_commands(self):
18 """
19 Check if numpad commands should be processed based on numlock state
20 Return True if numlock is OFF (commands should work)
21 Return False if numlock is ON (let keys type numbers)
22 """
23 # Return False if numlock is ON
24 return not self.env["input"]["new_num_lock"]
25
26 def shutdown(self):
27 pass
28
29 def clear_marks(self):
30 self.env["commandBuffer"]["Marks"]["1"] = None
31 self.env["commandBuffer"]["Marks"]["2"] = None
32
33 def is_mark_set(self):
34 return self.env["commandBuffer"]["Marks"]["1"] is not None
35
36 def is_single_mark(self):
37 return (
38 self.env["commandBuffer"]["Marks"]["1"] is not None
39 and self.env["commandBuffer"]["Marks"]["2"] is None
40 )
41
42 def is_multible_mark(self):
43 return (
44 self.env["commandBuffer"]["Marks"]["1"] is not None
45 and self.env["commandBuffer"]["Marks"]["2"] is not None
46 )
47
48 def set_mark(self):
49 curr_cursor = None
50 if self.env["screen"]["newCursorReview"]:
51 curr_cursor = self.env["screen"]["newCursorReview"].copy()
52 else:
53 curr_cursor = self.env["screen"]["new_cursor"].copy()
54 if not self.env["commandBuffer"]["Marks"]["1"]:
55 self.env["commandBuffer"]["Marks"]["1"] = curr_cursor.copy()
56 return 1
57 else:
58 self.env["commandBuffer"]["Marks"]["2"] = curr_cursor.copy()
59 return 2
60 return 0
61
62 def get_review_or_text_cursor(self):
63 if self.env["screen"]["newCursorReview"]:
64 return self.env["screen"]["newCursorReview"].copy()
65 else:
66 return self.env["screen"]["new_cursor"].copy()
67
68 def clear_review_cursor(self):
69 if not self.is_review_mode():
70 return
71 self.env["screen"]["oldCursorReview"] = None
72 self.env["screen"]["newCursorReview"] = None
73
74 def is_cursor_horizontal_move(self):
75 return (
76 self.env["screen"]["new_cursor"]["x"]
77 != self.env["screen"]["old_cursor"]["x"]
78 )
79
80 def is_cursor_vertical_move(self):
81 return (
82 self.env["screen"]["new_cursor"]["y"]
83 != self.env["screen"]["old_cursor"]["y"]
84 )
85
86 def is_review_mode(self):
87 return self.env["screen"]["newCursorReview"] is not None
88
89 def enter_review_mode_curr_text_cursor(self, overwrite=False):
90 if self.is_review_mode() and not overwrite:
91 return
92 self.env["screen"]["oldCursorReview"] = self.env["screen"][
93 "newCursorReview"
94 ]
95 if not self.env["screen"]["newCursorReview"]:
96 self.env["screen"]["newCursorReview"] = self.env["screen"][
97 "new_cursor"
98 ].copy()
99 if (
100 self.env["runtime"]["SettingsManager"].get_setting_as_bool(
101 "focus", "highlight"
102 )
103 and self.env["runtime"][
104 "AttributeManager"
105 ].is_attribute_cursor_active()
106 ):
107 self.env["screen"]["newCursorReview"] = (
108 self.env["runtime"]["AttributeManager"]
109 .get_curr_attribute_cursor()
110 .copy()
111 )
112
113 def set_review_cursor_position(self, x, y):
114 if not self.is_review_mode():
115 self.enter_review_mode_curr_text_cursor()
116 self.env["screen"]["oldCursorReview"] = self.env["screen"][
117 "newCursorReview"
118 ]
119 self.env["screen"]["newCursorReview"]["x"] = x
120 self.env["screen"]["newCursorReview"]["y"] = y
121
122 def is_application_window_set(self):
123 try:
124 curr_app = self.env["runtime"][
125 "ApplicationManager"
126 ].get_current_application()
127 if (
128 curr_app in self.env["commandBuffer"]["windowArea"]
129 and "1" in self.env["commandBuffer"]["windowArea"][curr_app]
130 and self.env["commandBuffer"]["windowArea"][curr_app]["1"]
131 is not None
132 ):
133 return True
134 except Exception as e:
135 self.env["runtime"]["DebugManager"].write_debug_out(
136 "CursorManager is_application_window_set: Error checking window area: "
137 + str(e),
138 debug.DebugLevel.ERROR,
139 )
140 return False
141
142 def set_window_for_application(self, start=None, end=None):
143 x1 = 0
144 x2 = 0
145 y1 = 0
146 y2 = 0
147 if start is None:
148 if not self.env["commandBuffer"]["Marks"]["1"]:
149 return False
150 else:
151 x1 = self.env["commandBuffer"]["Marks"]["1"]["x"]
152 y1 = self.env["commandBuffer"]["Marks"]["1"]["y"]
153 else:
154 x1 = start["x"]
155 y1 = start["y"]
156 if end is None:
157 if not self.env["commandBuffer"]["Marks"]["2"]:
158 return False
159 else:
160 x2 = self.env["commandBuffer"]["Marks"]["2"]["x"]
161 y2 = self.env["commandBuffer"]["Marks"]["2"]["y"]
162 else:
163 x2 = end["x"]
164 y2 = end["y"]
165
166 curr_app = self.env["runtime"][
167 "ApplicationManager"
168 ].get_current_application()
169 self.env["commandBuffer"]["windowArea"][curr_app] = {}
170
171 if x1 * y1 <= x2 * y2:
172 self.env["commandBuffer"]["windowArea"][curr_app]["1"] = {
173 "x": x1,
174 "y": y1,
175 }
176 self.env["commandBuffer"]["windowArea"][curr_app]["2"] = {
177 "x": x2,
178 "y": y2,
179 }
180 else:
181 self.env["commandBuffer"]["windowArea"][curr_app]["1"] = {
182 "x": x2,
183 "y": y2,
184 }
185 self.env["commandBuffer"]["windowArea"][curr_app]["2"] = {
186 "x": x1,
187 "y": y1,
188 }
189 return True
190
191 def clear_window_for_application(self):
192 curr_app = self.env["runtime"][
193 "ApplicationManager"
194 ].get_current_application()
195 try:
196 del self.env["commandBuffer"]["windowArea"][curr_app]
197 except Exception as e:
198 self.env["runtime"]["DebugManager"].write_debug_out(
199 "CursorManager clear_window_for_application: Error clearing window area: "
200 + str(e),
201 debug.DebugLevel.ERROR,
202 )
203 return False
204 return True