| 1 | //! Print dialog + live preview, then exit.
|
| 2 |
|
| 3 | use std::path::PathBuf;
|
| 4 |
|
| 5 | use eframe::egui;
|
| 6 |
|
| 7 | use posprint::io::{PosFile, PosMetadata};
|
| 8 | use posprint::protocol_url::PosprintLaunch;
|
| 9 |
|
| 10 | use crate::preview_panel::show_preview_panel;
|
| 11 | use crate::print_dialog::{PrintDialog, PrintDialogAction, PrintExecuteResult};
|
| 12 |
|
| 13 | pub struct PrintOnlyApp {
|
| 14 | body: String,
|
| 15 | metadata: PosMetadata,
|
| 16 | /// When set, used as title and as `@image` base directory parent.
|
| 17 | file_path: Option<PathBuf>,
|
| 18 | title_override: Option<String>,
|
| 19 | dialog: PrintDialog,
|
| 20 | preview_zoom: f32,
|
| 21 | fatal_error: Option<String>,
|
| 22 | exit_requested: bool,
|
| 23 | /// Auto-print once printers are ready (`noconfirm=1`).
|
| 24 | auto_print: bool,
|
| 25 | auto_print_attempted: bool,
|
| 26 | }
|
| 27 |
|
| 28 | impl PrintOnlyApp {
|
| 29 | pub fn new(path: PathBuf) -> Self {
|
| 30 | let mut app = Self::blank();
|
| 31 | app.file_path = Some(path.clone());
|
| 32 |
|
| 33 | match PosFile::load_from_path(&path) {
|
| 34 | Ok(file) => {
|
| 35 | app.body = file.body;
|
| 36 | app.metadata = file.metadata;
|
| 37 | if let Err(e) = app.dialog.prepare(&app.body) {
|
| 38 | app.fatal_error = Some(e);
|
| 39 | }
|
| 40 | }
|
| 41 | Err(e) => app.fatal_error = Some(format!("Could not open file: {e:?}")),
|
| 42 | }
|
| 43 |
|
| 44 | app
|
| 45 | }
|
| 46 |
|
| 47 | pub fn from_pos_file(file: PosFile, source_label: impl Into<String>) -> Self {
|
| 48 | let mut app = Self::blank();
|
| 49 | app.body = file.body;
|
| 50 | app.metadata = file.metadata;
|
| 51 | app.title_override = Some(source_label.into());
|
| 52 | if let Err(e) = app.dialog.prepare(&app.body) {
|
| 53 | app.fatal_error = Some(e);
|
| 54 | }
|
| 55 | app
|
| 56 | }
|
| 57 |
|
| 58 | pub fn from_launch(launch: PosprintLaunch) -> Self {
|
| 59 | let label = launch
|
| 60 | .file
|
| 61 | .metadata
|
| 62 | .title
|
| 63 | .clone()
|
| 64 | .unwrap_or_else(|| "posprint://".into());
|
| 65 | let mut app = Self::from_pos_file(launch.file, label);
|
| 66 | if launch.noconfirm {
|
| 67 | app.auto_print = true;
|
| 68 | }
|
| 69 | app
|
| 70 | }
|
| 71 |
|
| 72 | pub fn from_error(message: impl Into<String>) -> Self {
|
| 73 | let mut app = Self::blank();
|
| 74 | app.title_override = Some("posprint://".into());
|
| 75 | app.fatal_error = Some(message.into());
|
| 76 | app
|
| 77 | }
|
| 78 |
|
| 79 | fn blank() -> Self {
|
| 80 | Self {
|
| 81 | body: String::new(),
|
| 82 | metadata: PosMetadata::default(),
|
| 83 | file_path: None,
|
| 84 | title_override: None,
|
| 85 | dialog: PrintDialog::default(),
|
| 86 | preview_zoom: 1.0,
|
| 87 | fatal_error: None,
|
| 88 | exit_requested: false,
|
| 89 | auto_print: false,
|
| 90 | auto_print_attempted: false,
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | fn title(&self) -> String {
|
| 95 | if let Some(label) = &self.title_override {
|
| 96 | return format!("Print — {label}");
|
| 97 | }
|
| 98 | let name = self
|
| 99 | .file_path
|
| 100 | .as_ref()
|
| 101 | .and_then(|p| p.file_name())
|
| 102 | .map(|s| s.to_string_lossy().to_string())
|
| 103 | .unwrap_or_else(|| "Print".into());
|
| 104 | format!("Print — {name}")
|
| 105 | }
|
| 106 |
|
| 107 | fn request_exit(&mut self) {
|
| 108 | self.exit_requested = true;
|
| 109 | }
|
| 110 |
|
| 111 | fn try_auto_print(&mut self, ctx: &egui::Context) {
|
| 112 | if !self.auto_print || self.auto_print_attempted || self.fatal_error.is_some() {
|
| 113 | return;
|
| 114 | }
|
| 115 | if self.dialog.printers_loading {
|
| 116 | ctx.request_repaint();
|
| 117 | return;
|
| 118 | }
|
| 119 | self.auto_print_attempted = true;
|
| 120 | if self
|
| 121 | .dialog
|
| 122 | .execute_print(&self.body, &self.metadata, self.file_path.as_ref())
|
| 123 | == PrintExecuteResult::Printed
|
| 124 | {
|
| 125 | self.request_exit();
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | impl eframe::App for PrintOnlyApp {
|
| 131 | fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
| 132 | if self.exit_requested {
|
| 133 | ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
| 134 | return;
|
| 135 | }
|
| 136 |
|
| 137 | self.dialog.poll_printer_discovery(ctx);
|
| 138 | ctx.send_viewport_cmd(egui::ViewportCommand::Title(self.title()));
|
| 139 |
|
| 140 | if let Some(err) = self.fatal_error.clone() {
|
| 141 | egui::CentralPanel::default().show(ctx, |ui| {
|
| 142 | ui.heading("Print");
|
| 143 | ui.separator();
|
| 144 | ui.colored_label(egui::Color32::RED, err);
|
| 145 | if ui.button("Close").clicked() {
|
| 146 | self.request_exit();
|
| 147 | }
|
| 148 | });
|
| 149 | return;
|
| 150 | }
|
| 151 |
|
| 152 | self.try_auto_print(ctx);
|
| 153 |
|
| 154 | let mut action = PrintDialogAction::None;
|
| 155 |
|
| 156 | egui::CentralPanel::default().show(ctx, |ui| {
|
| 157 | let available = ui.available_size();
|
| 158 | let preview_width = (available.x * 0.55).clamp(280.0, available.x - 320.0);
|
| 159 |
|
| 160 | ui.horizontal(|ui| {
|
| 161 | ui.allocate_ui_with_layout(
|
| 162 | egui::vec2(preview_width, available.y),
|
| 163 | egui::Layout::top_down(egui::Align::Min),
|
| 164 | |ui| {
|
| 165 | ui.horizontal(|ui| {
|
| 166 | ui.heading("Preview");
|
| 167 | ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
| 168 | egui::ComboBox::from_id_salt("print_only_zoom")
|
| 169 | .selected_text(format!("{:.0}%", self.preview_zoom * 100.0))
|
| 170 | .show_ui(ui, |ui| {
|
| 171 | for pct in [50, 75, 100, 125, 150, 200] {
|
| 172 | let z = pct as f32 / 100.0;
|
| 173 | ui.selectable_value(
|
| 174 | &mut self.preview_zoom,
|
| 175 | z,
|
| 176 | format!("{pct}%"),
|
| 177 | );
|
| 178 | }
|
| 179 | });
|
| 180 | });
|
| 181 | });
|
| 182 | ui.separator();
|
| 183 | let base_dir = self.file_path.as_ref().and_then(|p| p.parent());
|
| 184 | show_preview_panel(
|
| 185 | ui,
|
| 186 | ctx,
|
| 187 | &self.body,
|
| 188 | &self.metadata,
|
| 189 | base_dir,
|
| 190 | &self.dialog.param_inputs,
|
| 191 | self.preview_zoom,
|
| 192 | "print_only_preview_",
|
| 193 | );
|
| 194 | },
|
| 195 | );
|
| 196 |
|
| 197 | ui.separator();
|
| 198 |
|
| 199 | ui.allocate_ui_with_layout(
|
| 200 | egui::vec2(ui.available_width(), available.y),
|
| 201 | egui::Layout::top_down(egui::Align::Min),
|
| 202 | |ui| {
|
| 203 | action = self.dialog.show_ui(ui);
|
| 204 | },
|
| 205 | );
|
| 206 | });
|
| 207 | });
|
| 208 |
|
| 209 | match action {
|
| 210 | PrintDialogAction::Print => {
|
| 211 | if self
|
| 212 | .dialog
|
| 213 | .execute_print(&self.body, &self.metadata, self.file_path.as_ref())
|
| 214 | == PrintExecuteResult::Printed
|
| 215 | {
|
| 216 | self.request_exit();
|
| 217 | }
|
| 218 | }
|
| 219 | PrintDialogAction::None => {}
|
| 220 | }
|
| 221 | }
|
| 222 | }
|