Skip to content

Commit 2bbada3

Browse files
committed
Cleanup and documentation
1 parent ca83f94 commit 2bbada3

File tree

4 files changed

+60
-26
lines changed

4 files changed

+60
-26
lines changed

Ice/IceBar/IceBar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ private struct IceBarItemView: View {
382382
.contentShape(Rectangle())
383383
.onTapGesture {
384384
closePanel()
385-
itemManager.tempShowItem(item)
385+
itemManager.tempShowItem(item, clickWhenFinished: true)
386386
}
387387
}
388388
}

Ice/LayoutBar/LayoutBarPaddingView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class LayoutBarPaddingView: NSView {
134134
Task {
135135
do {
136136
try await container.itemManager.move(item: item, to: destination)
137-
container.itemManager.removeTempShownItem(with: item.info)
137+
container.itemManager.removeTempShownItemFromCache(with: item.info)
138138
} catch {
139139
Logger.layoutBar.error("Error moving menu bar item: \(error)")
140140
let alert = NSAlert(error: error)

Ice/MenuBar/MenuBarItem.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ struct MenuBarItem {
3939
return !immovableItems.contains(info)
4040
}
4141

42+
/// A Boolean value that indicates whether the item can be hidden.
43+
var canBeHidden: Bool {
44+
let nonHideableItems = Set(MenuBarItemInfo.nonHideableItems)
45+
return !nonHideableItems.contains(info)
46+
}
47+
4248
/// The process identifier of the application that owns the item.
4349
var ownerPID: pid_t {
4450
window.ownerPID
@@ -111,11 +117,7 @@ struct MenuBarItem {
111117

112118
/// A string to use for logging purposes.
113119
var logString: String {
114-
if owningApplication == .current {
115-
title ?? "<null>"
116-
} else {
117-
displayName
118-
}
120+
String(describing: info)
119121
}
120122

121123
/// Creates a menu bar item from the given window.
@@ -301,7 +303,7 @@ extension MenuBarItemInfo {
301303
static let immovableItems = [clock, siri, controlCenter]
302304

303305
/// An array of items that can be moved, but cannot be hidden.
304-
static let nonHideableItems = [audioVideoModule, musicRecognition]
306+
static let nonHideableItems = [audioVideoModule, faceTime, musicRecognition]
305307

306308
/// Information for an item that represents the Ice icon.
307309
static let iceIcon = MenuBarItemInfo(
@@ -346,6 +348,12 @@ extension MenuBarItemInfo {
346348
title: "AudioVideoModule"
347349
)
348350

351+
/// Information for the "FaceTime" item.
352+
static let faceTime = MenuBarItemInfo(
353+
namespace: .controlCenter,
354+
title: "FaceTime"
355+
)
356+
349357
/// Information for the "MusicRecognition" (a.k.a. "Shazam") item.
350358
static let musicRecognition = MenuBarItemInfo(
351359
namespace: .controlCenter,

Ice/MenuBar/MenuBarItemManager.swift

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ class MenuBarItemManager: ObservableObject {
2121

2222
private var cancellables = Set<AnyCancellable>()
2323

24-
var isMenuOpen: Bool {
25-
WindowInfo.getAllWindows().contains { window in
26-
window.layer == 101
27-
}
28-
}
29-
3024
init(appState: AppState) {
3125
self.appState = appState
3226
}
@@ -51,6 +45,7 @@ class MenuBarItemManager: ObservableObject {
5145
cancellables = c
5246
}
5347

48+
/// Caches the current menu bar items.
5449
private func cacheMenuBarItems() {
5550
guard tempShownItemsInfo.isEmpty else {
5651
Logger.itemManager.info("Items are temporarily shown, so deferring cache")
@@ -69,8 +64,8 @@ class MenuBarItemManager: ObservableObject {
6964
update(&cachedMenuBarItems) { cachedItems in
7065
cachedItems.removeAll()
7166
for item in items {
72-
// audio video module cannot be hidden
73-
guard item.info != .audioVideoModule else {
67+
// only items that can be hidden should be included
68+
guard item.canBeHidden else {
7469
continue
7570
}
7671

@@ -102,6 +97,7 @@ class MenuBarItemManager: ObservableObject {
10297
}
10398
}
10499

100+
/// Gets the destination to return the given item to after it is temporarily shown.
105101
private func getReturnDestination(for item: MenuBarItem, in items: [MenuBarItem]) -> MoveDestination? {
106102
let info = item.info
107103
if let index = items.firstIndex(where: { $0.info == info }) {
@@ -114,6 +110,8 @@ class MenuBarItemManager: ObservableObject {
114110
return nil
115111
}
116112

113+
/// Schedules a timer for the given interval, attempting to rehide the current
114+
/// temporarily shown items when the timer fires.
117115
private func runTempShownItemTimer(for interval: TimeInterval) {
118116
tempShownItemsTimer?.invalidate()
119117
tempShownItemsTimer = .scheduledTimer(withTimeInterval: interval, repeats: false) { [weak self] timer in
@@ -127,7 +125,19 @@ class MenuBarItemManager: ObservableObject {
127125
}
128126
}
129127

130-
func tempShowItem(_ item: MenuBarItem) {
128+
/// Temporarily shows the given item.
129+
///
130+
/// This method moves the given item to the right of the control item for
131+
/// the "hidden" section. The item is cached alongside a destination that
132+
/// it will be automatically returned to. If `true` is passed to the
133+
/// `clickWhenFinished` parameter, the item is clicked once its movement
134+
/// is finished.
135+
///
136+
/// - Parameters:
137+
/// - item: An item to show.
138+
/// - clickWhenFinished: A Boolean value that indicates whether the item
139+
/// should be clicked once its movement has finished.
140+
func tempShowItem(_ item: MenuBarItem, clickWhenFinished: Bool) {
131141
let items = MenuBarItem.getMenuBarItemsPrivateAPI(onScreenOnly: false)
132142

133143
guard let destination = getReturnDestination(for: item, in: items) else {
@@ -142,19 +152,31 @@ class MenuBarItemManager: ObservableObject {
142152
tempShownItemsInfo.append((item, destination))
143153

144154
Task {
145-
MouseCursor.hide()
146-
do {
147-
try await slowMove(item: item, to: .rightOfItem(hiddenControlItem))
148-
try await leftClick(item: item)
149-
} catch {
150-
Logger.itemManager.error("ERROR: \(error)")
155+
if clickWhenFinished {
156+
MouseCursor.hide()
157+
do {
158+
try await slowMove(item: item, to: .rightOfItem(hiddenControlItem))
159+
try await leftClick(item: item)
160+
} catch {
161+
Logger.itemManager.error("ERROR: \(error)")
162+
}
163+
MouseCursor.show()
164+
} else {
165+
do {
166+
try await move(item: item, to: .rightOfItem(hiddenControlItem))
167+
} catch {
168+
Logger.itemManager.error("ERROR: \(error)")
169+
}
151170
}
152-
MouseCursor.show()
153171
}
154172

155173
runTempShownItemTimer(for: 20)
156174
}
157175

176+
/// Rehides all temporarily shown items.
177+
///
178+
/// If an item is currently showing its menu, this method waits for the menu
179+
/// to close before hiding the items.
158180
func rehideTempShownItems() async {
159181
if let menuWindow = WindowInfo.getAllWindows().first(where: { $0.layer == 101 }) {
160182
let menuCheckTask = Task.detached(timeout: .seconds(1)) {
@@ -184,7 +206,11 @@ class MenuBarItemManager: ObservableObject {
184206
tempShownItemsTimer = nil
185207
}
186208

187-
func removeTempShownItem(with info: MenuBarItemInfo) {
209+
/// Removes a temporarily shown item from the cache.
210+
///
211+
/// This has the effect of ensuring that the item will not be returned to
212+
/// its previous location.
213+
func removeTempShownItemFromCache(with info: MenuBarItemInfo) {
188214
tempShownItemsInfo.removeAll(where: { $0.item.info == info })
189215
}
190216
}
@@ -200,6 +226,7 @@ extension MenuBarItemManager {
200226
/// The menu bar item will be moved to the right of the given menu bar item.
201227
case rightOfItem(MenuBarItem)
202228

229+
/// A string to use for logging purposes.
203230
var logString: String {
204231
switch self {
205232
case .leftOfItem(let item):
@@ -886,5 +913,4 @@ private extension CGEvent {
886913
private extension Logger {
887914
static let itemManager = Logger(category: "MenuBarItemManager")
888915
static let move = Logger(category: "Move")
889-
static let arrange = Logger(category: "Arrange")
890916
}

0 commit comments

Comments
 (0)