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.
Show only the space's profile's extensions (#23)
Sagar Dagduaadb87ef5e8d
diff --git a/Sources/Cra/Services/ExtensionStore.swift b/Sources/Cra/Services/ExtensionStore.swiftindex 076ac05..bd1ca8b 100644--- a/Sources/Cra/Services/ExtensionStore.swift+++ b/Sources/Cra/Services/ExtensionStore.swift@@ -25,6 +25,11 @@ final class ExtensionStore {/// Installed by Chrome into the profile (Web Store); Chrome owns the folder and/// loads changes at once, so removing goes through chrome://extensions.var managedByChrome = false+ /// Chromium profile folders ("Default", "Profile <id>") it is installed in; nil for the+ /// --load-extension ones, which Chromium loads into every profile.+ var profiles: Set<String>?++ func isInstalled(inProfile name: String) -> Bool { profiles?.contains(name) ?? true }}/// The menu bar's Extensions menu and the toolbar read this one.@@ -66,19 +71,32 @@ final class ExtensionStore {let fm = FileManager.defaulttry? fm.createDirectory(at: ExtensionStore.directory, withIntermediateDirectories: true)let names = (try? fm.contentsOfDirectory(atPath: ExtensionStore.directory.path)) ?? []- var list = names.sorted().compactMap { name in+ let list = names.sorted().compactMap { name inSelf.entry(id: name, folder: ExtensionStore.directory.appendingPathComponent(name, isDirectory: true), extensionID: nil)}let loaded = Set(list.map(\.extensionID))- for e in ExtensionImporter.extensions(inUserData: AppPaths.chromiumCache) where e.isEnabled && !loaded.contains(e.id) {- guard var entry = Self.entry(id: e.id, folder: e.folder, extensionID: e.id) else { continue }- entry.managedByChrome = true- list.append(entry)+ // Each profile has its own Web Store extensions: note which profiles hold each one.+ let root = AppPaths.chromiumCache+ let profiles = ((try? fm.contentsOfDirectory(atPath: root.path)) ?? []).filter { $0 == "Default" || $0.hasPrefix("Profile ") }.sorted()+ var managed: [Entry] = []+ for profile in profiles {+ for e in ExtensionImporter.extensions(inProfile: root.appendingPathComponent(profile, isDirectory: true))+ where e.isEnabled && !loaded.contains(e.id) {+ if let i = managed.firstIndex(where: { $0.extensionID == e.id }) { managed[i].profiles?.insert(profile); continue }+ guard var entry = Self.entry(id: e.id, folder: e.folder, extensionID: e.id) else { continue }+ entry.managedByChrome = true+ entry.profiles = [profile]+ managed.append(entry)+ }}- entries = list+ entries = list + managed.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending }pinnedIDs.removeAll { id in !entries.contains { $0.extensionID == id } }}+ /// The extensions a page in this Chromium profile folder can run; running another+ /// profile's extension there crashes Chromium.+ func entries(inProfile name: String) -> [Entry] { entries.filter { $0.isInstalled(inProfile: name) } }+private static func entry(id: String, folder dir: URL, extensionID: String?) -> Entry? {guard let data = try? Data(contentsOf: dir.appendingPathComponent("manifest.json")),let m = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return nil }diff --git a/Sources/Cra/Services/Import/ExtensionImporter.swift b/Sources/Cra/Services/Import/ExtensionImporter.swiftindex 2543fd7..08f9192 100644--- a/Sources/Cra/Services/Import/ExtensionImporter.swift+++ b/Sources/Cra/Services/Import/ExtensionImporter.swift@@ -35,7 +35,7 @@ enum ExtensionImporter {return out.sorted { $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending }}- private static func extensions(inProfile dir: URL) -> [ImportableExtension] {+ static func extensions(inProfile dir: URL) -> [ImportableExtension] {var settings: [String: Any] = [:]// Newer Chromium keeps extension state in Secure Preferences; older in Preferences.for file in ["Preferences", "Secure Preferences"] {diff --git a/Sources/Cra/Services/SidebarStore.swift b/Sources/Cra/Services/SidebarStore.swiftindex b7d8d8d..b1e2bc1 100644--- a/Sources/Cra/Services/SidebarStore.swift+++ b/Sources/Cra/Services/SidebarStore.swift@@ -1325,6 +1325,15 @@ final class SidebarStore {return AppPaths.chromiumCache.appendingPathComponent(p.directoryName, isDirectory: true).path}+ /// The Chromium profile folder ("Default" or "Profile <id>") a space's pages load in;+ /// incognito spaces count as Default, whose off-the-record profile they use.+ func profileDirectoryName(space id: UUID) -> String {+ guard !isIncognito(space: id), let p = profile(spaces[id]?.profileID) else { return "Default" }+ return p.directoryName+ }++ func profileDirectoryName(item id: UUID) -> String { spaceID(ofItem: id).map { profileDirectoryName(space: $0) } ?? "Default" }+@discardableResultfunc createProfile(name: String) -> BrowserProfile {let p = BrowserProfile(id: UUID(), name: name)diff --git a/Sources/Cra/UI/ExtensionsMenu.swift b/Sources/Cra/UI/ExtensionsMenu.swiftindex 223d2aa..2fa7e96 100644--- a/Sources/Cra/UI/ExtensionsMenu.swift+++ b/Sources/Cra/UI/ExtensionsMenu.swift@@ -21,7 +21,7 @@ final class ExtensionsMenu: NSObject, NSMenuDelegate, NSMenuItemValidation {menu.removeAllItems()let store = ExtensionStore.sharedstore.reload()- let entries = store.entries.sorted { $0.name < $1.name }+ let entries = app.main.extensionEntries.sorted { $0.name < $1.name }for e in entries {let item = NSMenuItem(title: e.name, action: #selector(runExtension(_:)), keyEquivalent: "")item.target = selfdiff --git a/Sources/Cra/UI/PaneToolbarView.swift b/Sources/Cra/UI/PaneToolbarView.swiftindex b3b9310..e2dfa28 100644--- a/Sources/Cra/UI/PaneToolbarView.swift+++ b/Sources/Cra/UI/PaneToolbarView.swift@@ -117,9 +117,11 @@ final class PaneToolbarView: NSView {m.path = human.hasPrefix(m.host) ? String(human.dropFirst(m.host.count)) : ""}let extensions = ExtensionStore.shared- m.hasExtensions = !extensions.entries.isEmpty+ let entries = extensions.entries(inProfile: item.map { store.profileDirectoryName(item: $0.id) }+ ?? store.profileDirectoryName(space: store.focusedSpaceID(window: ui.windowID)))+ m.hasExtensions = !entries.isEmptym.pinned = extensions.pinnedIDs.compactMap { id in- extensions.entries.first { $0.extensionID == id }.map { Model.Ext(id: id, name: $0.name, icon: extensions.toolbarIcon(for: $0)) }+ entries.first { $0.extensionID == id }.map { Model.Ext(id: id, name: $0.name, icon: extensions.toolbarIcon(for: $0)) }}// Arc's toolbar colour: the page's theme-color when it declares one, else white until the// page reports one (MainWindowController.browserManagerDidUpdate uses the same rule).diff --git a/Sources/Cra/UI/Sidebar/List/SidebarView.swift b/Sources/Cra/UI/Sidebar/List/SidebarView.swiftindex b6bb0fb..f1e5bcc 100644--- a/Sources/Cra/UI/Sidebar/List/SidebarView.swift+++ b/Sources/Cra/UI/Sidebar/List/SidebarView.swift@@ -273,9 +273,11 @@ final class SidebarTopBarView: NSView {let host = URL(string: url)?.host ?? ""let text = tab == nil ? "Search or Enter URL…" : (host.isEmpty ? SuggestionEngine.humanURL(url) : host.replacingOccurrences(of: "www.", with: ""))let ext = ExtensionStore.shared- let pinned = ext.pinnedIDs.compactMap { id in ext.entries.first { $0.extensionID == id }.map { (id, $0.name, ext.toolbarIcon(for: $0)) } }+ let entries = ext.entries(inProfile: tab.map { store.profileDirectoryName(item: $0.id) }+ ?? store.profileDirectoryName(space: store.focusedSpaceID(window: ui.windowID)))+ let pinned = ext.pinnedIDs.compactMap { id in entries.first { $0.extensionID == id }.map { (id, $0.name, ext.toolbarIcon(for: $0)) } }pill.show(text: text, url: url, hasTab: tab != nil, status: ui.toolbarStatus, colors: colors, active: active,- pinned: pinned, hasExtensions: !ext.entries.isEmpty)+ pinned: pinned, hasExtensions: !entries.isEmpty)}needsLayout = trueif wasCompact != compact { superview?.superview?.needsLayout = true }diff --git a/Sources/Cra/UI/SiteControlCenter.swift b/Sources/Cra/UI/SiteControlCenter.swiftindex f54e579..371eee1 100644--- a/Sources/Cra/UI/SiteControlCenter.swift+++ b/Sources/Cra/UI/SiteControlCenter.swift@@ -36,10 +36,13 @@ final class SiteControlCenterView: NSView {private let colors: SiteControlCenterColorsprivate let actions: SiteControlCenterActionsprivate let store: SidebarStore+ /// The page's Chromium profile folder: only its extensions are offered.+ private let profile: Stringprivate var extensions: ExtensionStore { .shared }- init(url: String, dark: Bool, actions: SiteControlCenterActions, store: SidebarStore) {+ init(url: String, dark: Bool, profile: String, actions: SiteControlCenterActions, store: SidebarStore) {self.url = url+ self.profile = profilecolors = SiteControlCenterColors(dark: dark)self.actions = actionsself.store = store@@ -59,7 +62,7 @@ final class SiteControlCenterView: NSView {/// Arc: pinned extensions in toolbar order, then the rest by name.private var orderedExtensions: [ExtensionStore.Entry] {- let entries = extensions.entries+ let entries = extensions.entries(inProfile: profile)let pinned = extensions.pinnedIDs.compactMap { id in entries.first { $0.extensionID == id } }let rest = entries.filter { !extensions.isPinned($0.extensionID) }.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }@@ -536,7 +539,7 @@ final class SiteControlCenterPopover: NSObject, NSPopoverDelegate {var isShown: Bool { popover?.isShown == true }- func show(relativeTo rect: NSRect, of view: NSView, url: String, dark: Bool, store: SidebarStore, actions: SiteControlCenterActions) {+ func show(relativeTo rect: NSRect, of view: NSView, url: String, dark: Bool, profile: String, store: SidebarStore, actions: SiteControlCenterActions) {if let popover, popover.isShown { popover.performClose(nil); return }let p = NSPopover()p.behavior = .transient@@ -556,7 +559,7 @@ final class SiteControlCenterPopover: NSObject, NSPopoverDelegate {acts.siteSettings = closing(actions.siteSettings)acts.clearCache = closing(actions.clearCache)acts.clearCookies = closing(actions.clearCookies)- let content = SiteControlCenterView(url: url, dark: dark, actions: acts, store: store)+ let content = SiteControlCenterView(url: url, dark: dark, profile: profile, actions: acts, store: store)let host = NSViewController()host.view = contenthost.preferredContentSize = content.frame.sizediff --git a/Sources/Cra/UI/Windows/MainWindowController.swift b/Sources/Cra/UI/Windows/MainWindowController.swiftindex 1179ee5..52ee1d9 100644--- a/Sources/Cra/UI/Windows/MainWindowController.swift+++ b/Sources/Cra/UI/Windows/MainWindowController.swift@@ -279,6 +279,8 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserMguard !isTornDown else { return }let space = focusedSpacelet palette = space.theme.palette+ // Its extensions belong to the space's profile.+ if siteControlCenter.isShown, siteControlCenterSpaceID != space.id { siteControlCenter.close() }// refresh runs on every store change (each page title, each window move). A new root// view rebuilds its whole SwiftUI tree, and a new window appearance or background redraws// every view, so all of these follow the theme only when it changes. What the root@@ -1528,6 +1530,8 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM// MARK: Extensionsprivate let siteControlCenter = SiteControlCenterPopover()+ /// The space the Site Control Center was opened in.+ private var siteControlCenterSpaceID: UUID?/// Screen y of the focused card's toolbar bottom (its top edge when the toolbar is hidden),/// and the card's right edge.@@ -1564,6 +1568,7 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM/// Dia's Extensions menu. The popup hangs under the toolbar, its right edge at `buttonRightX`/// (screen x of the clicked button; nil = the extension's toolbar button or the card's right end).func runExtensionAction(_ id: String, buttonRightX: CGFloat? = nil) {+ guard extensionEntries.contains(where: { $0.extensionID == id }) else { return }guard let bar = toolbarBottomOnScreen() else { openExtensionPopupPage(id, anchor: nil); return }let anchor = buttonRightX.map { NSPoint(x: $0, y: urlBarBottomOnScreen() ?? bar.y) }?? extensionPopupAnchor(id) ?? NSPoint(x: bar.cardMaxX - 8, y: bar.y)@@ -1573,6 +1578,12 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM}}+ /// The Chromium profile folder the focused page loads in.+ var profileDirectoryName: String { selectedTabID.map(store.profileDirectoryName(item:)) ?? store.profileDirectoryName(space: focusedSpaceID) }++ /// The extensions installed in the focused page's profile.+ var extensionEntries: [ExtensionStore.Entry] { ExtensionStore.shared.entries(inProfile: profileDirectoryName) }+/// The right edge (screen x) of the pinned-extension button under the pointer, in the/// toolbar or the sidebar's URL pill (also in the floating sidebar's panel), so a popup/// hangs from the same place wherever the button was clicked (GitHub #8).@@ -1640,8 +1651,9 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserMguard let origin = URL(string: url).flatMap({ u in u.host.map { "\(u.scheme ?? "https")://\($0)" } }) else { return }self?.openTab("chrome://settings/content/siteDetails?site=" + (origin.addingPercentEncoding(withAllowedCharacters: .alphanumerics) ?? origin))}+ siteControlCenterSpaceID = focusedSpaceIDsiteControlCenter.show(relativeTo: rect, of: content, url: url, dark: focusedSpace.theme.palette.isDark,- store: store, actions: actions)+ profile: profileDirectoryName, store: store, actions: actions)}private func openTab(_ url: String) {