Skip to content

Commit 7056fcf

Browse files
committed
boxes: Configure private_box_view to send typing event.
The user's typing status is captured using urwid's connect_signal(), observing for changes in the msg_write_box. This commit adds signals and functions that the signals call, to send start and stop events in accordance with the wait periods after the user starts and stops typing. Fixes #593.
1 parent 0fbb9e2 commit 7056fcf

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

zulipterminal/ui_tools/boxes.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import re
22
import unicodedata
33
from collections import OrderedDict, defaultdict
4-
from datetime import date, datetime
4+
from datetime import date, datetime, timedelta
55
from sys import platform
6-
from time import ctime, time
6+
from time import ctime, sleep, time
77
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
88
from urllib.parse import urljoin, urlparse
99

@@ -22,7 +22,7 @@
2222
STREAM_TOPIC_SEPARATOR, TIME_MENTION_MARKER,
2323
)
2424
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,
2626
match_topics, match_user,
2727
)
2828
from zulipterminal.ui_tools.buttons import EditModeButton
@@ -57,6 +57,7 @@ def private_box_view(self, button: Any=None, email: str='',
5757
self.recipient_user_ids = recipient_user_ids
5858
if email == '' and button is not None:
5959
email = button.email
60+
self.send_next_typing_update = datetime.now()
6061
self.to_write_box = ReadlineEdit("To: ", edit_text=email)
6162
self.msg_write_box = ReadlineEdit(multiline=True)
6263
self.msg_write_box.enable_autocomplete(
@@ -75,6 +76,29 @@ def private_box_view(self, button: Any=None, email: str='',
7576
]
7677
self.focus_position = 1
7778

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+
78102
def stream_box_view(self, stream_id: int, caption: str='', title: str='',
79103
) -> None:
80104
self.set_editor_mode()
@@ -343,6 +367,8 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
343367
content=self.msg_write_box.edit_text
344368
)
345369
else:
370+
self.model.send_typing_event(self.recipient_user_ids, False)
371+
self.send_next_typing_update = datetime.now()
346372
if self.msg_edit_id:
347373
success = self.model.update_private_message(
348374
content=self.msg_write_box.edit_text,
@@ -355,12 +381,15 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
355381
)
356382
if success:
357383
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()
358386
if self.msg_edit_id:
359387
self.msg_edit_id = None
360388
self.keypress(size, 'esc')
361389
elif is_command_key('GO_BACK', key):
362390
self.msg_edit_id = None
363391
self.msg_body_edit_enabled = True
392+
self.model.send_typing_event(self.recipient_user_ids, False)
364393
self.view.controller.exit_editor_mode()
365394
self.main_view(False)
366395
self.view.middle_column.set_focus('body')

0 commit comments

Comments
 (0)