BlueGit

timfa posprint

Files

posprint / src / main.rs

main.rs

main.rs 2.8 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::protocol_url::{self, is_posprint_url};
14
15fn 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
21fn 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(launch) => run_print_only_launch(launch),
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
65fn run_print_only_path(path: std::path::PathBuf) -> eframe::Result<()> {
66 run_print_only_app(print_only_app::PrintOnlyApp::new(path))
67}
68
69fn run_print_only_launch(launch: protocol_url::PosprintLaunch) -> eframe::Result<()> {
70 run_print_only_app(print_only_app::PrintOnlyApp::from_launch(launch))
71}
72
73fn run_print_only_error(message: String) -> eframe::Result<()> {
74 run_print_only_app(print_only_app::PrintOnlyApp::from_error(message))
75}
76
77fn run_print_only_app(app: print_only_app::PrintOnlyApp) -> eframe::Result<()> {
78 let mut viewport = eframe::egui::ViewportBuilder::default()
79 .with_inner_size([960.0, 620.0])
80 .with_min_inner_size([720.0, 420.0]);
81 if let Some(icon) = load_viewport_icon() {
82 viewport = viewport.with_icon(icon);
83 }
84
85 let options = eframe::NativeOptions {
86 viewport,
87 ..Default::default()
88 };
89 eframe::run_native(
90 "posprint",
91 options,
92 Box::new(move |_cc| Ok(Box::new(app) as Box<dyn eframe::App>)),
93 )
94}