Add custom widget
This commit is contained in:
parent
fc895c5e44
commit
c976d17af7
55
rust-learning/src/gui.rs
Normal file
55
rust-learning/src/gui.rs
Normal file
@ -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,
|
||||
));
|
||||
});
|
||||
}
|
||||
}
|
40
rust-learning/src/gui/model_response.rs
Normal file
40
rust-learning/src/gui/model_response.rs
Normal file
@ -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
|
||||
}
|
||||
}
|
11
rust-learning/src/gui/state.rs
Normal file
11
rust-learning/src/gui/state.rs
Normal file
@ -0,0 +1,11 @@
|
||||
use eframe::CreationContext;
|
||||
|
||||
pub struct AppState {
|
||||
pub counter: i32,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new(_cc: &CreationContext<'_>) -> Self {
|
||||
Self { counter: 0 }
|
||||
}
|
||||
}
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user