BlueGit

timfa posprint

Files

posprint / src / main.rs

main.rs

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