@@ -8,6 +8,7 @@ public extension Notification.Name {
8
8
9
9
final class AudioInput {
10
10
private var callback : ( Bool ) -> Void
11
+ private var timer : Timer ?
11
12
private var devices : Set < AudioDevice > {
12
13
didSet {
13
14
let added = devices. subtracting ( oldValue)
@@ -18,10 +19,23 @@ final class AudioInput {
18
19
}
19
20
}
20
21
}
22
+ private var hasBluetooth : Bool {
23
+ didSet {
24
+ guard hasBluetooth != oldValue else { return }
25
+
26
+ timer? . invalidate ( )
27
+ guard hasBluetooth == true else { return }
28
+
29
+ timer = Timer . scheduledTimer ( withTimeInterval: 1.0 , repeats: true ) { _ in
30
+ NotificationCenter . default. post ( name: . deviceIsRunningSomewhereDidChange, object: nil )
31
+ }
32
+ }
33
+ }
21
34
22
35
init ( _ callback: @escaping ( Bool ) -> Void ) {
23
36
self . devices = Set ( )
24
37
self . callback = callback
38
+ self . hasBluetooth = false
25
39
}
26
40
27
41
private func updateDeviceList( ) {
@@ -30,11 +44,12 @@ final class AudioInput {
30
44
guard devices != self . devices else { return }
31
45
32
46
self . devices = devices
47
+ self . hasBluetooth = devices. contains ( where: \. isBluetooh)
33
48
}
34
49
35
50
private lazy var listener = Debouncer ( delay: 0.5 ) { [ weak self] in
36
51
guard let self else { return }
37
- self . callback ( self . devices . isRunningSomewhere ( ) ?? false )
52
+ self . callback ( self . isRunningSomewhere)
38
53
}
39
54
40
55
func startListener( ) {
@@ -49,3 +64,16 @@ final class AudioInput {
49
64
}
50
65
}
51
66
}
67
+
68
+ extension AudioInput {
69
+ private func hasOrangeDot( ) -> Bool {
70
+ let windows = CGWindowListCopyWindowInfo ( [ . optionOnScreenOnly, . excludeDesktopElements] , kCGNullWindowID) as? [ [ CFString : Any ] ]
71
+ return ( windows ?? [ ] ) . contains { $0 [ kCGWindowName] as? String == " StatusIndicator " }
72
+ }
73
+
74
+ private var isRunningSomewhere : Bool {
75
+ // FB12081267: bluetooth input devices always report that isRunningSomewhere == false
76
+ if hasBluetooth { return hasOrangeDot ( ) }
77
+ return devices. isRunningSomewhere ( ) ?? false
78
+ }
79
+ }
0 commit comments