| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Fenrir TTY screen reader |
| 5 |
# By Chrys, Storm Dragon, and contributors. |
| 6 |
|
| 7 |
import time |
| 8 |
|
| 9 |
from fenrirscreenreader.core.i18n import _ |
| 10 |
|
| 11 |
|
| 12 |
class command: |
| 13 |
def __init__(self): |
| 14 |
pass |
| 15 |
|
| 16 |
def initialize(self, environment): |
| 17 |
self.env = environment |
| 18 |
|
| 19 |
def shutdown(self): |
| 20 |
pass |
| 21 |
|
| 22 |
def get_description(self): |
| 23 |
return _("Announces large text insertions at the cursor") |
| 24 |
|
| 25 |
def run(self): |
| 26 |
if self.env["runtime"]["ScreenManager"].is_screen_change(): |
| 27 |
return |
| 28 |
if not self.env["runtime"]["ScreenManager"].is_delta(): |
| 29 |
return |
| 30 |
if not self.env["screen"].get("new_delta_is_typing", False): |
| 31 |
return |
| 32 |
|
| 33 |
x_move = ( |
| 34 |
self.env["screen"]["new_cursor"]["x"] |
| 35 |
- self.env["screen"]["old_cursor"]["x"] |
| 36 |
) |
| 37 |
if x_move < 5: |
| 38 |
return |
| 39 |
if self._is_recent_tab_input(): |
| 40 |
return |
| 41 |
|
| 42 |
delta_text = self.env["screen"]["new_delta"] |
| 43 |
if not self._matches_cursor_insert(x_move, delta_text): |
| 44 |
return |
| 45 |
|
| 46 |
curr_delta = delta_text |
| 47 |
if ( |
| 48 |
len(curr_delta.strip()) != len(curr_delta) |
| 49 |
and curr_delta.strip() != "" |
| 50 |
): |
| 51 |
curr_delta = curr_delta.strip() |
| 52 |
if not curr_delta: |
| 53 |
return |
| 54 |
|
| 55 |
do_interrupt = True |
| 56 |
if self.env["runtime"]["SettingsManager"].get_setting_as_bool( |
| 57 |
"speech", "auto_read_incoming" |
| 58 |
): |
| 59 |
do_interrupt = False |
| 60 |
|
| 61 |
self.env["runtime"]["OutputManager"].present_text( |
| 62 |
curr_delta, |
| 63 |
interrupt=do_interrupt, |
| 64 |
announce_capital=True, |
| 65 |
flush=False, |
| 66 |
) |
| 67 |
|
| 68 |
def _is_recent_tab_input(self): |
| 69 |
input_manager = self.env["runtime"].get("InputManager") |
| 70 |
if not input_manager: |
| 71 |
return False |
| 72 |
if input_manager.get_last_deepest_input() in [["KEY_TAB"]]: |
| 73 |
return True |
| 74 |
|
| 75 |
last_event = input_manager.get_last_event() |
| 76 |
if not last_event or last_event.get("event_name") != "KEY_TAB": |
| 77 |
return False |
| 78 |
try: |
| 79 |
return time.time() - input_manager.get_last_input_time() <= 0.5 |
| 80 |
except Exception: |
| 81 |
return False |
| 82 |
|
| 83 |
def _matches_cursor_insert(self, x_move, delta_text): |
| 84 |
if not delta_text or "\n" in delta_text: |
| 85 |
return False |
| 86 |
|
| 87 |
delta_len = len(delta_text) |
| 88 |
if x_move == delta_len: |
| 89 |
return True |
| 90 |
if abs(x_move - delta_len) <= 2: |
| 91 |
return True |
| 92 |
return delta_len > 10 and abs(x_move - delta_len) <= (delta_len * 0.2) |
| 93 |
|
| 94 |
def set_callback(self, callback): |
| 95 |
pass |