egui-qr/examples/main.rs

37 lines
858 B
Rust
Raw Normal View History

2025-01-10 20:34:31 +00:00
use eframe::{Frame, NativeOptions};
2025-01-10 21:02:25 +00:00
use egui::{CentralPanel, Context, TextEdit, TopBottomPanel, Widget};
2025-01-10 20:34:31 +00:00
use egui_qr::QrCodeWidget;
fn main() {
let _ = eframe::run_native(
"egui_qr",
NativeOptions::default(),
2025-01-11 03:06:31 +00:00
Box::new(|_| Ok(Box::new(App::new()))),
2025-01-10 20:34:31 +00:00
);
}
2025-01-10 21:02:25 +00:00
struct App {
data: String,
}
impl App {
pub fn new() -> Self {
Self {
data: String::new(),
}
}
}
2025-01-10 20:34:31 +00:00
impl eframe::App for App {
2025-01-10 21:02:25 +00:00
fn update(&mut self, ctx: &Context, _frame: &mut Frame) {
TopBottomPanel::top("main").show(ctx, |ui| TextEdit::singleline(&mut self.data).ui(ui));
2025-01-10 20:34:31 +00:00
CentralPanel::default().show(ctx, |ui| {
2025-01-10 21:02:25 +00:00
if let Ok(q) = QrCodeWidget::from_data(self.data.as_bytes()) {
q.ui(ui)
} else {
ui.label("Invalid data")
}
2025-01-10 20:34:31 +00:00
});
}
}