//! 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, /// When set, used as title and as `@image` base directory parent. file_path: Option, title_override: Option, 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: Some(path.clone()), title_override: None, 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 } pub fn from_pos_file(file: PosFile, source_label: impl Into) -> Self { let mut app = Self { body: file.body, metadata: file.metadata, file_path: None, title_override: Some(source_label.into()), dialog: PrintDialog::default(), fatal_error: None, exit_requested: false, }; if let Err(e) = app.dialog.prepare(&app.body) { app.fatal_error = Some(e); } app } pub fn from_error(message: impl Into) -> Self { Self { body: String::new(), metadata: PosMetadata::default(), file_path: None, title_override: Some("posprint://".into()), dialog: PrintDialog::default(), fatal_error: Some(message.into()), exit_requested: false, } } fn title(&self) -> String { if let Some(label) = &self.title_override { return format!("Print — {label}"); } let name = self .file_path .as_ref() .and_then(|p| p.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, self.file_path.as_ref()) == PrintExecuteResult::Printed { self.request_exit(); } } PrintDialogAction::None => {} } }); if !window_open { self.dialog.apply_inputs_to_last_params(); self.request_exit(); } } }