| 1 | use std::path::PathBuf;
|
| 2 |
|
| 3 | use eframe::egui;
|
| 4 |
|
| 5 | use posprint::dsl::{self, parse};
|
| 6 | use posprint::editor_assist::{
|
| 7 | apply_directive_completion, at_directive_prefix, barcode_insert_options,
|
| 8 | complete_directive, date_time_insert_options, directive_ghost_suffix, image_insert_options,
|
| 9 | insert_at_cursor, parameter_insert_options, qr_insert_options,
|
| 10 | };
|
| 11 | use posprint::help_view;
|
| 12 | use posprint::io::{PosFile, PosMetadata};
|
| 13 | use posprint::preview::{
|
| 14 | preview_char_width_px, preview_content_width_px, preview_font_size_px,
|
| 15 | preview_line_height_estimate, preview_line_spacing_px, preview_text_advance_px,
|
| 16 | preview_visual_scale, render_preview, PreviewLine, TextStyle,
|
| 17 | };
|
| 18 | use posprint::undo::TextUndo;
|
| 19 | use posprint::HELP_MARKDOWN;
|
| 20 |
|
| 21 | use crate::print_dialog::{PrintDialog, PrintDialogAction, PrintExecuteResult};
|
| 22 |
|
| 23 | #[derive(Clone, Copy, PartialEq, Eq)]
|
| 24 | enum ViewMode {
|
| 25 | Design,
|
| 26 | Help,
|
| 27 | }
|
| 28 |
|
| 29 | pub struct PosPrintApp {
|
| 30 | view_mode: ViewMode,
|
| 31 | body: String,
|
| 32 | metadata: PosMetadata,
|
| 33 | file_path: Option<PathBuf>,
|
| 34 | dirty: bool,
|
| 35 | parse_error: Option<String>,
|
| 36 | action_error: Option<String>,
|
| 37 | show_print_dialog: bool,
|
| 38 | print_dialog: PrintDialog,
|
| 39 | editor_cursor: usize,
|
| 40 | undo: TextUndo,
|
| 41 | preview_zoom: f32,
|
| 42 | }
|
| 43 |
|
| 44 | impl Default for PosPrintApp {
|
| 45 | fn default() -> Self {
|
| 46 | Self {
|
| 47 | view_mode: ViewMode::Design,
|
| 48 | body: default_template(),
|
| 49 | metadata: PosMetadata::default(),
|
| 50 | file_path: None,
|
| 51 | dirty: false,
|
| 52 | parse_error: None,
|
| 53 | action_error: None,
|
| 54 | show_print_dialog: false,
|
| 55 | print_dialog: PrintDialog::default(),
|
| 56 | editor_cursor: 0,
|
| 57 | undo: TextUndo::default(),
|
| 58 | preview_zoom: 1.0,
|
| 59 | }
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | impl PosPrintApp {
|
| 64 | pub fn new_with_file(path: Option<PathBuf>) -> Self {
|
| 65 | let mut app = Self::default();
|
| 66 | if let Some(path) = path {
|
| 67 | match PosFile::load_from_path(&path) {
|
| 68 | Ok(file) => app.load_pos_file(path, file),
|
| 69 | Err(e) => app.action_error = Some(format!("Open failed: {e:?}")),
|
| 70 | }
|
| 71 | }
|
| 72 | app
|
| 73 | }
|
| 74 |
|
| 75 | fn title(&self) -> String {
|
| 76 | let name = self
|
| 77 | .file_path
|
| 78 | .as_ref()
|
| 79 | .and_then(|p| p.file_name())
|
| 80 | .map(|s| s.to_string_lossy().to_string())
|
| 81 | .or_else(|| self.metadata.title.clone())
|
| 82 | .unwrap_or_else(|| "Untitled".into());
|
| 83 | if self.dirty {
|
| 84 | format!("* {name} — posprint")
|
| 85 | } else {
|
| 86 | format!("{name} — posprint")
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | fn pos_file(&self) -> PosFile {
|
| 91 | PosFile {
|
| 92 | version: 1,
|
| 93 | metadata: self.metadata.clone(),
|
| 94 | body: self.body.clone(),
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | fn load_pos_file(&mut self, path: PathBuf, file: PosFile) {
|
| 99 | self.body = file.body;
|
| 100 | self.metadata = file.metadata;
|
| 101 | self.file_path = Some(path);
|
| 102 | self.dirty = false;
|
| 103 | self.parse_error = None;
|
| 104 | self.action_error = None;
|
| 105 | self.undo = TextUndo::default();
|
| 106 | }
|
| 107 |
|
| 108 | fn new_document(&mut self) {
|
| 109 | *self = Self::default();
|
| 110 | }
|
| 111 |
|
| 112 | fn open_file_dialog(&mut self) {
|
| 113 | if let Some(path) = rfd::FileDialog::new()
|
| 114 | .add_filter("POS template", &["pos"])
|
| 115 | .pick_file()
|
| 116 | {
|
| 117 | match PosFile::load_from_path(&path) {
|
| 118 | Ok(file) => self.load_pos_file(path, file),
|
| 119 | Err(e) => self.action_error = Some(format!("Open failed: {e:?}")),
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | fn save(&mut self) {
|
| 125 | if let Some(path) = self.file_path.clone() {
|
| 126 | self.save_to_path(path);
|
| 127 | } else {
|
| 128 | self.save_as();
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | fn save_as(&mut self) {
|
| 133 | if let Some(path) = rfd::FileDialog::new()
|
| 134 | .add_filter("POS template", &["pos"])
|
| 135 | .set_file_name("document.pos")
|
| 136 | .save_file()
|
| 137 | {
|
| 138 | self.save_to_path(path);
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | fn save_to_path(&mut self, path: PathBuf) {
|
| 143 | if let Err(e) = self.pos_file().save_to_path(&path) {
|
| 144 | self.action_error = Some(format!("Save failed: {e:?}"));
|
| 145 | } else {
|
| 146 | self.file_path = Some(path);
|
| 147 | self.dirty = false;
|
| 148 | self.action_error = None;
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | fn validate_parse(&mut self) {
|
| 153 | self.parse_error = match parse(&self.body) {
|
| 154 | Ok(_) => None,
|
| 155 | Err(e) => Some(e.to_string()),
|
| 156 | };
|
| 157 | }
|
| 158 |
|
| 159 | fn handle_shortcuts(&mut self, ctx: &egui::Context) {
|
| 160 | let mut new_doc = false;
|
| 161 | let mut open = false;
|
| 162 | let mut save = false;
|
| 163 | let mut undo = false;
|
| 164 | let mut redo = false;
|
| 165 |
|
| 166 | ctx.input(|i| {
|
| 167 | if i.modifiers.command {
|
| 168 | if i.key_pressed(egui::Key::S) {
|
| 169 | save = true;
|
| 170 | }
|
| 171 | if i.key_pressed(egui::Key::N) {
|
| 172 | new_doc = true;
|
| 173 | }
|
| 174 | if i.key_pressed(egui::Key::O) {
|
| 175 | open = true;
|
| 176 | }
|
| 177 | if i.key_pressed(egui::Key::Z) {
|
| 178 | if i.modifiers.shift {
|
| 179 | redo = true;
|
| 180 | } else {
|
| 181 | undo = true;
|
| 182 | }
|
| 183 | }
|
| 184 | if i.modifiers.shift && i.key_pressed(egui::Key::Y) {
|
| 185 | redo = true;
|
| 186 | }
|
| 187 | }
|
| 188 | });
|
| 189 |
|
| 190 | if new_doc {
|
| 191 | self.new_document();
|
| 192 | }
|
| 193 | if open {
|
| 194 | self.open_file_dialog();
|
| 195 | }
|
| 196 | if save {
|
| 197 | self.save();
|
| 198 | }
|
| 199 | if undo {
|
| 200 | if let Some(prev) = self.undo.undo(&self.body) {
|
| 201 | self.body = prev;
|
| 202 | self.dirty = true;
|
| 203 | }
|
| 204 | }
|
| 205 | if redo {
|
| 206 | if let Some(next) = self.undo.redo(&self.body) {
|
| 207 | self.body = next;
|
| 208 | self.dirty = true;
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | fn apply_print_dialog_params_to_preview(&mut self) {
|
| 214 | self.print_dialog.apply_inputs_to_last_params();
|
| 215 | }
|
| 216 |
|
| 217 | fn close_print_dialog(&mut self) {
|
| 218 | self.apply_print_dialog_params_to_preview();
|
| 219 | self.show_print_dialog = false;
|
| 220 | }
|
| 221 |
|
| 222 | fn open_print_dialog(&mut self) {
|
| 223 | match self.print_dialog.prepare(&self.body) {
|
| 224 | Ok(()) => self.show_print_dialog = true,
|
| 225 | Err(e) => self.parse_error = Some(e),
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | fn execute_print(&mut self) {
|
| 230 | if self.print_dialog.execute_print(
|
| 231 | &self.body,
|
| 232 | &self.metadata,
|
| 233 | self.file_path.as_ref(),
|
| 234 | ) == PrintExecuteResult::Printed
|
| 235 | {
|
| 236 | self.show_print_dialog = false;
|
| 237 | }
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | impl eframe::App for PosPrintApp {
|
| 242 | fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
| 243 | self.validate_parse();
|
| 244 | if !self.show_print_dialog {
|
| 245 | self.handle_shortcuts(ctx);
|
| 246 | }
|
| 247 | self.print_dialog.poll_printer_discovery(ctx);
|
| 248 |
|
| 249 | let main_enabled = !self.show_print_dialog;
|
| 250 |
|
| 251 | egui::TopBottomPanel::top("menu").show(ctx, |ui| {
|
| 252 | ui.add_enabled_ui(main_enabled, |ui| {
|
| 253 | ui.horizontal_wrapped(|ui| {
|
| 254 | if ui.button("New").clicked() {
|
| 255 | self.new_document();
|
| 256 | }
|
| 257 | if ui.button("Open").clicked() {
|
| 258 | self.open_file_dialog();
|
| 259 | }
|
| 260 | if ui.button("Save").clicked() {
|
| 261 | self.save();
|
| 262 | }
|
| 263 | if ui.button("Save As").clicked() {
|
| 264 | self.save_as();
|
| 265 | }
|
| 266 | if ui.button("Print").clicked() {
|
| 267 | self.open_print_dialog();
|
| 268 | }
|
| 269 |
|
| 270 | ui.menu_button("Insert", |ui| {
|
| 271 | for opt in image_insert_options() {
|
| 272 | if ui.button(format!("Image — {}", opt.label)).clicked() {
|
| 273 | self.insert_snippet(&opt.snippet);
|
| 274 | ui.close_menu();
|
| 275 | }
|
| 276 | }
|
| 277 | ui.separator();
|
| 278 | ui.menu_button("Barcode", |ui| {
|
| 279 | for opt in barcode_insert_options() {
|
| 280 | if ui.button(&opt.label).clicked() {
|
| 281 | self.insert_snippet(&opt.snippet);
|
| 282 | ui.close_menu();
|
| 283 | }
|
| 284 | }
|
| 285 | });
|
| 286 | ui.menu_button("QR code", |ui| {
|
| 287 | for opt in qr_insert_options() {
|
| 288 | if ui.button(&opt.label).clicked() {
|
| 289 | self.insert_snippet(&opt.snippet);
|
| 290 | ui.close_menu();
|
| 291 | }
|
| 292 | }
|
| 293 | });
|
| 294 | ui.menu_button("Date / time", |ui| {
|
| 295 | for opt in date_time_insert_options() {
|
| 296 | if ui.button(&opt.label).clicked() {
|
| 297 | self.insert_snippet(&opt.snippet);
|
| 298 | ui.close_menu();
|
| 299 | }
|
| 300 | }
|
| 301 | });
|
| 302 | ui.menu_button("Parameters", |ui| {
|
| 303 | for opt in parameter_insert_options(&self.body) {
|
| 304 | if ui.button(&opt.label).clicked() {
|
| 305 | self.insert_snippet(&opt.snippet);
|
| 306 | ui.close_menu();
|
| 307 | }
|
| 308 | }
|
| 309 | });
|
| 310 | ui.separator();
|
| 311 | if ui.button("@feed").clicked() {
|
| 312 | self.insert_snippet("@feed 3\n");
|
| 313 | ui.close_menu();
|
| 314 | }
|
| 315 | if ui.button("@cut").clicked() {
|
| 316 | self.insert_snippet("@cut\n");
|
| 317 | ui.close_menu();
|
| 318 | }
|
| 319 | });
|
| 320 | });
|
| 321 |
|
| 322 | ui.separator();
|
| 323 | ui.horizontal(|ui| {
|
| 324 | ui.label("Page width (mm):");
|
| 325 | let mut width = self.metadata.page_width_mm;
|
| 326 | if ui
|
| 327 | .add(egui::DragValue::new(&mut width).range(40.0..=120.0).speed(1.0))
|
| 328 | .changed()
|
| 329 | {
|
| 330 | self.metadata.page_width_mm = width;
|
| 331 | self.dirty = true;
|
| 332 | }
|
| 333 | });
|
| 334 | });
|
| 335 |
|
| 336 | if let Some(err) = &self.parse_error {
|
| 337 | ui.colored_label(egui::Color32::RED, err);
|
| 338 | }
|
| 339 | if let Some(err) = &self.action_error {
|
| 340 | ui.colored_label(egui::Color32::RED, err);
|
| 341 | }
|
| 342 | });
|
| 343 |
|
| 344 | egui::TopBottomPanel::bottom("tabs").show(ctx, |ui| {
|
| 345 | ui.add_enabled_ui(main_enabled, |ui| {
|
| 346 | ui.horizontal(|ui| {
|
| 347 | ui.selectable_value(&mut self.view_mode, ViewMode::Design, "Design");
|
| 348 | ui.selectable_value(&mut self.view_mode, ViewMode::Help, "Help");
|
| 349 | if let Ok(doc) = parse(&self.body) {
|
| 350 | if !dsl::document_ends_with_cut(&doc) {
|
| 351 | ui.colored_label(
|
| 352 | egui::Color32::RED,
|
| 353 | "Warning: No @cut directive at end of file.",
|
| 354 | );
|
| 355 | }
|
| 356 | }
|
| 357 | });
|
| 358 | });
|
| 359 | });
|
| 360 |
|
| 361 | match self.view_mode {
|
| 362 | ViewMode::Design => {
|
| 363 | egui::SidePanel::right("preview_panel")
|
| 364 | .resizable(main_enabled)
|
| 365 | .min_width(260.0)
|
| 366 | .default_width(380.0)
|
| 367 | .show(ctx, |ui| {
|
| 368 | ui.add_enabled_ui(main_enabled, |ui| {
|
| 369 | ui.heading("Preview");
|
| 370 | ui.horizontal(|ui| {
|
| 371 | ui.label(format!(
|
| 372 | "Page width: {:.0} mm",
|
| 373 | self.metadata.page_width_mm
|
| 374 | ));
|
| 375 | ui.separator();
|
| 376 | ui.label("Zoom:");
|
| 377 | ui.add(
|
| 378 | egui::Slider::new(&mut self.preview_zoom, 0.25..=4.0)
|
| 379 | .logarithmic(true)
|
| 380 | .show_value(true),
|
| 381 | );
|
| 382 | if ui.button("100%").clicked() {
|
| 383 | self.preview_zoom = 1.0;
|
| 384 | }
|
| 385 | });
|
| 386 | ui.separator();
|
| 387 | self.show_preview(ui, ctx);
|
| 388 | });
|
| 389 | });
|
| 390 |
|
| 391 | egui::CentralPanel::default().show(ctx, |ui| {
|
| 392 | ui.add_enabled_ui(main_enabled, |ui| {
|
| 393 | ui.heading("Editor");
|
| 394 | self.show_editor(ui);
|
| 395 | });
|
| 396 | });
|
| 397 | }
|
| 398 | ViewMode::Help => {
|
| 399 | egui::CentralPanel::default().show(ctx, |ui| {
|
| 400 | ui.add_enabled_ui(main_enabled, |ui| {
|
| 401 | help_view::show_help(ui, HELP_MARKDOWN);
|
| 402 | });
|
| 403 | });
|
| 404 | }
|
| 405 | }
|
| 406 |
|
| 407 | if self.show_print_dialog {
|
| 408 | let mut open = true;
|
| 409 | egui::Window::new("Print")
|
| 410 | .collapsible(false)
|
| 411 | .resizable(true)
|
| 412 | .open(&mut open)
|
| 413 | .show(ctx, |ui| {
|
| 414 | match self.print_dialog.show_ui(ui, "Cancel") {
|
| 415 | PrintDialogAction::Cancel => self.close_print_dialog(),
|
| 416 | PrintDialogAction::Print => self.execute_print(),
|
| 417 | PrintDialogAction::None => {}
|
| 418 | }
|
| 419 | });
|
| 420 | if !open {
|
| 421 | self.apply_print_dialog_params_to_preview();
|
| 422 | }
|
| 423 | self.show_print_dialog = open;
|
| 424 | }
|
| 425 |
|
| 426 | ctx.send_viewport_cmd(egui::ViewportCommand::Title(self.title()));
|
| 427 | }
|
| 428 | }
|
| 429 |
|
| 430 | impl PosPrintApp {
|
| 431 | fn insert_snippet(&mut self, snippet: &str) {
|
| 432 | let before = self.body.clone();
|
| 433 | let (body, cursor) = insert_at_cursor(&self.body, self.editor_cursor, snippet);
|
| 434 | self.body = body;
|
| 435 | self.editor_cursor = cursor;
|
| 436 | self.undo.record(before);
|
| 437 | self.dirty = true;
|
| 438 | self.view_mode = ViewMode::Design;
|
| 439 | }
|
| 440 |
|
| 441 | fn show_editor(&mut self, ui: &mut egui::Ui) {
|
| 442 | let before = self.body.clone();
|
| 443 | let cursor_before = self.editor_cursor;
|
| 444 |
|
| 445 | // Autocomplete steals focus every frame; keep it off while the print dialog is open.
|
| 446 | let autocomplete_enabled = !self.show_print_dialog;
|
| 447 | let directive_ctx = autocomplete_enabled
|
| 448 | .then(|| at_directive_prefix(&self.body, cursor_before))
|
| 449 | .flatten();
|
| 450 | let autocomplete_active = directive_ctx.as_ref().is_some_and(|(_, prefix)| {
|
| 451 | complete_directive(prefix).is_some() || directive_ghost_suffix(prefix).is_some()
|
| 452 | });
|
| 453 |
|
| 454 | let mut tab_completion: Option<(usize, String)> = None;
|
| 455 | if autocomplete_active {
|
| 456 | if let Some((at_index, prefix)) = directive_ctx.clone() {
|
| 457 | ui.input_mut(|input| {
|
| 458 | if input.key_pressed(egui::Key::Tab) && !input.modifiers.any() {
|
| 459 | tab_completion = Some((at_index, prefix));
|
| 460 | input.consume_key(egui::Modifiers::NONE, egui::Key::Tab);
|
| 461 | }
|
| 462 | });
|
| 463 | }
|
| 464 | }
|
| 465 |
|
| 466 | let font_id = egui::TextStyle::Monospace.resolve(ui.style());
|
| 467 | let row_height = ui.fonts(|f| f.row_height(&font_id)).max(1.0);
|
| 468 | let rows = ((ui.available_height() / row_height).floor() as usize).clamp(8, 500);
|
| 469 |
|
| 470 | let output = egui::TextEdit::multiline(&mut self.body)
|
| 471 | .id_salt("main_editor")
|
| 472 | .desired_width(f32::INFINITY)
|
| 473 | .desired_rows(rows)
|
| 474 | .font(egui::TextStyle::Monospace)
|
| 475 | .lock_focus(autocomplete_active)
|
| 476 | .show(ui);
|
| 477 |
|
| 478 | if autocomplete_active {
|
| 479 | output.response.request_focus();
|
| 480 | }
|
| 481 |
|
| 482 | self.editor_cursor = output
|
| 483 | .cursor_range
|
| 484 | .as_ref()
|
| 485 | .map(|r| r.primary.ccursor.index)
|
| 486 | .or_else(|| {
|
| 487 | output
|
| 488 | .state
|
| 489 | .cursor
|
| 490 | .char_range()
|
| 491 | .map(|r| r.primary.index)
|
| 492 | })
|
| 493 | .unwrap_or(self.body.len());
|
| 494 |
|
| 495 | if let Some((at_index, prefix)) = tab_completion {
|
| 496 | if let Some(completed) = complete_directive(&prefix) {
|
| 497 | let undo_before = self.body.clone();
|
| 498 | self.editor_cursor =
|
| 499 | apply_directive_completion(&mut self.body, at_index, self.editor_cursor, completed);
|
| 500 | self.undo.record(undo_before);
|
| 501 | self.dirty = true;
|
| 502 | }
|
| 503 | } else if output.response.changed() {
|
| 504 | self.undo.record(before);
|
| 505 | self.dirty = true;
|
| 506 | }
|
| 507 |
|
| 508 | if autocomplete_enabled {
|
| 509 | if let Some((_at_index, prefix)) = at_directive_prefix(&self.body, self.editor_cursor) {
|
| 510 | if let Some(suffix) = directive_ghost_suffix(&prefix) {
|
| 511 | if let Some(cursor_range) = &output.cursor_range {
|
| 512 | let font_id = egui::TextStyle::Monospace.resolve(ui.style());
|
| 513 | let pos = output.galley.pos_from_cursor(&cursor_range.primary);
|
| 514 | let ghost_pos = output.galley_pos + pos.min.to_vec2();
|
| 515 | let ghost_color = ui.visuals().weak_text_color();
|
| 516 | ui.painter().text(
|
| 517 | ghost_pos,
|
| 518 | egui::Align2::LEFT_TOP,
|
| 519 | suffix,
|
| 520 | font_id,
|
| 521 | ghost_color,
|
| 522 | );
|
| 523 | }
|
| 524 | }
|
| 525 | }
|
| 526 | }
|
| 527 | }
|
| 528 |
|
| 529 | fn show_preview(&mut self, ui: &mut egui::Ui, ctx: &egui::Context) {
|
| 530 | let Ok(doc) = parse(&self.body) else {
|
| 531 | ui.label("Fix parse errors to preview.");
|
| 532 | return;
|
| 533 | };
|
| 534 |
|
| 535 | let base_dir = self.file_path.as_ref().and_then(|p| p.parent());
|
| 536 | let model = render_preview(
|
| 537 | &doc,
|
| 538 | &self.metadata,
|
| 539 | base_dir,
|
| 540 | &self.print_dialog.last_params,
|
| 541 | );
|
| 542 | let visual_scale = preview_visual_scale(self.preview_zoom);
|
| 543 | let content_width = preview_content_width_px(&self.metadata) * visual_scale;
|
| 544 | let char_width = preview_char_width_px(&self.metadata, self.preview_zoom);
|
| 545 | let line_spacing = preview_line_spacing_px(self.preview_zoom);
|
| 546 | // No horizontal padding: wrap columns must match the full paper width.
|
| 547 | let pad_y = 4.0 * visual_scale;
|
| 548 |
|
| 549 | egui::ScrollArea::both()
|
| 550 | .auto_shrink([false; 2])
|
| 551 | .show(ui, |ui| {
|
| 552 | egui::Frame::canvas(ui.style()).show(ui, |ui| {
|
| 553 | let mut total_height = pad_y * 2.0;
|
| 554 | for line in &model.lines {
|
| 555 | total_height +=
|
| 556 | preview_line_height_estimate(line, &self.metadata, self.preview_zoom);
|
| 557 | }
|
| 558 | total_height = total_height.max(100.0);
|
| 559 |
|
| 560 | let (_id, rect) = ui.allocate_space(egui::vec2(content_width, total_height));
|
| 561 | let painter = ui.painter_at(rect);
|
| 562 | painter.rect_filled(rect, 4.0, egui::Color32::WHITE);
|
| 563 | painter.rect_stroke(
|
| 564 | rect,
|
| 565 | 4.0,
|
| 566 | egui::Stroke::new(1.0_f32, egui::Color32::GRAY),
|
| 567 | egui::StrokeKind::Inside,
|
| 568 | );
|
| 569 |
|
| 570 | let mut y = rect.min.y + pad_y;
|
| 571 | let inner_width = content_width;
|
| 572 |
|
| 573 | for (idx, line) in model.lines.iter().enumerate() {
|
| 574 | match line {
|
| 575 | PreviewLine::Text {
|
| 576 | content,
|
| 577 | align,
|
| 578 | style,
|
| 579 | } => {
|
| 580 | let font_id = preview_text_font(*style, char_width);
|
| 581 | let galley = painter.layout_no_wrap(
|
| 582 | content.clone(),
|
| 583 | font_id,
|
| 584 | egui::Color32::BLACK,
|
| 585 | );
|
| 586 | let x = align_x(*align, rect.min.x, inner_width, galley.size().x);
|
| 587 | painter.galley(egui::pos2(x, y), galley, egui::Color32::BLACK);
|
| 588 | y += preview_text_advance_px(*style, self.preview_zoom);
|
| 589 | }
|
| 590 | PreviewLine::Rule => {
|
| 591 | painter.line_segment(
|
| 592 | [
|
| 593 | egui::pos2(rect.min.x, y),
|
| 594 | egui::pos2(rect.max.x, y),
|
| 595 | ],
|
| 596 | egui::Stroke::new(1.0_f32, egui::Color32::DARK_GRAY),
|
| 597 | );
|
| 598 | y += preview_line_spacing_px(self.preview_zoom);
|
| 599 | }
|
| 600 | PreviewLine::Graphic { image, align } => {
|
| 601 | let tex = gray_to_texture(
|
| 602 | ctx,
|
| 603 | &format!("preview_graphic_{idx}"),
|
| 604 | image,
|
| 605 | );
|
| 606 | let size = egui::vec2(
|
| 607 | tex.size()[0] as f32 * visual_scale,
|
| 608 | tex.size()[1] as f32 * visual_scale,
|
| 609 | );
|
| 610 | let x = align_x(*align, rect.min.x, inner_width, size.x);
|
| 611 | painter.image(
|
| 612 | tex.id(),
|
| 613 | egui::Rect::from_min_size(egui::pos2(x, y), size),
|
| 614 | egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)),
|
| 615 | egui::Color32::WHITE,
|
| 616 | );
|
| 617 | y += size.y;
|
| 618 | }
|
| 619 | PreviewLine::Error { message, align } => {
|
| 620 | let galley = painter.layout(
|
| 621 | message.clone(),
|
| 622 | egui::FontId::proportional(12.0 * visual_scale),
|
| 623 | egui::Color32::RED,
|
| 624 | inner_width,
|
| 625 | );
|
| 626 | let x = align_x(*align, rect.min.x, inner_width, galley.size().x);
|
| 627 | painter.galley(egui::pos2(x, y), galley, egui::Color32::RED);
|
| 628 | y += preview_line_spacing_px(self.preview_zoom);
|
| 629 | }
|
| 630 | PreviewLine::Spacer(n) => {
|
| 631 | y += line_spacing * (*n as f32);
|
| 632 | }
|
| 633 | }
|
| 634 | }
|
| 635 | });
|
| 636 | });
|
| 637 | }
|
| 638 | }
|
| 639 |
|
| 640 | fn preview_text_font(style: TextStyle, char_width: f32) -> egui::FontId {
|
| 641 | let normal = preview_font_size_px(char_width);
|
| 642 | match style {
|
| 643 | // GS ! 0x11 — double width and double height
|
| 644 | TextStyle::Heading1 => egui::FontId::monospace(normal * 2.0),
|
| 645 | TextStyle::Heading2 | TextStyle::Normal => egui::FontId::monospace(normal),
|
| 646 | }
|
| 647 | }
|
| 648 |
|
| 649 | fn align_x(align: dsl::Align, left: f32, inner_width: f32, item_width: f32) -> f32 {
|
| 650 | match align {
|
| 651 | dsl::Align::Left => left,
|
| 652 | dsl::Align::Center => left + (inner_width - item_width) / 2.0,
|
| 653 | dsl::Align::Right => left + inner_width - item_width,
|
| 654 | }
|
| 655 | }
|
| 656 |
|
| 657 | fn gray_to_texture(
|
| 658 | ctx: &egui::Context,
|
| 659 | id: &str,
|
| 660 | image: &image::GrayImage,
|
| 661 | ) -> egui::TextureHandle {
|
| 662 | let (w, h) = image.dimensions();
|
| 663 | let pixels: Vec<egui::Color32> = image
|
| 664 | .pixels()
|
| 665 | .map(|p| egui::Color32::from_gray(p.0[0]))
|
| 666 | .collect();
|
| 667 | let color_image = egui::ColorImage {
|
| 668 | size: [w as usize, h as usize],
|
| 669 | pixels,
|
| 670 | };
|
| 671 | ctx.load_texture(id, color_image, egui::TextureOptions::NEAREST)
|
| 672 | }
|
| 673 |
|
| 674 | fn default_template() -> String {
|
| 675 | String::new()
|
| 676 | }
|
| 677 |
|
| 678 | #[cfg(test)]
|
| 679 | mod tests {
|
| 680 | use super::*;
|
| 681 |
|
| 682 | #[test]
|
| 683 | fn default_template_is_empty() {
|
| 684 | let app = PosPrintApp::default();
|
| 685 | assert!(app.body.is_empty());
|
| 686 | assert!(parse(&app.body).is_ok());
|
| 687 | }
|
| 688 |
|
| 689 | #[test]
|
| 690 | fn title_shows_asterisk_before_name_when_dirty() {
|
| 691 | let mut app = PosPrintApp::default();
|
| 692 | app.dirty = true;
|
| 693 | assert!(app.title().starts_with("* "));
|
| 694 | }
|
| 695 | }
|