init
This commit is contained in:
commit
113ac26d38
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
2655
Cargo.lock
generated
Normal file
2655
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "egui_qr"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
egui = { version = "^0.20", default-features = false }
|
||||
qrcode = { version = "0.14.1", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
eframe = "^0.20"
|
23
examples/main.rs
Normal file
23
examples/main.rs
Normal file
@ -0,0 +1,23 @@
|
||||
use eframe::{Frame, NativeOptions};
|
||||
use egui::{CentralPanel, Context, Widget};
|
||||
use egui_qr::QrCodeWidget;
|
||||
use qrcode::QrCode;
|
||||
|
||||
fn main() {
|
||||
let _ = eframe::run_native(
|
||||
"egui_qr",
|
||||
NativeOptions::default(),
|
||||
Box::new(|_| Box::new(App)),
|
||||
);
|
||||
}
|
||||
|
||||
struct App;
|
||||
|
||||
impl eframe::App for App {
|
||||
fn update(&mut self, ctx: &Context, frame: &mut Frame) {
|
||||
let code = QrCode::new(b"hello").unwrap();
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
QrCodeWidget::new(&code).ui(ui);
|
||||
});
|
||||
}
|
||||
}
|
41
src/lib.rs
Normal file
41
src/lib.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use egui::{vec2, Color32, Rect, Response, Rounding, Sense, Stroke, Ui, Widget};
|
||||
use qrcode::{Color, QrCode};
|
||||
|
||||
pub struct QrCodeWidget<'a> {
|
||||
code: &'a QrCode,
|
||||
}
|
||||
|
||||
impl<'a> QrCodeWidget<'a> {
|
||||
pub fn new(code: &'a QrCode) -> Self {
|
||||
QrCodeWidget { code }
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for QrCodeWidget<'_> {
|
||||
fn ui(self, ui: &mut Ui) -> Response {
|
||||
let size = ui.available_size();
|
||||
|
||||
let (response, painter) = ui.allocate_painter(size, Sense::click());
|
||||
let w = self.code.width();
|
||||
let start = response.rect.min;
|
||||
let mut ctr = 0;
|
||||
let scale = (size.x / w as f32).floor();
|
||||
for c in self.code.to_colors() {
|
||||
let row = ctr / w;
|
||||
let col = ctr % w;
|
||||
let c_start = start + vec2(col as f32 * scale, row as f32 * scale);
|
||||
let c_end = c_start + vec2(scale, scale);
|
||||
painter.rect(
|
||||
Rect::from_min_max(c_start, c_end),
|
||||
Rounding::none(),
|
||||
match c {
|
||||
Color::Light => Color32::WHITE,
|
||||
Color::Dark => Color32::BLACK,
|
||||
},
|
||||
Stroke::NONE,
|
||||
);
|
||||
ctr += 1;
|
||||
}
|
||||
response
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user