BlueGit

timfa posprint

Files

posprint / src / print_only_app.rs

print_only_app.rs

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