From c976d17af713eddc23bc77a08fbeddd640139315 Mon Sep 17 00:00:00 2001 From: Ethan Date: Tue, 24 Dec 2024 23:15:30 -0500 Subject: [PATCH] Add custom widget --- rust-learning/src/gui.rs | 55 +++++++++++++++++++++++++ rust-learning/src/gui/model_response.rs | 40 ++++++++++++++++++ rust-learning/src/gui/state.rs | 11 +++++ rust-learning/src/main.rs | 53 +----------------------- 4 files changed, 108 insertions(+), 51 deletions(-) create mode 100644 rust-learning/src/gui.rs create mode 100644 rust-learning/src/gui/model_response.rs create mode 100644 rust-learning/src/gui/state.rs diff --git a/rust-learning/src/gui.rs b/rust-learning/src/gui.rs new file mode 100644 index 0000000..eb650d1 --- /dev/null +++ b/rust-learning/src/gui.rs @@ -0,0 +1,55 @@ +use eframe::egui; +use model_response::ModelResponse; +use state::AppState; + +pub mod model_response; +pub mod state; + +pub fn main() -> eframe::Result { + let options = eframe::NativeOptions { + viewport: eframe::egui::ViewportBuilder::default().with_inner_size([900.0, 600.0]), + ..Default::default() + }; + + eframe::run_native( + "Groq Model Comparison", + options, + Box::new(|cc| Ok(Box::new(AppState::new(cc)))), + ) +} + +impl eframe::App for AppState { + fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + egui::CentralPanel::default().show(ctx, |ui| { + ui.heading("Groq Model Comparison"); + ui.label("Compare Groq models with ease!"); + + ui.horizontal(|ui| { + let counter_color = match self.counter { + 0 => egui::Color32::WHITE, + _ if self.counter > 0 => egui::Color32::GREEN, + _ => egui::Color32::RED, + }; + + ui.label("Counter:"); + ui.label(egui::RichText::new(format!("{}", self.counter)).color(counter_color)); + }); + + ui.horizontal(|ui| { + if ui.button("Increment").clicked() { + self.counter += 1; + } + + if ui.button("Decrement").clicked() { + self.counter -= 1; + } + }); + + ui.add(ModelResponse::new( + "This is a test message".to_string(), + 200, + 100, + )); + }); + } +} diff --git a/rust-learning/src/gui/model_response.rs b/rust-learning/src/gui/model_response.rs new file mode 100644 index 0000000..c50a44c --- /dev/null +++ b/rust-learning/src/gui/model_response.rs @@ -0,0 +1,40 @@ +use eframe::egui::{self, Response, Ui, Widget}; + +pub struct ModelResponse { + pub message: String, + pub status: i32, + pub time: i32, +} + +impl ModelResponse { + pub fn new(message: String, status: i32, time: i32) -> Self { + Self { + message, + status, + time, + } + } +} + +impl Widget for ModelResponse { + fn ui(self, ui: &mut Ui) -> Response { + egui::Frame::none() + .inner_margin(8.0) + .rounding(4.0) + .fill(egui::Color32::DARK_GRAY) + .show(ui, |ui| { + ui.horizontal(|ui| { + ui.label("Status:"); + ui.label(self.status.to_string()); + }); + + ui.horizontal(|ui| { + ui.label("Time:"); + ui.label(self.time.to_string()); + }); + + ui.label(self.message) + }) + .response + } +} diff --git a/rust-learning/src/gui/state.rs b/rust-learning/src/gui/state.rs new file mode 100644 index 0000000..eac1df3 --- /dev/null +++ b/rust-learning/src/gui/state.rs @@ -0,0 +1,11 @@ +use eframe::CreationContext; + +pub struct AppState { + pub counter: i32, +} + +impl AppState { + pub fn new(_cc: &CreationContext<'_>) -> Self { + Self { counter: 0 } + } +} diff --git a/rust-learning/src/main.rs b/rust-learning/src/main.rs index cc8931e..8a5ebda 100644 --- a/rust-learning/src/main.rs +++ b/rust-learning/src/main.rs @@ -1,54 +1,5 @@ -use eframe::egui; +pub mod gui; fn main() -> eframe::Result { - let options = eframe::NativeOptions { - viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]), - ..Default::default() - }; - - eframe::run_native( - "rust-learning", - options, - Box::new(|cc| Ok(Box::new(MyApp::new(cc)))), - ) -} - -struct MyApp { - counter: i32, -} - -impl MyApp { - fn new(_cc: &eframe::CreationContext<'_>) -> Self { - Self { counter: 0 } - } -} - -impl eframe::App for MyApp { - fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { - egui::CentralPanel::default().show(ctx, |ui| { - ui.heading("Hello eframe!"); - ui.label("This is a simple eframe app."); - - ui.horizontal(|ui| { - let counter_color = match self.counter { - 0 => egui::Color32::WHITE, - _ if self.counter > 0 => egui::Color32::GREEN, - _ => egui::Color32::RED, - }; - - ui.label("Counter:"); - ui.label(egui::RichText::new(format!("{}", self.counter)).color(counter_color)); - }); - - ui.horizontal(|ui| { - if ui.button("Increment").clicked() { - self.counter += 1; - } - - if ui.button("Decrement").clicked() { - self.counter -= 1; - } - }); - }); - } + gui::main() }