#![windows_subsystem = "windows"] mod app; mod preview_panel; mod print_dialog; mod print_only_app; mod shell_integration; use std::sync::Arc; use clap::Parser; use posprint::protocol_url::{self, is_posprint_url}; fn load_viewport_icon() -> Option> { eframe::icon_data::from_png_bytes(include_bytes!("../pos-logo.png")) .ok() .map(Arc::new) } /// Attach to the parent console when launched from cmd/PowerShell so CLI output works /// with `windows_subsystem = "windows"` (no black console flash for GUI / protocol launches). #[cfg(windows)] fn attach_parent_console_if_present() { #[link(name = "kernel32")] extern "system" { fn AttachConsole(dw_process_id: u32) -> i32; } const ATTACH_PARENT_PROCESS: u32 = 0xFFFF_FFFF; unsafe { let _ = AttachConsole(ATTACH_PARENT_PROCESS); } } fn main() -> eframe::Result<()> { #[cfg(windows)] attach_parent_console_if_present(); shell_integration::ensure_shell_integration(); // Protocol URLs must be handled before clap: browsers pass the full URL as argv. if let Some(url) = std::env::args().nth(1).filter(|a| is_posprint_url(a)) { return match protocol_url::parse_posprint_url(&url) { Ok(launch) => run_print_only_launch(launch), Err(e) => run_print_only_error(format!("Invalid posprint:// URL: {e}")), }; } let cli = posprint::cli::Cli::parse(); if cli.command.is_some() { std::process::exit(posprint::cli::run(cli)); } if cli.print { let Some(path) = cli.file else { eprintln!("--print requires a .pos file path"); std::process::exit(1); }; return run_print_only_path(path); } let open_file = cli.file; let mut viewport = eframe::egui::ViewportBuilder::default().with_inner_size([1200.0, 750.0]); if let Some(icon) = load_viewport_icon() { viewport = viewport.with_icon(icon); } let options = eframe::NativeOptions { viewport, ..Default::default() }; eframe::run_native( "posprint", options, Box::new(move |_cc| { Ok(Box::new(app::PosPrintApp::new_with_file(open_file)) as Box) }), ) } fn run_print_only_path(path: std::path::PathBuf) -> eframe::Result<()> { run_print_only_app(print_only_app::PrintOnlyApp::new(path)) } fn run_print_only_launch(launch: protocol_url::PosprintLaunch) -> eframe::Result<()> { run_print_only_app(print_only_app::PrintOnlyApp::from_launch(launch)) } fn run_print_only_error(message: String) -> eframe::Result<()> { run_print_only_app(print_only_app::PrintOnlyApp::from_error(message)) } fn run_print_only_app(app: print_only_app::PrintOnlyApp) -> eframe::Result<()> { let mut viewport = eframe::egui::ViewportBuilder::default() .with_inner_size([960.0, 620.0]) .with_min_inner_size([720.0, 420.0]); if let Some(icon) = load_viewport_icon() { viewport = viewport.with_icon(icon); } let options = eframe::NativeOptions { viewport, ..Default::default() }; eframe::run_native( "posprint", options, Box::new(move |_cc| Ok(Box::new(app) as Box)), ) }