11use std:: ffi:: OsStr ;
22use std:: path:: Path ;
33
4- use crate :: filereader :: FileReader ;
4+ use crate :: file_reader :: FileReader ;
55use crate :: style;
66use crate :: vector_comparer:: { IVectorComparer , VectorComparer } ;
77use crate :: vector_exporter:: { ExportType , IVectorExporter , VectorExporter } ;
@@ -41,6 +41,30 @@ pub struct ApplicationContext {
4141 pub has_compared : bool ,
4242}
4343
44+ impl ApplicationContext {
45+ /// Display a native alert
46+ ///
47+ /// # Example
48+ ///
49+ /// ```rust
50+ /// display_alert("hello", "world", MessageType::Info)
51+ /// ```
52+ ///
53+ /// # Arguments
54+ ///
55+ /// * `title` - The alert title
56+ /// * `content` - the content of the alert
57+ /// * `message_type` - The `MessageType` for the alert
58+ fn display_alert ( & self , title : & str , content : & str , message_type : MessageType ) {
59+ MessageDialog :: new ( )
60+ . set_type ( message_type)
61+ . set_title ( title)
62+ . set_text ( content)
63+ . show_alert ( )
64+ . unwrap ( ) ;
65+ }
66+ }
67+
4468impl Sandbox for ApplicationContext {
4569 type Message = Message ;
4670
@@ -86,12 +110,12 @@ impl Sandbox for ApplicationContext {
86110 }
87111 Message :: ComparePressed => {
88112 if self . first_file . is_empty ( ) || self . second_file . is_empty ( ) {
89- MessageDialog :: new ( )
90- . set_type ( MessageType :: Warning )
91- . set_title ( "text-diff" )
92- . set_text ( "Please select two files first!" )
93- . show_alert ( )
94- . unwrap ( ) ;
113+ ApplicationContext :: display_alert (
114+ & self ,
115+ "text-diff" ,
116+ "Please select two files first!" ,
117+ MessageType :: Warning ,
118+ ) ;
95119 return ;
96120 }
97121
@@ -103,31 +127,25 @@ impl Sandbox for ApplicationContext {
103127 let lines_first_file = match lines_first_file {
104128 Ok ( d) => d,
105129 Err ( e) => {
106- MessageDialog :: new ( )
107- . set_type ( MessageType :: Error )
108- . set_title ( "text-diff" )
109- . set_text ( & format ! (
110- "Error while reading file {}!\n {}" ,
111- & self . first_file, e
112- ) )
113- . show_alert ( )
114- . unwrap ( ) ;
130+ ApplicationContext :: display_alert (
131+ & self ,
132+ "text-diff" ,
133+ & format ! ( "Error while reading file {}!\n {}" , & self . first_file, e) ,
134+ MessageType :: Error ,
135+ ) ;
115136 return ;
116137 }
117138 } ;
118139
119140 let lines_second_file = match lines_second_file {
120141 Ok ( d) => d,
121142 Err ( e) => {
122- MessageDialog :: new ( )
123- . set_type ( MessageType :: Error )
124- . set_title ( "text-diff" )
125- . set_text ( & format ! (
126- "Error while reading file {}!\n {}" ,
127- & self . second_file, e
128- ) )
129- . show_alert ( )
130- . unwrap ( ) ;
143+ ApplicationContext :: display_alert (
144+ & self ,
145+ "text-diff" ,
146+ & format ! ( "Error while reading file {}!\n {}" , & self . second_file, e) ,
147+ MessageType :: Error ,
148+ ) ;
131149 return ;
132150 }
133151 } ;
@@ -179,23 +197,20 @@ impl Sandbox for ApplicationContext {
179197 Ok ( _) => return ,
180198 Err ( e) => match e {
181199 crate :: vector_exporter:: ExportError :: IoError ( e) => {
182- MessageDialog :: new ( )
183- . set_type ( MessageType :: Error )
184- . set_title ( "text-diff" )
185- . set_text ( & format ! ( "Error while writing to file {}!\n {}" , & path, e) )
186- . show_alert ( )
187- . unwrap ( ) ;
200+ ApplicationContext :: display_alert (
201+ & self ,
202+ "text-diff" ,
203+ & format ! ( "Error while writing to file {}!\n {}" , & path, e) ,
204+ MessageType :: Error ,
205+ ) ;
188206 }
189207 crate :: vector_exporter:: ExportError :: JsonError ( e) => {
190- MessageDialog :: new ( )
191- . set_type ( MessageType :: Error )
192- . set_title ( "text-diff" )
193- . set_text ( & format ! (
194- "Error while creating JSON for file {}!\n {}" ,
195- & path, e
196- ) )
197- . show_alert ( )
198- . unwrap ( ) ;
208+ ApplicationContext :: display_alert (
209+ & self ,
210+ "text-diff" ,
211+ & format ! ( "Error while creating JSON for file {}!\n {}" , & path, e) ,
212+ MessageType :: Error ,
213+ ) ;
199214 }
200215 } ,
201216 } ;
@@ -206,15 +221,11 @@ impl Sandbox for ApplicationContext {
206221 fn view ( & mut self ) -> Element < ' _ , Self :: Message > {
207222 let title = Text :: new ( "text-diff" )
208223 . width ( Length :: Fill )
209- . size ( 85 )
210- . color ( [ 0.5 , 0.5 , 0.5 ] )
224+ . size ( 80 )
211225 . horizontal_alignment ( alignment:: Horizontal :: Center ) ;
212226
213227 let choose_theme = style:: Theme :: ALL . iter ( ) . fold (
214- Row :: new ( )
215- . width ( Length :: Fill )
216- . align_items ( Alignment :: Center )
217- . spacing ( 10 ) ,
228+ Row :: new ( ) . width ( Length :: Fill ) . spacing ( 10 ) ,
218229 |row, theme| {
219230 row. push (
220231 Radio :: new (
@@ -330,15 +341,16 @@ impl Sandbox for ApplicationContext {
330341 diff_text = Text :: new ( "No differences detected!" )
331342 }
332343
333- let diff_column = self . differences . iter ( ) . fold (
334- Column :: new ( ) . spacing ( 10 ) ,
335- |column, theme| column. push ( Text :: new ( format ! ( "- {}" , theme) ) ) ,
336- ) ;
344+ let diff_column = self
345+ . differences
346+ . iter ( )
347+ . fold ( Column :: new ( ) . spacing ( 10 ) , |column, theme| {
348+ column. push ( Text :: new ( format ! ( "- {}" , theme) ) )
349+ } ) ;
337350
338351 let scroll_container = Column :: new ( ) . width ( Length :: Fill ) . push ( diff_column) ;
339352 let scroll = Scrollable :: new ( & mut self . scrollable )
340- . push ( Container :: new ( scroll_container)
341- . width ( Length :: Fill ) )
353+ . push ( Container :: new ( scroll_container) . width ( Length :: Fill ) )
342354 . max_height ( 150 )
343355 . style ( self . theme ) ;
344356
@@ -357,19 +369,19 @@ impl Sandbox for ApplicationContext {
357369 . on_press ( Message :: ExportPressed )
358370 . style ( self . theme ) ;
359371
360- content = content. push (
361- Column :: new ( )
362- . width ( Length :: Fill )
363- . align_items ( Alignment :: End )
364- . spacing ( 20 )
365- . push ( btn_export) ,
366- ) ;
372+ content = content
373+ . push (
374+ Column :: new ( )
375+ . width ( Length :: Fill )
376+ . align_items ( Alignment :: End )
377+ . spacing ( 20 )
378+ . push ( btn_export) ,
379+ )
380+ . push ( Rule :: horizontal ( 20 ) . style ( self . theme ) ) ;
367381 }
368382 }
369383
370- content = content
371- . push ( Rule :: horizontal ( 20 ) . style ( self . theme ) )
372- . push ( choose_theme) ;
384+ content = content. push ( choose_theme) ;
373385
374386 Container :: new ( content)
375387 . width ( Length :: Fill )
0 commit comments