|
|
|
@@ -333,6 +333,8 @@ fn main() -> eframe::Result {
|
|
|
|
|
// window.
|
|
|
|
|
// Absolute for this instance too: links relative to a document resolve against its folder,
|
|
|
|
|
// never the working directory.
|
|
|
|
|
let startup_time = std::time::Instant::now();
|
|
|
|
|
startup_stage(startup_time, "claiming application instance");
|
|
|
|
|
let files = single_instance::absolute(&files);
|
|
|
|
|
let instance = match single_instance::claim(&files, may_hand_off, combine, launch_profile::current().root()) {
|
|
|
|
|
single_instance::Claim::HandedOff => return Ok(()),
|
|
|
|
@@ -360,6 +362,7 @@ fn main() -> eframe::Result {
|
|
|
|
|
// unless `PDFCRAFT_TITLEBAR` overrides them. The store is read here and opened again by the
|
|
|
|
|
// app (each window attempt opens its own: the OpenGL retry runs the creator again).
|
|
|
|
|
// The interface language is read the same way, for the fonts the runtime installs with.
|
|
|
|
|
startup_stage(startup_time, "reading startup preferences");
|
|
|
|
|
let (title_bar, saved_lang) = {
|
|
|
|
|
let settings = pdfcraft_ui_egui::settings::open();
|
|
|
|
|
let saved = settings.get();
|
|
|
|
@@ -371,7 +374,7 @@ fn main() -> eframe::Result {
|
|
|
|
|
let frame = craft::window::policy::decide_for(craft::window::Prefix::new("PDFCRAFT"), craft::window::policy::Backend::from_env(), title_bar);
|
|
|
|
|
let lang = startup_lang(saved_lang.as_deref(), &options);
|
|
|
|
|
let choice = renderer_choice(std::env::var("PDFCRAFT_RENDERER").ok().as_deref());
|
|
|
|
|
let launch = Launch { files, options, control, create_images, combine, frame, lang, two_paged, nightly_watch: nightly_watch.clone() };
|
|
|
|
|
let launch = Launch { startup_time, files, options, control, create_images, combine, frame, lang, two_paged, nightly_watch: nightly_watch.clone() };
|
|
|
|
|
// Finder, Open With and the Dock deliver files as Apple events, not arguments; catch the one
|
|
|
|
|
// that launched us as well as later ones. Lives until the event loop returns.
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
@@ -379,6 +382,7 @@ fn main() -> eframe::Result {
|
|
|
|
|
// Set once the app is created, which is after the renderer has started.
|
|
|
|
|
let started = Rc::new(Cell::new(false));
|
|
|
|
|
let first = if choice == RendererChoice::Gl { eframe::Renderer::Glow } else { eframe::Renderer::Wgpu };
|
|
|
|
|
startup_stage(startup_time, if choice == RendererChoice::Gl { "initializing OpenGL" } else { "initializing wgpu" });
|
|
|
|
|
let result = eframe::run_native(
|
|
|
|
|
app_title(),
|
|
|
|
|
native_options(launch.integrated(), first),
|
|
|
|
@@ -397,6 +401,7 @@ fn main() -> eframe::Result {
|
|
|
|
|
// Old or unusual GPUs and drivers (#461, #435, #392) can't give wgpu a device; OpenGL
|
|
|
|
|
// usually still works there, so that's better than quitting.
|
|
|
|
|
log::error!("the GPU renderer (wgpu) didn't start: {e}. Starting with OpenGL instead; set PDFCRAFT_RENDERER=gl to skip wgpu.");
|
|
|
|
|
startup_stage(startup_time, "retrying with OpenGL");
|
|
|
|
|
eframe::run_native(
|
|
|
|
|
app_title(),
|
|
|
|
|
native_options(launch.integrated(), eframe::Renderer::Glow),
|
|
|
|
@@ -415,6 +420,10 @@ fn main() -> eframe::Result {
|
|
|
|
|
final_result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn startup_stage(start: std::time::Instant, stage: &str) {
|
|
|
|
|
log::info!(target: "pdfcraft::startup", "+{} ms: {stage}", start.elapsed().as_millis());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Which renderer to start with, from `PDFCRAFT_RENDERER`.
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
|
enum RendererChoice {
|
|
|
|
@@ -527,6 +536,7 @@ fn native_options(integrated: bool, renderer: eframe::Renderer) -> eframe::Nativ
|
|
|
|
|
/// What the command line asked for, kept so a second run (with OpenGL) can start the same way.
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
struct Launch {
|
|
|
|
|
startup_time: std::time::Instant,
|
|
|
|
|
files: Vec<String>,
|
|
|
|
|
options: Vec<(String, String)>,
|
|
|
|
|
/// The control server's port and token, when `--control` asked for one.
|
|
|
|
@@ -626,9 +636,10 @@ fn app_creator<'a>(
|
|
|
|
|
instance: Option<&'a single_instance::Server>,
|
|
|
|
|
#[cfg(target_os = "macos")] apple_events: &'a apple_events::AppleEvents,
|
|
|
|
|
) -> eframe::AppCreator<'a> {
|
|
|
|
|
let Launch { files, options, control, create_images, combine, frame, lang, two_paged, nightly_watch } = launch;
|
|
|
|
|
let Launch { startup_time, files, options, control, create_images, combine, frame, lang, two_paged, nightly_watch } = launch;
|
|
|
|
|
Box::new(move |cc| {
|
|
|
|
|
started.set(true);
|
|
|
|
|
startup_stage(startup_time, "renderer ready, installing fonts and runtime");
|
|
|
|
|
install_runtime(cc, frame, lang)?;
|
|
|
|
|
let ctx = cc.egui_ctx.clone();
|
|
|
|
|
let wake = craft::host::Wake::new(move || ctx.request_repaint());
|
|
|
|
@@ -642,6 +653,7 @@ fn app_creator<'a>(
|
|
|
|
|
// drains them into the intake.
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
let apple_events_poll = apple_events.connect(intake.sender(), &cc.egui_ctx);
|
|
|
|
|
startup_stage(startup_time, "initializing application and recovery");
|
|
|
|
|
let mut app = PdfCraftApp::with_intake(&cc.egui_ctx, intake);
|
|
|
|
|
let exit_watch = nightly_watch.clone();
|
|
|
|
|
app.at_exit = Some(Box::new(move || nightly_session::finish_once(&exit_watch, true)));
|
|
|
|
@@ -653,6 +665,7 @@ fn app_creator<'a>(
|
|
|
|
|
app.units = pdfcraft_ui_egui::units::Unit::system();
|
|
|
|
|
// The system's reopen-windows choice (macOS) starts the preference; a saved choice replaces it.
|
|
|
|
|
app.reopen_last_session = reopen::system_default();
|
|
|
|
|
startup_stage(startup_time, "applying preferences");
|
|
|
|
|
let settings = pdfcraft_ui_egui::settings::open();
|
|
|
|
|
app.apply_settings(settings.get());
|
|
|
|
|
app.attach_settings(settings);
|
|
|
|
@@ -698,6 +711,7 @@ fn app_creator<'a>(
|
|
|
|
|
if let Some(warning) = &pdfcraft_ui_egui::settings::resolution().warning {
|
|
|
|
|
app.notify(warning.clone());
|
|
|
|
|
}
|
|
|
|
|
startup_stage(startup_time, "restoring session and opening requested documents");
|
|
|
|
|
let defer_layout = two_paged && !files.is_empty() && !create_images;
|
|
|
|
|
if create_images {
|
|
|
|
|
if let Err(e) = app.begin_image_import_paths(&cc.egui_ctx, &files) {
|
|
|
|
@@ -718,6 +732,8 @@ fn app_creator<'a>(
|
|
|
|
|
log::warn!("--{k} {v}: {e}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
startup_stage(startup_time, "entering UI event loop");
|
|
|
|
|
app.startup_time = Some(startup_time);
|
|
|
|
|
Ok(Box::new(app))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
@@ -786,6 +802,8 @@ fn configure_gpu(native: &mut eframe::NativeOptions) {
|
|
|
|
|
// no display names its GPU, as over Remote Desktop; `pick_adapter` then ranks by kind,
|
|
|
|
|
// most frugal first, as wgpu's own low-power choice does.
|
|
|
|
|
if !displays.is_empty() || compositor.is_some() || cfg!(target_os = "windows") {
|
|
|
|
|
let explicit_renderer = renderer_choice(std::env::var("PDFCRAFT_RENDERER").ok().as_deref()) == RendererChoice::Wgpu;
|
|
|
|
|
let backend_chosen = explicit_renderer || std::env::var_os("WGPU_BACKEND").is_some();
|
|
|
|
|
setup.native_adapter_selector = Some(std::sync::Arc::new(move |adapters, surface| {
|
|
|
|
|
let usable: Vec<&eframe::wgpu::Adapter> = adapters.iter().filter(|a| surface.is_none_or(|s| a.is_surface_supported(s))).collect();
|
|
|
|
|
let infos: Vec<(u32, u32, eframe::wgpu::DeviceType, eframe::wgpu::Backend)> = usable
|
|
|
|
@@ -797,7 +815,7 @@ fn configure_gpu(native: &mut eframe::NativeOptions) {
|
|
|
|
|
.collect();
|
|
|
|
|
let picked = pick_adapter(&infos, &displays, compositor).and_then(|i| usable.get(i)).map(|a| (*a).clone());
|
|
|
|
|
if let Some(a) = &picked
|
|
|
|
|
&& !wgpu_draws_with(a.get_info().backend, cfg!(target_os = "windows"), std::env::var_os("WGPU_BACKEND").is_some())
|
|
|
|
|
&& !wgpu_draws_with(a.get_info().backend, cfg!(target_os = "windows"), backend_chosen)
|
|
|
|
|
{
|
|
|
|
|
log::warn!(
|
|
|
|
|
"wgpu found no Direct3D 12 adapter for {}, only its OpenGL backend: drawing with eframe's OpenGL renderer instead",
|
|
|
|
@@ -830,7 +848,7 @@ fn configure_gpu(native: &mut eframe::NativeOptions) {
|
|
|
|
|
/// Bridge, and there that backend fails in `Surface::configure` a few seconds after the window
|
|
|
|
|
/// opens: too late for the start-up retry with OpenGL, so the app closed (#519, #4134, #5553).
|
|
|
|
|
/// eframe's own OpenGL renderer works on the same GPUs, so wgpu declines and that retry takes over.
|
|
|
|
|
/// A backend asked for with `WGPU_BACKEND` (`backend_chosen`) is kept.
|
|
|
|
|
/// An explicit `PDFCRAFT_RENDERER=wgpu` or `WGPU_BACKEND` selection (`backend_chosen`) is kept.
|
|
|
|
|
fn wgpu_draws_with(backend: eframe::wgpu::Backend, windows: bool, backend_chosen: bool) -> bool {
|
|
|
|
|
!(windows && backend == eframe::wgpu::Backend::Gl && !backend_chosen)
|
|
|
|
|
}
|
|
|
|
|