| 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 | file_path: PathBuf,
|
| 15 | dialog: PrintDialog,
|
| 16 | fatal_error: Option<String>,
|
| 17 | exit_requested: bool,
|
| 18 | }
|
| 19 |
|
| 20 | impl PrintOnlyApp {
|
| 21 | pub fn new(path: PathBuf) -> Self {
|
| 22 | let mut app = Self {
|
| 23 | body: String::new(),
|
| 24 | metadata: PosMetadata::default(),
|
| 25 | file_path: path.clone(),
|
| 26 | dialog: PrintDialog::default(),
|
| 27 | fatal_error: None,
|
| 28 | exit_requested: false,
|
| 29 | };
|
| 30 |
|
| 31 | match PosFile::load_from_path(&path) {
|
| 32 | Ok(file) => {
|
| 33 | app.body = file.body;
|
| 34 | app.metadata = file.metadata;
|
| 35 | if let Err(e) = app.dialog.prepare(&app.body) {
|
| 36 | app.fatal_error = Some(e);
|
| 37 | }
|
| 38 | }
|
| 39 | Err(e) => app.fatal_error = Some(format!("Could not open file: {e:?}")),
|
| 40 | }
|
| 41 |
|
| 42 | app
|
| 43 | }
|
| 44 |
|
| 45 | fn title(&self) -> String {
|
| 46 | let name = self
|
| 47 | .file_path
|
| 48 | .file_name()
|
| 49 | .map(|s| s.to_string_lossy().to_string())
|
| 50 | .unwrap_or_else(|| "Print".into());
|
| 51 | format!("Print — {name}")
|
| 52 | }
|
| 53 |
|
| 54 | fn request_exit(&mut self) {
|
| 55 | self.exit_requested = true;
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | impl eframe::App for PrintOnlyApp {
|
| 60 | fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
| 61 | if self.exit_requested {
|
| 62 | ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
| 63 | return;
|
| 64 | }
|
| 65 |
|
| 66 | self.dialog.poll_printer_discovery(ctx);
|
| 67 | ctx.send_viewport_cmd(egui::ViewportCommand::Title(self.title()));
|
| 68 |
|
| 69 | if let Some(err) = self.fatal_error.clone() {
|
| 70 | egui::CentralPanel::default().show(ctx, |ui| {
|
| 71 | ui.heading("Print");
|
| 72 | ui.separator();
|
| 73 | ui.colored_label(egui::Color32::RED, err);
|
| 74 | if ui.button("Close").clicked() {
|
| 75 | self.request_exit();
|
| 76 | }
|
| 77 | });
|
| 78 | return;
|
| 79 | }
|
| 80 |
|
| 81 | let mut window_open = true;
|
| 82 | egui::Window::new("Print")
|
| 83 | .collapsible(false)
|
| 84 | .resizable(false)
|
| 85 | .open(&mut window_open)
|
| 86 | .anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0])
|
| 87 | .show(ctx, |ui| {
|
| 88 | match self.dialog.show_ui(ui, "Cancel") {
|
| 89 | PrintDialogAction::Cancel => self.request_exit(),
|
| 90 | PrintDialogAction::Print => {
|
| 91 | if self.dialog.execute_print(&self.body, &self.metadata, Some(&self.file_path))
|
| 92 | == PrintExecuteResult::Printed
|
| 93 | {
|
| 94 | self.request_exit();
|
| 95 | }
|
| 96 | }
|
| 97 | PrintDialogAction::None => {}
|
| 98 | }
|
| 99 | });
|
| 100 |
|
| 101 | if !window_open {
|
| 102 | self.dialog.apply_inputs_to_last_params();
|
| 103 | self.request_exit();
|
| 104 | }
|
| 105 | }
|
| 106 | }
|