Add model selection

This commit is contained in:
Ethan 2024-12-25 19:15:41 -05:00
parent c976d17af7
commit 56efe5fb5c
4 changed files with 77 additions and 2 deletions

View File

@ -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,

View File

@ -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
}

View File

@ -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<String>,
}
impl<'a> ModelSelection<'a> {
pub fn new(app_state: &'a mut AppState, models_available: Vec<String>) -> 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<String> = 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
}
}

View File

@ -2,10 +2,14 @@ use eframe::CreationContext;
pub struct AppState {
pub counter: i32,
pub model_selection: Vec<String>,
}
impl AppState {
pub fn new(_cc: &CreationContext<'_>) -> Self {
Self { counter: 0 }
Self {
counter: 0,
model_selection: vec![],
}
}
}