@@ -426,6 +426,15 @@ enum State {
426
426
} ,
427
427
}
428
428
429
+ enum PromptInputMode {
430
+ Enabled ,
431
+ Disabled ,
432
+ }
433
+ enum PromptInputButton {
434
+ Send ,
435
+ Stop ,
436
+ }
437
+
429
438
#[ derive( Live , LiveHook , Widget ) ]
430
439
pub struct ChatPanel {
431
440
#[ deref]
@@ -607,124 +616,79 @@ impl ChatPanel {
607
616
is_streaming : false ,
608
617
..
609
618
} => {
610
- self . enable_or_disable_prompt_buttons ( cx) ;
611
- self . show_prompt_send_button ( cx) ;
619
+ self . activate_prompt_input ( cx, PromptInputMode :: Enabled , PromptInputButton :: Send ) ;
612
620
}
613
621
State :: ModelSelectedWithChat {
614
622
is_streaming : true , ..
615
623
} => {
616
- let prompt_input = self . text_input ( id ! ( main_prompt_input. prompt) ) ;
617
- prompt_input. apply_over (
618
- cx,
619
- live ! {
620
- draw_text: { prompt_enabled: 0.0 }
621
- } ,
622
- ) ;
623
- self . show_prompt_input_stop_button ( cx) ;
624
+ self . activate_prompt_input ( cx, PromptInputMode :: Disabled , PromptInputButton :: Stop ) ;
624
625
}
625
626
State :: ModelLoading => {
626
- self . show_prompt_send_button ( cx) ;
627
- self . disable_prompt_buttons ( cx) ;
628
-
629
- let prompt_input = self . text_input ( id ! ( main_prompt_input. prompt) ) ;
630
- prompt_input. apply_over (
631
- cx,
632
- live ! {
633
- draw_text: { prompt_enabled: 0.0 }
634
- } ,
635
- ) ;
627
+ self . activate_prompt_input ( cx, PromptInputMode :: Disabled , PromptInputButton :: Send ) ;
636
628
}
637
629
_ => { }
638
630
}
639
631
}
640
632
641
- fn enable_or_disable_prompt_buttons ( & mut self , cx : & mut Cx ) {
633
+ fn activate_prompt_input (
634
+ & mut self ,
635
+ cx : & mut Cx ,
636
+ mode : PromptInputMode ,
637
+ button : PromptInputButton ,
638
+ ) {
642
639
let prompt_input = self . text_input ( id ! ( main_prompt_input. prompt) ) ;
643
- let enable = if !prompt_input. text ( ) . is_empty ( ) {
644
- 1.0
645
- } else {
646
- 0.0
647
- } ;
648
640
649
- prompt_input. apply_over (
650
- cx,
651
- live ! {
652
- draw_text: { prompt_enabled: ( enable) }
653
- } ,
654
- ) ;
655
- }
656
-
657
- fn show_prompt_send_button ( & mut self , cx : & mut Cx ) {
658
- self . button ( id ! ( main_prompt_input. prompt_send_button) )
659
- . set_visible ( true ) ;
660
- self . button ( id ! ( main_prompt_input. prompt_stop_button) )
661
- . set_visible ( false ) ;
641
+ let enabled = match mode {
642
+ PromptInputMode :: Enabled => !prompt_input. text ( ) . is_empty ( ) ,
643
+ PromptInputMode :: Disabled => false ,
644
+ } ;
662
645
663
- let prompt_input = self . text_input ( id ! ( main_prompt_input. prompt) ) ;
664
- if !prompt_input. text ( ) . is_empty ( ) {
665
- self . enable_prompt_buttons ( cx) ;
646
+ let ( button_color, prompt_enabled) = if enabled {
647
+ ( vec3 ( 0.0 , 0.0 , 0.0 ) , 1.0 )
666
648
} else {
667
- self . disable_prompt_buttons ( cx) ;
668
- }
669
- }
670
-
671
- fn show_prompt_input_stop_button ( & mut self , cx : & mut Cx ) {
672
- self . button ( id ! ( main_prompt_input. prompt_send_button) )
673
- . set_visible ( false ) ;
674
- self . button ( id ! ( main_prompt_input. prompt_stop_button) )
675
- . set_visible ( true ) ;
676
-
677
- self . enable_prompt_buttons ( cx) ;
678
- }
679
-
680
- fn enable_prompt_buttons ( & mut self , cx : & mut Cx ) {
681
- let enabled_color = vec3 ( 0.0 , 0.0 , 0.0 ) ;
682
- let send_button = self . button ( id ! ( main_prompt_input. prompt_send_button) ) ;
683
- send_button. set_enabled ( true ) ;
684
- send_button. apply_over (
685
- cx,
686
- live ! {
687
- draw_bg: {
688
- color: ( enabled_color)
689
- }
690
- } ,
691
- ) ;
649
+ // The color code is #D0D5DD
650
+ ( vec3 ( 0.816 , 0.835 , 0.867 ) , 0.0 )
651
+ } ;
692
652
693
- let stop_button = self . button ( id ! ( main_prompt_input. prompt_stop_button) ) ;
694
- stop_button. set_enabled ( true ) ;
695
- stop_button. apply_over (
653
+ prompt_input. apply_over (
696
654
cx,
697
655
live ! {
698
- draw_bg: {
699
- color: ( enabled_color)
700
- }
656
+ draw_text: { prompt_enabled: ( prompt_enabled) }
701
657
} ,
702
658
) ;
703
- }
704
659
705
- fn disable_prompt_buttons ( & mut self , cx : & mut Cx ) {
706
- let disabled_color = vec3 ( 0.816 , 0.835 , 0.867 ) ; // #D0D5DD
707
660
let send_button = self . button ( id ! ( main_prompt_input. prompt_send_button) ) ;
708
- send_button. set_enabled ( false ) ;
709
- send_button. apply_over (
710
- cx,
711
- live ! {
712
- draw_bg: {
713
- color: ( disabled_color)
714
- }
715
- } ,
716
- ) ;
717
-
718
661
let stop_button = self . button ( id ! ( main_prompt_input. prompt_stop_button) ) ;
719
- stop_button. set_enabled ( false ) ;
720
- stop_button. apply_over (
721
- cx,
722
- live ! {
723
- draw_bg: {
724
- color: ( disabled_color)
725
- }
726
- } ,
727
- ) ;
662
+ match button {
663
+ PromptInputButton :: Send => {
664
+ // The send button is enabled or not based on the prompt input
665
+ send_button. set_visible ( true ) ;
666
+ send_button. set_enabled ( enabled) ;
667
+ send_button. apply_over (
668
+ cx,
669
+ live ! {
670
+ draw_bg: {
671
+ color: ( button_color)
672
+ }
673
+ } ,
674
+ ) ;
675
+ stop_button. set_visible ( false ) ;
676
+ }
677
+ PromptInputButton :: Stop => {
678
+ // The stop button is always enabled, when visible
679
+ stop_button. set_visible ( true ) ;
680
+ stop_button. set_enabled ( true ) ;
681
+ stop_button. apply_over (
682
+ cx,
683
+ live ! {
684
+ draw_bg: {
685
+ color: #x000
686
+ }
687
+ } ,
688
+ ) ;
689
+ send_button. set_visible ( false ) ;
690
+ }
691
+ }
728
692
}
729
693
730
694
fn scroll_messages_to_bottom ( & mut self , cx : & mut Cx ) {
@@ -747,7 +711,7 @@ impl ChatPanel {
747
711
fn handle_prompt_input_actions ( & mut self , cx : & mut Cx , actions : & Actions , scope : & mut Scope ) {
748
712
let prompt_input = self . text_input ( id ! ( main_prompt_input. prompt) ) ;
749
713
if let Some ( _text) = prompt_input. changed ( actions) {
750
- self . update_prompt_input ( cx) ;
714
+ self . redraw ( cx) ;
751
715
}
752
716
753
717
if self
@@ -773,10 +737,10 @@ impl ChatPanel {
773
737
let prompt_input = self . text_input ( id ! ( main_prompt_input. prompt) ) ;
774
738
prompt_input. set_text_and_redraw ( cx, "" ) ;
775
739
prompt_input. set_cursor ( 0 , 0 ) ;
776
- self . update_prompt_input ( cx) ;
777
740
778
741
// Scroll to the bottom when the message is sent
779
742
self . scroll_messages_to_bottom ( cx) ;
743
+ self . redraw ( cx) ;
780
744
}
781
745
782
746
fn update_view ( & mut self , cx : & mut Cx2d , scope : & mut Scope ) {
0 commit comments