@@ -72,6 +72,8 @@ impl ChatHook {
72
72
/// access to its wrapper [ChatHook], you can modify the task before it is executed.
73
73
///
74
74
/// See [Chat::tasks] and [Chat::hook] for more information.
75
+ // TODO: Using indexes for many uperation like `UpdateMessage` is not ideal. In the future
76
+ // messages may need to have a unique identifier.
75
77
#[ derive( Debug ) ]
76
78
pub enum ChatTask {
77
79
/// When received back, it will send the whole chat context to the bot.
@@ -97,6 +99,9 @@ pub enum ChatTask {
97
99
98
100
/// When received back, it will clear the prompt input.
99
101
ClearPrompt ,
102
+
103
+ /// When received back, the chat will scroll to the bottom.
104
+ ScrollToBottom ,
100
105
}
101
106
102
107
impl From < ChatTask > for Vec < ChatTask > {
@@ -319,7 +324,7 @@ impl Chat {
319
324
is_writing : true ,
320
325
} ) ;
321
326
322
- messages . scroll_to_bottom ( cx) ;
327
+ self . dispatch ( cx, ChatTask :: ScrollToBottom . into ( ) ) ;
323
328
324
329
messages
325
330
. messages
@@ -342,15 +347,22 @@ impl Chat {
342
347
343
348
ui. defer_with_redraw ( move |me, cx, _scope| {
344
349
me. messages_ref ( ) . write_with ( |messages| {
345
- messages
350
+ let mut body = messages
346
351
. messages
347
352
. last_mut ( )
348
353
. expect ( "no message where to put delta" )
349
354
. body
350
- . push_str ( & delta) ;
355
+ . clone ( ) ;
356
+
357
+ body. push_str ( & delta) ;
358
+
359
+ me. dispatch (
360
+ cx,
361
+ ChatTask :: UpdateMessage ( messages. messages . len ( ) - 1 , body) . into ( ) ,
362
+ ) ;
351
363
352
364
if messages. is_at_bottom ( ) {
353
- messages . scroll_to_bottom ( cx) ;
365
+ me . dispatch ( cx, ChatTask :: ScrollToBottom . into ( ) ) ;
354
366
}
355
367
} ) ;
356
368
} ) ;
@@ -400,6 +412,15 @@ impl Chat {
400
412
cx. action ( action) ;
401
413
}
402
414
415
+ /// Performs a set of tasks in the [Chat] widget immediately.
416
+ ///
417
+ /// This is not hookable, no actions are emitted from this.
418
+ pub fn perform ( & mut self , cx : & mut Cx , tasks : & [ ChatTask ] ) {
419
+ for task in tasks {
420
+ self . handle_primitive_task ( cx, & task) ;
421
+ }
422
+ }
423
+
403
424
/// Get a reader to each [ChatTask] available in the event.
404
425
///
405
426
/// This yields individual tasks and not the whole set of tasks in the underlying hook.
@@ -420,9 +441,7 @@ impl Chat {
420
441
self . hook ( event) . write_with ( |hook| {
421
442
hook. executed = true ;
422
443
if let Some ( tasks) = hook. tasks . as_mut ( ) {
423
- for task in tasks {
424
- self . handle_primitive_task ( cx, task) ;
425
- }
444
+ self . perform ( cx, tasks) ;
426
445
}
427
446
} ) ;
428
447
}
@@ -478,6 +497,9 @@ impl Chat {
478
497
ChatTask :: ClearPrompt => {
479
498
self . prompt_input_ref ( ) . write ( ) . reset ( cx) ; // `reset` comes from command text input.
480
499
}
500
+ ChatTask :: ScrollToBottom => {
501
+ self . messages_ref ( ) . write ( ) . scroll_to_bottom ( cx) ;
502
+ }
481
503
}
482
504
}
483
505
}
0 commit comments