//! Minimal UI: print dialog only, then exit. use std::path::PathBuf; use eframe::egui; use posprint::io::{PosFile, PosMetadata}; use crate::print_dialog::{PrintDialog, PrintDialogAction, PrintExecuteResult}; pub struct PrintOnlyApp { body: String, metadata: PosMetadata, file_path: PathBuf, dialog: PrintDialog, fatal_error: Option, exit_requested: bool, } impl PrintOnlyApp { pub fn new(path: PathBuf) -> Self { let mut app = Self { body: String::new(), metadata: PosMetadata::default(), file_path: path.clone(), dialog: PrintDialog::default(), fatal_error: None, exit_requested: false, }; match PosFile::load_from_path(&path) { Ok(file) => { app.body = file.body; app.metadata = file.metadata; if let Err(e) = app.dialog.prepare(&app.body) { app.fatal_error = Some(e); } } Err(e) => app.fatal_error = Some(format!("Could not open file: {e:?}")), } app } fn title(&self) -> String { let name = self .file_path .file_name() .map(|s| s.to_string_lossy().to_string()) .unwrap_or_else(|| "Print".into()); format!("Print — {name}") } fn request_exit(&mut self) { self.exit_requested = true; } } impl eframe::App for PrintOnlyApp { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { if self.exit_requested { ctx.send_viewport_cmd(egui::ViewportCommand::Close); return; } self.dialog.poll_printer_discovery(ctx); ctx.send_viewport_cmd(egui::ViewportCommand::Title(self.title())); if let Some(err) = self.fatal_error.clone() { egui::CentralPanel::default().show(ctx, |ui| { ui.heading("Print"); ui.separator(); ui.colored_label(egui::Color32::RED, err); if ui.button("Close").clicked() { self.request_exit(); } }); return; } let mut window_open = true; egui::Window::new("Print") .collapsible(false) .resizable(false) .open(&mut window_open) .anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0]) .show(ctx, |ui| { match self.dialog.show_ui(ui, "Cancel") { PrintDialogAction::Cancel => self.request_exit(), PrintDialogAction::Print => { if self.dialog.execute_print(&self.body, &self.metadata, Some(&self.file_path)) == PrintExecuteResult::Printed { self.request_exit(); } } PrintDialogAction::None => {} } }); if !window_open { self.dialog.apply_inputs_to_last_params(); self.request_exit(); } } }