Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

Settings ▸ Tabs: open links in new tabs in the background (#183)

Victor74e7ca0c750d
diff --git a/Packages/CraCore/Sources/CraCore/LinkMenu.swift b/Packages/CraCore/Sources/CraCore/LinkMenu.swift
new file mode 100644
index 0000000..94bb5e2
--- /dev/null
+++ b/Packages/CraCore/Sources/CraCore/LinkMenu.swift
@@ -0,0 +1,23 @@
+import Foundation
+
+/// The two "open link" items at the top of a page's context menu (GitHub #183).
+///
+/// Arc's "Open Link in New Tab" switches to the tab it makes, and so does Cra's by default, with
+/// "Open Link in Background Tab" below it. With Settings ▸ Tabs ▸ "Open links in new tabs in the
+/// background" on, the first item stays behind the page, as in Chrome, and the second one is the
+/// explicit way to switch. ⌘-click (background) and ⇧⌘-click (switch) say what they want and don't
+/// follow the setting; neither does a target=_blank click, which the page asked for.
+public enum LinkMenu {
+ public struct Item: Equatable, Sendable {
+ public let title: String
+ public let opensInBackground: Bool
+ }
+
+ public static func items(openLinksInBackground: Bool) -> [Item] {
+ openLinksInBackground
+ ? [Item(title: "Open Link in New Tab", opensInBackground: true),
+ Item(title: "Open Link in Foreground Tab", opensInBackground: false)]
+ : [Item(title: "Open Link in New Tab", opensInBackground: false),
+ Item(title: "Open Link in Background Tab", opensInBackground: true)]
+ }
+}
diff --git a/Packages/CraCore/Sources/CraCore/SidebarModel.swift b/Packages/CraCore/Sources/CraCore/SidebarModel.swift
index ae9833a..8cb044e 100644
--- a/Packages/CraCore/Sources/CraCore/SidebarModel.swift
+++ b/Packages/CraCore/Sources/CraCore/SidebarModel.swift
@@ -475,6 +475,9 @@ public struct AppSettings: Codable, Equatable {
/// Arc's "Open a Peek window when clicking on links with Shift held"
/// (shiftClickModalEntryPointEnabled).
public var peekOnShiftClick: Bool = true
+ /// GitHub #183, not in Arc (Chrome's way): a page's "Open Link in New Tab" leaves the page in
+ /// front and the new tab loading behind it (LinkMenu). Off like Arc, which switches to it.
+ public var openLinksInBackground: Bool = false
/// Arc's Previews ▸ Folders (folderPreviewEnabled): resting the pointer on a closed folder
/// lists its tabs, most recent first, with a search field.
public var folderPreviews: Bool = true
@@ -675,6 +678,7 @@ extension AppSettings {
peekLinksToOtherSites = try c.decodeIfPresent(Bool.self, forKey: .peekLinksToOtherSites) ?? true
peekInEveryTab = try c.decodeIfPresent(Bool.self, forKey: .peekInEveryTab) ?? false
peekOnShiftClick = try c.decodeIfPresent(Bool.self, forKey: .peekOnShiftClick) ?? true
+ openLinksInBackground = try c.decodeIfPresent(Bool.self, forKey: .openLinksInBackground) ?? false
folderPreviews = try c.decodeIfPresent(Bool.self, forKey: .folderPreviews) ?? true
hapticFeedbackOnReorder = try c.decodeIfPresent(Bool.self, forKey: .hapticFeedbackOnReorder) ?? true
warnBeforeQuitting = try c.decodeIfPresent(Bool.self, forKey: .warnBeforeQuitting) ?? false
diff --git a/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/input/settings.json b/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/input/settings.json
index 3083135..1f1a62f 100644
--- a/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/input/settings.json
+++ b/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/input/settings.json
@@ -7,7 +7,7 @@
{"id": "E0000000-0000-0000-0000-000000000001", "match": "contains", "text": "meet.google.com", "destination": {"mostRecentSpace": {}}},
{"id": "E0000000-0000-0000-0000-000000000002", "match": "equals", "text": "example.com/standup", "destination": {"littleWindow": {}}}
],
- "newTabButtonAtTop": false, "newTabButtonDefaultMigrated": true, "newTabsAtTop": false,
+ "newTabButtonAtTop": false, "newTabButtonDefaultMigrated": true, "newTabsAtTop": false, "openLinksInBackground": true,
"peekInEveryTab": true, "peekLinksToOtherSites": false, "peekOnShiftClick": false, "perfHidesSiteNames": true, "perfRecording": false,
"searchEngine": "kagi", "searchSuggestions": false, "showSpaceHeaderOnSingleSpace": false, "showStatusBar": false,
"showToolbar": true, "tabSleep": "fourHours", "tabSleepAfter": "oneHour", "tabSleepEnabled": true,
diff --git a/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-bad-routes.json b/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-bad-routes.json
index 582b03e..2932b90 100644
--- a/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-bad-routes.json
+++ b/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-bad-routes.json
@@ -23,6 +23,7 @@
"newTabButtonAtTop" : true,
"newTabButtonDefaultMigrated" : true,
"newTabsAtTop" : true,
+ "openLinksInBackground" : false,
"peekInEveryTab" : false,
"peekLinksToOtherSites" : true,
"peekOnShiftClick" : true,
diff --git a/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-empty.json b/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-empty.json
index a3ff129..8dffc70 100644
--- a/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-empty.json
+++ b/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-empty.json
@@ -32,6 +32,7 @@
"newTabButtonAtTop" : true,
"newTabButtonDefaultMigrated" : true,
"newTabsAtTop" : true,
+ "openLinksInBackground" : false,
"peekInEveryTab" : false,
"peekLinksToOtherSites" : true,
"peekOnShiftClick" : true,
diff --git a/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-legacy.json b/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-legacy.json
index d5bf063..f91d76f 100644
--- a/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-legacy.json
+++ b/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings-legacy.json
@@ -32,6 +32,7 @@
"newTabButtonAtTop" : true,
"newTabButtonDefaultMigrated" : true,
"newTabsAtTop" : true,
+ "openLinksInBackground" : false,
"peekInEveryTab" : false,
"peekLinksToOtherSites" : true,
"peekOnShiftClick" : true,
diff --git a/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings.json b/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings.json
index 68bd11f..b2b7f8d 100644
--- a/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings.json
+++ b/Packages/CraCore/Tests/CraCoreTests/Fixtures/Model/recorded/settings.json
@@ -42,6 +42,7 @@
"newTabButtonAtTop" : false,
"newTabButtonDefaultMigrated" : true,
"newTabsAtTop" : false,
+ "openLinksInBackground" : true,
"peekInEveryTab" : true,
"peekLinksToOtherSites" : false,
"peekOnShiftClick" : false,
diff --git a/Packages/CraCore/Tests/CraCoreTests/LinkMenuTests.swift b/Packages/CraCore/Tests/CraCoreTests/LinkMenuTests.swift
new file mode 100644
index 0000000..f314ea9
--- /dev/null
+++ b/Packages/CraCore/Tests/CraCoreTests/LinkMenuTests.swift
@@ -0,0 +1,22 @@
+import Testing
+import CraCore
+import Foundation
+
+@Suite struct LinkMenuTests {
+ @Test func defaultSwitchesToTheNewTabLikeArc() {
+ let items = LinkMenu.items(openLinksInBackground: false)
+ #expect(items.map(\.title) == ["Open Link in New Tab", "Open Link in Background Tab"])
+ #expect(items.map(\.opensInBackground) == [false, true])
+ }
+
+ @Test func settingOnKeepsThePageInFrontAndOffersTheSwitch() {
+ let items = LinkMenu.items(openLinksInBackground: true)
+ #expect(items.map(\.title) == ["Open Link in New Tab", "Open Link in Foreground Tab"])
+ #expect(items.map(\.opensInBackground) == [true, false])
+ }
+
+ @Test func settingIsOffForSettingsWithoutIt() throws {
+ let s = try JSONDecoder().decode(AppSettings.self, from: Data("{}".utf8))
+ #expect(!s.openLinksInBackground)
+ }
+}
diff --git a/Sources/Cra/App/AppDelegate.swift b/Sources/Cra/App/AppDelegate.swift
index 3d9d398..3553960 100644
--- a/Sources/Cra/App/AppDelegate.swift
+++ b/Sources/Cra/App/AppDelegate.swift
@@ -176,6 +176,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
}
observeContentBlocking()
observeHttpsOnly()
+ observeLinkMenu()
logSecureDNS()
}
@@ -231,6 +232,15 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
}
}
+ /// Settings ▸ Tabs ▸ "Open links in new tabs in the background" (GitHub #183): the page
+ /// context menu's two open-link items (LinkMenu), read when the next menu opens.
+ private func observeLinkMenu() {
+ observeChanges(of: { [store] in store.settings.openLinksInBackground }) { background in
+ let items = LinkMenu.items(openLinksInBackground: background)
+ CraCEFRuntime.setLinkMenuTitles(items.map(\.title), opensInBackground: items.map { NSNumber(value: $0.opensInBackground) })
+ }
+ }
+
private func observeContentBlocking() {
observeChanges(of: { [store] in store.settings.blockAdsAndTrackers }, apply: CraCEFRuntime.setContentBlockingEnabled)
}
diff --git a/Sources/Cra/UI/Settings/TabsPane.swift b/Sources/Cra/UI/Settings/TabsPane.swift
index a16d25b..3019435 100644
--- a/Sources/Cra/UI/Settings/TabsPane.swift
+++ b/Sources/Cra/UI/Settings/TabsPane.swift
@@ -40,6 +40,11 @@ struct TabsPane: View {
.disabled(!store.settings.peekLinksToOtherSites)
.padding(.leading, 20)
SettingsCheckbox(title: "Open a Peek window when clicking on links with Shift held", isOn: $store.settings.peekOnShiftClick)
+ // GitHub #183, Chrome's way and not Arc's: the page stays in front.
+ SettingsCheckbox(title: "Open links in new tabs in the background", isOn: $store.settings.openLinksInBackground)
+ Text("“Open Link in New Tab” keeps the page you’re reading in front. “Open Link in Foreground Tab” and ⇧⌘-click still switch to the new tab.")
+ .font(.system(size: 11)).foregroundStyle(.secondary)
+ .fixedSize(horizontal: false, vertical: true)
HStack(spacing: 8) {
Button("Air Traffic Control…") { showsAirTrafficControl = true }
Text(routesSummary).font(.system(size: 11)).foregroundStyle(.secondary)
diff --git a/Sources/CraCEF/CraCEF.mm b/Sources/CraCEF/CraCEF.mm
index bd0e4e8..6ca4f82 100644
--- a/Sources/CraCEF/CraCEF.mm
+++ b/Sources/CraCEF/CraCEF.mm
@@ -85,6 +85,9 @@ static CefString CefStringFromNS(NSString* s) {
/// Page context menus shown by Cra (RunContextMenu) instead of Chrome.
static BOOL g_ownContextMenus = NO;
+// The two "open link" items on a page's context menu, from CraCore's LinkMenu (GitHub #183).
+static NSArray<NSString*>* g_linkMenuTitles = @[@"Open Link in New Tab", @"Open Link in Background Tab"];
+static NSArray<NSNumber*>* g_linkMenuBackground = @[@NO, @YES];
/// Page.addScriptToEvaluateOnNewDocument params every page browser gets once it exists
/// (the page's world, then Cra's isolated world).
@@ -140,8 +143,8 @@ static NSMenu* CraMenuFromModel(CefRefPtr<CefMenuModel> model, CraMenuPick* pick
}
enum CraMenuCommand {
- kMenuOpenLinkInNewTab = MENU_ID_USER_FIRST + 1,
- kMenuOpenLinkInBackgroundTab,
+ kMenuOpenLinkFirst = MENU_ID_USER_FIRST + 1,
+ kMenuOpenLinkSecond,
kMenuCopyLink,
kMenuInspectElement,
kMenuOpenImageInNewTab,
@@ -1590,8 +1593,11 @@ public:
CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model) override {
size_t index = 0;
if (!params->GetLinkUrl().empty()) {
- model->InsertItemAt(index++, kMenuOpenLinkInNewTab, "Open Link in New Tab");
- model->InsertItemAt(index++, kMenuOpenLinkInBackgroundTab, "Open Link in Background Tab");
+ model->InsertItemAt(index++, kMenuOpenLinkFirst, g_linkMenuTitles[0].UTF8String);
+ model->InsertItemAt(index++, kMenuOpenLinkSecond, g_linkMenuTitles[1].UTF8String);
+ // Chrome-style menus carry Chrome's own "Open Link in New Tab" further down, which
+ // always opens in the background: one item by that name, the one the setting rules (#183).
+ model->Remove(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB);
model->InsertItemAt(index++, kMenuCopyLink, "Copy Link");
model->InsertSeparatorAt(index++);
}
@@ -1617,11 +1623,10 @@ public:
CraBrowser* o = owner_;
if (!o) return false;
switch (command_id) {
- case kMenuOpenLinkInNewTab:
- [o.delegate browser:o requestsNewTabWithURL:NSStringFromCef(params->GetLinkUrl()) inBackground:NO];
- return true;
- case kMenuOpenLinkInBackgroundTab:
- [o.delegate browser:o requestsNewTabWithURL:NSStringFromCef(params->GetLinkUrl()) inBackground:YES];
+ case kMenuOpenLinkFirst:
+ case kMenuOpenLinkSecond:
+ [o.delegate browser:o requestsNewTabWithURL:NSStringFromCef(params->GetLinkUrl())
+ inBackground:g_linkMenuBackground[command_id - kMenuOpenLinkFirst].boolValue];
return true;
case kMenuOpenImageInNewTab:
[o.delegate browser:o requestsNewTabWithURL:NSStringFromCef(params->GetSourceUrl()) inBackground:NO];
@@ -2465,6 +2470,11 @@ static void MarkProfileExitedCleanly(NSString* profileDir) {
+ (void)setChromeHosting:(BOOL)enabled { g_chromeHosting = enabled; }
+ (void)setOwnContextMenus:(BOOL)enabled { g_ownContextMenus = enabled; }
++ (void)setLinkMenuTitles:(NSArray<NSString*>*)titles opensInBackground:(NSArray<NSNumber*>*)background {
+ if (titles.count != 2 || background.count != 2) return;
+ g_linkMenuTitles = [titles copy];
+ g_linkMenuBackground = [background copy];
+}
+ (void)setDocumentStartScript:(NSString*)script isolatedScript:(NSString*)isolatedScript world:(NSString*)world {
NSMutableArray<NSString*>* all = [NSMutableArray array];
for (NSDictionary* params in @[@{@"source": script ?: @"", @"runImmediately": @YES},
diff --git a/Sources/CraCEF/include/CraCEF.h b/Sources/CraCEF/include/CraCEF.h
index a99a7e0..de98045 100644
--- a/Sources/CraCEF/include/CraCEF.h
+++ b/Sources/CraCEF/include/CraCEF.h
@@ -236,6 +236,9 @@ typedef NS_ENUM(NSInteger, CraDownloadState) {
+ (BOOL)chromeHosting;
/// Page context menus are shown by Cra, not Chrome (pages embedded in their cards).
+ (void)setOwnContextMenus:(BOOL)enabled;
+/// The two "open link" items at the top of a page's context menu, in order: their titles and
+/// whether each opens its tab in the background. Exactly two; read when a menu opens.
++ (void)setLinkMenuTitles:(NSArray<NSString *> *)titles opensInBackground:(NSArray<NSNumber *> *)background;
/// Run in every frame of every page browser before the page's own scripts (and once in the
/// document already loaded): `script` in the page's world, `isolatedScript` in the isolated
/// world named `world` (invisible to the page). Set before browsers are created.