Skip to content
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
87 changes: 72 additions & 15 deletions HiddenBarTests/MenuBarEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,17 @@ final class MultiDisplayLayoutTests: XCTestCase {
systemIdentifier: "menu-bar-overflow-indicator")

func testOverflowIndicatorMarksTheReadIncompleteAndIsNoItem() {
let layout = MenuBarLayoutResolver.resolve(
inventory: [overflow, listed("com.visible", on: portrait, fromRight: 150)],
separatorFrame: arrow(on: portrait, fromRight: 200), alwaysHiddenSeparatorFrame: nil,
displays: bothDisplays, isLTR: true, excludingBundle: nil)
XCTAssertTrue(layout.isIncomplete)
XCTAssertEqual(layout.sections, ["com.visible": .visible])
XCTAssertEqual(layout.unprovenVisibleSystemItems, [], "the chevron is not an item macOS could be asked to keep")
for identifier in ["menu-bar-overflow-indicator", "menu-bar-overflow-indicator-expanded"] {
var indicator = overflow
indicator.systemIdentifier = identifier
let layout = MenuBarLayoutResolver.resolve(
inventory: [indicator, listed("com.visible", on: portrait, fromRight: 150)],
separatorFrame: arrow(on: portrait, fromRight: 200), alwaysHiddenSeparatorFrame: nil,
displays: bothDisplays, isLTR: true, excludingBundle: nil)
XCTAssertTrue(layout.isIncomplete, identifier)
XCTAssertEqual(layout.sections, ["com.visible": .visible])
XCTAssertEqual(layout.unprovenVisibleSystemItems, [], "the chevron is not an item macOS could be asked to keep")
}
}

func testIncompleteReadTakesMissingAppsFromAnEarlierRead() {
Expand All @@ -330,23 +334,76 @@ final class MultiDisplayLayoutTests: XCTestCase {
["com.kept": .visible, "com.moved": .hidden, "com.hidden": .hidden])
}

private func engineOnBothDisplays(_ inventory: FakeInventory, _ visibility: FakeVisibility, arrowOn display: MenuBarDisplay) -> NativeVisibilityEngine {
private func engineOnBothDisplays(_ inventory: FakeInventory, _ visibility: FakeVisibility, arrowOn display: MenuBarDisplay,
running: Set<String> = [],
memory: MenuBarSectionMemory = InMemorySectionMemory(),
pinned: MenuBarSectionMemory = InMemorySectionMemory(),
pins: Bool = false) -> NativeVisibilityEngine {
let items = FakeItems()
return NativeVisibilityEngine(items: items, inventory: inventory, visibility: visibility,
ownBundleIdentifier: "com.dwarvesv.minimalbar",
itemFrame: { _ in arrow(on: display, fromRight: 200) },
displays: { bothDisplays }, isLTR: { true })
itemFrame: { $0 === items.toggleItem ? arrow(on: display, fromRight: 200) : nil },
displays: { bothDisplays }, runningBundleIdentifiers: { running }, isLTR: { true },
sectionMemory: memory, pinnedSections: pinned, pinsSections: { pins })
}

func testOverflowingFirstReadLeavesTheBarWhole() {
func testOverflowingFirstReadHidesListedAppsAndKeepsUnlistedAppsVisible() {
let inventory = FakeInventory()
inventory.items = [overflow, listed("com.visible", on: portrait, fromRight: 150)]
inventory.items = [overflow, listed("com.visible", on: portrait, fromRight: 150),
listed("com.hidden", on: portrait, fromRight: 300)]
let visibility = FakeVisibility()
let engine = engineOnBothDisplays(inventory, visibility, arrowOn: portrait)
let engine = engineOnBothDisplays(inventory, visibility, arrowOn: portrait,
running: ["com.unlisted", "com.apple.MenuBarAgent", "com.dwarvesv.minimalbar"])
var result: CollapseResult?
engine.collapse { result = $0 }
XCTAssertEqual(result, .unavailable)
XCTAssertTrue(visibility.requests.isEmpty)
XCTAssertEqual(visibility.requests.first?.bundles, ["com.dwarvesv.minimalbar", "com.unlisted", "com.visible"])
XCTAssertEqual(engine.menuBarItemOwners, ["com.hidden", "com.visible"], "an unlisted running app is not a known menu-bar owner")
guard !visibility.requests.isEmpty else { return }
visibility.succeed(0)
XCTAssertEqual(result, .collapsed)
XCTAssertEqual(engine.state, .collapsed)
}

func testOverflowingFirstReadUsesRememberedSectionsWhenPinsAreOff() {
let inventory = FakeInventory()
inventory.items = [overflow, listed("com.visible", on: portrait, fromRight: 150)]
let visibility = FakeVisibility()
let memory = InMemorySectionMemory()
memory.sections = ["com.saved-hidden": .hidden, "com.saved-visible": .visible]
let pinned = InMemorySectionMemory()
pinned.sections = ["com.saved-hidden": .visible]
let engine = engineOnBothDisplays(inventory, visibility, arrowOn: portrait,
running: ["com.saved-hidden", "com.saved-visible", "com.unknown"],
memory: memory, pinned: pinned)
engine.collapse { _ in }
XCTAssertEqual(visibility.requests.first?.bundles,
["com.dwarvesv.minimalbar", "com.saved-visible", "com.unknown", "com.visible"])
XCTAssertEqual(engine.layout?.sections["com.saved-hidden"], .hidden)
}

func testOverflowingFirstReadUsesPinnedSectionsWithoutInventingDrags() {
let inventory = FakeInventory()
inventory.items = [overflow, listed("com.visible", on: portrait, fromRight: 150)]
let visibility = FakeVisibility()
let memory = InMemorySectionMemory()
memory.sections = ["com.saved": .visible]
let pinned = InMemorySectionMemory()
pinned.sections = ["com.saved": .hidden]
let engine = engineOnBothDisplays(inventory, visibility, arrowOn: portrait,
running: ["com.saved", "com.unknown"],
memory: memory, pinned: pinned, pins: true)
engine.collapse { _ in }
XCTAssertEqual(visibility.requests.first?.bundles, ["com.dwarvesv.minimalbar", "com.unknown", "com.visible"])
XCTAssertEqual(pinned.sections["com.saved"], .hidden)
XCTAssertEqual(memory.sections["com.saved"], .visible, "an omitted icon was not observed moving")
XCTAssertNil(pinned.sections["com.unknown"], "a running app need not own any menu-bar icon")
guard !visibility.requests.isEmpty else { return }
visibility.succeed(0)
engine.expand()
inventory.items = [listed("com.saved", on: portrait, fromRight: 150)]
engine.collapse { _ in }
XCTAssertEqual(visibility.requests.last?.bundles, ["com.dwarvesv.minimalbar"])
XCTAssertEqual(pinned.sections["com.saved"], .hidden)
}

// Collapsed once on the wide display, then again while the narrow one is
Expand Down
3 changes: 2 additions & 1 deletion hidden/Features/Palette/PaletteSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ enum PaletteSnapshot {
wallpaper.frame = backdrop.bounds
wallpaper.startPoint = CGPoint(x: 0, y: 1)
wallpaper.endPoint = CGPoint(x: 1, y: 0)
wallpaper.colors = (appearance == .darkAqua ? [0x101A30, 0x1D1A36, 0x2E1C2E] : [0xD6E3F5, 0xE3DAF0, 0xF2E2D6]).map { hex in
let wallpaperColors: [Int] = appearance == .darkAqua ? [0x101A30, 0x1D1A36, 0x2E1C2E] : [0xD6E3F5, 0xE3DAF0, 0xF2E2D6]
wallpaper.colors = wallpaperColors.map { hex in
NSColor(srgbRed: CGFloat((hex >> 16) & 0xFF) / 255, green: CGFloat((hex >> 8) & 0xFF) / 255,
blue: CGFloat(hex & 0xFF) / 255, alpha: 1).cgColor
}
Expand Down
3 changes: 3 additions & 0 deletions hidden/Features/StatusBar/Engine/MenuBarEngineFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ enum MenuBarEngineFactory {
})
let engine = NativeVisibilityEngine(items: items,
captureActivity: CaptureActivityMonitor(),
runningBundleIdentifiers: {
Set(NSWorkspace.shared.runningApplications.compactMap(\.bundleIdentifier))
},
runsFromApplicationsFolder: runsFromApplicationsFolder,
pausesHidingForUnkeepableItems: { Preferences.pauseHidingForUnkeepableItems },
expandsWhenCaptureStarts: { Preferences.showWholeBarWhenCaptureStarts },
Expand Down
27 changes: 17 additions & 10 deletions hidden/Features/StatusBar/Engine/NativeVisibilityEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ final class NativeVisibilityEngine: MenuBarEngine {
private let ownBundleIdentifier: String?
private let itemFrame: (NSStatusItem) -> CGRect?
private let displays: () -> [MenuBarDisplay]
private let runningBundleIdentifiers: () -> Set<String>
private let isLTR: () -> Bool
private let runsFromApplicationsFolder: () -> Bool

Expand Down Expand Up @@ -180,6 +181,7 @@ final class NativeVisibilityEngine: MenuBarEngine {
ownBundleIdentifier: String? = Bundle.main.bundleIdentifier,
itemFrame: @escaping (NSStatusItem) -> CGRect? = { $0.button?.window?.frame },
displays: @escaping () -> [MenuBarDisplay] = MenuBarDisplay.current,
runningBundleIdentifiers: @escaping () -> Set<String> = { [] },
isLTR: @escaping () -> Bool = { Constant.isUsingLTRLanguage },
runsFromApplicationsFolder: @escaping () -> Bool = { true },
pausesHidingForUnkeepableItems: @escaping () -> Bool = { false },
Expand Down Expand Up @@ -208,6 +210,7 @@ final class NativeVisibilityEngine: MenuBarEngine {
self.ownBundleIdentifier = ownBundleIdentifier
self.itemFrame = itemFrame
self.displays = displays
self.runningBundleIdentifiers = runningBundleIdentifiers
self.isLTR = isLTR
self.runsFromApplicationsFolder = runsFromApplicationsFolder
self.sectionMemory = sectionMemory
Expand Down Expand Up @@ -454,14 +457,9 @@ final class NativeVisibilityEngine: MenuBarEngine {
isLTR: isLTR,
excludingBundle: self.ownBundleIdentifier)
if layout.isIncomplete {
// Apps macOS left out would be hidden although they may be
// right of the arrow; with no complete read to take them from,
// nothing may be hidden.
guard let complete = self.lastCompleteLayout else {
NSLog("NativeVisibility: the active menu bar overflows and no complete read exists yet; leaving the bar whole")
return body(nil)
if let complete = self.lastCompleteLayout {
layout = layout.fillingIn(from: complete)
}
layout = layout.fillingIn(from: complete)
NSLog("NativeVisibility: the active menu bar overflows; apps it does not list keep their last known section")
} else {
self.lastCompleteLayout = layout
Expand All @@ -482,8 +480,15 @@ final class NativeVisibilityEngine: MenuBarEngine {
// What the read listed, before apps that only started are filled in:
// those may have no menu-bar icon at all.
self.onUnrestrictedRead?(layout, inventory)
for bundle in self.launchedSinceRead {
// Started, but its icon was not listed yet.
var missing = self.launchedSinceRead
if layout.isIncomplete {
missing.formUnion(self.runningBundleIdentifiers())
}
for bundle in missing where layout.sections[bundle] == nil
&& bundle != self.ownBundleIdentifier
&& !MenuBarLayoutResolver.systemItemOwners.contains(bundle) {
// A launch or overflow can leave an app out of the read. Its
// saved section wins; unknown apps stay visible.
layout.sections[bundle] = self.rememberedSection(of: bundle)
}
NSLog("NativeVisibility: arrow at x=\(boundary.midX); visible \(layout.bundles(in: [.visible])), hidden \(layout.bundles(in: [.hidden])), always hidden \(layout.bundles(in: [.alwaysHidden]))")
Expand Down Expand Up @@ -697,7 +702,9 @@ final class NativeVisibilityEngine: MenuBarEngine {
var menuBarItemOwners: Set<String> {
let read = layout?.sections.keys.map { $0 } ?? []
let complete = lastCompleteLayout?.sections.keys.map { $0 } ?? []
return Set(read + complete)
// Overflow allows other running apps too; only observed owners can
// defer auto-collapse for a menu-bar popup.
return Set(read + complete).intersection(sectionMemory.sections.keys)
}

var hiddenSectionBundles: Set<String> {
Expand Down
3 changes: 2 additions & 1 deletion hidden/Features/StatusBar/Layout/MenuBarLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ enum MenuBarLayoutResolver {
}
continue
}
if systemItemOwners.contains(key), item.systemIdentifier == overflowIndicator {
if systemItemOwners.contains(key),
item.systemIdentifier == overflowIndicator || item.systemIdentifier == "\(overflowIndicator)-expanded" {
layout.isIncomplete = true
continue
}
Expand Down
1 change: 1 addition & 0 deletions hidden/Features/StatusBar/StatusBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ class StatusBarController: MenuBarItemProvider {
if wantsFullBar {
NSApp.setActivationPolicy(.regular)
}
autoCollapseIfNeeded()
}

private var collapsedArrowImage: NSImage? {
Expand Down