Skip to content

Commit c1ed1a3

Browse files
committed
feat: add tauri::Builder::cef_command_line_args method
1 parent 0a54cb8 commit c1ed1a3

File tree

7 files changed

+77
-17
lines changed

7 files changed

+77
-17
lines changed

crates/tauri-runtime-cef/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ edition.workspace = true
1010
rust-version.workspace = true
1111

1212
[dependencies]
13-
tauri-runtime = { version = "2.9.1", path = "../tauri-runtime" }
13+
tauri-runtime = { version = "2.9.1", path = "../tauri-runtime", features = [
14+
"cef",
15+
] }
1416
tauri-utils = { version = "2.8.0", path = "../tauri-utils", features = [
1517
"html-manipulation",
1618
] }

crates/tauri-runtime-cef/src/cef_impl.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ wrap_app! {
251251
pub struct TauriApp<T: UserEvent> {
252252
context: Context<T>,
253253
custom_schemes: Vec<String>,
254+
command_line_args: Vec<(String, Option<String>)>,
254255
}
255256

256257
impl App {
@@ -270,6 +271,25 @@ wrap_app! {
270271
}
271272
}
272273
}
274+
275+
fn on_before_command_line_processing(
276+
&self,
277+
_process_type: Option<&CefString>,
278+
command_line: Option<&mut CommandLine>,
279+
) {
280+
if let Some(command_line) = command_line {
281+
for (arg, value) in &self.command_line_args {
282+
if let Some(value) = value {
283+
command_line.append_switch_with_value(
284+
Some(&CefString::from(arg.as_str())),
285+
Some(&CefString::from(value.as_str())),
286+
);
287+
} else {
288+
command_line.append_switch(Some(&CefString::from(arg.as_str())));
289+
}
290+
}
291+
}
292+
}
273293
}
274294
}
275295

@@ -667,7 +687,9 @@ wrap_window_delegate! {
667687
.unwrap_or(1.0);
668688
let mut min_w: i32 = 0;
669689
let mut min_h: i32 = 0;
670-
let attributes = self.attributes.borrow();
690+
let Ok(attributes) = self.attributes.try_borrow() else {
691+
return cef::Size { width: 0, height: 0 };
692+
};
671693
if let Some(min_size) = attributes.min_inner_size {
672694
let logical = min_size.to_logical::<u32>(scale);
673695
min_w = min_w.max(logical.width as i32);
@@ -699,7 +721,10 @@ wrap_window_delegate! {
699721
.unwrap_or(1.0);
700722
let mut max_w: Option<i32> = None;
701723
let mut max_h: Option<i32> = None;
702-
let attributes = self.attributes.borrow();
724+
let Ok(attributes) = self.attributes.try_borrow() else {
725+
return cef::Size { width: 0, height: 0 };
726+
};
727+
703728
if let Some(max_size) = attributes.max_inner_size {
704729
let logical = max_size.to_logical::<u32>(scale);
705730
max_w = Some(logical.width as i32);
@@ -731,7 +756,9 @@ wrap_window_delegate! {
731756
return;
732757
};
733758

734-
if let Some(app_window) = self.windows.borrow().get(&self.window_id) {
759+
let Ok(windows) = self.windows.try_borrow() else { return; };
760+
761+
if let Some(app_window) = windows.get(&self.window_id) {
735762
for wrapper in &app_window.webviews {
736763
if let (Some(overlay), Some(b)) = (&wrapper.overlay, &*wrapper.bounds.lock().unwrap()) {
737764
let new_rect = cef::Rect {

crates/tauri-runtime-cef/src/lib.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,11 @@ impl<T: UserEvent> CefRuntime<T> {
18531853
next_window_event_id: Default::default(),
18541854
};
18551855

1856-
let mut app = cef_impl::TauriApp::new(cef_context.clone(), runtime_args.custom_schemes);
1856+
let mut app = cef_impl::TauriApp::new(
1857+
cef_context.clone(),
1858+
runtime_args.custom_schemes,
1859+
runtime_args.cef_command_line_args,
1860+
);
18571861

18581862
let cmd = args.as_cmd_line().unwrap();
18591863
let switch = CefString::from("type");
@@ -2054,19 +2058,23 @@ impl<T: UserEvent> Runtime<T> for CefRuntime<T> {
20542058
fn run<F: FnMut(RunEvent<T>) + 'static>(self, callback: F) {
20552059
let callback = Arc::new(RefCell::new(callback));
20562060
let event_tx_ = self.event_tx.clone();
2057-
let _ = self.context.cef_context.callback.replace(Box::new(move |event| {
2058-
if let RunEvent::Exit = event {
2059-
// notify the event loop to exit
2060-
let _ = event_tx_.send(RunEvent::Exit);
2061-
} else {
2062-
// Try to call callback directly, if busy queue to channel
2063-
if let Ok(mut cb) = callback.try_borrow_mut() {
2064-
cb(event);
2061+
let _ = self
2062+
.context
2063+
.cef_context
2064+
.callback
2065+
.replace(Box::new(move |event| {
2066+
if let RunEvent::Exit = event {
2067+
// notify the event loop to exit
2068+
let _ = event_tx_.send(RunEvent::Exit);
20652069
} else {
2066-
let _ = event_tx_.send(event);
2070+
// Try to call callback directly, if busy queue to channel
2071+
if let Ok(mut cb) = callback.try_borrow_mut() {
2072+
cb(event);
2073+
} else {
2074+
let _ = event_tx_.send(event);
2075+
}
20672076
}
2068-
}
2069-
}));
2077+
}));
20702078

20712079
'main_loop: loop {
20722080
while let Ok(event) = self.event_rx.try_recv() {

crates/tauri-runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ objc2-web-kit = { version = "0.3", features = ["objc2-app-kit", "WKWebView"] }
6464
[features]
6565
devtools = []
6666
macos-private-api = []
67+
cef = []

crates/tauri-runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ pub struct RuntimeInitArgs {
394394
pub msg_hook: Option<Box<dyn FnMut(*const std::ffi::c_void) -> bool + 'static>>,
395395
pub identifier: String,
396396
pub custom_schemes: Vec<String>,
397+
#[cfg(feature = "cef")]
398+
pub cef_command_line_args: Vec<(String, Option<String>)>,
397399
}
398400

399401
/// The webview runtime interface.

crates/tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ tracing = ["dep:tracing", "tauri-macros/tracing", "tauri-runtime-wry?/tracing"]
191191
test = []
192192
compression = ["tauri-macros/compression", "tauri-utils/compression"]
193193
wry = ["webview2-com", "webkit2gtk", "tauri-runtime-wry"]
194-
cef = ["tauri-runtime-cef"]
194+
cef = ["tauri-runtime-cef", "tauri-runtime/cef"]
195195
# TODO: Remove in v3 - wry does not have this feature anymore
196196
objc-exception = []
197197
linux-libxdo = ["tray-icon/libxdo", "muda/libxdo"]

crates/tauri/src/app.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,9 @@ pub struct Builder<R: Runtime> {
14271427
device_event_filter: DeviceEventFilter,
14281428

14291429
pub(crate) invoke_key: String,
1430+
1431+
#[cfg(feature = "cef")]
1432+
cef_command_line_args: Vec<(String, Option<String>)>,
14301433
}
14311434

14321435
#[derive(Template)]
@@ -1501,6 +1504,8 @@ impl<R: Runtime> Builder<R> {
15011504
webview_event_listeners: Vec::new(),
15021505
device_event_filter: Default::default(),
15031506
invoke_key,
1507+
#[cfg(feature = "cef")]
1508+
cef_command_line_args: Vec::new(),
15041509
}
15051510
}
15061511
}
@@ -2074,6 +2079,19 @@ tauri::Builder::default()
20742079
self
20752080
}
20762081

2082+
/// Sets CEF command line arguments.
2083+
#[cfg(feature = "cef")]
2084+
pub fn cef_command_line_args<K: Into<String>, V: Into<String>>(
2085+
mut self,
2086+
args: Vec<(K, Option<V>)>,
2087+
) -> Self {
2088+
self.cef_command_line_args = args
2089+
.into_iter()
2090+
.map(|(k, v)| (k.into(), v.map(|v| v.into())))
2091+
.collect();
2092+
self
2093+
}
2094+
20772095
/// Change the device event filter mode.
20782096
///
20792097
/// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]
@@ -2152,6 +2170,8 @@ tauri::Builder::default()
21522170
let runtime_args = RuntimeInitArgs {
21532171
identifier: manager.config.identifier.clone(),
21542172
custom_schemes,
2173+
#[cfg(feature = "cef")]
2174+
cef_command_line_args: self.cef_command_line_args,
21552175
#[cfg(any(
21562176
target_os = "linux",
21572177
target_os = "dragonfly",

0 commit comments

Comments
 (0)