BlueGit

timfa posprint

Files

posprint / src / main.rs

main.rs

main.rs 3.3 KiB Download Raw
1#![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
21/// Attach to the parent console when launched from cmd/PowerShell so CLI output works
22/// with `windows_subsystem = "windows"` (no black console flash for GUI / protocol launches).
23#[cfg(windows)]
24fn attach_parent_console_if_present() {
25 #[link(name = "kernel32")]
26 extern "system" {
27 fn AttachConsole(dw_process_id: u32) -> i32;
28 }
29 const ATTACH_PARENT_PROCESS: u32 = 0xFFFF_FFFF;
30 unsafe {
31 let _ = AttachConsole(ATTACH_PARENT_PROCESS);
32 }
33}
34
35fn main() -> eframe::Result<()> {
36 #[cfg(windows)]
37 attach_parent_console_if_present();
38
39 shell_integration::ensure_shell_integration();
40
41 // Protocol URLs must be handled before clap: browsers pass the full URL as argv.
42 if let Some(url) = std::env::args().nth(1).filter(|a| is_posprint_url(a)) {
43 return match protocol_url::parse_posprint_url(&url) {
44 Ok(launch) => run_print_only_launch(launch),
45 Err(e) => run_print_only_error(format!("Invalid posprint:// URL: {e}")),
46 };
47 }
48
49 let cli = posprint::cli::Cli::parse();
50 if cli.command.is_some() {
51 std::process::exit(posprint::cli::run(cli));
52 }
53
54 if cli.print {
55 let Some(path) = cli.file else {
56 eprintln!("--print requires a .pos file path");
57 std::process::exit(1);
58 };
59 return run_print_only_path(path);
60 }
61
62 let open_file = cli.file;
63
64 let mut viewport = eframe::egui::ViewportBuilder::default().with_inner_size([1200.0, 750.0]);
65 if let Some(icon) = load_viewport_icon() {
66 viewport = viewport.with_icon(icon);
67 }
68
69 let options = eframe::NativeOptions {
70 viewport,
71 ..Default::default()
72 };
73 eframe::run_native(
74 "posprint",
75 options,
76 Box::new(move |_cc| {
77 Ok(Box::new(app::PosPrintApp::new_with_file(open_file)) as Box<dyn eframe::App>)
78 }),
79 )
80}
81
82fn run_print_only_path(path: std::path::PathBuf) -> eframe::Result<()> {
83 run_print_only_app(print_only_app::PrintOnlyApp::new(path))
84}
85
86fn run_print_only_launch(launch: protocol_url::PosprintLaunch) -> eframe::Result<()> {
87 run_print_only_app(print_only_app::PrintOnlyApp::from_launch(launch))
88}
89
90fn run_print_only_error(message: String) -> eframe::Result<()> {
91 run_print_only_app(print_only_app::PrintOnlyApp::from_error(message))
92}
93
94fn run_print_only_app(app: print_only_app::PrintOnlyApp) -> eframe::Result<()> {
95 let mut viewport = eframe::egui::ViewportBuilder::default()
96 .with_inner_size([960.0, 620.0])
97 .with_min_inner_size([720.0, 420.0]);
98 if let Some(icon) = load_viewport_icon() {
99 viewport = viewport.with_icon(icon);
100 }
101
102 let options = eframe::NativeOptions {
103 viewport,
104 ..Default::default()
105 };
106 eframe::run_native(
107 "posprint",
108 options,
109 Box::new(move |_cc| Ok(Box::new(app) as Box<dyn eframe::App>)),
110 )
111}