1
1
import re
2
2
import unicodedata
3
3
from collections import OrderedDict , defaultdict
4
- from datetime import date , datetime
4
+ from datetime import date , datetime , timedelta
5
5
from sys import platform
6
- from time import ctime , time
6
+ from time import ctime , sleep , time
7
7
from typing import Any , Callable , Dict , List , Optional , Tuple , Union
8
8
from urllib .parse import urljoin , urlparse
9
9
22
22
STREAM_TOPIC_SEPARATOR , TIME_MENTION_MARKER ,
23
23
)
24
24
from zulipterminal .helper import (
25
- Message , format_string , match_emoji , match_group , match_stream ,
25
+ Message , asynch , format_string , match_emoji , match_group , match_stream ,
26
26
match_topics , match_user ,
27
27
)
28
28
from zulipterminal .ui_tools .buttons import EditModeButton
@@ -57,6 +57,7 @@ def private_box_view(self, button: Any=None, email: str='',
57
57
self .recipient_user_ids = recipient_user_ids
58
58
if email == '' and button is not None :
59
59
email = button .email
60
+ self .send_next_typing_update = datetime .now ()
60
61
self .to_write_box = ReadlineEdit ("To: " , edit_text = email )
61
62
self .msg_write_box = ReadlineEdit (multiline = True )
62
63
self .msg_write_box .enable_autocomplete (
@@ -75,6 +76,29 @@ def private_box_view(self, button: Any=None, email: str='',
75
76
]
76
77
self .focus_position = 1
77
78
79
+ TYPING_STARTED_WAIT_PERIOD = 10
80
+ TYPING_STOPPED_WAIT_PERIOD = 5
81
+
82
+ start_period_delta = timedelta (seconds = TYPING_STARTED_WAIT_PERIOD )
83
+ stop_period_delta = timedelta (seconds = TYPING_STOPPED_WAIT_PERIOD )
84
+
85
+ def on_type_send_status (edit : str , new_edit_text : str ) -> None :
86
+ if not new_edit_text == "" :
87
+ current_time = datetime .now ()
88
+ self .LAST_TYPE_UPDATE = datetime .now ()
89
+ if current_time > self .send_next_typing_update :
90
+ self .model .send_typing_event (self .recipient_user_ids , True )
91
+ self .send_next_typing_update += start_period_delta
92
+
93
+ @asynch
94
+ def on_idle_send_status (* args : str ) -> None :
95
+ sleep (TYPING_STOPPED_WAIT_PERIOD )
96
+ if datetime .now () > self .LAST_TYPE_UPDATE + stop_period_delta :
97
+ self .model .send_typing_event (self .recipient_user_ids , False )
98
+
99
+ urwid .connect_signal (self .msg_write_box , 'change' , on_type_send_status )
100
+ urwid .connect_signal (self .msg_write_box , 'change' , on_idle_send_status )
101
+
78
102
def stream_box_view (self , stream_id : int , caption : str = '' , title : str = '' ,
79
103
) -> None :
80
104
self .set_editor_mode ()
@@ -343,6 +367,8 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
343
367
content = self .msg_write_box .edit_text
344
368
)
345
369
else :
370
+ self .model .send_typing_event (self .recipient_user_ids , False )
371
+ self .send_next_typing_update = datetime .now ()
346
372
if self .msg_edit_id :
347
373
success = self .model .update_private_message (
348
374
content = self .msg_write_box .edit_text ,
@@ -355,12 +381,15 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
355
381
)
356
382
if success :
357
383
self .msg_write_box .edit_text = ''
384
+ self .model .send_typing_event (self .recipient_user_ids , False )
385
+ self .send_next_typing_update = datetime .now ()
358
386
if self .msg_edit_id :
359
387
self .msg_edit_id = None
360
388
self .keypress (size , 'esc' )
361
389
elif is_command_key ('GO_BACK' , key ):
362
390
self .msg_edit_id = None
363
391
self .msg_body_edit_enabled = True
392
+ self .model .send_typing_event (self .recipient_user_ids , False )
364
393
self .view .controller .exit_editor_mode ()
365
394
self .main_view (False )
366
395
self .view .middle_column .set_focus ('body' )
0 commit comments