Skip to content

Commit

Permalink
Merge pull request #4 from kristopherjohnson/status-item-button
Browse files Browse the repository at this point in the history
Use status item button
  • Loading branch information
kristopherjohnson authored Oct 22, 2019
2 parents d2caa07 + 0d160e3 commit ac0188e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 325 deletions.
12 changes: 0 additions & 12 deletions MenubarCountdown.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

/* Begin PBXBuildFile section */
4E48658E1BEE6CE500C159BF /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E48658D1BEE6CE500C159BF /* Log.swift */; };
4E4865911BEE703A00C159BF /* MenubarIcon.png in Resources */ = {isa = PBXBuildFile; fileRef = 4E48658F1BEE703A00C159BF /* MenubarIcon.png */; };
4E4865921BEE703A00C159BF /* MenubarIconInverse.png in Resources */ = {isa = PBXBuildFile; fileRef = 4E4865901BEE703A00C159BF /* MenubarIconInverse.png */; };
4E4865941BEE705E00C159BF /* UserDefaults.plist in Resources */ = {isa = PBXBuildFile; fileRef = 4E4865931BEE705E00C159BF /* UserDefaults.plist */; };
4E4865961BEE721C00C159BF /* CATransactionExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E4865951BEE721C00C159BF /* CATransactionExtensions.swift */; };
4E4865981BEE749E00C159BF /* StartTimerDialogController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E4865971BEE749E00C159BF /* StartTimerDialogController.swift */; };
Expand All @@ -29,7 +27,6 @@
4EB204401BED89F900D83EF3 /* MenubarCountdownUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB2043F1BED89F900D83EF3 /* MenubarCountdownUITests.swift */; };
4EB2044E1BED908300D83EF3 /* Stopwatch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB2044D1BED908300D83EF3 /* Stopwatch.swift */; };
4EB204501BEE29D700D83EF3 /* AppUserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB2044F1BEE29D700D83EF3 /* AppUserDefaults.swift */; };
4EB204521BEE2E8B00D83EF3 /* StatusItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB204511BEE2E8B00D83EF3 /* StatusItemView.swift */; };
4EB204541BEE307900D83EF3 /* CALayerExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EB204531BEE307900D83EF3 /* CALayerExtensions.swift */; };
/* End PBXBuildFile section */

Expand All @@ -52,8 +49,6 @@

/* Begin PBXFileReference section */
4E48658D1BEE6CE500C159BF /* Log.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = "<group>"; };
4E48658F1BEE703A00C159BF /* MenubarIcon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = MenubarIcon.png; sourceTree = "<group>"; };
4E4865901BEE703A00C159BF /* MenubarIconInverse.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = MenubarIconInverse.png; sourceTree = "<group>"; };
4E4865931BEE705E00C159BF /* UserDefaults.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = UserDefaults.plist; sourceTree = "<group>"; };
4E4865951BEE721C00C159BF /* CATransactionExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CATransactionExtensions.swift; sourceTree = "<group>"; };
4E4865971BEE749E00C159BF /* StartTimerDialogController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StartTimerDialogController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -81,7 +76,6 @@
4EB204411BED89F900D83EF3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4EB2044D1BED908300D83EF3 /* Stopwatch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Stopwatch.swift; sourceTree = "<group>"; };
4EB2044F1BEE29D700D83EF3 /* AppUserDefaults.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppUserDefaults.swift; sourceTree = "<group>"; };
4EB204511BEE2E8B00D83EF3 /* StatusItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusItemView.swift; sourceTree = "<group>"; };
4EB204531BEE307900D83EF3 /* CALayerExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CALayerExtensions.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -136,8 +130,6 @@
children = (
4E4865A81BEFA07300C159BF /* MenuTimerIcon.icns */,
4EB204261BED89F900D83EF3 /* Assets.xcassets */,
4E48658F1BEE703A00C159BF /* MenubarIcon.png */,
4E4865901BEE703A00C159BF /* MenubarIconInverse.png */,
);
name = "Image Resources";
sourceTree = "<group>";
Expand Down Expand Up @@ -184,7 +176,6 @@
4E48659F1BEE85D200C159BF /* MenubarCountdown-Bridging-Header.h */,
4E4865B11BEFC63700C159BF /* StartTimerDialog.xib */,
4E4865971BEE749E00C159BF /* StartTimerDialogController.swift */,
4EB204511BEE2E8B00D83EF3 /* StatusItemView.swift */,
4EB2044D1BED908300D83EF3 /* Stopwatch.swift */,
4E4865AB1BEFA0F000C159BF /* Text Resources */,
4E4865991BEE79EA00C159BF /* TextField.swift */,
Expand Down Expand Up @@ -317,13 +308,11 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4E4865921BEE703A00C159BF /* MenubarIconInverse.png in Resources */,
4E4865941BEE705E00C159BF /* UserDefaults.plist in Resources */,
4E4865A91BEFA07300C159BF /* MenuTimerIcon.icns in Resources */,
4E4865B51BEFC63700C159BF /* TimerExpiredAlert.xib in Resources */,
4EB204271BED89F900D83EF3 /* Assets.xcassets in Resources */,
4E4865B41BEFC63700C159BF /* StartTimerDialog.xib in Resources */,
4E4865911BEE703A00C159BF /* MenubarIcon.png in Resources */,
4E4865B31BEFC63700C159BF /* MainMenu.xib in Resources */,
4E4865AD1BEFA12A00C159BF /* Credits.rtf in Resources */,
);
Expand All @@ -350,7 +339,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4EB204521BEE2E8B00D83EF3 /* StatusItemView.swift in Sources */,
4E4865981BEE749E00C159BF /* StartTimerDialogController.swift in Sources */,
4EB204541BEE307900D83EF3 /* CALayerExtensions.swift in Sources */,
4E48659A1BEE79EA00C159BF /* TextField.swift in Sources */,
Expand Down
57 changes: 36 additions & 21 deletions MenubarCountdown/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {

var statusItem: NSStatusItem!

var statusItemView: StatusItemView!

/**
Reference to menu loaded from MainMenu.xib.
*/
Expand All @@ -72,18 +70,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

stopwatch.reset()

let statusBar = NSStatusBar.system
statusItem = statusBar.statusItem(withLength: NSStatusItem.variableLength)

statusItemView = StatusItemView()
statusItemView.statusItem = statusItem
statusItemView.menu = menu
statusItemView.toolTip = NSLocalizedString("Menubar Countdown",
comment: "Status Item Tooltip")
// #KJ TODO: 'view' is deprecated. Use the standard button property instead.
statusItem.view = statusItemView

updateStatusItemTitle(timeRemaining: 0)
initializeStatusItem()

if UserDefaults.standard.bool(forKey: AppUserDefaults.showStartDialogOnLaunchKey) {
showStartTimerDialog(self)
Expand Down Expand Up @@ -133,6 +120,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

/**
Create `statusItem` and set its initial state.
*/
func initializeStatusItem() {
let statusBar = NSStatusBar.system
statusItem = statusBar.statusItem(withLength: NSStatusItem.variableLength)

statusItem.menu = menu
statusItem.button?.wantsLayer = true
statusItem.button?.toolTip = NSLocalizedString("Menubar Countdown",
comment: "Status Item Tooltip")
statusItem.button?.font = NSFont.monospacedDigitSystemFont(ofSize: 0,
weight: .regular)
showStatusItemIcon()
}

/**
Sets the text of the menu bar status item.
*/
Expand All @@ -159,7 +162,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {
else {
timeString = NSString(format: "%02d:%02d", hours, minutes) as String
}
statusItemView.title = timeString
statusItem.button?.title = timeString
}

/**
Change the status item to an hourglass icon
*/
func showStatusItemIcon() {
statusItem.button?.title = "⌛️"
}

func startBlinking() {
statusItem.button?.layer?.addBlinkAnimation()
}

func stopBlinking() {
statusItem.button?.layer?.removeBlinkAnimation()
}

// MARK: Timer expiration
Expand All @@ -180,7 +198,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let defaults = UserDefaults.standard

if defaults.bool(forKey: AppUserDefaults.blinkOnExpirationKey) {
statusItemView.isTitleBlinking = true
startBlinking()
}

if defaults.bool(forKey: AppUserDefaults.playAlertSoundOnExpirationKey) {
Expand Down Expand Up @@ -287,7 +305,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
startTimerDialogController.showDialog()
}


/**
Start the timer.

Expand All @@ -311,7 +328,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
stopwatch.reset()

updateStatusItemTitle(timeRemaining: timerSettingSeconds)
statusItemView.showTitle()

waitForNextSecond()
}
Expand All @@ -330,8 +346,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
canPause = false
canResume = false

statusItemView.isTitleBlinking = false
statusItemView.showIcon()
stopBlinking()
showStatusItemIcon()
}

/**
Expand Down Expand Up @@ -366,7 +382,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
stopwatch.reset()

updateStatusItemTitle(timeRemaining: timerSettingSeconds)
statusItemView.showTitle()

waitForNextSecond()
}
Expand Down
Binary file removed MenubarCountdown/MenubarIcon.png
Binary file not shown.
Binary file removed MenubarCountdown/MenubarIconInverse.png
Binary file not shown.
Loading

0 comments on commit ac0188e

Please sign in to comment.