11use ratatui:: {
2- layout:: { Alignment , Rect } ,
2+ layout:: { Alignment , Constraint , Direction , Layout , Rect } ,
33 style:: { Color , Modifier , Style } ,
44 text:: { Line , Text } ,
55 widgets:: { Block , BorderType , Borders , Clear , Paragraph , Wrap } ,
@@ -25,38 +25,71 @@ impl Notification {
2525 Self {
2626 message,
2727 level,
28- ttl : 8 ,
28+ ttl : 4 ,
2929 }
3030 }
3131
32- pub fn render ( & mut self , frame : & mut Frame , block : Rect ) {
32+ pub fn render ( & self , index : usize , frame : & mut Frame ) {
3333 let ( color, title) = match self . level {
3434 NotificationLevel :: Info => ( Color :: Green , "Info" ) ,
3535 NotificationLevel :: Warning => ( Color :: Yellow , "Warning" ) ,
3636 NotificationLevel :: Error => ( Color :: Red , "Error" ) ,
3737 } ;
3838
39- let text = Text :: from ( vec ! [
40- Line :: styled(
41- title,
42- Style :: default ( ) . fg( color) . add_modifier( Modifier :: BOLD ) ,
43- )
44- . alignment( Alignment :: Center ) ,
45- Line :: raw( self . message. as_str( ) ) ,
39+ let mut text = Text :: from ( vec ! [
40+ Line :: from( title) . style( Style :: new( ) . fg( color) . add_modifier( Modifier :: BOLD ) )
4641 ] ) ;
4742
48- let para = Paragraph :: new ( text)
43+ text. extend ( Text :: from ( self . message . as_str ( ) ) ) ;
44+
45+ let notification_height = text. height ( ) as u16 + 2 ;
46+ let notification_width = text. width ( ) as u16 + 4 ;
47+
48+ let block = Paragraph :: new ( text)
4949 . wrap ( Wrap { trim : false } )
5050 . alignment ( Alignment :: Center )
5151 . block (
5252 Block :: default ( )
5353 . borders ( Borders :: ALL )
5454 . style ( Style :: default ( ) )
55- . border_type ( BorderType :: Rounded )
55+ . border_type ( BorderType :: Thick )
5656 . border_style ( Style :: default ( ) . fg ( color) ) ,
5757 ) ;
5858
59- frame. render_widget ( Clear , block) ;
60- frame. render_widget ( para, block) ;
59+ let area = notification_rect (
60+ index as u16 ,
61+ notification_height,
62+ notification_width,
63+ frame. size ( ) ,
64+ ) ;
65+
66+ frame. render_widget ( Clear , area) ;
67+ frame. render_widget ( block, area) ;
6168 }
6269}
70+
71+ pub fn notification_rect ( offset : u16 , height : u16 , width : u16 , r : Rect ) -> Rect {
72+ let popup_layout = Layout :: default ( )
73+ . direction ( Direction :: Vertical )
74+ . constraints (
75+ [
76+ Constraint :: Length ( height * offset) ,
77+ Constraint :: Length ( height) ,
78+ Constraint :: Min ( 1 ) ,
79+ ]
80+ . as_ref ( ) ,
81+ )
82+ . split ( r) ;
83+
84+ Layout :: default ( )
85+ . direction ( Direction :: Horizontal )
86+ . constraints (
87+ [
88+ Constraint :: Min ( 1 ) ,
89+ Constraint :: Length ( width) ,
90+ Constraint :: Length ( 2 ) ,
91+ ]
92+ . as_ref ( ) ,
93+ )
94+ . split ( popup_layout[ 1 ] ) [ 1 ]
95+ }
0 commit comments