| 1 | use std::collections::HashMap;
|
| 2 | use std::path::PathBuf;
|
| 3 |
|
| 4 | use eframe::egui;
|
| 5 |
|
| 6 | use posprint::dsl::{self, parse};
|
| 7 | use posprint::editor_assist::{
|
| 8 | at_directive_prefix, barcode_insert_options, directive_completions, image_insert_options,
|
| 9 | insert_at_cursor, qr_insert_options,
|
| 10 | };
|
| 11 | use posprint::pipeline;
|
| 12 | use posprint::io::{PosFile, PosMetadata, PreviewMode};
|
| 13 | use posprint::params::collect_parameter_names;
|
| 14 | use posprint::preview::{render_preview, PreviewLine, TextStyle};
|
| 15 | use posprint::print::{list_usb_serial_printers, print_bytes, SerialTransport, PrinterDevice};
|
| 16 | use posprint::HELP_MARKDOWN;
|
| 17 |
|
| 18 | #[derive(Clone, Copy, PartialEq, Eq)]
|
| 19 | enum Tab {
|
| 20 | Editor,
|
| 21 | Preview,
|
| 22 | Help,
|
| 23 | }
|
| 24 |
|
| 25 | pub struct PosPrintApp {
|
| 26 | tab: Tab,
|
| 27 | body: String,
|
| 28 | metadata: PosMetadata,
|
| 29 | file_path: Option<PathBuf>,
|
| 30 | dirty: bool,
|
| 31 | parse_error: Option<String>,
|
| 32 | status_message: Option<String>,
|
| 33 | last_params: HashMap<String, String>,
|
| 34 | show_print_dialog: bool,
|
| 35 | print_param_inputs: HashMap<String, String>,
|
| 36 | print_param_names: Vec<String>,
|
| 37 | printers: Vec<PrinterDevice>,
|
| 38 | selected_printer: Option<usize>,
|
| 39 | print_error: Option<String>,
|
| 40 | editor_cursor: usize,
|
| 41 | autocomplete: Vec<&'static str>,
|
| 42 | }
|
| 43 |
|
| 44 | impl Default for PosPrintApp {
|
| 45 | fn default() -> Self {
|
| 46 | Self {
|
| 47 | tab: Tab::Editor,
|
| 48 | body: default_template(),
|
| 49 | metadata: PosMetadata::default(),
|
| 50 | file_path: None,
|
| 51 | dirty: false,
|
| 52 | parse_error: None,
|
| 53 | status_message: None,
|
| 54 | last_params: HashMap::new(),
|
| 55 | show_print_dialog: false,
|
| 56 | print_param_inputs: HashMap::new(),
|
| 57 | print_param_names: Vec::new(),
|
| 58 | printers: Vec::new(),
|
| 59 | selected_printer: None,
|
| 60 | print_error: None,
|
| 61 | editor_cursor: 0,
|
| 62 | autocomplete: Vec::new(),
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | impl PosPrintApp {
|
| 68 | pub fn new() -> Self {
|
| 69 | Self::default()
|
| 70 | }
|
| 71 |
|
| 72 | fn title(&self) -> String {
|
| 73 | let name = self
|
| 74 | .file_path
|
| 75 | .as_ref()
|
| 76 | .and_then(|p| p.file_name())
|
| 77 | .map(|s| s.to_string_lossy().to_string())
|
| 78 | .or_else(|| self.metadata.title.clone())
|
| 79 | .unwrap_or_else(|| "Untitled".into());
|
| 80 | if self.dirty {
|
| 81 | format!("{name} * — posprint")
|
| 82 | } else {
|
| 83 | format!("{name} — posprint")
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | fn pos_file(&self) -> PosFile {
|
| 88 | PosFile {
|
| 89 | version: 1,
|
| 90 | metadata: self.metadata.clone(),
|
| 91 | body: self.body.clone(),
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | fn load_pos_file(&mut self, path: PathBuf, file: PosFile) {
|
| 96 | self.body = file.body;
|
| 97 | self.metadata = file.metadata;
|
| 98 | self.file_path = Some(path);
|
| 99 | self.dirty = false;
|
| 100 | self.parse_error = None;
|
| 101 | self.status_message = Some("File loaded".into());
|
| 102 | }
|
| 103 |
|
| 104 | fn validate_parse(&mut self) {
|
| 105 | self.parse_error = match parse(&self.body) {
|
| 106 | Ok(_) => None,
|
| 107 | Err(e) => Some(e.to_string()),
|
| 108 | };
|
| 109 | }
|
| 110 |
|
| 111 | fn open_print_dialog(&mut self) {
|
| 112 | self.print_error = None;
|
| 113 | match parse(&self.body) {
|
| 114 | Ok(doc) => {
|
| 115 | self.print_param_names = collect_parameter_names(&doc);
|
| 116 | self.print_param_inputs.clear();
|
| 117 | for name in &self.print_param_names {
|
| 118 | let value = self
|
| 119 | .last_params
|
| 120 | .get(name)
|
| 121 | .cloned()
|
| 122 | .unwrap_or_default();
|
| 123 | self.print_param_inputs.insert(name.clone(), value);
|
| 124 | }
|
| 125 | self.show_print_dialog = true;
|
| 126 | self.refresh_printers();
|
| 127 | }
|
| 128 | Err(e) => self.parse_error = Some(e.to_string()),
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | fn refresh_printers(&mut self) {
|
| 133 | match list_usb_serial_printers() {
|
| 134 | Ok(list) => {
|
| 135 | self.printers = list;
|
| 136 | if self.selected_printer.is_none() && !self.printers.is_empty() {
|
| 137 | self.selected_printer = Some(0);
|
| 138 | }
|
| 139 | }
|
| 140 | Err(e) => self.print_error = Some(e.to_string()),
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | fn execute_print(&mut self) {
|
| 145 | self.print_error = None;
|
| 146 | if parse(&self.body).is_err() {
|
| 147 | self.print_error = Some("Template has parse errors".into());
|
| 148 | return;
|
| 149 | }
|
| 150 |
|
| 151 | for name in &self.print_param_names {
|
| 152 | if !self.print_param_inputs.contains_key(name) {
|
| 153 | self.print_error = Some(format!("Missing value for parameter {name}"));
|
| 154 | return;
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | self.last_params = self.print_param_inputs.clone();
|
| 159 |
|
| 160 | let base_dir = self.file_path.as_ref().and_then(|p| p.parent());
|
| 161 | let file = self.pos_file();
|
| 162 | let bytes = match pipeline::encode_pos_file(&file, base_dir, &self.last_params) {
|
| 163 | Ok(b) => b,
|
| 164 | Err(e) => {
|
| 165 | self.print_error = Some(e.to_string());
|
| 166 | return;
|
| 167 | }
|
| 168 | };
|
| 169 |
|
| 170 | let idx = match self.selected_printer {
|
| 171 | Some(i) if i < self.printers.len() => i,
|
| 172 | _ => {
|
| 173 | self.print_error = Some("Select a printer".into());
|
| 174 | return;
|
| 175 | }
|
| 176 | };
|
| 177 |
|
| 178 | let port = self.printers[idx].port_name.clone();
|
| 179 | match SerialTransport::open(&port) {
|
| 180 | Ok(mut transport) => match print_bytes(&mut transport, &bytes) {
|
| 181 | Ok(()) => {
|
| 182 | self.status_message = Some(format!("Printed to {port}"));
|
| 183 | self.show_print_dialog = false;
|
| 184 | }
|
| 185 | Err(e) => self.print_error = Some(format!("{e:?}")),
|
| 186 | },
|
| 187 | Err(e) => self.print_error = Some(format!("{e:?}")),
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | impl eframe::App for PosPrintApp {
|
| 193 | fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
| 194 | self.validate_parse();
|
| 195 |
|
| 196 | egui::TopBottomPanel::top("menu").show(ctx, |ui| {
|
| 197 | ui.horizontal_wrapped(|ui| {
|
| 198 | if ui.button("New").clicked() {
|
| 199 | *self = Self::default();
|
| 200 | }
|
| 201 | if ui.button("Open").clicked() {
|
| 202 | if let Some(path) = rfd::FileDialog::new()
|
| 203 | .add_filter("POS template", &["pos"])
|
| 204 | .pick_file()
|
| 205 | {
|
| 206 | match PosFile::load_from_path(&path) {
|
| 207 | Ok(file) => self.load_pos_file(path, file),
|
| 208 | Err(e) => self.status_message = Some(format!("Open failed: {e:?}")),
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 | if ui.button("Save").clicked() {
|
| 213 | if let Some(path) = self.file_path.clone() {
|
| 214 | if let Err(e) = self.pos_file().save_to_path(&path) {
|
| 215 | self.status_message = Some(format!("Save failed: {e:?}"));
|
| 216 | } else {
|
| 217 | self.dirty = false;
|
| 218 | self.status_message = Some("Saved".into());
|
| 219 | }
|
| 220 | } else if ui.button("Save As").clicked() {
|
| 221 | // handled below when no path
|
| 222 | }
|
| 223 | }
|
| 224 | if ui.button("Save As").clicked() {
|
| 225 | if let Some(path) = rfd::FileDialog::new()
|
| 226 | .add_filter("POS template", &["pos"])
|
| 227 | .set_file_name("document.pos")
|
| 228 | .save_file()
|
| 229 | {
|
| 230 | if let Err(e) = self.pos_file().save_to_path(&path) {
|
| 231 | self.status_message = Some(format!("Save failed: {e:?}"));
|
| 232 | } else {
|
| 233 | self.file_path = Some(path);
|
| 234 | self.dirty = false;
|
| 235 | self.status_message = Some("Saved".into());
|
| 236 | }
|
| 237 | }
|
| 238 | }
|
| 239 | if ui.button("Print").clicked() {
|
| 240 | self.open_print_dialog();
|
| 241 | }
|
| 242 |
|
| 243 | ui.menu_button("Insert", |ui| {
|
| 244 | for opt in image_insert_options() {
|
| 245 | if ui.button(format!("Image — {}", opt.label)).clicked() {
|
| 246 | self.insert_snippet(&opt.snippet);
|
| 247 | ui.close_menu();
|
| 248 | }
|
| 249 | }
|
| 250 | ui.separator();
|
| 251 | ui.menu_button("Barcode", |ui| {
|
| 252 | for opt in barcode_insert_options() {
|
| 253 | if ui.button(&opt.label).clicked() {
|
| 254 | self.insert_snippet(&opt.snippet);
|
| 255 | ui.close_menu();
|
| 256 | }
|
| 257 | }
|
| 258 | });
|
| 259 | ui.menu_button("QR code", |ui| {
|
| 260 | for opt in qr_insert_options() {
|
| 261 | if ui.button(&opt.label).clicked() {
|
| 262 | self.insert_snippet(&opt.snippet);
|
| 263 | ui.close_menu();
|
| 264 | }
|
| 265 | }
|
| 266 | });
|
| 267 | ui.separator();
|
| 268 | if ui.button("@feed").clicked() {
|
| 269 | self.insert_snippet("@feed 3\n");
|
| 270 | ui.close_menu();
|
| 271 | }
|
| 272 | if ui.button("@cut").clicked() {
|
| 273 | self.insert_snippet("@cut\n");
|
| 274 | ui.close_menu();
|
| 275 | }
|
| 276 | });
|
| 277 | });
|
| 278 |
|
| 279 | ui.separator();
|
| 280 | ui.horizontal(|ui| {
|
| 281 | ui.label("Page width (mm):");
|
| 282 | let mut width = self.metadata.page_width_mm;
|
| 283 | if ui
|
| 284 | .add(egui::DragValue::new(&mut width).range(40.0..=120.0).speed(1.0))
|
| 285 | .changed()
|
| 286 | {
|
| 287 | self.metadata.page_width_mm = width;
|
| 288 | self.dirty = true;
|
| 289 | }
|
| 290 | ui.label("Preview:");
|
| 291 | egui::ComboBox::from_id_salt("preview_mode")
|
| 292 | .selected_text(match self.metadata.preview_mode {
|
| 293 | PreviewMode::Autoscale => "Auto-scale",
|
| 294 | PreviewMode::Fixed => "Fixed width",
|
| 295 | })
|
| 296 | .show_ui(ui, |ui| {
|
| 297 | if ui
|
| 298 | .selectable_value(
|
| 299 | &mut self.metadata.preview_mode,
|
| 300 | PreviewMode::Autoscale,
|
| 301 | "Auto-scale",
|
| 302 | )
|
| 303 | .changed()
|
| 304 | {
|
| 305 | self.dirty = true;
|
| 306 | }
|
| 307 | if ui
|
| 308 | .selectable_value(
|
| 309 | &mut self.metadata.preview_mode,
|
| 310 | PreviewMode::Fixed,
|
| 311 | "Fixed width",
|
| 312 | )
|
| 313 | .changed()
|
| 314 | {
|
| 315 | self.dirty = true;
|
| 316 | }
|
| 317 | });
|
| 318 | });
|
| 319 |
|
| 320 | if let Some(err) = &self.parse_error {
|
| 321 | ui.colored_label(egui::Color32::RED, err);
|
| 322 | }
|
| 323 | if let Some(msg) = &self.status_message {
|
| 324 | ui.label(msg);
|
| 325 | }
|
| 326 | });
|
| 327 |
|
| 328 | egui::TopBottomPanel::bottom("tabs").show(ctx, |ui| {
|
| 329 | ui.horizontal(|ui| {
|
| 330 | ui.selectable_value(&mut self.tab, Tab::Editor, "Editor");
|
| 331 | ui.selectable_value(&mut self.tab, Tab::Preview, "Preview");
|
| 332 | ui.selectable_value(&mut self.tab, Tab::Help, "Help");
|
| 333 | });
|
| 334 | });
|
| 335 |
|
| 336 | egui::CentralPanel::default().show(ctx, |ui| match self.tab {
|
| 337 | Tab::Editor => self.show_editor(ui),
|
| 338 | Tab::Preview => self.show_preview(ui),
|
| 339 | Tab::Help => self.show_help(ui),
|
| 340 | });
|
| 341 |
|
| 342 | if self.show_print_dialog {
|
| 343 | egui::Window::new("Print")
|
| 344 | .collapsible(false)
|
| 345 | .resizable(true)
|
| 346 | .show(ctx, |ui| self.show_print_dialog_ui(ui));
|
| 347 | }
|
| 348 |
|
| 349 | ctx.send_viewport_cmd(egui::ViewportCommand::Title(self.title()));
|
| 350 | }
|
| 351 | }
|
| 352 |
|
| 353 | impl PosPrintApp {
|
| 354 | fn insert_snippet(&mut self, snippet: &str) {
|
| 355 | let (body, cursor) = insert_at_cursor(&self.body, self.editor_cursor, snippet);
|
| 356 | self.body = body;
|
| 357 | self.editor_cursor = cursor;
|
| 358 | self.dirty = true;
|
| 359 | self.tab = Tab::Editor;
|
| 360 | }
|
| 361 |
|
| 362 | fn show_editor(&mut self, ui: &mut egui::Ui) {
|
| 363 | let output = egui::TextEdit::multiline(&mut self.body)
|
| 364 | .id_salt("main_editor")
|
| 365 | .desired_width(f32::INFINITY)
|
| 366 | .desired_rows(30)
|
| 367 | .font(egui::TextStyle::Monospace)
|
| 368 | .show(ui);
|
| 369 |
|
| 370 | self.editor_cursor = output
|
| 371 | .state
|
| 372 | .cursor
|
| 373 | .char_range()
|
| 374 | .map(|r| r.primary.index)
|
| 375 | .unwrap_or(self.body.len());
|
| 376 |
|
| 377 | if output.response.changed() {
|
| 378 | self.dirty = true;
|
| 379 | }
|
| 380 |
|
| 381 | if let Some((_start, prefix)) = at_directive_prefix(&self.body, self.editor_cursor) {
|
| 382 | self.autocomplete = directive_completions(&prefix);
|
| 383 | if !self.autocomplete.is_empty() {
|
| 384 | egui::Area::new(egui::Id::new("directive_ac"))
|
| 385 | .order(egui::Order::Foreground)
|
| 386 | .show(ui.ctx(), |ui| {
|
| 387 | ui.set_min_width(120.0);
|
| 388 | ui.label("Directive autocomplete:");
|
| 389 | for dir in &self.autocomplete {
|
| 390 | if ui.button(format!("@{dir}")).clicked() {
|
| 391 | let line_start = self.body[..self.editor_cursor]
|
| 392 | .rfind('\n')
|
| 393 | .map(|i| i + 1)
|
| 394 | .unwrap_or(0);
|
| 395 | let line_end = self.body[self.editor_cursor..]
|
| 396 | .find('\n')
|
| 397 | .map(|i| self.editor_cursor + i)
|
| 398 | .unwrap_or(self.body.len());
|
| 399 | let mut line = self.body[line_start..line_end].to_string();
|
| 400 | if let Some(at) = line.find('@') {
|
| 401 | line.replace_range(at + 1.., dir);
|
| 402 | self.body.replace_range(line_start..line_end, &line);
|
| 403 | self.editor_cursor = line_start + at + 1 + dir.len();
|
| 404 | self.dirty = true;
|
| 405 | }
|
| 406 | }
|
| 407 | }
|
| 408 | });
|
| 409 | }
|
| 410 | } else {
|
| 411 | self.autocomplete.clear();
|
| 412 | }
|
| 413 | }
|
| 414 |
|
| 415 | fn show_preview(&mut self, ui: &mut egui::Ui) {
|
| 416 | let Ok(doc) = parse(&self.body) else {
|
| 417 | ui.label("Fix parse errors to preview.");
|
| 418 | return;
|
| 419 | };
|
| 420 | let model = render_preview(&doc, &self.metadata);
|
| 421 | let chars = model.width_chars as f32;
|
| 422 | let scale = match self.metadata.preview_mode {
|
| 423 | PreviewMode::Autoscale => {
|
| 424 | let avail = ui.available_width().max(200.0);
|
| 425 | (avail / (chars * 8.0)).clamp(0.8, 3.0)
|
| 426 | }
|
| 427 | PreviewMode::Fixed => model.width_mm / 80.0,
|
| 428 | };
|
| 429 |
|
| 430 | egui::Frame::canvas(ui.style()).show(ui, |ui| {
|
| 431 | let width = chars * 8.0 * scale;
|
| 432 | let (_id, rect) = ui.allocate_space(egui::vec2(width, ui.available_height()));
|
| 433 | let painter = ui.painter_at(rect);
|
| 434 | painter.rect_filled(rect, 4.0, egui::Color32::WHITE);
|
| 435 | painter.rect_stroke(
|
| 436 | rect,
|
| 437 | 4.0,
|
| 438 | egui::Stroke::new(1.0_f32, egui::Color32::GRAY),
|
| 439 | egui::StrokeKind::Inside,
|
| 440 | );
|
| 441 |
|
| 442 | let mut y = rect.min.y + 8.0;
|
| 443 | let line_height = 18.0 * scale;
|
| 444 | for line in &model.lines {
|
| 445 | match line {
|
| 446 | PreviewLine::Text {
|
| 447 | content,
|
| 448 | align,
|
| 449 | style,
|
| 450 | } => {
|
| 451 | let font_id = match style {
|
| 452 | TextStyle::Heading1 => egui::FontId::proportional(22.0 * scale),
|
| 453 | TextStyle::Heading2 => egui::FontId::proportional(18.0 * scale),
|
| 454 | TextStyle::Bold => egui::FontId::proportional(16.0 * scale),
|
| 455 | TextStyle::Italic => egui::FontId::proportional(15.0 * scale),
|
| 456 | TextStyle::Code => egui::FontId::monospace(14.0 * scale),
|
| 457 | TextStyle::Normal => egui::FontId::monospace(14.0 * scale),
|
| 458 | };
|
| 459 | let galley = painter.layout(
|
| 460 | content.clone(),
|
| 461 | font_id,
|
| 462 | egui::Color32::BLACK,
|
| 463 | width,
|
| 464 | );
|
| 465 | let x = match align {
|
| 466 | dsl::Align::Left => rect.min.x + 8.0,
|
| 467 | dsl::Align::Center => {
|
| 468 | rect.min.x + (width - galley.size().x) / 2.0
|
| 469 | }
|
| 470 | dsl::Align::Right => rect.max.x - galley.size().x - 8.0,
|
| 471 | };
|
| 472 | painter.galley(egui::pos2(x, y), galley, egui::Color32::BLACK);
|
| 473 | y += line_height;
|
| 474 | }
|
| 475 | PreviewLine::Rule => {
|
| 476 | painter.line_segment(
|
| 477 | [
|
| 478 | egui::pos2(rect.min.x + 8.0, y),
|
| 479 | egui::pos2(rect.max.x - 8.0, y),
|
| 480 | ],
|
| 481 | egui::Stroke::new(1.0_f32, egui::Color32::DARK_GRAY),
|
| 482 | );
|
| 483 | y += line_height;
|
| 484 | }
|
| 485 | PreviewLine::Barcode { symbology, data, .. } => {
|
| 486 | let text = format!("[barcode {symbology}: {data}]");
|
| 487 | let galley = painter.layout(
|
| 488 | text,
|
| 489 | egui::FontId::monospace(14.0 * scale),
|
| 490 | egui::Color32::BLACK,
|
| 491 | width,
|
| 492 | );
|
| 493 | painter.galley(egui::pos2(rect.min.x + 8.0, y), galley, egui::Color32::BLACK);
|
| 494 | y += line_height * 1.5;
|
| 495 | }
|
| 496 | PreviewLine::Qr { data, .. } => {
|
| 497 | let text = format!("[qr: {data}]");
|
| 498 | let galley = painter.layout(
|
| 499 | text,
|
| 500 | egui::FontId::monospace(14.0 * scale),
|
| 501 | egui::Color32::BLACK,
|
| 502 | width,
|
| 503 | );
|
| 504 | painter.galley(egui::pos2(rect.min.x + 8.0, y), galley, egui::Color32::BLACK);
|
| 505 | y += line_height * 1.5;
|
| 506 | }
|
| 507 | PreviewLine::Image { label, .. } => {
|
| 508 | let galley = painter.layout(
|
| 509 | label.clone(),
|
| 510 | egui::FontId::monospace(14.0 * scale),
|
| 511 | egui::Color32::DARK_BLUE,
|
| 512 | width,
|
| 513 | );
|
| 514 | painter.galley(egui::pos2(rect.min.x + 8.0, y), galley, egui::Color32::BLACK);
|
| 515 | y += line_height * 1.5;
|
| 516 | }
|
| 517 | PreviewLine::Spacer(n) => {
|
| 518 | y += line_height * (*n as f32);
|
| 519 | }
|
| 520 | }
|
| 521 | }
|
| 522 | });
|
| 523 | }
|
| 524 |
|
| 525 | fn show_help(&mut self, ui: &mut egui::Ui) {
|
| 526 | egui::ScrollArea::vertical().show(ui, |ui| {
|
| 527 | ui.add(
|
| 528 | egui::Label::new(HELP_MARKDOWN)
|
| 529 | .wrap()
|
| 530 | .selectable(true),
|
| 531 | );
|
| 532 | });
|
| 533 | }
|
| 534 |
|
| 535 | fn show_print_dialog_ui(&mut self, ui: &mut egui::Ui) {
|
| 536 | ui.heading("Parameters");
|
| 537 | if self.print_param_names.is_empty() {
|
| 538 | ui.label("No parameters in this template.");
|
| 539 | } else {
|
| 540 | for name in self.print_param_names.clone() {
|
| 541 | ui.horizontal(|ui| {
|
| 542 | ui.label(format!("{name}:"));
|
| 543 | let entry = self
|
| 544 | .print_param_inputs
|
| 545 | .entry(name)
|
| 546 | .or_insert_with(String::new);
|
| 547 | ui.text_edit_singleline(entry);
|
| 548 | });
|
| 549 | }
|
| 550 | }
|
| 551 |
|
| 552 | ui.separator();
|
| 553 | ui.heading("USB printer");
|
| 554 | if ui.button("Refresh").clicked() {
|
| 555 | self.refresh_printers();
|
| 556 | }
|
| 557 | if self.printers.is_empty() {
|
| 558 | ui.label("No serial ports found. Connect a USB printer and refresh.");
|
| 559 | } else {
|
| 560 | for (i, printer) in self.printers.iter().enumerate() {
|
| 561 | ui.selectable_value(&mut self.selected_printer, Some(i), &printer.display_name);
|
| 562 | }
|
| 563 | }
|
| 564 |
|
| 565 | if let Some(err) = &self.print_error {
|
| 566 | ui.colored_label(egui::Color32::RED, err);
|
| 567 | }
|
| 568 |
|
| 569 | ui.horizontal(|ui| {
|
| 570 | if ui.button("Cancel").clicked() {
|
| 571 | self.show_print_dialog = false;
|
| 572 | }
|
| 573 | if ui.button("Print").clicked() {
|
| 574 | self.execute_print();
|
| 575 | }
|
| 576 | });
|
| 577 | }
|
| 578 | }
|
| 579 |
|
| 580 | fn default_template() -> String {
|
| 581 | r#"# ${StoreName}
|
| 582 | :center:
|
| 583 |
|
| 584 | Receipt: ${DocumentNumber}
|
| 585 | Date: ${Date}
|
| 586 |
|
| 587 | ---
|
| 588 | :right:
|
| 589 | **TOTAL: ${GrandTotal}**
|
| 590 |
|
| 591 | @barcode CODE128 ${DocumentNumber}
|
| 592 | @feed 2
|
| 593 | @qr https://example.com/r/${DocumentNumber}
|
| 594 |
|
| 595 | Thank you!
|
| 596 | @feed 4
|
| 597 | @cut
|
| 598 | "#
|
| 599 | .to_string()
|
| 600 | }
|
| 601 |
|
| 602 | #[cfg(test)]
|
| 603 | mod tests {
|
| 604 | use super::*;
|
| 605 |
|
| 606 | #[test]
|
| 607 | fn default_template_parses() {
|
| 608 | let app = PosPrintApp::default();
|
| 609 | assert!(parse(&app.body).is_ok());
|
| 610 | }
|
| 611 |
|
| 612 | #[test]
|
| 613 | fn collect_params_from_default_template() {
|
| 614 | let app = PosPrintApp::default();
|
| 615 | let doc = parse(&app.body).unwrap();
|
| 616 | let names = collect_parameter_names(&doc);
|
| 617 | assert!(names.contains(&"StoreName".to_string()));
|
| 618 | assert!(names.contains(&"DocumentNumber".to_string()));
|
| 619 | }
|
| 620 | }
|