Skip to content

Commit

Permalink
Update menu bar item getters
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Jul 21, 2024
1 parent ea8cacb commit 9c01743
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Ice/Events/EventManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ extension EventManager {
else {
return false
}
let menuBarItems = MenuBarItem.getMenuBarItemsPrivateAPI(for: screen.displayID, onScreenOnly: true)
let menuBarItems = MenuBarItem.getMenuBarItems(on: screen.displayID, using: .privateAPI, onScreenOnly: true)
return menuBarItems.contains { $0.frame.contains(mouseLocation) }
}

Expand Down
32 changes: 23 additions & 9 deletions Ice/MenuBar/MenuBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,13 @@ struct MenuBarItem {

// MARK: MenuBarItem Getters
extension MenuBarItem {
/// Returns an array of menu bar items in the menu bar for the given display.
static func getMenuBarItemsCoreGraphics(for display: CGDirectDisplayID, onScreenOnly: Bool) -> [MenuBarItem] {
/// The method to use to get the menu bar items.
enum GetterMethod {
case coreGraphics
case privateAPI
}

private static func getMenuBarItemsCoreGraphics(on display: CGDirectDisplayID, onScreenOnly: Bool) -> [MenuBarItem] {
let windows = if onScreenOnly {
WindowInfo.getOnScreenWindows(excludeDesktopWindows: true)
} else {
Expand All @@ -188,12 +193,7 @@ extension MenuBarItem {
.sortedByOrderInMenuBar()
}

/// Returns an array of menu bar items using private APIs to retrieve the
/// windows.
///
/// - Parameter onScreenOnly: A Boolean value that indicates whether only
/// the items that are on screen should be returned.
static func getMenuBarItemsPrivateAPI(for display: CGDirectDisplayID, onScreenOnly: Bool) -> [MenuBarItem] {
private static func getMenuBarItemsPrivateAPI(on display: CGDirectDisplayID, onScreenOnly: Bool) -> [MenuBarItem] {
var option: Bridging.WindowListOption = [.menuBarItems]
if onScreenOnly {
option.insert(.onScreen)
Expand All @@ -212,7 +212,21 @@ extension MenuBarItem {
.sortedByOrderInMenuBar()
}

/// Returns an array of menu bar items using private APIs to retrieve
/// Returns an array of menu bar items in the menu bar on the given display.
///
/// - Parameters:
/// - display: The display to retrieve the menu bar items on.
/// - method: The method to use to get the items.
/// - onScreenOnly: A Boolean value that indicates whether only the items
/// that are on screen should be returned.
static func getMenuBarItems(on display: CGDirectDisplayID, using method: GetterMethod, onScreenOnly: Bool) -> [MenuBarItem] {
switch method {
case .coreGraphics: getMenuBarItemsCoreGraphics(on: display, onScreenOnly: onScreenOnly)
case .privateAPI: getMenuBarItemsPrivateAPI(on: display, onScreenOnly: onScreenOnly)
}
}

/// Returns an array of menu bar items using a private API to retrieve
/// the windows.
///
/// - Parameters:
Expand Down
2 changes: 1 addition & 1 deletion Ice/MenuBar/MenuBarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ final class MenuBarManager: ObservableObject {
return
}

let items = MenuBarItem.getMenuBarItemsCoreGraphics(for: displayID, onScreenOnly: true)
let items = MenuBarItem.getMenuBarItems(on: displayID, using: .coreGraphics, onScreenOnly: true)

// get the leftmost item on the screen; the application menu should
// be hidden if the item's minX is close to the maxX of the menu
Expand Down
2 changes: 1 addition & 1 deletion Ice/MenuBarAppearance/MenuBarOverlayPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ private class MenuBarOverlayPanelContentView: NSView {
return CGRect(x: rect.minX, y: rect.minY, width: maxX, height: rect.height)
}()
let trailingPathBounds: CGRect = {
let items = MenuBarItem.getMenuBarItemsPrivateAPI(for: screen.displayID, onScreenOnly: true)
let items = MenuBarItem.getMenuBarItems(on: screen.displayID, using: .privateAPI, onScreenOnly: true)
guard !items.isEmpty else {
return .zero
}
Expand Down

0 comments on commit 9c01743

Please sign in to comment.