| 1 | //! Minimal UI: print dialog only, then exit.
|
| 2 |
|
| 3 | use std::path::PathBuf;
|
| 4 |
|
| 5 | use eframe::egui;
|
| 6 |
|
| 7 | use posprint::io::{PosFile, PosMetadata};
|
| 8 |
|
| 9 | use crate::print_dialog::{PrintDialog, PrintDialogAction, PrintExecuteResult};
|
| 10 |
|
| 11 | pub struct PrintOnlyApp {
|
| 12 | body: String,
|
| 13 | metadata: PosMetadata,
|
| 14 | /// When set, used as title and as `@image` base directory parent.
|
| 15 | file_path: Option<PathBuf>,
|
| 16 | title_override: Option<String>,
|
| 17 | dialog: PrintDialog,
|
| 18 | fatal_error: Option<String>,
|
| 19 | exit_requested: bool,
|
| 20 | }
|
| 21 |
|
| 22 | impl PrintOnlyApp {
|
| 23 | pub fn new(path: PathBuf) -> Self {
|
| 24 | let mut app = Self {
|
| 25 | body: String::new(),
|
| 26 | metadata: PosMetadata::default(),
|
| 27 | file_path: Some(path.clone()),
|
| 28 | title_override: None,
|
| 29 | dialog: PrintDialog::default(),
|
| 30 | fatal_error: None,
|
| 31 | exit_requested: false,
|
| 32 | };
|
| 33 |
|
| 34 | match PosFile::load_from_path(&path) {
|
| 35 | Ok(file) => {
|
| 36 | app.body = file.body;
|
| 37 | app.metadata = file.metadata;
|
| 38 | if let Err(e) = app.dialog.prepare(&app.body) {
|
| 39 | app.fatal_error = Some(e);
|
| 40 | }
|
| 41 | }
|
| 42 | Err(e) => app.fatal_error = Some(format!("Could not open file: {e:?}")),
|
| 43 | }
|
| 44 |
|
| 45 | app
|
| 46 | }
|
| 47 |
|
| 48 | pub fn from_pos_file(file: PosFile, source_label: impl Into<String>) -> Self {
|
| 49 | let mut app = Self {
|
| 50 | body: file.body,
|
| 51 | metadata: file.metadata,
|
| 52 | file_path: None,
|
| 53 | title_override: Some(source_label.into()),
|
| 54 | dialog: PrintDialog::default(),
|
| 55 | fatal_error: None,
|
| 56 | exit_requested: false,
|
| 57 | };
|
| 58 | if let Err(e) = app.dialog.prepare(&app.body) {
|
| 59 | app.fatal_error = Some(e);
|
| 60 | }
|
| 61 | app
|
| 62 | }
|
| 63 |
|
| 64 | pub fn from_error(message: impl Into<String>) -> Self {
|
| 65 | Self {
|
| 66 | body: String::new(),
|
| 67 | metadata: PosMetadata::default(),
|
| 68 | file_path: None,
|
| 69 | title_override: Some("posprint://".into()),
|
| 70 | dialog: PrintDialog::default(),
|
| 71 | fatal_error: Some(message.into()),
|
| 72 | exit_requested: false,
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | fn title(&self) -> String {
|
| 77 | if let Some(label) = &self.title_override {
|
| 78 | return format!("Print — {label}");
|
| 79 | }
|
| 80 | let name = self
|
| 81 | .file_path
|
| 82 | .as_ref()
|
| 83 | .and_then(|p| p.file_name())
|
| 84 | .map(|s| s.to_string_lossy().to_string())
|
| 85 | .unwrap_or_else(|| "Print".into());
|
| 86 | format!("Print — {name}")
|
| 87 | }
|
| 88 |
|
| 89 | fn request_exit(&mut self) {
|
| 90 | self.exit_requested = true;
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | impl eframe::App for PrintOnlyApp {
|
| 95 | fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
| 96 | if self.exit_requested {
|
| 97 | ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
| 98 | return;
|
| 99 | }
|
| 100 |
|
| 101 | self.dialog.poll_printer_discovery(ctx);
|
| 102 | ctx.send_viewport_cmd(egui::ViewportCommand::Title(self.title()));
|
| 103 |
|
| 104 | if let Some(err) = self.fatal_error.clone() {
|
| 105 | egui::CentralPanel::default().show(ctx, |ui| {
|
| 106 | ui.heading("Print");
|
| 107 | ui.separator();
|
| 108 | ui.colored_label(egui::Color32::RED, err);
|
| 109 | if ui.button("Close").clicked() {
|
| 110 | self.request_exit();
|
| 111 | }
|
| 112 | });
|
| 113 | return;
|
| 114 | }
|
| 115 |
|
| 116 | let mut window_open = true;
|
| 117 | egui::Window::new("Print")
|
| 118 | .collapsible(false)
|
| 119 | .resizable(false)
|
| 120 | .open(&mut window_open)
|
| 121 | .anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0])
|
| 122 | .show(ctx, |ui| {
|
| 123 | match self.dialog.show_ui(ui, "Cancel") {
|
| 124 | PrintDialogAction::Cancel => self.request_exit(),
|
| 125 | PrintDialogAction::Print => {
|
| 126 | if self
|
| 127 | .dialog
|
| 128 | .execute_print(&self.body, &self.metadata, self.file_path.as_ref())
|
| 129 | == PrintExecuteResult::Printed
|
| 130 | {
|
| 131 | self.request_exit();
|
| 132 | }
|
| 133 | }
|
| 134 | PrintDialogAction::None => {}
|
| 135 | }
|
| 136 | });
|
| 137 |
|
| 138 | if !window_open {
|
| 139 | self.dialog.apply_inputs_to_last_params();
|
| 140 | self.request_exit();
|
| 141 | }
|
| 142 | }
|
| 143 | }
|