From 91156d295c9a24d5d2d2f7072e10fb481401eff7 Mon Sep 17 00:00:00 2001 From: Timon Date: Fri, 25 Jul 2025 21:38:54 +0000 Subject: [PATCH] Desktop: Handle another instance is already running (#2938) Handle another instance is already running --- desktop/src/cef/context.rs | 12 +++++++++--- desktop/src/cef/internal/browser_process_handler.rs | 4 ++++ desktop/src/main.rs | 8 ++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/desktop/src/cef/context.rs b/desktop/src/cef/context.rs index 04ebffb4..a26aa933 100644 --- a/desktop/src/cef/context.rs +++ b/desktop/src/cef/context.rs @@ -1,4 +1,4 @@ -use cef::sys::CEF_API_VERSION_LAST; +use cef::sys::{CEF_API_VERSION_LAST, cef_resultcode_t}; use cef::{App, BrowserSettings, Client, DictionaryValue, ImplBrowser, ImplBrowserHost, ImplCommandLine, RenderHandler, RequestContext, WindowInfo, browser_host_create_browser_sync, initialize}; use cef::{Browser, CefString, Settings, api_hash, args::Args, execute_process}; use thiserror::Error; @@ -74,7 +74,11 @@ impl Context { let result = initialize(Some(self.args.as_main_args()), Some(&settings), Some(&mut cef_app), std::ptr::null_mut()); if result != 1 { - return Err(InitError::InitializationFailed); + let cef_exit_code = cef::get_exit_code() as u32; + if cef_exit_code == cef_resultcode_t::CEF_RESULT_CODE_NORMAL_EXIT_PROCESS_NOTIFIED as u32 { + return Err(InitError::AlreadyRunning); + } + return Err(InitError::InitializationFailed(cef_exit_code)); } let render_handler = RenderHandlerImpl::new(event_handler.clone()); @@ -146,5 +150,7 @@ pub(crate) enum SetupError { #[derive(Error, Debug)] pub(crate) enum InitError { #[error("initialization failed")] - InitializationFailed, + InitializationFailed(u32), + #[error("Another instance is already running")] + AlreadyRunning, } diff --git a/desktop/src/cef/internal/browser_process_handler.rs b/desktop/src/cef/internal/browser_process_handler.rs index 5179dd3b..4edacc5c 100644 --- a/desktop/src/cef/internal/browser_process_handler.rs +++ b/desktop/src/cef/internal/browser_process_handler.rs @@ -29,6 +29,10 @@ impl ImplBrowserProcessHandler for BrowserProcessHan self.event_handler.schedule_cef_message_loop_work(Instant::now() + Duration::from_millis(delay_ms as u64)); } + fn on_already_running_app_relaunch(&self, _command_line: Option<&mut cef::CommandLine>, _current_directory: Option<&CefString>) -> ::std::os::raw::c_int { + 1 // Return 1 to prevent default behavior of opening a empty browser window + } + fn get_raw(&self) -> *mut _cef_browser_process_handler_t { self.object.cast() } diff --git a/desktop/src/main.rs b/desktop/src/main.rs index c53848f8..6ce35179 100644 --- a/desktop/src/main.rs +++ b/desktop/src/main.rs @@ -41,8 +41,12 @@ fn main() { let wgpu_context = futures::executor::block_on(WgpuContext::new()); let cef_context = match cef_context.init(cef::CefHandler::new(window_size_receiver, event_loop.create_proxy(), wgpu_context.clone())) { Ok(c) => c, - Err(cef::InitError::InitializationFailed) => { - tracing::error!("Cef initialization failed"); + Err(cef::InitError::AlreadyRunning) => { + tracing::error!("Another instance is already running, Exiting."); + exit(0); + } + Err(cef::InitError::InitializationFailed(code)) => { + tracing::error!("Cef initialization failed with code: {code}"); exit(1); } };