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