diff --git a/rust-learning/src/gui.rs b/rust-learning/src/gui.rs index eb650d1..512603d 100644 --- a/rust-learning/src/gui.rs +++ b/rust-learning/src/gui.rs @@ -3,6 +3,7 @@ use model_response::ModelResponse; use state::AppState; pub mod model_response; +pub mod model_selection; pub mod state; pub fn main() -> eframe::Result { @@ -45,6 +46,11 @@ impl eframe::App for AppState { } }); + 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, diff --git a/rust-learning/src/gui/model_response.rs b/rust-learning/src/gui/model_response.rs index c50a44c..fdb0cb9 100644 --- a/rust-learning/src/gui/model_response.rs +++ b/rust-learning/src/gui/model_response.rs @@ -33,7 +33,13 @@ impl Widget for ModelResponse { ui.label(self.time.to_string()); }); - ui.label(self.message) + egui::Frame::none() + .inner_margin(8.0) + .rounding(4.0) + .fill(egui::Color32::WHITE) + .show(ui, |ui| { + 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 new file mode 100644 index 0000000..271855c --- /dev/null +++ b/rust-learning/src/gui/model_selection.rs @@ -0,0 +1,59 @@ +use eframe::egui::{self, Response, Ui, Widget}; + +use super::state::AppState; + +pub struct ModelSelection<'a> { + app_state: &'a mut AppState, + pub models_available: Vec, +} + +impl<'a> ModelSelection<'a> { + pub fn new(app_state: &'a mut AppState, models_available: Vec) -> Self { + Self { + app_state, + models_available, + } + } +} + +impl<'a> Widget for ModelSelection<'a> { + fn ui(self, ui: &mut Ui) -> Response { + ui.horizontal(|ui| { + egui::ComboBox::from_label("") + .selected_text("Models") + .show_ui(ui, |ui| { + for model in self.models_available { + let contained = self.app_state.model_selection.contains(&model); + + let label = ui.selectable_label(contained, &model); + + if label.clicked() && !contained { + self.app_state.model_selection.push(model.clone()); + } + } + }); + + let mut model_to_remove: Option = None; + + for model in &self.app_state.model_selection { + egui::Frame::none() + .inner_margin(8.0) + .rounding(4.0) + .fill(egui::Color32::WHITE) + .show(ui, |ui| { + ui.horizontal(|ui| { + ui.label(model); + if ui.button("X").clicked() { + model_to_remove.replace(model.clone()); + } + }); + }); + } + + if let Some(model) = model_to_remove { + self.app_state.model_selection.retain(|m| m != &model); + } + }) + .response + } +} diff --git a/rust-learning/src/gui/state.rs b/rust-learning/src/gui/state.rs index eac1df3..6b4f5dc 100644 --- a/rust-learning/src/gui/state.rs +++ b/rust-learning/src/gui/state.rs @@ -2,10 +2,14 @@ use eframe::CreationContext; pub struct AppState { pub counter: i32, + pub model_selection: Vec, } impl AppState { pub fn new(_cc: &CreationContext<'_>) -> Self { - Self { counter: 0 } + Self { + counter: 0, + model_selection: vec![], + } } }