| 1 | #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
| 2 |
|
| 3 | mod app;
|
| 4 | mod print_dialog;
|
| 5 | mod print_only_app;
|
| 6 | mod shell_integration;
|
| 7 |
|
| 8 | use std::sync::Arc;
|
| 9 |
|
| 10 | use clap::Parser;
|
| 11 |
|
| 12 | use posprint::io::PosFile;
|
| 13 | use posprint::protocol_url::{self, is_posprint_url};
|
| 14 |
|
| 15 | fn load_viewport_icon() -> Option<Arc<eframe::egui::IconData>> {
|
| 16 | eframe::icon_data::from_png_bytes(include_bytes!("../pos-logo.png"))
|
| 17 | .ok()
|
| 18 | .map(Arc::new)
|
| 19 | }
|
| 20 |
|
| 21 | fn main() -> eframe::Result<()> {
|
| 22 | shell_integration::ensure_shell_integration();
|
| 23 |
|
| 24 | // Protocol URLs must be handled before clap: browsers pass the full URL as argv.
|
| 25 | if let Some(url) = std::env::args().nth(1).filter(|a| is_posprint_url(a)) {
|
| 26 | return match protocol_url::parse_posprint_url(&url) {
|
| 27 | Ok(file) => run_print_only_pos_file(file),
|
| 28 | Err(e) => run_print_only_error(format!("Invalid posprint:// URL: {e}")),
|
| 29 | };
|
| 30 | }
|
| 31 |
|
| 32 | let cli = posprint::cli::Cli::parse();
|
| 33 | if cli.command.is_some() {
|
| 34 | std::process::exit(posprint::cli::run(cli));
|
| 35 | }
|
| 36 |
|
| 37 | if cli.print {
|
| 38 | let Some(path) = cli.file else {
|
| 39 | eprintln!("--print requires a .pos file path");
|
| 40 | std::process::exit(1);
|
| 41 | };
|
| 42 | return run_print_only_path(path);
|
| 43 | }
|
| 44 |
|
| 45 | let open_file = cli.file;
|
| 46 |
|
| 47 | let mut viewport = eframe::egui::ViewportBuilder::default().with_inner_size([1200.0, 750.0]);
|
| 48 | if let Some(icon) = load_viewport_icon() {
|
| 49 | viewport = viewport.with_icon(icon);
|
| 50 | }
|
| 51 |
|
| 52 | let options = eframe::NativeOptions {
|
| 53 | viewport,
|
| 54 | ..Default::default()
|
| 55 | };
|
| 56 | eframe::run_native(
|
| 57 | "posprint",
|
| 58 | options,
|
| 59 | Box::new(move |_cc| {
|
| 60 | Ok(Box::new(app::PosPrintApp::new_with_file(open_file)) as Box<dyn eframe::App>)
|
| 61 | }),
|
| 62 | )
|
| 63 | }
|
| 64 |
|
| 65 | fn run_print_only_path(path: std::path::PathBuf) -> eframe::Result<()> {
|
| 66 | run_print_only_app(print_only_app::PrintOnlyApp::new(path))
|
| 67 | }
|
| 68 |
|
| 69 | fn run_print_only_pos_file(file: PosFile) -> eframe::Result<()> {
|
| 70 | let label = file
|
| 71 | .metadata
|
| 72 | .title
|
| 73 | .clone()
|
| 74 | .unwrap_or_else(|| "posprint://".into());
|
| 75 | run_print_only_app(print_only_app::PrintOnlyApp::from_pos_file(file, label))
|
| 76 | }
|
| 77 |
|
| 78 | fn run_print_only_error(message: String) -> eframe::Result<()> {
|
| 79 | run_print_only_app(print_only_app::PrintOnlyApp::from_error(message))
|
| 80 | }
|
| 81 |
|
| 82 | fn run_print_only_app(app: print_only_app::PrintOnlyApp) -> eframe::Result<()> {
|
| 83 | let mut viewport = eframe::egui::ViewportBuilder::default()
|
| 84 | .with_inner_size([440.0, 420.0])
|
| 85 | .with_min_inner_size([380.0, 320.0]);
|
| 86 | if let Some(icon) = load_viewport_icon() {
|
| 87 | viewport = viewport.with_icon(icon);
|
| 88 | }
|
| 89 |
|
| 90 | let options = eframe::NativeOptions {
|
| 91 | viewport,
|
| 92 | ..Default::default()
|
| 93 | };
|
| 94 | eframe::run_native(
|
| 95 | "posprint",
|
| 96 | options,
|
| 97 | Box::new(move |_cc| Ok(Box::new(app) as Box<dyn eframe::App>)),
|
| 98 | )
|
| 99 | }
|