Skip to content

Commit

Permalink
Disabled debug logging by default
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Oct 10, 2024
1 parent ada1c2d commit 7d2d07a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ let package = Package(
targets: [
.target(
name: "MenuBarExtraAccess",
swiftSettings: [.define("DEBUG", .when(configuration: .debug))]
swiftSettings: [
// un-comment to enable debug logging
// .define("MENUBAREXTRAACCESS_DEBUG_LOGGING=1")
]
),
.testTarget(name: "MenuBarExtraAccessTests", dependencies: ["MenuBarExtraAccess"]),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension View {
self
.onAppear {
guard let window = MenuBarExtraUtils.window(for: .index(index)) else {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Cannot call introspection block for status item because its window could not be found.")
#endif

Expand Down
14 changes: 7 additions & 7 deletions Sources/MenuBarExtraAccess/MenuBarExtraAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ struct MenuBarExtraAccess<Content: Scene>: Scene {
// for example.
observerContainer.setupStatusItemButtonStateObserver {
MenuBarExtraUtils.newStatusItemButtonStateObserver(index: index) { change in
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Status item button state observer: called with change: \(change.newValue?.description ?? "nil")")
#endif

Expand All @@ -105,7 +105,7 @@ struct MenuBarExtraAccess<Content: Scene>: Scene {
guard let newVal = change.newValue else { return }
let newBool = newVal != .off
if isMenuPresented != newBool {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Status item button state observer: Setting isMenuPresented to \(newBool)")
#endif

Expand All @@ -119,15 +119,15 @@ struct MenuBarExtraAccess<Content: Scene>: Scene {
// observerContainer.setupGlobalMouseDownMonitor {
// // note that this won't fire when mouse events within the app cause the window to dismiss
// MenuBarExtraUtils.newGlobalMouseDownEventsMonitor { event in
// #if DEBUG
// #if MENUBAREXTRAACCESS_DEBUG_LOGGING
// print("Global mouse-down events monitor: called with event: \(event.type)")
// #endif
//
// // close window when user clicks outside of it
//
// MenuBarExtraUtils.setPresented(for: .index(index), state: false)
//
// #if DEBUG
// #if MENUBAREXTRAACCESS_DEBUG_LOGGING
// print("Global mouse-down events monitor: Setting isMenuPresented to false")
// #endif
//
Expand All @@ -138,22 +138,22 @@ struct MenuBarExtraAccess<Content: Scene>: Scene {
observerContainer.setupWindowObservers(
index: index,
didBecomeKey: { window in
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("MenuBarExtra index \(index) drop-down window did become key.")
#endif

MenuBarExtraUtils.setKnownPresented(for: .index(index), state: true)
isMenuPresented = true
},
didResignKey: { window in
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("MenuBarExtra index \(index) drop-down window did resign as key.")
#endif

// it's possible for a window to resign key without actually closing, so let's
// close it as a failsafe.
if window.isVisible {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Closing MenuBarExtra index \(index) drop-down window as a result of it resigning as key.")
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum MenuBarExtraUtils {

/// Toggle MenuBarExtra menu/window presentation state.
static func togglePresented(for ident: StatusItemIdentity? = nil) {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("MenuBarExtraUtils.\(#function) called for status item \(ident?.description ?? "nil")")
#endif

Expand All @@ -25,7 +25,7 @@ enum MenuBarExtraUtils {

/// Set MenuBarExtra menu/window presentation state.
static func setPresented(for ident: StatusItemIdentity? = nil, state: Bool) {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("MenuBarExtraUtils.\(#function) called for status item \(ident?.description ?? "nil") with state \(state)")
#endif

Expand All @@ -35,7 +35,7 @@ enum MenuBarExtraUtils {

/// Set MenuBarExtra menu/window presentation state only when its state is reliably known.
static func setKnownPresented(for ident: StatusItemIdentity? = nil, state: Bool) {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("MenuBarExtraUtils.\(#function) called for status item \(ident?.description ?? "nil") with state \(state)")
#endif

Expand Down Expand Up @@ -111,7 +111,7 @@ extension MenuBarExtraUtils {
switch ident {
case .id(let menuBarExtraID):
guard let match = menuBarWindows.first(where: { $0.menuBarExtraID == menuBarExtraID }) else {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("MenuBarExtraUtils.\(#function): Window could not be found for status item with ID \"\(menuBarExtraID).")
#endif

Expand All @@ -138,7 +138,7 @@ extension MenuBarExtraUtils {
_ handler: @escaping (_ change: NSKeyValueObservedChange<NSControl.StateValue>) -> Void
) -> NSStatusItem.ButtonStateObserver? {
guard let statusItem = MenuBarExtraUtils.statusItem(for: .index(index)) else {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Can't register menu bar extra state observer: Can't find status item. It may not yet exist.")
#endif

Expand All @@ -147,7 +147,7 @@ extension MenuBarExtraUtils {

guard let observer = statusItem.stateObserverMenuBased(handler)
else {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Can't register menu bar extra state observer: Can't generate observer.")
#endif

Expand Down Expand Up @@ -189,7 +189,7 @@ extension MenuBarExtraUtils {
index: Int
) -> NSStatusItem.ButtonStatePublisher? {
guard let statusItem = MenuBarExtraUtils.statusItem(for: .index(index)) else {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Can't register menu bar extra state observer: Can't find status item. It may not yet exist.")
#endif

Expand All @@ -198,7 +198,7 @@ extension MenuBarExtraUtils {

guard let publisher = statusItem.buttonStatePublisher()
else {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Can't register menu bar extra state observer: Can't generate publisher.")
#endif

Expand Down Expand Up @@ -279,7 +279,7 @@ extension NSStatusItem {
guard let behavior = button?.target, // SwiftUI.WindowMenuBarExtraBehavior <- internal
let mirror = Mirror(reflecting: behavior).superclassMirror
else {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Could not find status item's target.")
#endif

Expand All @@ -301,7 +301,7 @@ extension NSStatusItem {
// however, WindowMenuBarExtraBehavior does contain an explicit `isMenuBased` Bool we can read
guard let mirror = Mirror(reflecting: behavior).superclassMirror
else {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Could not find status item's target.")
#endif

Expand Down Expand Up @@ -362,7 +362,7 @@ extension Mirror {
return hashed
}

#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("Could not determine MenuBarExtra ID")
#endif

Expand Down
8 changes: 4 additions & 4 deletions Sources/MenuBarExtraAccess/NSStatusItem Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension NSStatusItem {
/// Toggles the menu/window state by mimicking a menu item button press.
@_disfavoredOverload
public func togglePresented() {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("NSStatusItem.\(#function) called")
#endif

Expand All @@ -31,7 +31,7 @@ extension NSStatusItem {
/// Toggles the menu/window state by mimicking a menu item button press.
@_disfavoredOverload
internal func setPresented(state: Bool) {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("NSStatusItem.\(#function) called with state: \(state)")
#endif

Expand All @@ -46,13 +46,13 @@ extension NSStatusItem {

@_disfavoredOverload
internal func updateHighlight() {
#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("NSStatusItem.\(#function) called")
#endif

let s = button?.state != .off

#if DEBUG
#if MENUBAREXTRAACCESS_DEBUG_LOGGING
print("NSStatusItem.\(#function): State detected as \(s)")
#endif

Expand Down

0 comments on commit 7d2d07a

Please sign in to comment.