//! Print dialog + live preview, then exit. use std::path::PathBuf; use eframe::egui; use posprint::io::{PosFile, PosMetadata}; use posprint::protocol_url::PosprintLaunch; use crate::preview_panel::show_preview_panel; 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, preview_zoom: f32, fatal_error: Option, exit_requested: bool, /// Auto-print once printers are ready (`noconfirm=1`). auto_print: bool, auto_print_attempted: bool, } impl PrintOnlyApp { pub fn new(path: PathBuf) -> Self { let mut app = Self::blank(); app.file_path = Some(path.clone()); 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::blank(); app.body = file.body; app.metadata = file.metadata; app.title_override = Some(source_label.into()); if let Err(e) = app.dialog.prepare(&app.body) { app.fatal_error = Some(e); } app } pub fn from_launch(launch: PosprintLaunch) -> Self { let label = launch .file .metadata .title .clone() .unwrap_or_else(|| "posprint://".into()); let mut app = Self::from_pos_file(launch.file, label); if launch.noconfirm { app.auto_print = true; } app } pub fn from_error(message: impl Into) -> Self { let mut app = Self::blank(); app.title_override = Some("posprint://".into()); app.fatal_error = Some(message.into()); app } fn blank() -> Self { Self { body: String::new(), metadata: PosMetadata::default(), file_path: None, title_override: None, dialog: PrintDialog::default(), preview_zoom: 1.0, fatal_error: None, exit_requested: false, auto_print: false, auto_print_attempted: 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; } fn try_auto_print(&mut self, ctx: &egui::Context) { if !self.auto_print || self.auto_print_attempted || self.fatal_error.is_some() { return; } if self.dialog.printers_loading { ctx.request_repaint(); return; } self.auto_print_attempted = true; if self .dialog .execute_print(&self.body, &self.metadata, self.file_path.as_ref()) == PrintExecuteResult::Printed { self.request_exit(); } } } 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; } self.try_auto_print(ctx); let mut action = PrintDialogAction::None; egui::CentralPanel::default().show(ctx, |ui| { let available = ui.available_size(); let preview_width = (available.x * 0.55).clamp(280.0, available.x - 320.0); ui.horizontal(|ui| { ui.allocate_ui_with_layout( egui::vec2(preview_width, available.y), egui::Layout::top_down(egui::Align::Min), |ui| { ui.horizontal(|ui| { ui.heading("Preview"); ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { egui::ComboBox::from_id_salt("print_only_zoom") .selected_text(format!("{:.0}%", self.preview_zoom * 100.0)) .show_ui(ui, |ui| { for pct in [50, 75, 100, 125, 150, 200] { let z = pct as f32 / 100.0; ui.selectable_value( &mut self.preview_zoom, z, format!("{pct}%"), ); } }); }); }); ui.separator(); let base_dir = self.file_path.as_ref().and_then(|p| p.parent()); show_preview_panel( ui, ctx, &self.body, &self.metadata, base_dir, &self.dialog.param_inputs, self.preview_zoom, "print_only_preview_", ); }, ); ui.separator(); ui.allocate_ui_with_layout( egui::vec2(ui.available_width(), available.y), egui::Layout::top_down(egui::Align::Min), |ui| { action = self.dialog.show_ui(ui); }, ); }); }); match action { PrintDialogAction::Print => { if self .dialog .execute_print(&self.body, &self.metadata, self.file_path.as_ref()) == PrintExecuteResult::Printed { self.request_exit(); } } PrintDialogAction::None => {} } } }