Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions toggleMute/toggleMute.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
BD391AE9263C3F5600848049 /* LaunchAtLogin in Frameworks */ = {isa = PBXBuildFile; productRef = BD391AE8263C3F5600848049 /* LaunchAtLogin */; };
C3C528C02DF0162800BBC393 /* KeyboardShortcuts in Frameworks */ = {isa = PBXBuildFile; productRef = C3C528BF2DF0162800BBC393 /* KeyboardShortcuts */; };
C3DA000A2B88C00900B6BCCA /* EventMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3DA00092B88C00900B6BCCA /* EventMonitor.swift */; };
D9A1F4022E0099AA00112233 /* AudioInputController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9A1F4012E0099AA00112233 /* AudioInputController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -51,6 +52,7 @@
44EB8BD723AC2ABD005A4A0B /* TouchBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarController.swift; sourceTree = "<group>"; };
44EB8BDB23AC350D005A4A0B /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = "<group>"; };
C3DA00092B88C00900B6BCCA /* EventMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventMonitor.swift; sourceTree = "<group>"; };
D9A1F4012E0099AA00112233 /* AudioInputController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioInputController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -130,6 +132,7 @@
44D551DF2390830E0065505A /* SharedFileList.h */,
44D551DE2390830E0065505A /* SharedFileList.m */,
449F398223173003008A0DBD /* Preferences.swift */,
D9A1F4012E0099AA00112233 /* AudioInputController.swift */,
44D551DD2390830E0065505A /* SettingsController-Bridging-Header.h */,
44EB8BD623AC28E3005A4A0B /* NSTouchBar-Private.h */,
);
Expand Down Expand Up @@ -274,6 +277,7 @@
449F398D23173610008A0DBD /* SettingsController.swift in Sources */,
449F398323173003008A0DBD /* Preferences.swift in Sources */,
C3DA000A2B88C00900B6BCCA /* EventMonitor.swift in Sources */,
D9A1F4022E0099AA00112233 /* AudioInputController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
13 changes: 7 additions & 6 deletions toggleMute/toggleMute/Bootstrap/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
@objc func runTimedCode(){

mainController.getCurrentVolume()

if(defaults.integer(forKey: "currentSetVolume") < 5) {
touchBarController.toggleMuteStateHard(setMute: true)
} else {
touchBarController.toggleMuteStateHard(setMute: false)
}

// Sync UI to the actual device state so the icon reflects external mute
// changes (e.g. from the system menu). Reads the real CoreAudio mute
// property — falls back to "volume == 0" only when no mute property is
// exposed. If we can't tell, leave the UI alone instead of flipping it.
guard let muted = AudioInputController.isMuted() else { return }
touchBarController.toggleMuteStateHard(setMute: muted)

}

Expand Down
26 changes: 10 additions & 16 deletions toggleMute/toggleMute/Controllers/MainController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,17 @@ class MainController: NSViewController, UNUserNotificationCenterDelegate {


func getCurrentVolume() {

let setInputVolume = "return input volume of (get volume settings)"
var error: NSDictionary?

if let scriptObject = NSAppleScript(source: setInputVolume) {

if let outputString = scriptObject.executeAndReturnError(&error).stringValue {

currentSetVolume = Int(outputString)!
defaults.set(currentSetVolume, forKey: "currentSetVolume")

} else if (error != nil) {
// no error handling currently as it just works always :D
}

// AppleScript's `input volume of (get volume settings)` is unreliable on
// Aggregate Devices (often returns a stale 100). Read the actual input
// volume via CoreAudio. If muted, report 0 so the slider/UI reflects it.
if AudioInputController.isMuted() == true {
currentSetVolume = 0
} else if let v = AudioInputController.volume() {
currentSetVolume = Int((v * 100).rounded())
} else {
return
}

defaults.set(currentSetVolume, forKey: "currentSetVolume")
}


Expand Down
46 changes: 21 additions & 25 deletions toggleMute/toggleMute/Controllers/TouchBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,12 @@ class TouchBarController {


func setNewVolume(newValue: Int) {

let setInputAndResetOutputVolume =
"""
set volume input volume \(newValue)
set currentVol to output volume of (get volume settings)
set volume output volume currentVol
"""
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: setInputAndResetOutputVolume) {
scriptObject.executeAndReturnError(&error)
}

// Drives the *gain* of the default input device (used when restoring the
// user's preferred input volume on unmute, and when the slider moves).
// AppleScript's `set volume input volume` is a no-op on Aggregate Devices,
// so go through CoreAudio.
let clamped = max(0, min(100, newValue))
AudioInputController.setVolume(Float32(clamped) / 100.0)
}


Expand All @@ -107,41 +101,43 @@ class TouchBarController {
redMenuBarIcon = defaults.bool(forKey: "redMenuBarIcon")

if(!setMute && isMuted){

defaults.set(false, forKey: "isMuted")

button?.image = imageUnmute?.tint(color: .alternateSelectedControlTextColor)

button?.layer?.backgroundColor = CGColor(red: 0, green: 0, blue: 0 , alpha: 0)
touchBarButton?.image = imageUnmute
touchBarButton?.bezelColor = NSColor.clear

AudioInputController.setMuted(false)

var unmuteVal = 80

if(isKeyPresentInUserDefaults(key: "defaultInputVol")){
unmuteVal = defaults.integer(forKey: "defaultInputVol")
}

setNewVolume(newValue: unmuteVal)

} else if(setMute && !isMuted) {

defaults.set(true, forKey: "isMuted")

button?.image = imageMute?.tint(color: .selectedMenuItemTextColor)
button?.layer?.backgroundColor = CGColor(red: 0, green: 0, blue: 0 , alpha: 0)

touchBarButton?.image = imageMute
touchBarButton?.bezelColor = NSColor.red
setNewVolume(newValue: 0)


AudioInputController.setMuted(true)

if(redMenuBarIcon){
button?.image = imageMute?.tint(color: .red)
}

if(redMenuBarIconBackground){
button?.layer?.backgroundColor = CGColor(red: 1.0, green: 0, blue: 0 , alpha: 1.0)
}

}

}
Expand Down
Loading