Desktop: Handle another instance is already running (#2938)

Handle another instance is already running
This commit is contained in:
Timon 2025-07-25 21:38:54 +00:00 committed by GitHub
parent 4fec24893e
commit 91156d295c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 5 deletions

View File

@ -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<Setup> {
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,
}

View File

@ -29,6 +29,10 @@ impl<H: CefEventHandler + Clone> 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()
}

View File

@ -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);
}
};