2017-05-26 22:22:31 +02:00
|
|
|
//
|
|
|
|
// Browser.swift
|
|
|
|
// Evergren
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 2/23/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2020-05-21 02:54:25 +02:00
|
|
|
import Foundation
|
2017-05-26 22:22:31 +02:00
|
|
|
import RSWeb
|
|
|
|
|
2017-10-06 03:12:58 +02:00
|
|
|
struct Browser {
|
2017-05-26 22:22:31 +02:00
|
|
|
|
2020-05-29 00:03:28 +02:00
|
|
|
/// The user-specified default browser for opening web pages.
|
|
|
|
///
|
|
|
|
/// The user-assigned default browser, or `nil` if none was assigned
|
|
|
|
/// (i.e., the system default should be used).
|
2020-05-19 04:29:53 +02:00
|
|
|
static var defaultBrowser: MacWebBrowser? {
|
2020-07-02 05:17:38 +02:00
|
|
|
if let bundleID = AppDefaults.shared.defaultBrowserID, let browser = MacWebBrowser(bundleIdentifier: bundleID) {
|
2020-05-19 04:29:53 +02:00
|
|
|
return browser
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 00:03:28 +02:00
|
|
|
|
|
|
|
/// Opens a URL in the default browser.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - urlString: The URL to open.
|
|
|
|
/// - invert: Whether to invert the "open in background in browser" preference
|
2020-05-26 04:32:56 +02:00
|
|
|
static func open(_ urlString: String, invertPreference invert: Bool = false) {
|
2017-10-06 03:12:58 +02:00
|
|
|
// Opens according to prefs.
|
2020-07-02 05:17:38 +02:00
|
|
|
open(urlString, inBackground: invert ? !AppDefaults.shared.openInBrowserInBackground : AppDefaults.shared.openInBrowserInBackground)
|
2017-10-06 03:12:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 00:03:28 +02:00
|
|
|
|
|
|
|
/// Opens a URL in the default browser.
|
|
|
|
///
|
|
|
|
/// - Parameters:
|
|
|
|
/// - urlString: The URL to open.
|
|
|
|
/// - inBackground: Whether to open the URL in the background or not.
|
|
|
|
/// - Note: Some browsers (specifically Chromium-derived ones) will ignore the request
|
|
|
|
/// to open in the background.
|
2017-10-06 03:12:58 +02:00
|
|
|
static func open(_ urlString: String, inBackground: Bool) {
|
|
|
|
if let url = URL(string: urlString) {
|
2020-05-19 04:29:53 +02:00
|
|
|
if let defaultBrowser = defaultBrowser {
|
|
|
|
defaultBrowser.openURL(url, inBackground: inBackground)
|
|
|
|
} else {
|
|
|
|
MacWebBrowser.openURL(url, inBackground: inBackground)
|
|
|
|
}
|
2017-10-06 03:12:58 +02:00
|
|
|
}
|
2017-05-26 22:22:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 02:41:23 +02:00
|
|
|
extension Browser {
|
|
|
|
|
|
|
|
static var titleForOpenInBrowserInverted: String {
|
2020-07-02 05:17:38 +02:00
|
|
|
let openInBackgroundPref = AppDefaults.shared.openInBrowserInBackground
|
2020-05-21 02:41:23 +02:00
|
|
|
|
|
|
|
return openInBackgroundPref ?
|
|
|
|
NSLocalizedString("Open in Browser in Foreground", comment: "Open in Browser in Foreground menu item title") :
|
|
|
|
NSLocalizedString("Open in Browser in Background", comment: "Open in Browser in Background menu item title")
|
|
|
|
}
|
|
|
|
}
|