| 1 | //! Print dialog + live preview, then exit.
|
| 2 |
|
| 3 | use std::path::PathBuf;
|
| 4 | use std::sync::mpsc::{self, Receiver, TryRecvError};
|
| 5 |
|
| 6 | use eframe::egui;
|
| 7 |
|
| 8 | use posprint::config::AppConfig;
|
| 9 | use posprint::io::{PosFile, PosMetadata};
|
| 10 | use posprint::protocol_url::PosprintLaunch;
|
| 11 | use posprint::referrer_verify;
|
| 12 |
|
| 13 | use crate::preview_panel::show_preview_panel;
|
| 14 | use crate::print_dialog::{PrintDialog, PrintDialogAction, PrintExecuteResult};
|
| 15 |
|
| 16 | pub struct PrintOnlyApp {
|
| 17 | body: String,
|
| 18 | metadata: PosMetadata,
|
| 19 | /// When set, used as title and as `@image` base directory parent.
|
| 20 | file_path: Option<PathBuf>,
|
| 21 | title_override: Option<String>,
|
| 22 | dialog: PrintDialog,
|
| 23 | preview_zoom: f32,
|
| 24 | fatal_error: Option<String>,
|
| 25 | exit_requested: bool,
|
| 26 | /// Auto-print once printers are ready (after trust if needed).
|
| 27 | auto_print: bool,
|
| 28 | /// Pending first-time trust confirmation for this hostname.
|
| 29 | pending_trust: Option<String>,
|
| 30 | auto_print_attempted: bool,
|
| 31 | /// Background GET of `referrer=` for noconfirm.
|
| 32 | referrer_rx: Option<Receiver<Result<String, String>>>,
|
| 33 | verifying_referrer: bool,
|
| 34 | }
|
| 35 |
|
| 36 | impl PrintOnlyApp {
|
| 37 | pub fn new(path: PathBuf) -> Self {
|
| 38 | let mut app = Self::blank();
|
| 39 | app.file_path = Some(path.clone());
|
| 40 |
|
| 41 | match PosFile::load_from_path(&path) {
|
| 42 | Ok(file) => {
|
| 43 | app.body = file.body;
|
| 44 | app.metadata = file.metadata;
|
| 45 | if let Err(e) = app.dialog.prepare(&app.body) {
|
| 46 | app.fatal_error = Some(e);
|
| 47 | }
|
| 48 | }
|
| 49 | Err(e) => app.fatal_error = Some(format!("Could not open file: {e:?}")),
|
| 50 | }
|
| 51 |
|
| 52 | app
|
| 53 | }
|
| 54 |
|
| 55 | pub fn from_pos_file(file: PosFile, source_label: impl Into<String>) -> Self {
|
| 56 | let mut app = Self::blank();
|
| 57 | app.body = file.body;
|
| 58 | app.metadata = file.metadata;
|
| 59 | app.title_override = Some(source_label.into());
|
| 60 | if let Err(e) = app.dialog.prepare(&app.body) {
|
| 61 | app.fatal_error = Some(e);
|
| 62 | }
|
| 63 | app
|
| 64 | }
|
| 65 |
|
| 66 | pub fn from_launch(launch: PosprintLaunch) -> Self {
|
| 67 | let label = launch
|
| 68 | .file
|
| 69 | .metadata
|
| 70 | .title
|
| 71 | .clone()
|
| 72 | .unwrap_or_else(|| "posprint://".into());
|
| 73 | let mut app = Self::from_pos_file(launch.file, label);
|
| 74 | if !launch.noconfirm {
|
| 75 | return app;
|
| 76 | }
|
| 77 |
|
| 78 | match launch.referrer {
|
| 79 | None => {
|
| 80 | app.fatal_error = Some(
|
| 81 | "noconfirm=1 requires referrer=<https://host/path?…> so posprint can verify the print with that site."
|
| 82 | .into(),
|
| 83 | );
|
| 84 | }
|
| 85 | Some(referrer) => {
|
| 86 | app.start_referrer_verification(referrer);
|
| 87 | }
|
| 88 | }
|
| 89 | app
|
| 90 | }
|
| 91 |
|
| 92 | pub fn from_error(message: impl Into<String>) -> Self {
|
| 93 | let mut app = Self::blank();
|
| 94 | app.title_override = Some("posprint://".into());
|
| 95 | app.fatal_error = Some(message.into());
|
| 96 | app
|
| 97 | }
|
| 98 |
|
| 99 | fn blank() -> Self {
|
| 100 | Self {
|
| 101 | body: String::new(),
|
| 102 | metadata: PosMetadata::default(),
|
| 103 | file_path: None,
|
| 104 | title_override: None,
|
| 105 | dialog: PrintDialog::default(),
|
| 106 | preview_zoom: 1.0,
|
| 107 | fatal_error: None,
|
| 108 | exit_requested: false,
|
| 109 | auto_print: false,
|
| 110 | pending_trust: None,
|
| 111 | auto_print_attempted: false,
|
| 112 | referrer_rx: None,
|
| 113 | verifying_referrer: false,
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | fn start_referrer_verification(&mut self, referrer: String) {
|
| 118 | self.verifying_referrer = true;
|
| 119 | let (tx, rx) = mpsc::channel();
|
| 120 | self.referrer_rx = Some(rx);
|
| 121 | std::thread::spawn(move || {
|
| 122 | let result = referrer_verify::verify_referrer(&referrer).map_err(|e| e.to_string());
|
| 123 | let _ = tx.send(result);
|
| 124 | });
|
| 125 | }
|
| 126 |
|
| 127 | fn poll_referrer_verification(&mut self, ctx: &egui::Context) {
|
| 128 | let Some(rx) = &self.referrer_rx else {
|
| 129 | return;
|
| 130 | };
|
| 131 | match rx.try_recv() {
|
| 132 | Ok(Ok(host)) => {
|
| 133 | self.referrer_rx = None;
|
| 134 | self.verifying_referrer = false;
|
| 135 | let cfg = AppConfig::load();
|
| 136 | if cfg.is_trusted(&host) {
|
| 137 | self.auto_print = true;
|
| 138 | } else {
|
| 139 | self.pending_trust = Some(host);
|
| 140 | }
|
| 141 | }
|
| 142 | Ok(Err(e)) => {
|
| 143 | self.referrer_rx = None;
|
| 144 | self.verifying_referrer = false;
|
| 145 | self.fatal_error = Some(e);
|
| 146 | }
|
| 147 | Err(TryRecvError::Empty) => ctx.request_repaint(),
|
| 148 | Err(TryRecvError::Disconnected) => {
|
| 149 | self.referrer_rx = None;
|
| 150 | self.verifying_referrer = false;
|
| 151 | self.fatal_error = Some("referrer verification interrupted".into());
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | fn title(&self) -> String {
|
| 157 | if let Some(label) = &self.title_override {
|
| 158 | return format!("Print — {label}");
|
| 159 | }
|
| 160 | let name = self
|
| 161 | .file_path
|
| 162 | .as_ref()
|
| 163 | .and_then(|p| p.file_name())
|
| 164 | .map(|s| s.to_string_lossy().to_string())
|
| 165 | .unwrap_or_else(|| "Print".into());
|
| 166 | format!("Print — {name}")
|
| 167 | }
|
| 168 |
|
| 169 | fn request_exit(&mut self) {
|
| 170 | self.exit_requested = true;
|
| 171 | }
|
| 172 |
|
| 173 | fn try_auto_print(&mut self, ctx: &egui::Context) {
|
| 174 | if !self.auto_print || self.auto_print_attempted || self.fatal_error.is_some() {
|
| 175 | return;
|
| 176 | }
|
| 177 | if self.dialog.printers_loading {
|
| 178 | ctx.request_repaint();
|
| 179 | return;
|
| 180 | }
|
| 181 | self.auto_print_attempted = true;
|
| 182 | if self
|
| 183 | .dialog
|
| 184 | .execute_print(&self.body, &self.metadata, self.file_path.as_ref())
|
| 185 | == PrintExecuteResult::Printed
|
| 186 | {
|
| 187 | self.request_exit();
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | impl eframe::App for PrintOnlyApp {
|
| 193 | fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
| 194 | if self.exit_requested {
|
| 195 | ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
| 196 | return;
|
| 197 | }
|
| 198 |
|
| 199 | self.dialog.poll_printer_discovery(ctx);
|
| 200 | self.poll_referrer_verification(ctx);
|
| 201 | ctx.send_viewport_cmd(egui::ViewportCommand::Title(self.title()));
|
| 202 |
|
| 203 | if self.verifying_referrer {
|
| 204 | egui::CentralPanel::default().show(ctx, |ui| {
|
| 205 | ui.heading("Verifying print request…");
|
| 206 | ui.separator();
|
| 207 | ui.horizontal(|ui| {
|
| 208 | ui.spinner();
|
| 209 | ui.label("Checking referrer URL with the issuing website.");
|
| 210 | });
|
| 211 | if ui.button("Cancel").clicked() {
|
| 212 | self.request_exit();
|
| 213 | }
|
| 214 | });
|
| 215 | return;
|
| 216 | }
|
| 217 |
|
| 218 | if let Some(origin) = self.pending_trust.clone() {
|
| 219 | egui::CentralPanel::default().show(ctx, |ui| {
|
| 220 | ui.heading("Trust this site?");
|
| 221 | ui.separator();
|
| 222 | ui.label(format!(
|
| 223 | "“{origin}” verified this print request and wants to print without confirmation (noconfirm)."
|
| 224 | ));
|
| 225 | ui.label(
|
| 226 | "If you trust this site, future verified noconfirm prints from this domain will not ask again.",
|
| 227 | );
|
| 228 | ui.add_space(12.0);
|
| 229 | ui.horizontal(|ui| {
|
| 230 | if ui.button("Trust and print").clicked() {
|
| 231 | let mut cfg = AppConfig::load();
|
| 232 | cfg.trust_origin(&origin);
|
| 233 | if let Err(e) = cfg.save() {
|
| 234 | self.fatal_error = Some(format!("Could not save trust setting: {e}"));
|
| 235 | self.pending_trust = None;
|
| 236 | } else {
|
| 237 | self.pending_trust = None;
|
| 238 | self.auto_print = true;
|
| 239 | }
|
| 240 | }
|
| 241 | if ui.button("Cancel").clicked() {
|
| 242 | self.request_exit();
|
| 243 | }
|
| 244 | });
|
| 245 | ui.add_space(8.0);
|
| 246 | ui.label("You can revoke trust later under Settings in the designer.");
|
| 247 | });
|
| 248 | return;
|
| 249 | }
|
| 250 |
|
| 251 | if let Some(err) = self.fatal_error.clone() {
|
| 252 | egui::CentralPanel::default().show(ctx, |ui| {
|
| 253 | ui.heading("Print");
|
| 254 | ui.separator();
|
| 255 | ui.colored_label(egui::Color32::RED, err);
|
| 256 | if ui.button("Close").clicked() {
|
| 257 | self.request_exit();
|
| 258 | }
|
| 259 | });
|
| 260 | return;
|
| 261 | }
|
| 262 |
|
| 263 | self.try_auto_print(ctx);
|
| 264 |
|
| 265 | let mut action = PrintDialogAction::None;
|
| 266 |
|
| 267 | egui::CentralPanel::default().show(ctx, |ui| {
|
| 268 | let available = ui.available_size();
|
| 269 | let preview_width = (available.x * 0.55).clamp(280.0, available.x - 320.0);
|
| 270 |
|
| 271 | ui.horizontal(|ui| {
|
| 272 | ui.allocate_ui_with_layout(
|
| 273 | egui::vec2(preview_width, available.y),
|
| 274 | egui::Layout::top_down(egui::Align::Min),
|
| 275 | |ui| {
|
| 276 | ui.horizontal(|ui| {
|
| 277 | ui.heading("Preview");
|
| 278 | ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
| 279 | egui::ComboBox::from_id_salt("print_only_zoom")
|
| 280 | .selected_text(format!("{:.0}%", self.preview_zoom * 100.0))
|
| 281 | .show_ui(ui, |ui| {
|
| 282 | for pct in [50, 75, 100, 125, 150, 200] {
|
| 283 | let z = pct as f32 / 100.0;
|
| 284 | ui.selectable_value(
|
| 285 | &mut self.preview_zoom,
|
| 286 | z,
|
| 287 | format!("{pct}%"),
|
| 288 | );
|
| 289 | }
|
| 290 | });
|
| 291 | });
|
| 292 | });
|
| 293 | ui.separator();
|
| 294 | let base_dir = self.file_path.as_ref().and_then(|p| p.parent());
|
| 295 | show_preview_panel(
|
| 296 | ui,
|
| 297 | ctx,
|
| 298 | &self.body,
|
| 299 | &self.metadata,
|
| 300 | base_dir,
|
| 301 | &self.dialog.param_inputs,
|
| 302 | self.preview_zoom,
|
| 303 | "print_only_preview_",
|
| 304 | );
|
| 305 | },
|
| 306 | );
|
| 307 |
|
| 308 | ui.separator();
|
| 309 |
|
| 310 | ui.allocate_ui_with_layout(
|
| 311 | egui::vec2(ui.available_width(), available.y),
|
| 312 | egui::Layout::top_down(egui::Align::Min),
|
| 313 | |ui| {
|
| 314 | action = self.dialog.show_ui(ui);
|
| 315 | },
|
| 316 | );
|
| 317 | });
|
| 318 | });
|
| 319 |
|
| 320 | match action {
|
| 321 | PrintDialogAction::Print => {
|
| 322 | if self
|
| 323 | .dialog
|
| 324 | .execute_print(&self.body, &self.metadata, self.file_path.as_ref())
|
| 325 | == PrintExecuteResult::Printed
|
| 326 | {
|
| 327 | self.request_exit();
|
| 328 | }
|
| 329 | }
|
| 330 | PrintDialogAction::None => {}
|
| 331 | }
|
| 332 | }
|
| 333 | }
|