Skip to content

macos: add dont_switch flag #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ pub struct BrowserOptions {
suppress_output: bool,
target_hint: String,
dry_run: bool,
#[cfg(target_os = "macos")]
dont_switch: bool,
}

impl fmt::Display for BrowserOptions {
Expand All @@ -203,6 +205,8 @@ impl std::default::Default for BrowserOptions {
suppress_output: true,
target_hint,
dry_run: false,
#[cfg(target_os = "macos")]
dont_switch: false,
}
}
}
Expand Down Expand Up @@ -238,6 +242,13 @@ impl BrowserOptions {
self.dry_run = dry_run;
self
}

#[cfg(target_os = "macos")]
/// Do not switch to the browser window. This is macOS only right now.
pub fn with_dont_switch(&mut self, dont_switch: bool) -> &mut Self {
self.dont_switch = dont_switch;
self
}
}

/// Opens the URL on the default browser of this platform
Expand Down
7 changes: 6 additions & 1 deletion src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ pub(super) fn open_browser_internal(

let urls_v = [cf_url];
let urls_arr = CFArray::<CFURL>::from_CFTypes(&urls_v);
let mut launch_flags = LS_LAUNCH_FLAG_DEFAULTS | LS_LAUNCH_FLAG_ASYNC;
if options.dont_switch {
launch_flags |= LS_LAUNCH_FLAG_DONT_SWITCH;
}
let spec = LSLaunchURLSpec {
app_url: browser_cf_url.as_concrete_TypeRef(),
item_urls: urls_arr.as_concrete_TypeRef(),
pass_thru_params: std::ptr::null(),
launch_flags: LS_LAUNCH_FLAG_DEFAULTS | LS_LAUNCH_FLAG_ASYNC,
launch_flags,
async_ref_con: std::ptr::null(),
};

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

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