12
12
from .store import Store
13
13
14
14
15
+ MARK_READ_ALARM_PERIOD = 3
16
+
17
+
15
18
def get_icon (name ):
16
19
return Store .instance .config ['icons' ][name ]
17
20
@@ -25,9 +28,18 @@ def __init__(self, widget, color):
25
28
tline = '' , trcorner = '' , rline = '' , bline = '' , brcorner = '' )
26
29
super (Box , self ).__init__ (body , urwid .AttrSpec (color , 'h235' ))
27
30
31
+
28
32
class Attachment (Box ):
29
- def __init__ (self , color = None , service_name = None , title = None , title_link = None ,
30
- author_name = None , pretext = None , text = None , fields = None , footer = None ):
33
+ def __init__ (self ,
34
+ color = None ,
35
+ service_name = None ,
36
+ title = None ,
37
+ title_link = None ,
38
+ author_name = None ,
39
+ pretext = None ,
40
+ text = None ,
41
+ fields = None ,
42
+ footer = None ):
31
43
body = []
32
44
if not color :
33
45
color = 'CCCCCC'
@@ -64,7 +76,7 @@ def file(self, image):
64
76
65
77
66
78
class BreadCrumbs (urwid .Text ):
67
- def __init__ (self , elements = [] ):
79
+ def __init__ (self , elements = () ):
68
80
separator = ('separator' , ' {} ' .format (get_icon ('divider' )))
69
81
body = []
70
82
for element in elements :
@@ -206,20 +218,24 @@ def keypress(self, size, key):
206
218
207
219
class ChatBox (urwid .Frame ):
208
220
__metaclass__ = urwid .MetaSignals
209
- signals = ['go_to_sidebar' , 'open_quick_switcher' , 'set_insert_mode' ]
221
+ signals = ['go_to_sidebar' , 'open_quick_switcher' , 'set_insert_mode' , 'mark_read' ]
210
222
211
- def __init__ (self , messages , header , message_box ):
223
+ def __init__ (self , messages , header , message_box , event_loop ):
212
224
self ._header = header
213
225
self .message_box = message_box
214
- self .body = ChatBoxMessages (messages = messages )
226
+ self .body = ChatBoxMessages (messages = messages , event_loop = event_loop )
215
227
self .body .scroll_to_bottom ()
216
228
urwid .connect_signal (self .body , 'set_date' , self ._header .on_set_date )
217
229
urwid .connect_signal (self .body , 'set_insert_mode' , self .set_insert_mode )
230
+ urwid .connect_signal (self .body , 'mark_read' , self .mark_as_read )
218
231
super (ChatBox , self ).__init__ (self .body , header = header , footer = self .message_box )
219
232
220
233
def set_insert_mode (self ):
221
234
urwid .emit_signal (self , 'set_insert_mode' )
222
235
236
+ def mark_as_read (self , data ):
237
+ urwid .emit_signal (self , 'mark_read' , data )
238
+
223
239
def keypress (self , size , key ):
224
240
keymap = Store .instance .config ['keymap' ]
225
241
if key == keymap ['open_quick_switcher' ]:
@@ -241,12 +257,14 @@ def header(self, header):
241
257
242
258
class ChatBoxMessages (urwid .ListBox ):
243
259
__metaclass__ = urwid .MetaSignals
244
- signals = ['set_auto_scroll' , 'set_date' , 'set_insert_mode' ]
260
+ signals = ['set_auto_scroll' , 'set_date' , 'set_insert_mode' , 'mark_read' ]
245
261
246
- def __init__ (self , messages = [] ):
262
+ def __init__ (self , messages = (), event_loop = None ):
247
263
self .body = urwid .SimpleFocusListWalker (messages )
248
264
super (ChatBoxMessages , self ).__init__ (self .body )
249
265
self .auto_scroll = True
266
+ self .last_keypress = (0 , None , 0 )
267
+ self .event_loop = event_loop
250
268
251
269
@property
252
270
def auto_scroll (self ):
@@ -256,23 +274,47 @@ def auto_scroll(self):
256
274
def auto_scroll (self , switch ):
257
275
if type (switch ) != bool :
258
276
return
277
+
259
278
self ._auto_scroll = switch
260
279
urwid .emit_signal (self , 'set_auto_scroll' , switch )
261
280
281
+ def mark_read_emit (self , loop , data ):
282
+ urwid .emit_signal (self , 'mark_read' , data )
283
+
262
284
def keypress (self , size , key ):
263
285
keymap = Store .instance .config ['keymap' ]
264
286
self .handle_floating_date (size )
287
+
288
+ if key in (keymap ['cursor_up' ], keymap ['cursor_down' ], 'up' , 'down' , ):
289
+ now = time .time ()
290
+ max_focus = self .get_focus ()[1 ]
291
+
292
+ if now - self .last_keypress [0 ] < MARK_READ_ALARM_PERIOD and self .last_keypress [1 ] is not None :
293
+ if max_focus < self .last_keypress [2 ]:
294
+ max_focus = self .last_keypress [2 ]
295
+
296
+ self .event_loop .remove_alarm (self .last_keypress [1 ])
297
+
298
+ self .last_keypress = (
299
+ now ,
300
+ self .event_loop .set_alarm_in (MARK_READ_ALARM_PERIOD , self .mark_read_emit , max_focus ),
301
+ max_focus
302
+ )
303
+
265
304
# Go to insert mode
266
305
if key == 'down' and self .get_focus ()[1 ] == len (self .body ) - 1 :
267
306
urwid .emit_signal (self , 'set_insert_mode' )
268
307
return True
308
+
269
309
super (ChatBoxMessages , self ).keypress (size , key )
310
+
270
311
if key in ('page up' , 'page down' ):
271
312
self .auto_scroll = self .get_focus ()[1 ] == len (self .body ) - 1
313
+
272
314
if key == keymap ['cursor_up' ]:
273
315
self .keypress (size , 'up' )
274
316
if key == keymap ['cursor_down' ]:
275
- self .keypress (size ,'down' )
317
+ self .keypress (size , 'down' )
276
318
277
319
def mouse_event (self , size , event , button , col , row , focus ):
278
320
self .handle_floating_date (size )
@@ -290,6 +332,7 @@ def scroll_to_new_messages(self):
290
332
for index , widget in enumerate (self .body ):
291
333
if isinstance (widget , NewMessagesDivider ):
292
334
return self .set_focus (index )
335
+
293
336
return self .scroll_to_bottom ()
294
337
295
338
def scroll_to_bottom (self ):
@@ -440,10 +483,19 @@ def __init__(self, is_edited=False, is_starred=False):
440
483
441
484
class Message (urwid .AttrMap ):
442
485
__metaclass__ = urwid .MetaSignals
443
- signals = ['delete_message' , 'edit_message' , 'go_to_profile' , 'go_to_sidebar' , 'quit_application' , 'set_insert_mode' ]
444
-
445
- def __init__ (self , ts , user , text , indicators , reactions = [], attachments = []):
486
+ signals = [
487
+ 'delete_message' ,
488
+ 'edit_message' ,
489
+ 'go_to_profile' ,
490
+ 'go_to_sidebar' ,
491
+ 'quit_application' ,
492
+ 'set_insert_mode' ,
493
+ 'mark_read' ,
494
+ ]
495
+
496
+ def __init__ (self , ts , channel_id , user , text , indicators , reactions = (), attachments = ()):
446
497
self .ts = ts
498
+ self .channel_id = channel_id
447
499
self .user_id = user .id
448
500
self .markdown_text = text
449
501
self .original_text = text .original_text
@@ -837,7 +889,7 @@ def __init__(self, id, name, color=None, is_app=False):
837
889
self .id = id
838
890
if not color :
839
891
color = '333333'
840
- color = '#{}' .format (shorten_hex (color ))
892
+ color = '#{}' .format (shorten_hex (color ))
841
893
markup = [
842
894
(urwid .AttrSpec (color , 'h235' ), '{} ' .format (name ))
843
895
]
0 commit comments