/ src / gui.rs
gui.rs
 1  mod views;
 2  
 3  pub struct Error(eframe::Error);
 4  
 5  pub fn gui(message: &mut crate::Message, circle_length: &mut usize, inputs: bool) -> anyhow::Result<()> {
 6      let options = eframe::NativeOptions::default();
 7      eframe::run_native(
 8          "Encoded Message", 
 9          options, 
10          Box::new(|_cc| Ok(Box::<Gui>::new(
11                      Gui::new(message, circle_length, inputs)
12          )))
13      ).map_err(crate::Error::from)?;
14      Ok(())
15  }
16  
17  
18  struct Gui<'a> {
19      display: views::Display<'a>,
20  }
21  impl<'a> Gui<'a> {
22      fn new(message: &'a mut crate::Message, circle_length: &'a mut usize, inputs: bool) -> Self { 
23          let circles = crate::encrypt_message(message, circle_length);
24          let line_colors: Vec<egui::Color32> = (0..circles.len())
25              .map(|_|views::random_color()).collect();
26  
27          Self {
28              display: match inputs {
29                      true => views::display_gui(),
30                      false => views::display_plot(circles, circle_length, line_colors),
31                  },
32  
33          }
34      }
35  
36  }
37  
38  impl eframe::App for Gui<'_> {
39      fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
40          egui::CentralPanel::default().show(ctx,|ui| {
41              (self.display)(ui)
42          });
43      }
44  }
45  impl From<eframe::Error> for Error {
46      fn from(err: eframe::Error) -> Self {
47          Error(err)
48      }
49  }
50  
51  impl From<Error> for anyhow::Error {
52      fn from(err: Error) -> Self {
53         anyhow::anyhow!(err.0.to_string()) 
54      }
55  }