Skip to content

Commit 8a82bcf

Browse files
committed
macos: add dont_switch flag
1 parent 061e65e commit 8a82bcf

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ pub struct BrowserOptions {
185185
suppress_output: bool,
186186
target_hint: String,
187187
dry_run: bool,
188+
#[cfg(target_os = "macos")]
189+
dont_switch: bool,
188190
}
189191

190192
impl fmt::Display for BrowserOptions {
@@ -203,6 +205,8 @@ impl std::default::Default for BrowserOptions {
203205
suppress_output: true,
204206
target_hint,
205207
dry_run: false,
208+
#[cfg(target_os = "macos")]
209+
dont_switch: false,
206210
}
207211
}
208212
}
@@ -238,6 +242,13 @@ impl BrowserOptions {
238242
self.dry_run = dry_run;
239243
self
240244
}
245+
246+
#[cfg(target_os = "macos")]
247+
/// Do not switch to the browser window. This is macOS only right now.
248+
pub fn with_dont_switch(&mut self, dont_switch: bool) -> &mut Self {
249+
self.dont_switch = dont_switch;
250+
self
251+
}
241252
}
242253

243254
/// Opens the URL on the default browser of this platform

src/macos.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ pub(super) fn open_browser_internal(
5555

5656
let urls_v = [cf_url];
5757
let urls_arr = CFArray::<CFURL>::from_CFTypes(&urls_v);
58+
let mut launch_flags = LS_LAUNCH_FLAG_DEFAULTS | LS_LAUNCH_FLAG_ASYNC;
59+
if options.dont_switch {
60+
launch_flags |= LS_LAUNCH_FLAG_DONT_SWITCH;
61+
}
5862
let spec = LSLaunchURLSpec {
5963
app_url: browser_cf_url.as_concrete_TypeRef(),
6064
item_urls: urls_arr.as_concrete_TypeRef(),
6165
pass_thru_params: std::ptr::null(),
62-
launch_flags: LS_LAUNCH_FLAG_DEFAULTS | LS_LAUNCH_FLAG_ASYNC,
66+
launch_flags,
6367
async_ref_con: std::ptr::null(),
6468
};
6569

@@ -169,6 +173,7 @@ const LSROLE_VIEWER: LSRolesMask = 0x00000002;
169173
// as per https://developer.apple.com/documentation/coreservices/lslaunchflags/klslaunchdefaults?language=objc
170174
const LS_LAUNCH_FLAG_DEFAULTS: u32 = 0x00000001;
171175
const LS_LAUNCH_FLAG_ASYNC: u32 = 0x00010000;
176+
const LS_LAUNCH_FLAG_DONT_SWITCH: u32 = 0x00000200;
172177

173178
#[repr(C, packed(2))] // Header contains `#pragma pack(push, 2)`.
174179
struct LSLaunchURLSpec {

0 commit comments

Comments
 (0)