Skip to content

Commit 91156d2

Browse files
Desktop: Handle another instance is already running (#2938)
Handle another instance is already running
1 parent 4fec248 commit 91156d2

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

desktop/src/cef/context.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cef::sys::CEF_API_VERSION_LAST;
1+
use cef::sys::{CEF_API_VERSION_LAST, cef_resultcode_t};
22
use cef::{App, BrowserSettings, Client, DictionaryValue, ImplBrowser, ImplBrowserHost, ImplCommandLine, RenderHandler, RequestContext, WindowInfo, browser_host_create_browser_sync, initialize};
33
use cef::{Browser, CefString, Settings, api_hash, args::Args, execute_process};
44
use thiserror::Error;
@@ -74,7 +74,11 @@ impl Context<Setup> {
7474

7575
let result = initialize(Some(self.args.as_main_args()), Some(&settings), Some(&mut cef_app), std::ptr::null_mut());
7676
if result != 1 {
77-
return Err(InitError::InitializationFailed);
77+
let cef_exit_code = cef::get_exit_code() as u32;
78+
if cef_exit_code == cef_resultcode_t::CEF_RESULT_CODE_NORMAL_EXIT_PROCESS_NOTIFIED as u32 {
79+
return Err(InitError::AlreadyRunning);
80+
}
81+
return Err(InitError::InitializationFailed(cef_exit_code));
7882
}
7983

8084
let render_handler = RenderHandlerImpl::new(event_handler.clone());
@@ -146,5 +150,7 @@ pub(crate) enum SetupError {
146150
#[derive(Error, Debug)]
147151
pub(crate) enum InitError {
148152
#[error("initialization failed")]
149-
InitializationFailed,
153+
InitializationFailed(u32),
154+
#[error("Another instance is already running")]
155+
AlreadyRunning,
150156
}

desktop/src/cef/internal/browser_process_handler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ impl<H: CefEventHandler + Clone> ImplBrowserProcessHandler for BrowserProcessHan
2929
self.event_handler.schedule_cef_message_loop_work(Instant::now() + Duration::from_millis(delay_ms as u64));
3030
}
3131

32+
fn on_already_running_app_relaunch(&self, _command_line: Option<&mut cef::CommandLine>, _current_directory: Option<&CefString>) -> ::std::os::raw::c_int {
33+
1 // Return 1 to prevent default behavior of opening a empty browser window
34+
}
35+
3236
fn get_raw(&self) -> *mut _cef_browser_process_handler_t {
3337
self.object.cast()
3438
}

desktop/src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ fn main() {
4141
let wgpu_context = futures::executor::block_on(WgpuContext::new());
4242
let cef_context = match cef_context.init(cef::CefHandler::new(window_size_receiver, event_loop.create_proxy(), wgpu_context.clone())) {
4343
Ok(c) => c,
44-
Err(cef::InitError::InitializationFailed) => {
45-
tracing::error!("Cef initialization failed");
44+
Err(cef::InitError::AlreadyRunning) => {
45+
tracing::error!("Another instance is already running, Exiting.");
46+
exit(0);
47+
}
48+
Err(cef::InitError::InitializationFailed(code)) => {
49+
tracing::error!("Cef initialization failed with code: {code}");
4650
exit(1);
4751
}
4852
};

0 commit comments

Comments
 (0)