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.
Tabs on one host keep their own favicons (#211)
Victor061a0ef7f348
diff --git a/Packages/CraCore/Sources/CraCore/FaviconKey.swift b/Packages/CraCore/Sources/CraCore/FaviconKey.swiftindex 25097dd..bc73d45 100644--- a/Packages/CraCore/Sources/CraCore/FaviconKey.swift+++ b/Packages/CraCore/Sources/CraCore/FaviconKey.swift@@ -1,9 +1,41 @@import Foundation-/// What favicons are cached under: the page's host, lowercased (Arc: SidebarItemsFaviconCache).+/// What favicons are cached under. A site's key is the page's host, lowercased: the icon a+/// suggestion or a not-yet-loaded tab borrows from the last page seen there. A tab's own icon+/// goes under its icon URL's key as well, so tabs on one host keep their own icons (GitHub #211;+/// Arc caches one favicon per sidebar item, SidebarItemsFaviconCache).public enum FaviconKey {public static func key(for urlString: String) -> String? {guard let host = URL(string: urlString)?.host, !host.isEmpty else { return nil }return host.lowercased()}++ /// The key of the icon at `iconURL` on the page at `pageURL`: "<host>#<hash of the icon URL>".+ /// AWS consoles and localhost apps have one host and many icons.+ public static func key(for pageURL: String, iconURL: String) -> String? {+ key(for: pageURL).map { key(site: $0, iconURL: iconURL) }+ }++ /// The same for a site key already known (a tab's).+ public static func key(site: String, iconURL: String) -> String {+ guard !iconURL.isEmpty else { return site }+ // FNV-1a: stable across launches (the key names a file on disk), unlike Hasher.+ var hash: UInt64 = 0xcbf2_9ce4_8422_2325+ for byte in iconURL.utf8 { hash = (hash ^ UInt64(byte)) &* 0x100_0000_01b3 }+ return site + "#" + String(hash, radix: 16)+ }++ /// The site key an icon key falls back to.+ public static func siteKey(of key: String) -> String {+ key.firstIndex(of: "#").map { String(key[..<$0]) } ?? key+ }++ /// A tab's key after it goes to `url`. On the same host it keeps its own icon until the new+ /// page's arrives (Chromium sends none when the icon is the same); elsewhere it shows the+ /// site's last icon meanwhile.+ public static func key(after current: String?, goingTo url: String) -> String? {+ guard let site = key(for: url) else { return current }+ if let current, siteKey(of: current) == site { return current }+ return site+ }}diff --git a/Packages/CraCore/Sources/CraCore/SidebarModel.swift b/Packages/CraCore/Sources/CraCore/SidebarModel.swiftindex a4536c6..05a26d4 100644--- a/Packages/CraCore/Sources/CraCore/SidebarModel.swift+++ b/Packages/CraCore/Sources/CraCore/SidebarModel.swift@@ -64,7 +64,7 @@ public struct TabData: Codable, Equatable {public var muted: Bool = falsepublic var timeLastActiveAt: Date?public var activeTabBeforeCreationID: UUID?- /// Host used as favicon cache key.+ /// Favicon cache key: the host, then the page's own icon once it loads (FaviconKey, #211).public var faviconKey: String?/// Favicon cache key of an image the user picked; wins over the site's favicon.public var customIconKey: String?@@ -103,7 +103,7 @@ public struct TabData: Codable, Equatable {guard url != savedURL else { return }url = savedURLif let homeTitle { savedTitle = homeTitle }- if let key = FaviconKey.key(for: savedURL) { faviconKey = key }+ faviconKey = FaviconKey.key(after: faviconKey, goingTo: savedURL)}public init(url: String, title: String = "") {diff --git a/Packages/CraCore/Tests/CraCoreTests/FaviconKeyTests.swift b/Packages/CraCore/Tests/CraCoreTests/FaviconKeyTests.swiftindex 2e59cca..f6e7009 100644--- a/Packages/CraCore/Tests/CraCoreTests/FaviconKeyTests.swift+++ b/Packages/CraCore/Tests/CraCoreTests/FaviconKeyTests.swift@@ -13,4 +13,34 @@ struct FaviconKeyTests {func keyIsTheLowercasedHost(url: String, key: String?) {#expect(FaviconKey.key(for: url) == key)}++ @Test(.bug("https://github.com/VictorKildahl/cra/issues/211"))+ func tabsOnOneHostKeepTheirOwnIcons() throws {+ let rds = try #require(FaviconKey.key(for: "https://console.aws.amazon.com/rds/home", iconURL: "https://a.b/rds.ico"))+ let eks = try #require(FaviconKey.key(for: "https://console.aws.amazon.com/eks/home", iconURL: "https://a.b/eks.ico"))+ let app1 = try #require(FaviconKey.key(for: "http://localhost:3000/", iconURL: "http://localhost:3000/favicon.ico"))+ let app2 = try #require(FaviconKey.key(for: "http://localhost:5173/", iconURL: "http://localhost:5173/favicon.ico"))+ #expect(rds != eks && app1 != app2)+ #expect(FaviconKey.siteKey(of: rds) == "console.aws.amazon.com" && FaviconKey.siteKey(of: app1) == "localhost")+ // The key names a file on disk: it must not change between launches.+ #expect(FaviconKey.key(site: "localhost", iconURL: "http://localhost:3000/favicon.ico") == app1)+ #expect(FaviconKey.key(site: "localhost", iconURL: "") == "localhost")+ #expect(FaviconKey.siteKey(of: "custom-abc") == "custom-abc")+ }++ @Test func aNavigationKeepsTheIconOnTheSameHost() {+ let own = FaviconKey.key(site: "localhost", iconURL: "http://localhost:3000/favicon.ico")+ #expect(FaviconKey.key(after: own, goingTo: "http://localhost:3000/settings") == own)+ #expect(FaviconKey.key(after: own, goingTo: "https://example.org/") == "example.org")+ #expect(FaviconKey.key(after: nil, goingTo: "https://example.org/") == "example.org")+ #expect(FaviconKey.key(after: own, goingTo: "about:blank") == own)+ }++ @Test func backToThePinnedPageKeepsItsIconOnTheSameHost() {+ var tab = TabData(url: "https://example.org/home")+ tab.url = "https://other.net/page"+ tab.faviconKey = FaviconKey.key(site: "other.net", iconURL: "https://other.net/i.png")+ tab.returnHome()+ #expect(tab.faviconKey == "example.org")+ }}diff --git a/Sources/Cra/Services/BrowserManager.swift b/Sources/Cra/Services/BrowserManager.swiftindex 67fdb1d..19e2153 100644--- a/Sources/Cra/Services/BrowserManager.swift+++ b/Sources/Cra/Services/BrowserManager.swift@@ -1098,10 +1098,16 @@ final class BrowserManager: NSObject, @preconcurrency CraBrowserDelegate {func browser(_ browser: CraBrowser, didLoadFavicon image: NSImage, forURL iconURL: String) {guard let id = tabID(of: browser), let tab = store.item(id)?.tab else { return }- let key = FaviconKey.key(for: tab.url) ?? tab.faviconKey- guard let key else { return }- favicons.store(image, forKey: key, persist: !browser.isIncognito)- if tab.faviconKey != key { store.updateTab(id, url: tab.url) }+ // The tab shows its own page's icon; the site key keeps the latest for suggestions and+ // pages not loaded yet. Tabs on one host (AWS, localhost) no longer share one (#211).+ guard let site = FaviconKey.key(for: tab.url) ?? tab.faviconKey.map(FaviconKey.siteKey(of:)) else { return }+ let key = FaviconKey.key(site: site, iconURL: iconURL)+ let persist = !browser.isIncognito+ favicons.store(image, forKey: site, persist: persist)+ // A page that draws its icon (data:, an unread badge) would leave a file per count.+ let fetched = iconURL.hasPrefix("https:") || iconURL.hasPrefix("http:")+ if key != site { favicons.store(image, forKey: key, persist: persist && fetched) }+ store.setFaviconKey(id, key)}func browser(_ browser: CraBrowser, didReportThemeColor hex: String) {diff --git a/Sources/Cra/Services/FaviconCache.swift b/Sources/Cra/Services/FaviconCache.swiftindex 6bfb372..e2efafe 100644--- a/Sources/Cra/Services/FaviconCache.swift+++ b/Sources/Cra/Services/FaviconCache.swift@@ -1,7 +1,7 @@import AppKitimport Observation-/// Favicons keyed by host (FaviconKey). Chromium hands us PNG bytes through the bridge;+/// Favicons keyed by host or by a tab's own icon (FaviconKey). Chromium hands us PNG bytes through the bridge;/// we keep them in memory and on disk so the sidebar is populated on launch/// before any tab has loaded (Arc: SidebarItemsFaviconCache).@MainActor@@ -24,7 +24,9 @@ final class FaviconCache {return img}}- return nil+ // A tab's own icon not on disk (cleared, or saved by an incognito page): the site's (#211).+ let site = FaviconKey.siteKey(of: key)+ return site == key ? nil : image(forKey: site)}func image(for urlString: String) -> NSImage? { image(forKey: FaviconKey.key(for: urlString)) }diff --git a/Sources/Cra/Services/SidebarStore.swift b/Sources/Cra/Services/SidebarStore.swiftindex 94b15f6..867481f 100644--- a/Sources/Cra/Services/SidebarStore.swift+++ b/Sources/Cra/Services/SidebarStore.swift@@ -499,7 +499,7 @@ final class SidebarStore {t.url = url// A pinned tab that never had a page (a new pane of a pinned split) takes its first one as home.if !isPinned(id) || t.savedURL.isEmpty || t.savedURL == "about:blank" { t.savedURL = url }- if let key = FaviconKey.key(for: url) { t.faviconKey = key }+ t.faviconKey = FaviconKey.key(after: t.faviconKey, goingTo: url)}// A live folder's tab keeps the pull request's title (Arc shows that, not the page's).if let title, !(t.liveItemURL.map { t.url.hasPrefix($0) } ?? false) { t.noteTitle(title, pinned: isPinned(id)) }@@ -516,6 +516,16 @@ final class SidebarStore {sidebarFile.save(document, delay: 2)}+ /// The tab's page loaded its own icon, cached under `key` (#211).+ func setFaviconKey(_ id: UUID, _ key: String) {+ guard var it = items[id], var t = it.tab, t.faviconKey != key else { return }+ t.faviconKey = key+ it.data = .tab(t)+ items[id] = it+ // Rows and the toolbar observe the tab; the window's layout doesn't change.+ sidebarFile.save(document, delay: 2)+ }+/// Only saved: nothing on screen reads it back until the tab's page is made again. Incognito/// tabs are never restored, so they keep none.func setThemeColor(_ id: UUID, _ hex: String) {diff --git a/Tests/BrowserManagerSupport/AppServices.swift b/Tests/BrowserManagerSupport/AppServices.swiftindex efb1fca..0b05060 100644--- a/Tests/BrowserManagerSupport/AppServices.swift+++ b/Tests/BrowserManagerSupport/AppServices.swift@@ -37,6 +37,7 @@ enum TestRoot { case today }if let title { items[id]?.tab?.savedTitle = title }}func setThemeColor(_ id: UUID, _ hex: String) { items[id]?.tab?.themeColor = hex }+ func setFaviconKey(_ id: UUID, _ key: String) { items[id]?.tab?.faviconKey = key }func recordVisit(url: String, title: String) {}func clearHistory(_ range: BrowsingDataClear.Range) {}func selectTab(_ id: UUID, window: UUID?) {}@@ -60,7 +61,11 @@ enum TestRoot { case today }var webStoreProfiles: [String] = []func installFromWebStore(using browser: CraBrowser, profile: String) { webStoreProfiles.append(profile) }}-enum FaviconKey { static func key(for url: String) -> String? { url } }+enum FaviconKey {+ static func key(for url: String) -> String? { url }+ static func key(site: String, iconURL: String) -> String { site + "#" + iconURL }+ static func siteKey(of key: String) -> String { key }+}enum BrowsingDataClear {enum Range { case allTime }static func historyRange(messageJSON: String, now: Date) -> Range? { nil }diff --git a/Tests/Fixtures/Pages/icon-blue.png b/Tests/Fixtures/Pages/icon-blue.pngnew file mode 100644index 0000000..e3dab5cBinary files /dev/null and b/Tests/Fixtures/Pages/icon-blue.png differdiff --git a/Tests/Fixtures/Pages/icon-orange.png b/Tests/Fixtures/Pages/icon-orange.pngnew file mode 100644index 0000000..36659e5Binary files /dev/null and b/Tests/Fixtures/Pages/icon-orange.png differdiff --git a/Tests/Scenarios/same-host-favicons.scenario b/Tests/Scenarios/same-host-favicons.scenarionew file mode 100644index 0000000..e695449--- /dev/null+++ b/Tests/Scenarios/same-host-favicons.scenario@@ -0,0 +1,25 @@+# Two tabs on one host keep their own icons (GitHub #211: every AWS console or localhost tab+# took the icon the last one loaded). Both pages come from the same local server.+budget rss=600 stalls50=10+port 47124+cra://debug?windowSize=1280x800+cra://open?url=${PAGE}?n=orange+expect-log loaded 127\.0\.0\.1 \(HTTP 200\)+cra://debug?pageJS=document.title='Orange',document.head.append(Object.assign(document.createElement('link'),{rel:'icon',href:'icon-orange.png'}))+wait 3+cra://open?url=${PAGE}?n=blue+wait 3+cra://debug?pageJS=document.title='Blue',document.head.append(Object.assign(document.createElement('link'),{rel:'icon',href:'icon-blue.png'}))+wait 3+snapshot TabRowView,0 same-host-favicon-newer+snapshot TabRowView,1 same-host-favicon-older+# A page on the same host, with no icon of its own yet, keeps the tab's.+cra://navigate?url=${PAGE}?n=blue2+wait 3+snapshot TabRowView,0 same-host-favicon-newer-same-host+cra://debug?checkStore=1+# Each tab's icon is saved: after a relaunch, before any page loads, both are still their own.+relaunch+wait 3+snapshot TabRowView,1 same-host-favicon-older+cra://debug?checkStore=1diff --git a/Tests/Snapshots/same-host-favicon-newer-same-host.png b/Tests/Snapshots/same-host-favicon-newer-same-host.pngnew file mode 100644index 0000000..ec45b9cBinary files /dev/null and b/Tests/Snapshots/same-host-favicon-newer-same-host.png differdiff --git a/Tests/Snapshots/same-host-favicon-newer.png b/Tests/Snapshots/same-host-favicon-newer.pngnew file mode 100644index 0000000..9b6ee67Binary files /dev/null and b/Tests/Snapshots/same-host-favicon-newer.png differdiff --git a/Tests/Snapshots/same-host-favicon-older.png b/Tests/Snapshots/same-host-favicon-older.pngnew file mode 100644index 0000000..64f10eaBinary files /dev/null and b/Tests/Snapshots/same-host-favicon-older.png differdiff --git a/Tests/StoreTests.swift b/Tests/StoreTests.swiftindex 1508ba6..6946af7 100644--- a/Tests/StoreTests.swift+++ b/Tests/StoreTests.swift@@ -30,6 +30,7 @@ enum StoreTests {persistenceFaults()incognitoStaysOffDisk()pinnedURL()+ tabFavicons()moveTabToSpace()folderPeek()closeSuccessor()@@ -421,6 +422,26 @@ enum StoreTests {expect(store.space(id)?.theme == red && savedTheme() == red, "Save in the sheet writes the theme and ends the preview")}+ /// Tabs on one host keep their own icons (GitHub #211: every AWS console or localhost tab+ /// took the icon the last one loaded).+ @MainActor+ private static func tabFavicons() {+ let store = AppComposition(clock: ManualScheduler()).store+ let rds = store.createTab(url: "https://console.aws.amazon.com/rds/home", select: false)+ let eks = store.createTab(url: "https://console.aws.amazon.com/eks/home", select: false)+ let rdsIcon = FaviconKey.key(for: "https://console.aws.amazon.com/rds/home", iconURL: "https://console.aws.amazon.com/rds/favicon.ico")!+ let eksIcon = FaviconKey.key(for: "https://console.aws.amazon.com/eks/home", iconURL: "https://console.aws.amazon.com/eks/favicon.ico")!+ store.setFaviconKey(rds.id, rdsIcon)+ store.setFaviconKey(eks.id, eksIcon)+ expect(store.item(rds.id)?.tab?.iconKey == rdsIcon && store.item(eks.id)?.tab?.iconKey == eksIcon && rdsIcon != eksIcon,+ "two tabs on one host show their own icons")+ store.updateTab(rds.id, url: "https://console.aws.amazon.com/rds/home#databases", title: "Databases")+ expect(store.item(rds.id)?.tab?.faviconKey == rdsIcon, "a page on the same host keeps the tab's icon until its own loads")+ store.updateTab(rds.id, url: "https://example.org/")+ expect(store.item(rds.id)?.tab?.faviconKey == "example.org", "another host shows that site's icon")+ expect(store.item(eks.id)?.tab?.faviconKey == eksIcon, "and the other tab never changes")+ }+/// Edit Pinned URL… (#54, PR #55 by Anoninz): a new pinned URL keeps the item's/// name, icon, colour and place, and survives a relaunch.@MainActor