use eframe::egui;
use eframe::egui::Context;
use egui_extras::RetainedImage;
use image::DynamicImage;
use rqrr::MetaData;
use crate::gui::app_loop::LocalState;
use crate::gui::common::bounded_text_area;
use crate::gui::qr_render::qr_encode;
pub struct QrState {
pub show_window: bool,
pub last_image: Option<DynamicImage>,
pub contents: Option<String>,
pub metadata: Option<MetaData>,
pub retained_image: Option<RetainedImage>
}
pub struct QrShowState {
pub show_window: bool,
pub qr_image: Option<RetainedImage>,
pub qr_text: Option<String>
}
impl QrShowState {
pub fn enable(&mut self, content: impl Into<String>) {
self.show_window = true;
let string = content.into();
self.qr_text = Some(string.clone());
let enc = qr_encode(string);
self.qr_image = Some(enc);
}
}
impl Default for QrShowState {
fn default() -> Self {
Self {
show_window: false,
qr_image: None,
qr_text: None,
}
}
}
impl Default for QrState {
fn default() -> Self {
Self {
show_window: false,
last_image: None,
contents: None,
metadata: None,
retained_image: None,
}
}
}
impl QrState {
pub fn enable(&mut self) {
self.show_window = true;
self.contents = None;
self.metadata = None;
self.retained_image = None;
self.last_image = None;
}
pub fn disable(&mut self) {
self.show_window = false;
}
pub fn read_tick(&mut self) {
}
}
pub fn qr_window(
ctx: &Context, state: &mut LocalState
) {
if state.qr_state.show_window {
state.qr_state.read_tick();
}
egui::Window::new("QR Code Camera Parser")
.open(&mut state.qr_state.show_window)
.resizable(false)
.collapsible(false)
.min_width(500.0)
.default_width(500.0)
.show(ctx, |ui| {
ui.vertical(|ui| {
if let Some(i) = state.qr_state.retained_image.as_ref() {
i.show_scaled(ui, 0.2);
}
});
});
}
pub fn qr_show_window(
ctx: &Context, state: &mut LocalState
) {
egui::Window::new("QR Code")
.open(&mut state.qr_show_state.show_window)
.resizable(false)
.default_pos(egui::Pos2::new(0.0, 0.0))
.anchor(egui::Align2::LEFT_TOP, egui::Vec2::new(0.0, 0.0))
.collapsible(false)
.min_width(500.0)
.default_width(500.0)
.show(ctx, |ui| {
ui.vertical(|ui| {
if let Some(i) = state.qr_show_state.qr_image.as_ref() {
i.show_scaled(ui, 1.0);
}
if let Some(t) = &mut state.qr_show_state.qr_text.clone() {
bounded_text_area(ui, t)
}
});
});
}