From eb2a0d12c4302c26e211c17e2236cd7c15fc34b3 Mon Sep 17 00:00:00 2001 From: Ethan Date: Thu, 26 Dec 2024 14:33:07 -0500 Subject: [PATCH] Add prompt input --- rust-learning/src/gui.rs | 35 ++++++------------------ rust-learning/src/gui/model_response.rs | 24 ++++++++++------ rust-learning/src/gui/model_selection.rs | 25 ++++++++++++----- rust-learning/src/gui/prompt_input.rs | 29 ++++++++++++++++++++ rust-learning/src/gui/state.rs | 10 ++++--- 5 files changed, 76 insertions(+), 47 deletions(-) create mode 100644 rust-learning/src/gui/prompt_input.rs diff --git a/rust-learning/src/gui.rs b/rust-learning/src/gui.rs index 512603d..fd44618 100644 --- a/rust-learning/src/gui.rs +++ b/rust-learning/src/gui.rs @@ -1,9 +1,9 @@ use eframe::egui; -use model_response::ModelResponse; use state::AppState; pub mod model_response; pub mod model_selection; +pub mod prompt_input; pub mod state; pub fn main() -> eframe::Result { @@ -25,37 +25,18 @@ impl eframe::App for AppState { 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(model_selection::ModelSelection::new( self, vec!["Model 1".to_string(), "Model 2".to_string()], )); - ui.add(ModelResponse::new( - "This is a test message".to_string(), - 200, - 100, - )); + ui.add(prompt_input::PromptInput::new(self)); + + ui.horizontal(|ui| { + for model in &self.selected_models { + ui.vertical(|ui| ui.add(model)); + } + }); }); } } diff --git a/rust-learning/src/gui/model_response.rs b/rust-learning/src/gui/model_response.rs index fdb0cb9..19227ca 100644 --- a/rust-learning/src/gui/model_response.rs +++ b/rust-learning/src/gui/model_response.rs @@ -1,14 +1,16 @@ use eframe::egui::{self, Response, Ui, Widget}; pub struct ModelResponse { + pub name: String, pub message: String, pub status: i32, pub time: i32, } impl ModelResponse { - pub fn new(message: String, status: i32, time: i32) -> Self { + pub fn new(name: String, message: String, status: i32, time: i32) -> Self { Self { + name, message, status, time, @@ -16,7 +18,7 @@ impl ModelResponse { } } -impl Widget for ModelResponse { +impl Widget for &ModelResponse { fn ui(self, ui: &mut Ui) -> Response { egui::Frame::none() .inner_margin(8.0) @@ -24,13 +26,17 @@ impl Widget for ModelResponse { .fill(egui::Color32::DARK_GRAY) .show(ui, |ui| { ui.horizontal(|ui| { - ui.label("Status:"); - ui.label(self.status.to_string()); - }); + ui.label(&self.name); - ui.horizontal(|ui| { - ui.label("Time:"); - ui.label(self.time.to_string()); + ui.horizontal(|ui| { + ui.label("Status:"); + ui.label(self.status.to_string()); + }); + + ui.horizontal(|ui| { + ui.label("Time:"); + ui.label(self.time.to_string()); + }); }); egui::Frame::none() @@ -38,7 +44,7 @@ impl Widget for ModelResponse { .rounding(4.0) .fill(egui::Color32::WHITE) .show(ui, |ui| { - ui.label(egui::RichText::new(self.message).color(egui::Color32::BLACK)); + ui.label(egui::RichText::new(&self.message).color(egui::Color32::BLACK)); }); }) .response diff --git a/rust-learning/src/gui/model_selection.rs b/rust-learning/src/gui/model_selection.rs index 271855c..1a455c0 100644 --- a/rust-learning/src/gui/model_selection.rs +++ b/rust-learning/src/gui/model_selection.rs @@ -1,6 +1,6 @@ use eframe::egui::{self, Response, Ui, Widget}; -use super::state::AppState; +use super::{model_response::ModelResponse, state::AppState}; pub struct ModelSelection<'a> { app_state: &'a mut AppState, @@ -23,35 +23,46 @@ impl<'a> Widget for ModelSelection<'a> { .selected_text("Models") .show_ui(ui, |ui| { for model in self.models_available { - let contained = self.app_state.model_selection.contains(&model); + let selected_models = self + .app_state + .selected_models + .iter() + .map(|m| m.name.clone()) + .collect::>(); + let contained = selected_models.contains(&model); let label = ui.selectable_label(contained, &model); if label.clicked() && !contained { - self.app_state.model_selection.push(model.clone()); + self.app_state.selected_models.push(ModelResponse { + name: model.clone(), + message: "No message".to_string(), + status: 0, + time: 0, + }); } } }); let mut model_to_remove: Option = None; - for model in &self.app_state.model_selection { + for model in &self.app_state.selected_models { egui::Frame::none() .inner_margin(8.0) .rounding(4.0) .fill(egui::Color32::WHITE) .show(ui, |ui| { ui.horizontal(|ui| { - ui.label(model); + ui.label(model.name.clone()); if ui.button("X").clicked() { - model_to_remove.replace(model.clone()); + model_to_remove.replace(model.name.clone()); } }); }); } if let Some(model) = model_to_remove { - self.app_state.model_selection.retain(|m| m != &model); + self.app_state.selected_models.retain(|m| m.name != model); } }) .response diff --git a/rust-learning/src/gui/prompt_input.rs b/rust-learning/src/gui/prompt_input.rs new file mode 100644 index 0000000..c093de3 --- /dev/null +++ b/rust-learning/src/gui/prompt_input.rs @@ -0,0 +1,29 @@ +use eframe::egui::{Response, Ui, Widget}; + +use super::state::AppState; + +pub struct PromptInput<'a> { + app_state: &'a mut AppState, +} + +impl<'a> PromptInput<'a> { + pub fn new(app_state: &'a mut AppState) -> Self { + Self { app_state } + } +} + +impl<'a> Widget for PromptInput<'a> { + fn ui(self, ui: &mut Ui) -> Response { + ui.horizontal(|ui| { + let label = ui.label("Prompt:"); + ui.text_edit_singleline(&mut self.app_state.prompt_input) + .labelled_by(label.id); + + if ui.button("Submit").clicked() { + self.app_state.prompt_input.clear(); + } + }); + + ui.label(&self.app_state.prompt_input) + } +} diff --git a/rust-learning/src/gui/state.rs b/rust-learning/src/gui/state.rs index 6b4f5dc..205b5ea 100644 --- a/rust-learning/src/gui/state.rs +++ b/rust-learning/src/gui/state.rs @@ -1,15 +1,17 @@ use eframe::CreationContext; +use super::model_response::ModelResponse; + pub struct AppState { - pub counter: i32, - pub model_selection: Vec, + pub selected_models: Vec, + pub prompt_input: String, } impl AppState { pub fn new(_cc: &CreationContext<'_>) -> Self { Self { - counter: 0, - model_selection: vec![], + selected_models: vec![], + prompt_input: String::new(), } } }