Skip to content

Commit

Permalink
Make sure control items maintain the correct order
Browse files Browse the repository at this point in the history
Closes #164
  • Loading branch information
jordanbaird committed Jun 25, 2024
1 parent 18a31a0 commit 31de70e
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions Ice/MenuBar/MenuBarItemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,27 @@ class MenuBarItemManager: ObservableObject {

func performSetup() {
configureCancellables()
DispatchQueue.main.async {
self.cacheCurrentMenuBarItems()
}
}

private func configureCancellables() {
var c = Set<AnyCancellable>()

Timer.publish(every: 3, on: .main, in: .default)
.autoconnect()
.merge(with: Just(.now))
.sink { [weak self] _ in
self?.cacheCurrentMenuBarItems()
guard let self else {
return
}
Task {
let items = MenuBarItem.getMenuBarItemsPrivateAPI(onScreenOnly: false)
do {
try await self.arrangeItems(items)
} catch {
Logger.itemManager.error("Error arranging items: \(error)")
}
self.cacheItems(items)
}
}
.store(in: &c)

Expand All @@ -52,7 +61,7 @@ class MenuBarItemManager: ObservableObject {

extension MenuBarItemManager {
/// Caches the current menu bar items.
private func cacheCurrentMenuBarItems() {
private func cacheItems(_ items: [MenuBarItem]) {
guard tempShownItemsInfo.isEmpty else {
Logger.itemManager.info("Items are temporarily shown, so deferring cache")
return
Expand All @@ -65,8 +74,6 @@ extension MenuBarItemManager {
}
}

let items = MenuBarItem.getMenuBarItemsPrivateAPI(onScreenOnly: false)

guard
let hiddenControlItem = items.first(where: { $0.info == .hiddenControlItem }),
let alwaysHiddenControlItem = items.first(where: { $0.info == .alwaysHiddenControlItem })
Expand Down Expand Up @@ -865,6 +872,24 @@ extension MenuBarItemManager {
}
}

// MARK: - Arrange Items

extension MenuBarItemManager {
func arrangeItems(_ items: [MenuBarItem]) async throws {
guard
let hiddenControlItem = items.first(where: { $0.info == .hiddenControlItem }),
let alwaysHiddenControlItem = items.first(where: { $0.info == .alwaysHiddenControlItem })
else {
return
}

// make sure the always-hidden control item is to the left of the hidden control item
if hiddenControlItem.frame.maxX <= alwaysHiddenControlItem.frame.minX {
try await slowMove(item: alwaysHiddenControlItem, to: .leftOfItem(hiddenControlItem))
}
}
}

// MARK: - CGEvent Helpers

private extension CGEvent {
Expand Down

0 comments on commit 31de70e

Please sign in to comment.