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
8 changes: 8 additions & 0 deletions toggleMute/toggleMute.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
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 */; };
MH001A0001000001 /* SoundFeedbackManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = MH001A0001000000 /* SoundFeedbackManager.swift */; };
MH001A0002000001 /* MuteHUDWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = MH001A0002000000 /* MuteHUDWindowController.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -51,6 +53,8 @@
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>"; };
MH001A0001000000 /* SoundFeedbackManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoundFeedbackManager.swift; sourceTree = "<group>"; };
MH001A0002000000 /* MuteHUDWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MuteHUDWindowController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -73,6 +77,7 @@
447B1DF8236795F100587E3A /* MainController.swift */,
44EB8BDB23AC350D005A4A0B /* SettingsViewController.swift */,
44EB8BD723AC2ABD005A4A0B /* TouchBarController.swift */,
MH001A0002000000 /* MuteHUDWindowController.swift */,
);
path = Controllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -132,6 +137,7 @@
449F398223173003008A0DBD /* Preferences.swift */,
44D551DD2390830E0065505A /* SettingsController-Bridging-Header.h */,
44EB8BD623AC28E3005A4A0B /* NSTouchBar-Private.h */,
MH001A0001000000 /* SoundFeedbackManager.swift */,
);
path = Support;
sourceTree = "<group>";
Expand Down Expand Up @@ -274,6 +280,8 @@
449F398D23173610008A0DBD /* SettingsController.swift in Sources */,
449F398323173003008A0DBD /* Preferences.swift in Sources */,
C3DA000A2B88C00900B6BCCA /* EventMonitor.swift in Sources */,
MH001A0001000001 /* SoundFeedbackManager.swift in Sources */,
MH001A0002000001 /* MuteHUDWindowController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
6 changes: 6 additions & 0 deletions toggleMute/toggleMute/Bootstrap/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele

touchBarController.configureUI()

// Pre-warm singletons so NSPanel and NSSound are ready before first toggle
DispatchQueue.main.async {
_ = SoundFeedbackManager.shared
_ = MuteHUDWindowController.shared
}

KeyboardShortcuts.onKeyDown(for: .toggleMuteShortcut) {
self.touchBarController.toggleMuteState()
print("start")
Expand Down
140 changes: 140 additions & 0 deletions toggleMute/toggleMute/Controllers/MuteHUDWindowController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import Cocoa

final class MuteHUDWindowController {

static let shared = MuteHUDWindowController()
private var preferences = Preferences()

private let hudSize = CGSize(width: 200, height: 200)

private lazy var hudWindow: NSPanel = {
let panel = NSPanel(
contentRect: NSRect(origin: .zero, size: hudSize),
styleMask: [.borderless, .nonactivatingPanel],
backing: .buffered,
defer: true
)
panel.level = .modalPanel
panel.isOpaque = false
panel.backgroundColor = .clear
panel.hasShadow = false
panel.collectionBehavior = [.canJoinAllSpaces, .stationary, .ignoresCycle]
panel.isMovable = false
panel.contentView = buildContentView()
return panel
}()

private var iconView: NSImageView!
private var label: NSTextField!

private var dismissWork: DispatchWorkItem?

private init() {}

// MARK: - Public API

func show(muted: Bool) {
guard preferences.showHudEnabled else { return }

let window = hudWindow

// Update content (using contentTintColor for macOS 11 compatibility)
let symbolName = muted ? "mic.slash.fill" : "mic.fill"
if let img = NSImage(systemSymbolName: symbolName, accessibilityDescription: nil) {
let cfg = NSImage.SymbolConfiguration(pointSize: 72, weight: .regular)
iconView.image = img.withSymbolConfiguration(cfg)
}
iconView.contentTintColor = muted ? .systemRed : .labelColor
label.stringValue = muted ? "Muted" : "Unmuted"

positionWindow()

dismissWork?.cancel()

if let layer = window.contentView?.layer {
layer.removeAllAnimations()
layer.opacity = 1.0
}

// Immediately make visible
window.alphaValue = 1.0
window.orderFrontRegardless()

// Schedule fade-out after 1.0 s
let work = DispatchWorkItem { [weak self] in
self?.fadeOut()
}
dismissWork = work
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: work)
}

// MARK: - Private helpers

private func buildContentView() -> NSView {
let container = NSView(frame: NSRect(origin: .zero, size: hudSize))
container.wantsLayer = true

let blur = NSVisualEffectView(frame: container.bounds)
blur.material = .popover
blur.blendingMode = .behindWindow
blur.state = .active
blur.wantsLayer = true
blur.layer?.cornerRadius = 18
blur.layer?.masksToBounds = true
blur.autoresizingMask = [.width, .height]
container.addSubview(blur)

let iv = NSImageView(frame: NSRect(x: 0, y: 76, width: hudSize.width, height: 80))
iv.imageScaling = .scaleProportionallyUpOrDown
iv.autoresizingMask = [.minXMargin, .maxXMargin]
blur.addSubview(iv)
iconView = iv

let tf = NSTextField(frame: NSRect(x: 0, y: 32, width: hudSize.width, height: 24))
tf.isEditable = false
tf.isBordered = false
tf.isBezeled = false
tf.drawsBackground = false
tf.alignment = .center
tf.font = .systemFont(ofSize: 18, weight: .bold)
tf.textColor = .labelColor
tf.autoresizingMask = [.minXMargin, .maxXMargin]
blur.addSubview(tf)
label = tf

return container
}

private func positionWindow() {
guard let screen = NSScreen.main else { return }
let screenFrame = screen.visibleFrame
let x = screenFrame.midX - hudSize.width / 2
let y = screenFrame.minY + 70
hudWindow.setFrameOrigin(NSPoint(x: x, y: y))
}

private func fadeOut() {
guard let layer = hudWindow.contentView?.layer else { return }

CATransaction.begin()
CATransaction.setCompletionBlock { [weak self] in
// When animation completes naturally (not cancelled), hide the window
if layer.opacity == 0 {
self?.hudWindow.orderOut(nil)
}
}

let anim = CABasicAnimation(keyPath: "opacity")
anim.fromValue = 1.0
anim.toValue = 0.0
anim.duration = 0.3
anim.timingFunction = CAMediaTimingFunction(name: .easeIn)
anim.fillMode = .forwards
anim.isRemovedOnCompletion = false

layer.opacity = 0.0
layer.add(anim, forKey: "fade")

CATransaction.commit()
}
}
15 changes: 14 additions & 1 deletion toggleMute/toggleMute/Controllers/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class SettingsViewController: NSViewController {
@IBOutlet var launchAtLoginCheckBox: NSButton!
@IBOutlet weak var redMenuBarIconCheckBox: NSButton!
@IBOutlet weak var redMenuBarBackgroundCheckBox: NSButton!
@IBOutlet weak var muteSoundCheckBox: NSButton!
@IBOutlet weak var showHudCheckBox: NSButton!
@IBOutlet var quitButton: NSButton!
@IBOutlet weak var shortcutSubView: NSView!
@IBOutlet weak var versionLabel: NSTextField!
Expand Down Expand Up @@ -65,7 +67,10 @@ class SettingsViewController: NSViewController {
} else {
redMenuBarIconCheckBox.state = .off
}


muteSoundCheckBox.state = preferences.muteSoundEnabled ? .on : .off
showHudCheckBox.state = preferences.showHudEnabled ? .on : .off

let recorder = KeyboardShortcuts.RecorderCocoa(for: .toggleMuteShortcut)

recorder.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -131,6 +136,14 @@ class SettingsViewController: NSViewController {
}


@IBAction func didTouchMuteSound(_ sender: NSButton) {
preferences.muteSoundEnabled = sender.state == .on
}

@IBAction func didTouchShowHud(_ sender: NSButton) {
preferences.showHudEnabled = sender.state == .on
}

@IBAction func didTouchClose(_ sender: Any) {

NSApplication.shared.terminate(nil)
Expand Down
20 changes: 17 additions & 3 deletions toggleMute/toggleMute/Controllers/TouchBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class TouchBarController {

if(isMuted) {
defaults.set(false, forKey: "isMuted")
toggleMuteStateHard(setMute: true)
toggleMuteStateHard(setMute: true, playFeedback: false)
} else {
defaults.set(true, forKey: "isMuted")
toggleMuteStateHard(setMute: false)
toggleMuteStateHard(setMute: false, playFeedback: false)
}

}
Expand Down Expand Up @@ -99,7 +99,7 @@ class TouchBarController {
}


func toggleMuteStateHard(setMute: Bool) {
func toggleMuteStateHard(setMute: Bool, playFeedback: Bool = true) {

let button = delegateController.statusItem.button
isMuted = defaults.bool(forKey: "isMuted")
Expand All @@ -122,6 +122,13 @@ class TouchBarController {
}

setNewVolume(newValue: unmuteVal)

DispatchQueue.main.async {
if playFeedback {
SoundFeedbackManager.shared.playUnmute()
MuteHUDWindowController.shared.show(muted: false)
}
}

} else if(setMute && !isMuted) {

Expand All @@ -141,6 +148,13 @@ class TouchBarController {
if(redMenuBarIconBackground){
button?.layer?.backgroundColor = CGColor(red: 1.0, green: 0, blue: 0 , alpha: 1.0)
}

DispatchQueue.main.async {
if playFeedback {
SoundFeedbackManager.shared.playMute()
MuteHUDWindowController.shared.show(muted: true)
}
}

}

Expand Down
Loading